Subversion Repositories programming

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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