Graphs and Networks with Networkx
Total Page:16
File Type:pdf, Size:1020Kb
graphs and networks with networkx 1 Python Software for Complex Networks networkx defining and drawing graphs 2 Analysis of Graphs connectivity of random graphs shortest paths 3 Graph Isomorphism are two graphs isomorphic? nauty and Traces 4 Directed Graphs defining and drawing digraphs computing the maximum flow MCS 507 Lecture 25 Mathematical, Statistical and Scientific Software Jan Verschelde, 23 October 2019 Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 1 / 35 graphs and networks with networkx 1 Python Software for Complex Networks networkx defining and drawing graphs 2 Analysis of Graphs connectivity of random graphs shortest paths 3 Graph Isomorphism are two graphs isomorphic? nauty and Traces 4 Directed Graphs defining and drawing digraphs computing the maximum flow Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 2 / 35 networkx Python software for complex networks NetworkX is a free Python library for graphs and networks. Its core is written in pure Python. It interfaces easily with code written in C/C++ and FORTRAN. Initial release: 11 April 2005, v2.4 released 16 October 2019. Aric A. Hagberg, Daniel A. Schult, Pieter J. Swart: Exploring Network Structure, Dynamics, and Function using NetworkX. In the Proceedings of the 7th Python in Science conference (SciPy 2008), pages 11-15. Scales to 10 million nodes and 100 million edges. Integrated into SageMath. web site: http://networkx.github.com Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 3 / 35 graphs and networks with networkx 1 Python Software for Complex Networks networkx defining and drawing graphs 2 Analysis of Graphs connectivity of random graphs shortest paths 3 Graph Isomorphism are two graphs isomorphic? nauty and Traces 4 Directed Graphs defining and drawing digraphs computing the maximum flow Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 4 / 35 adding nodes to a graph Let us make a tree with a root and 5 children. To add a single node, use the method add_node(), to add a list of nodes, use add_nodes_from(). import networkx as nx T = nx.Graph() T.add_node(’root’) children = range(5) T.add_nodes_from(children) print(’the nodes :’, T.nodes()) The output is the nodes : [’root’, 0, 1, 2, 3, 4] Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 5 / 35 adding edges to a graph To add a single edge, use the method add_edge(), to add a list of edges, use add_edges_from(). The script of the previous slide continues below: T.add_edge(’root’, 0) edges = [(’root’, child) for child in children[1:]] T.add_edges_from(edges) print(’the edges :’, T.edges()) The output is the edges : [(’root’, 0), (’root’, 1), (’root’, 2), \ (’root’, 3), (’root’, 4)] Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 6 / 35 number of nodes, edges; list the neighbors print(’the number of nodes :’, T.number_of_nodes()) print(’the number of edges :’, T.number_of_edges()) rootneighbors = T.neighbors(’root’) print(’neighbors of root : ’, end=’’) print([neighbor for neighbor in rootneighbors]) print(’number of neighbors of root :’, T.degree(’root’)) print(’number of neighbors of 2 :’, T.degree(2)) The output is the number of nodes : 6 the number of edges : 5 neighbors of root : [0, 1, 2, 3, 4] number of neighbors of root : 5 number of neighbors of 2 : 1 Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 7 / 35 drawing graphs with matplotlib NetworkX contains many examples of classic graphs. The code below defines a complete bipartite graph with 3 and 5 nodes in each part. import networkx as nx from matplotlib import pyplot as plt k_3_5 = nx.complete_bipartite_graph(3, 5) nx.draw_circular(k_3_5) plt.show() Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 8 / 35 a complete bipartite graph K3,5 Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 9 / 35 graphs and networks with networkx 1 Python Software for Complex Networks networkx defining and drawing graphs 2 Analysis of Graphs connectivity of random graphs shortest paths 3 Graph Isomorphism are two graphs isomorphic? nauty and Traces 4 Directed Graphs defining and drawing digraphs computing the maximum flow Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 10 / 35 connectivity of random graphs Consider the following problem: 1 n is the number of nodes in a graph G; 2 p is the probability that two nodes are connected. What is the probability that G is connected? To explore this problem, we define random graphs. For each pair (i, j) of nodes, we flip a loaded coin to decide whether there is an edge between i and j. Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 11 / 35 graphs from numpy matrices import numpy as np import networkx as nx from random import random, seed from matplotlib import pyplot as plt seed(20191023) p = 0.057 # probability nodes are connected coinflip = lambda: (1 if random() < p else 0) dim = 20 flips = [[coinflip() for j in range(dim)] \ for i in range(dim)] print(flips) A = np.matrix(flips, dtype=int) print(A.shape) G = nx.from_numpy_matrix(A) Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 12 / 35 the adjacency matrix of a graph We can verify that the adjacency matrix is A: print(G.nodes()) print(G.edges()) B = nx.adjacency_matrix(G) C = nx.to_numpy_matrix(G) The nodes are [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, \ 11, 12, 13, 14, 15, 16, 17, 18, 19] and the edges are [(0, 10), (0, 19), (1, 2), (1, 11), (1, 16), (1, 3), \ (1, 10), (2, 16), (3, 17), (5, 13), (6, 6), (6, 8), \ (7, 19), (8, 10), (8, 15), (9, 9), (9, 15), (10, 19), \ (12, 18), (12, 19), (13, 16)] The adjacency matrix depends on the node labeling. Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 13 / 35 the connectivity of a graph print(’Is G connected ?’, nx.is_connected(G)) print(’number of connected components :’, \ nx.number_connected_components(G)) print(’connected components :’, [c for c in nx.connected_components(G)]) nx.draw_networkx(G, pos=nx.circular_layout(G)) plt.show() The output is Is G connected ? False number of connected components : 3 connected components : [{0, 1, 2, 3, 5, 6, 7, 8, 9, \ 10, 11, 12, 13, 15, 16, 17, 18, 19}, {4}, {14}] Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 14 / 35 a random graph with 20 nodes Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 15 / 35 graphs and networks with networkx 1 Python Software for Complex Networks networkx defining and drawing graphs 2 Analysis of Graphs connectivity of random graphs shortest paths 3 Graph Isomorphism are two graphs isomorphic? nauty and Traces 4 Directed Graphs defining and drawing digraphs computing the maximum flow Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 16 / 35 computing shortest paths For a graph G defined by an adjacency matrix A we compute all shortest paths from node 0: G = nx.from_numpy_matrix(A) for i in range(1, 20): try: P = nx.all_shortest_paths(G, 0, i) print(’paths from 0 to’, i, ’:’, [p for p in P]) except: print(i, ’cannot be reached from 0’) An exception occurs if no path exists. Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 17 / 35 output on a random graph paths from 0 to 1 : [[0, 10, 1]] paths from 0 to 2 : [[0, 10, 1, 2]] paths from 0 to 3 : [[0, 10, 1, 3]] 4 cannot be reached from 0 paths from 0 to 5 : [[0, 10, 1, 16, 13, 5]] paths from 0 to 6 : [[0, 10, 8, 6]] paths from 0 to 7 : [[0, 19, 7]] paths from 0 to 8 : [[0, 10, 8]] paths from 0 to 9 : [[0, 10, 8, 15, 9]] paths from 0 to 10 : [[0, 10]] paths from 0 to 11 : [[0, 10, 1, 11]] paths from 0 to 12 : [[0, 19, 12]] paths from 0 to 13 : [[0, 10, 1, 16, 13]] 14 cannot be reached from 0 paths from 0 to 15 : [[0, 10, 8, 15]] paths from 0 to 16 : [[0, 10, 1, 16]] paths from 0 to 17 : [[0, 10, 1, 3, 17]] paths from 0 to 18 : [[0, 19, 12, 18]] paths from 0 to 19 : [[0, 19]] Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 18 / 35 graphs and networks with networkx 1 Python Software for Complex Networks networkx defining and drawing graphs 2 Analysis of Graphs connectivity of random graphs shortest paths 3 Graph Isomorphism are two graphs isomorphic? nauty and Traces 4 Directed Graphs defining and drawing digraphs computing the maximum flow Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 19 / 35 the isomorphism of two graphs The two graphs below are isomorphic: a = B, b = A, c = D, d = C. Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 20 / 35 defining two graphs import networkx as nx from matplotlib import pyplot as plt G = nx.Graph() G.add_nodes_from([’a’, ’b’, ’c’, ’d’]) G.add_edges_from([(’a’, ’b’), (’a’, ’c’), \ (’b’, ’c’), (’b’, ’d’), (’c’, ’d’)]) H = nx.Graph() H.add_nodes_from([’A’, ’B’, ’C’, ’D’]) H.add_edges_from([(’A’, ’B’), (’A’, ’C’), \ (’A’, ’D’), (’B’, ’D’), (’C’, ’D’)]) Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 21 / 35 computing whether two graphs are isomorphic print(’Are G and H isomorphic?’, \ nx.is_isomorphic(G, H)) fig = plt.figure() ax = fig.add_subplot(211) nx.draw_networkx(G, pos=nx.circular_layout(G), \ node_color=’r’) ax = fig.add_subplot(212) nx.draw_networkx(H, pos=nx.circular_layout(H),