ioftools / networkxMiCe / networkxmaster / examples / drawing / plot_atlas.py @ 5cef0f13
History  View  Annotate  Download (2.7 KB)
1 
#!/usr/bin/env python


2 
"""

3 
=====

4 
Atlas

5 
=====

6 

7 
Atlas of all graphs of 6 nodes or less.

8 

9 
"""

10 
# Author: Aric Hagberg (hagberg@lanl.gov)

11  
12 
# Copyright (C) 20042019 by

13 
# Aric Hagberg <hagberg@lanl.gov>

14 
# Dan Schult <dschult@colgate.edu>

15 
# Pieter Swart <swart@lanl.gov>

16 
# All rights reserved.

17 
# BSD license.

18  
19 
import random 
20  
21 
try:

22 
import pygraphviz 
23 
from networkx.drawing.nx_agraph import graphviz_layout 
24 
except ImportError: 
25 
try:

26 
import pydot 
27 
from networkx.drawing.nx_pydot import graphviz_layout 
28 
except ImportError: 
29 
raise ImportError("This example needs Graphviz and either " 
30 
"PyGraphviz or pydot.")

31  
32 
import matplotlib.pyplot as plt 
33  
34 
import networkx as nx 
35 
from networkx.algorithms.isomorphism.isomorph import graph_could_be_isomorphic as isomorphic 
36 
from networkx.generators.atlas import graph_atlas_g 
37  
38  
39 
def atlas6(): 
40 
""" Return the atlas of all connected graphs of 6 nodes or less.

41 
Attempt to check for isomorphisms and remove.

42 
"""

43  
44 
Atlas = graph_atlas_g()[0:208] # 208 
45 
# remove isolated nodes, only connected graphs are left

46 
U = nx.Graph() # graph for union of all graphs in atlas

47 
for G in Atlas: 
48 
zerodegree = [n for n in G if G.degree(n) == 0] 
49 
for n in zerodegree: 
50 
G.remove_node(n) 
51 
U = nx.disjoint_union(U, G) 
52  
53 
# list of graphs of all connected components

54 
C = nx.connected_component_subgraphs(U) 
55  
56 
UU = nx.Graph() 
57 
# do quick isomorphiclike check, not a true isomorphism checker

58 
nlist = [] # list of nonisomorphic graphs

59 
for G in C: 
60 
# check against all nonisomorphic graphs so far

61 
if not iso(G, nlist): 
62 
nlist.append(G) 
63 
UU = nx.disjoint_union(UU, G) # union the nonisomorphic graphs

64 
return UU

65  
66  
67 
def iso(G1, glist): 
68 
"""Quick and dirty nonisomorphism checker used to check isomorphisms."""

69 
for G2 in glist: 
70 
if isomorphic(G1, G2):

71 
return True 
72 
return False 
73  
74  
75 
if __name__ == '__main__': 
76 
G = atlas6() 
77  
78 
print("graph has %d nodes with %d edges"

79 
% (nx.number_of_nodes(G), nx.number_of_edges(G))) 
80 
print(nx.number_connected_components(G), "connected components")

81  
82 
plt.figure(1, figsize=(8, 8)) 
83 
# layout graphs with positions using graphviz neato

84 
pos = graphviz_layout(G, prog="neato")

85 
# color nodes the same in each connected subgraph

86 
C = nx.connected_component_subgraphs(G) 
87 
for g in C: 
88 
c = [random.random()] * nx.number_of_nodes(g) # random color...

89 
nx.draw(g, 
90 
pos, 
91 
node_size=40,

92 
node_color=c, 
93 
vmin=0.0,

94 
vmax=1.0,

95 
with_labels=False

96 
) 
97 
plt.show() 