Network Analysis with Python


# draw the graph using information about the nodes geographic position



Yüklə 1,5 Mb.
Pdf görüntüsü
səhifə10/12
tarix22.03.2023
ölçüsü1,5 Mb.
#89129
1   ...   4   5   6   7   8   9   10   11   12
l109-tutorial 2015

# draw the graph using information about the nodes geographic position
pos_dict = {}
for node_id, node_info in node_data.items():
pos_dict[node_id] = (node_info[2], node_info[1])
nx.draw(cam_net, pos=pos_dict, with_labels=False, node_size=25)
plt.savefig('cam_net_graph.pdf')
plt.close()
37


Basic analysis: working with JSON data
• Computing network centrality metrics can be slow, especially for large networks.
• JSON (JavaScript Object Notation) is a lightweight data interchange format which 
can be used to serialize and deserialize Python objects (dictionaries and lists).
import json
# Utility function: saves data in JSON format
def dump_json(out_file_name, result):
with open(out_file_name, 'w') as out_file:
out_file.write(json.dumps(result, indent=4, separators=(',', ': ')))
# Utility function: loads JSON data into a Python object
def load_json(file_name):
with open(file_name) as f:
return json.loads(f.read())
path = 'betwenness_centrality.txt' # Example
dump_json(path, bet_cen)
saved_centrality = load_json(path) # Result is a Python dictionary
38


4. Writing your own code
39


Writing your own code: BFS
• With Python and NetworkX it is easy to write any graph-based algorithm
from collections import deque
def breadth_first_search(g, source):
queue = deque([(None, source)])
enqueued = set([source])
while queue:
parent, n = queue.popleft()

Yüklə 1,5 Mb.

Dostları ilə paylaş:
1   ...   4   5   6   7   8   9   10   11   12




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin