<<

graphs and networks with 1 Python Software for Complex Networks networkx defining and drawing graphs 2 Analysis of Graphs connectivity of random graphs shortest paths 3 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 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.(’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 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.(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 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 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), \ node_color=’g’) plt.show()

Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 22 / 35 computing the mapping between the nodes

from networkx.algorithms import isomorphism GH = isomorphism.GraphMatcher(G, H) print(GH.is_isomorphic()) print(GH.mapping)

The output is

True {’b’: ’A’, ’a’: ’B’, ’d’: ’C’, ’c’: ’D’}

Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 23 / 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 24 / 35 nauty and Traces

nauty and Traces are programs for computing automorphism groups of graphs and digraphs. They can also produce a canonical label. They are written in a portable subset of C, and run on a considerable number of different systems. B.D. McKay and A. Piperno: Practical Graph Isomorphism, II, Journal of Symbolic Computation 60: 94–112, 2014. A quote from the web site, following the license: These days most people use nauty via a larger package such as Magma, Sage, or GAP, and often they don’t even know they are using nauty. web page: pallini.di.uniroma1.it

Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 25 / 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 26 / 35 defining a random

import networkx as nx from matplotlib import pyplot as plt n=10 m=3*n DG = nx.gnm_random_graph(n, m, seed=2019, \ directed=True) print(DG.nodes()) print(DG.edges()) nx.draw_networkx(DG, pos=nx.circular_layout(DG), \ node_color=’y’) plt.show()

Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 27 / 35 the drawing of a random directed graph

Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 28 / 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 29 / 35 adding capacities to each edge

We continue with the random directed graph and add capacity one to each edge.

WG = nx.DiGraph() WG.add_nodes_from(DG.nodes()) for (u, v) in DG.edges(): WG.add_edge(u, v, capacity=1.0) for (u, v) in WG.edges(): edge = (u, v)

Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 30 / 35 computing the maximum flow

value, dict = nx.maximum_flow(WG, 0, n-1, \ capacity=’capacity’) print(’the value of the flow :’, value) print(’the dictionary of the flow :’) for key in dict: print(key, ’:’, dict[key], end=’’) d = dict[key] print(’ ’, key, ’ ->’, end=’’) forkind: if d[k] > 0: print(’ ’, k, end=’’) print()

Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 31 / 35 the dictionary output

the value of the flow : 3.0 the dictionary of the flow : 0 : {5: 1.0, 3: 1.0, 2: 1.0} 0 -> 5 3 2 1 : {5: 0, 6: 0, 9: 1.0} 1 -> 9 2 : {3: 0, 1: 1.0} 2 -> 1 3 : {4: 1.0, 5: 0} 3 -> 4 4 : {6: 0, 3: 0, 7: 0, 9: 1.0, 8: 0} 4 -> 9 5 : {9: 1.0, 7: 0, 1: 0} 5 -> 9 6 : {7: 0, 0: 0, 2: 0} 6 -> 7 : {2: 0, 9: 0, 5: 0} 7 -> 8 : {2: 0, 6: 0} 8 -> 9 : {3: 0, 6: 0, 4: 0, 5: 0} 9 ->

Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 32 / 35 Summary + Assignments Assignments: 1 Consider the connectivity of random graphs. 1 Generate random graphs for various probabilities p and dimensions n larger than 20. What is the threshold value pn for p such that your generated graphs are connected? 2 What is the evolution of pn as n increases? Take sufficiently many large values for n so a trend is noticeable. Organize your experiments with repeatable loops which produce tables of results. For both questions, you may work with a fixed seed for the random number generator.

2 Consider connected random graphs where pn, the probability that two edges are connected, is the threshold probability that the entire graph is connected. What is the length of the shortest path as n increases? As in the previous exercise, you may work with a fixed seed so the results of your experiments are reproducible.

Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 33 / 35 more exercises

3 Consider the two isomorphic graphs G and H of slide 21. Explain how to apply SageMath to compute the graph isomorphism mapping between G and H. 4 Examine the cost of the graph isomorphism problem. The dimensions of the input are n, the number of nodes in a graph, and m, the number of edges in a graph. Generate two graphs with sufficiently large n and m so solving the isomorphism problem takes at least several seconds. Then do the following: 1 Keeping n constant, double m a couple of times and observe the increased running time. Does it grow like O(m), O(m2),or something else? 2 Swap n and m and repeat the doubling experiment. Does your experiments suggest that the algorithm runs in polynomial time?

Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 34 / 35 one last exercise

5 Examine the cost of the maximum flow problem. The dimensions of the input are n, the number of nodes in a graph, and m, the number of edges in a graph. Generate a graph with sufficiently large n and m so solving the maximum flow problem takes at least several seconds. Then do the following: 1 Keeping n constant, double m a couple of times and observe the increased running time. Does it grow like O(m) or O(m2)? 2 Swap n and m and repeat the doubling experiment. Consider the cost analysis of the Ford Fulkerson algorithm and verify whether your experiments agree with this cost analysis.

Scientific Software (MCS 507) graphs and networks with networkx L-25 23 October 2019 35 / 35