15-388/688 - Practical Data Science: Graph and network processing
- J. Zico Kolter
Carnegie Mellon University Fall 2019
1
15-388/688 - Practical Data Science: Graph and network processing - - PowerPoint PPT Presentation
15-388/688 - Practical Data Science: Graph and network processing J. Zico Kolter Carnegie Mellon University Fall 2019 1 Outline Networks and graph Representing graphs Graph algorithms Graph libraries 2 Outline Networks and graph
1
2
3
4
5
6
7
8
Graphs from http://snap.stanford.edu, visualizations from http://www.cise.ufl.edu/research/sparse/matrices/SNAP/
9
10
11
Node Edges A [B] B [C] C [A,D] D []
12
Node (key) Edges A {B:1.0} B {C:1.0} C {A:1.0,D:1.0} D {}
13
14
π΅ = 1 1 1 1 (From) A B C D A B C D (To)
15
16
17
18
19
20
21
22
ν 11ν
1 |ν | 1
23
π΅ = 1 1 1 1 π = 1 1 0.5 0.25 0.25 0.25 0.5 0.25 Μ π = 0.025 0.025 0.925 0.025 0.025 0.925 0.025 0.025 0.475 0.25 0.025 0.25 0.025 0.25 0.475 0.25 π = 0.1 π¦ β 0.21 0.26 0.31 0.21
24
25
26
27
49 33 12 1
28
29
30
1 2 3 4 5 6 7 8 9 10 11 12 13 14
31
32
33
import networkx as nx G = nx.Graph() # undirected graph G = nx.DiGraph() # directed graph # add and remove edges G.add_edges_from([("A","B"), ("B","C"), ("C","A"), ("C","D")]) G.remove_edge("A","B") G.add_edge("A","B") G.remove_edges_from([("A","B"), ("B","C")]) G.add_edges_from([("A","B"), ("B","C")]) # also add_node(), remove_node(), add_nodes_form(), remove_nodes_from()
34
for i in G.nodes(): # loop over nodes print i for i,j in G.edges(): # loop over edges print i,j G.node["A"]["node_property"] = "node_value" G.edge["A"]["B"]["edge_property"] = "edge_value" G.nodes(data=True) # iterator over nodes returning properties G.edges(data=True) # iterator over edges returning properties print G["C"] # {'A': {}, 'D': {}}
35
import matplotlib.pyplot as plt %matplotlib inline nx.draw(G,with_labels=True) plt.savefig("mpl_graph.pdf")
36
nx.shortest_path_length(G,source="A") # iterater over path lengths nx.pagerank(G,alpha=0.9) # dictionary of node ranks # NOTE: this requires Networkx 2.0 nx.girvan_newman(G) # iterator over partitions at different hierarchy levels