Skip to content

Instantly share code, notes, and snippets.

@mikk-c
Created September 4, 2020 08:21
Show Gist options
  • Save mikk-c/690226e73540049a89e920542e7f65b1 to your computer and use it in GitHub Desktop.
Save mikk-c/690226e73540049a89e920542e7f65b1 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import networkx as nx"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def net_distance(G1, G2):\n",
" d = 0\n",
" for n in G1.nodes:\n",
" neighbors1 = set(G1.neighbors(n))\n",
" neighbors2 = set(G2.neighbors(n))\n",
" d += len(neighbors1 - neighbors2) + len(neighbors2 - neighbors1)\n",
" return d"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"G1 = nx.read_edgelist(\"1/data1.txt\", delimiter = \"\\t\", nodetype = int)\n",
"G2 = nx.read_edgelist(\"1/data2.txt\", delimiter = \"\\t\", nodetype = int)\n",
"G3 = nx.read_edgelist(\"1/data3.txt\", delimiter = \"\\t\", nodetype = int)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"G1-G2 difference: 1434\n",
"G1-G3 difference: 1414\n",
"G2-G3 difference: 1444\n"
]
}
],
"source": [
"print(\"G1-G2 difference: %s\" % net_distance(G1, G2))\n",
"print(\"G1-G3 difference: %s\" % net_distance(G1, G3))\n",
"print(\"G2-G3 difference: %s\" % net_distance(G2, G3))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment