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 |
|
377 |
ira |
7 |
import Graph
|
|
|
8 |
import yapgvb
|
|
|
9 |
|
|
|
10 |
class Key (object):
|
|
|
11 |
"""A class that makes a unique key for each pair of vertices."""
|
|
|
12 |
|
|
|
13 |
def __init__ (self, v1, v2):
|
|
|
14 |
self.__key = [v1, v2]
|
|
|
15 |
self.__key.sort ()
|
|
|
16 |
|
|
|
17 |
def __eq__ (self, rhs):
|
|
|
18 |
return (self.__key[0] == rhs.__key[0]) and (self.__key[1] == rhs.__key[1])
|
|
|
19 |
|
|
|
20 |
|
|
|
21 |
class DrawGraph (object):
|
|
|
22 |
"""A class that will draw an unweighted, undirected graph given the vertices and
|
|
|
23 |
edges in the same form that is used for the Graph class."""
|
|
|
24 |
|
|
|
25 |
def __init__ (self, name, vertices, edges):
|
|
|
26 |
"""Constructor"""
|
|
|
27 |
self.__name = str(name)
|
|
|
28 |
self.__vertices = vertices
|
|
|
29 |
self.__edges = edges
|
|
|
30 |
|
|
|
31 |
def render_graph (self, filename):
|
378 |
ira |
32 |
"""Draw the graph given into the file given. This will render
|
|
|
33 |
to SVG, PNG, and JPG."""
|
377 |
ira |
34 |
dg = yapgvb.Graph (self.__name)
|
|
|
35 |
g = Graph.Graph (self.__vertices, self.__edges)
|
|
|
36 |
|
|
|
37 |
visited = []
|
|
|
38 |
|
|
|
39 |
for v in g.vertices:
|
|
|
40 |
node = dg.add_node (str(v), label=str(v),
|
|
|
41 |
shape=yapgvb.shapes.circle, fontsize=14)
|
|
|
42 |
|
|
|
43 |
for c in g.get_children (v):
|
|
|
44 |
k = Key (v, c)
|
|
|
45 |
|
|
|
46 |
if k not in visited:
|
|
|
47 |
node_c = dg.add_node (str(c), label=str(c),
|
|
|
48 |
shape=yapgvb.shapes.circle,
|
|
|
49 |
fontsize=14)
|
|
|
50 |
node >> node_c
|
|
|
51 |
visited.append (k)
|
|
|
52 |
|
|
|
53 |
# Do the rendering
|
|
|
54 |
dg.layout (yapgvb.engines.neato)
|
|
|
55 |
dg.render (filename)
|
|
|
56 |
|
|
|
57 |
|
|
|
58 |
def main ():
|
|
|
59 |
|
|
|
60 |
vertices = ['a', 'b', 'c', 'd', 'e']
|
|
|
61 |
edges = ( ['a', 'b'], ['a', 'c'], ['a', 'd'], ['b', 'a'], ['b', 'c'],
|
|
|
62 |
['b', 'e'], ['e', 'b'], ['e', 'c'], ['e', 'd'],
|
|
|
63 |
['d', 'a'], ['d', 'c'], ['d', 'e'], ['c', 'a'],
|
|
|
64 |
['c', 'b'], ['c', 'd'], ['c', 'e'] )
|
|
|
65 |
|
|
|
66 |
print 'Drawing graph g1.svg'
|
|
|
67 |
DrawGraph ('g1', vertices, edges).render_graph ('g1.svg')
|
|
|
68 |
|
|
|
69 |
vertices = range (11)
|
|
|
70 |
edges = ( [0, 1], [0, 9], [0, 8], [8, 10], [8, 7], [9, 6], [9, 5],
|
|
|
71 |
[6, 7], [1, 4], [1, 3], [1, 2], [2, 0] )
|
|
|
72 |
|
|
|
73 |
print 'Drawing graph g2.svg'
|
|
|
74 |
DrawGraph ('g2', vertices, edges).render_graph ('g2.svg')
|
|
|
75 |
|
|
|
76 |
vertices = range (3)
|
|
|
77 |
edges = ( [0, 1], [1, 2], [2, 0] )
|
|
|
78 |
|
|
|
79 |
print 'Drawing graph g3.svg'
|
|
|
80 |
DrawGraph ('g3', vertices, edges).render_graph ('g3.svg')
|
|
|
81 |
|
|
|
82 |
|
|
|
83 |
if __name__ == '__main__':
|
|
|
84 |
main ()
|
|
|
85 |
|