| 377 |
ira |
1 |
#!/usr/bin/env python
|
|
|
2 |
|
| 378 |
ira |
3 |
__author__ = "Ira W. Snyder (devel@irasnyder.com)"
|
|
|
4 |
__copyright__ = "Copyright (c) 2006 Ira W. Snyder (devel@irasnyder.com)"
|
|
|
5 |
__license__ = "GNU GPL v2 (or, at your option, any later version)"
|
|
|
6 |
|
| 395 |
ira |
7 |
# Fix for the lame version of python on the school's computers (v2.1)
|
|
|
8 |
try:
|
|
|
9 |
(True, False)
|
|
|
10 |
except NameError:
|
|
|
11 |
(True, False) = (1, 0)
|
|
|
12 |
|
|
|
13 |
# Make sure not to try graphviz if it's not installed (school again)
|
|
|
14 |
no_graphviz = False
|
|
|
15 |
try:
|
|
|
16 |
import yapgvb
|
|
|
17 |
except:
|
|
|
18 |
no_graphviz = True
|
|
|
19 |
|
| 377 |
ira |
20 |
import Graph
|
|
|
21 |
|
| 395 |
ira |
22 |
class Key:
|
| 377 |
ira |
23 |
"""A class that makes a unique key for each pair of vertices."""
|
|
|
24 |
|
|
|
25 |
def __init__ (self, v1, v2):
|
|
|
26 |
self.__key = [v1, v2]
|
|
|
27 |
self.__key.sort ()
|
|
|
28 |
|
|
|
29 |
def __eq__ (self, rhs):
|
|
|
30 |
return (self.__key[0] == rhs.__key[0]) and (self.__key[1] == rhs.__key[1])
|
|
|
31 |
|
|
|
32 |
|
| 395 |
ira |
33 |
class DrawGraph:
|
| 377 |
ira |
34 |
"""A class that will draw an unweighted, undirected graph given the vertices and
|
|
|
35 |
edges in the same form that is used for the Graph class."""
|
|
|
36 |
|
| 379 |
ira |
37 |
def __init__ (self, name, graph):
|
| 377 |
ira |
38 |
"""Constructor"""
|
|
|
39 |
self.__name = str(name)
|
| 379 |
ira |
40 |
self.__graph = graph
|
| 377 |
ira |
41 |
|
| 395 |
ira |
42 |
def render_stupid (self, prop=None):
|
|
|
43 |
"""Print the graph using the not very good method of telling whether
|
|
|
44 |
we go up,down,left,right between every node."""
|
|
|
45 |
if prop == None:
|
|
|
46 |
raise ValueError # no propery given
|
|
|
47 |
|
|
|
48 |
g = self.__graph
|
|
|
49 |
added = True
|
|
|
50 |
count = 0
|
|
|
51 |
|
|
|
52 |
while added:
|
|
|
53 |
added = False
|
|
|
54 |
|
|
|
55 |
for v in g.vertices.keys():
|
|
|
56 |
if g.get_vertex_value (v) == str(count):
|
|
|
57 |
print getattr (g.vertices[v].raw_obj, prop),
|
|
|
58 |
added = True
|
|
|
59 |
|
|
|
60 |
count += 1
|
|
|
61 |
|
|
|
62 |
print # nothing, to end the getattr() print above
|
|
|
63 |
print '%d nodes in the solution' % len(g.vertices)
|
|
|
64 |
|
|
|
65 |
def render_graphviz (self, filename, layout_engine=yapgvb.engines.neato):
|
| 378 |
ira |
66 |
"""Draw the graph given into the file given. This will render
|
|
|
67 |
to SVG, PNG, and JPG."""
|
| 395 |
ira |
68 |
|
|
|
69 |
if no_graphviz:
|
|
|
70 |
raise ValueError # no yapgvb / graphviz installed!
|
|
|
71 |
|
| 377 |
ira |
72 |
dg = yapgvb.Graph (self.__name)
|
| 379 |
ira |
73 |
g = self.__graph
|
| 377 |
ira |
74 |
|
|
|
75 |
visited = []
|
|
|
76 |
|
|
|
77 |
for v in g.vertices:
|
|
|
78 |
node = dg.add_node (str(v), label=str(v),
|
| 391 |
ira |
79 |
shape=g.get_vertex_shape(v), fontsize=14)
|
| 377 |
ira |
80 |
|
|
|
81 |
for c in g.get_children (v):
|
|
|
82 |
k = Key (v, c)
|
|
|
83 |
|
|
|
84 |
if k not in visited:
|
|
|
85 |
node_c = dg.add_node (str(c), label=str(c),
|
| 391 |
ira |
86 |
shape=g.get_vertex_shape (c),
|
| 377 |
ira |
87 |
fontsize=14)
|
| 379 |
ira |
88 |
edge = dg.add_edge (node, node_c)
|
|
|
89 |
edge.color = g.get_edge_color (v, c)
|
|
|
90 |
edge.label = g.get_edge_label (v, c)
|
| 377 |
ira |
91 |
visited.append (k)
|
|
|
92 |
|
|
|
93 |
# Do the rendering
|
| 387 |
ira |
94 |
dg.layout (layout_engine)
|
| 377 |
ira |
95 |
dg.render (filename)
|
|
|
96 |
|
|
|
97 |
|
|
|
98 |
def main ():
|
|
|
99 |
|
|
|
100 |
vertices = ['a', 'b', 'c', 'd', 'e']
|
|
|
101 |
edges = ( ['a', 'b'], ['a', 'c'], ['a', 'd'], ['b', 'a'], ['b', 'c'],
|
|
|
102 |
['b', 'e'], ['e', 'b'], ['e', 'c'], ['e', 'd'],
|
|
|
103 |
['d', 'a'], ['d', 'c'], ['d', 'e'], ['c', 'a'],
|
|
|
104 |
['c', 'b'], ['c', 'd'], ['c', 'e'] )
|
|
|
105 |
|
|
|
106 |
print 'Drawing graph g1.svg'
|
| 379 |
ira |
107 |
g1 = Graph.Graph (vertices, edges)
|
| 395 |
ira |
108 |
DrawGraph ('g1', g1).render_graphviz ('g1.svg')
|
| 377 |
ira |
109 |
|
|
|
110 |
vertices = range (11)
|
|
|
111 |
edges = ( [0, 1], [0, 9], [0, 8], [8, 10], [8, 7], [9, 6], [9, 5],
|
|
|
112 |
[6, 7], [1, 4], [1, 3], [1, 2], [2, 0] )
|
|
|
113 |
|
|
|
114 |
print 'Drawing graph g2.svg'
|
| 379 |
ira |
115 |
g2 = Graph.Graph (vertices, edges)
|
| 395 |
ira |
116 |
DrawGraph ('g2', g2).render_graphviz ('g2.svg')
|
| 377 |
ira |
117 |
|
|
|
118 |
vertices = range (3)
|
|
|
119 |
edges = ( [0, 1], [1, 2], [2, 0] )
|
|
|
120 |
|
|
|
121 |
print 'Drawing graph g3.svg'
|
| 379 |
ira |
122 |
g3 = Graph.Graph (vertices, edges)
|
| 395 |
ira |
123 |
DrawGraph ('g3', g3).render_graphviz ('g3.svg')
|
| 377 |
ira |
124 |
|
|
|
125 |
|
|
|
126 |
if __name__ == '__main__':
|
|
|
127 |
main ()
|
|
|
128 |
|