Mercurial > public > think_complexity
annotate RegularGraphTest.py @ 29:65b70b4e12ad
Chapter 5, exercise 3.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 06 Jan 2013 18:11:17 -0600 |
parents | 8e44660965ef |
children |
rev | line source |
---|---|
bgneal@5 | 1 """Tests our regular graph making abilities by displaying examples. |
bgneal@5 | 2 |
bgneal@5 | 3 """ |
bgneal@5 | 4 import string |
bgneal@5 | 5 |
bgneal@5 | 6 from Graph import Vertex, Graph, GraphError |
bgneal@5 | 7 from GraphWorld import GraphWorld, CircleLayout |
bgneal@5 | 8 |
bgneal@5 | 9 def main(script_name, n, k): |
bgneal@5 | 10 |
bgneal@5 | 11 # Attempt to create a regular graph of order n and degree k |
bgneal@5 | 12 |
bgneal@5 | 13 n, k = int(n), int(k) |
bgneal@5 | 14 |
bgneal@5 | 15 labels = string.ascii_lowercase + string.ascii_uppercase |
bgneal@5 | 16 vs = [Vertex(c) for c in labels[:n]] |
bgneal@5 | 17 |
bgneal@5 | 18 # create graph and layout |
bgneal@5 | 19 g = Graph(vs) |
bgneal@5 | 20 g.add_regular_edges(k) |
bgneal@5 | 21 layout = CircleLayout(g) |
bgneal@5 | 22 |
bgneal@5 | 23 # draw the graph |
bgneal@5 | 24 |
bgneal@5 | 25 gw = GraphWorld() |
bgneal@5 | 26 gw.show_graph(g, layout) |
bgneal@5 | 27 gw.mainloop() |
bgneal@5 | 28 |
bgneal@5 | 29 |
bgneal@5 | 30 if __name__ == '__main__': |
bgneal@5 | 31 import sys |
bgneal@5 | 32 try: |
bgneal@5 | 33 main(*sys.argv) |
bgneal@5 | 34 except GraphError, ex: |
bgneal@5 | 35 sys.stderr.write("GraphError: {}\n".format(ex)) |