Commit 1859fc9f authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 0b525e3e
#!/usr/bin/env python #!/usr/bin/env python
"""generate all connected graphs up to N nodes""" """generate all connected graphs up to N nodes"""
from __future__ import print_statement from __future__ import print_function
import sys
class Graph: class Graph:
# .nodes {nodes} # .nodes {nodes}
# .edges {edges} # .edges {edges}
def __init__(nodes, edges): def __init__(g, nodes, edges):
self.nodes = set(nodes) g.nodes = set(nodes)
self.edges = set(edges) g.edges = set(edges)
def __str__(self): def __str__(g):
return "Graph nodes=%s edges=%s" % (self.nodes, self.edges) return "Graph nodes=%s edges=%s" % (g.nodes, g.edges)
# generate all connected graphs with N(nodes) =< N # generate all connected graphs with N(nodes) =< N
...@@ -23,8 +24,8 @@ def genGraphs(N): ...@@ -23,8 +24,8 @@ def genGraphs(N):
nodes = [] # of all nodes nodes = [] # of all nodes
Sprevedges = set() # set of edges for all graphs with (i-1) nodes Sprevedges = set() # set of edges for all graphs with (i-1) nodes
for i in range(N): for i in range(1,N+1):
n = ('r' if i == 0 else 'n%d' % i) n = ('r' if i == 1 else 'n%d' % (i-1))
nodes = nodes + [n] nodes = nodes + [n]
Sedges = set() Sedges = set()
...@@ -32,7 +33,7 @@ def genGraphs(N): ...@@ -32,7 +33,7 @@ def genGraphs(N):
# is connected to at least one node in the previous graph. # is connected to at least one node in the previous graph.
# for all combinations to which nodes n is connected in the previous graph. # for all combinations to which nodes n is connected in the previous graph.
for j in range(1,2**(i-1)): for j in range(1, 1<<(i-1)):
Snewconn = set() Snewconn = set()
for k in range(i-1): for k in range(i-1):
if j & (1 << k): if j & (1 << k):
...@@ -42,7 +43,7 @@ def genGraphs(N): ...@@ -42,7 +43,7 @@ def genGraphs(N):
for newconn in Snewconn: for newconn in Snewconn:
newedges = edges.union(newconn) newedges = edges.union(newconn)
Sedges.add(newedges) Sedges.add(newedges)
yield Graph(node, newedges) yield Graph(nodes, newedges)
Sprevedges = Sedges Sprevedges = Sedges
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment