Created
September 4, 2020 07:54
-
-
Save mikk-c/ba7eacce1669a8f17120917ee6c329f1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import copy\n", | |
"import numpy as np\n", | |
"import networkx as nx" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def read_vector(filename):\n", | |
" v = {}\n", | |
" with open(filename, 'r') as f:\n", | |
" for line in f:\n", | |
" fields = line.strip().split('\\t')\n", | |
" value = float(fields[1])\n", | |
" if value > 0: \n", | |
" v[int(fields[0])] = value\n", | |
" v = {x: v[x] / sum(v.values()) for x in v}\n", | |
" return v" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"G = nx.read_edgelist(\"1/data.txt\", delimiter = \"\\t\", nodetype = int)\n", | |
"\n", | |
"v1 = read_vector(\"1/vector1.txt\")\n", | |
"v2 = read_vector(\"1/vector2.txt\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"5.296478772596982\n" | |
] | |
} | |
], | |
"source": [ | |
"spls = dict(nx.shortest_path_length(G))\n", | |
"\n", | |
"print(sum(v1[origin] * v2[dest] * spls[dest][origin] for origin in v1 for dest in v2))" | |
] | |
} | |
], | |
"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