Subversion Repositories programming

Rev

Rev 399 | Details | Compare with Previous | 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
399 ira 36
		we go up,down,left,right between every node.
37
		NOTE: This tells us HOW each node was generated, not the path taken
38
		through the graph. Use render_graphviz() for a MUCH improved visualization"""
395 ira 39
		if prop == None:
40
			raise ValueError # no propery given
41
 
42
		g = self.__graph
43
 
399 ira 44
		# Get the vertices sorted in the order in which they were added to
45
		# the graph.
46
		sorted_vertices = [(int (g.get_vertex_value (v)), g.vertices[v].raw_obj)
47
					for v in g.vertices.keys()]
48
		sorted_vertices.sort()
395 ira 49
 
399 ira 50
		for v in sorted_vertices:
51
			print getattr (v[1], prop),
395 ira 52
 
399 ira 53
		print
54
		print '%d nodes in the solution\n' % (len(g.vertices), )
395 ira 55
 
397 ira 56
	def render_graphviz (self, filename, layout_engine=yapgvb.engines.neato):
378 ira 57
		"""Draw the graph given into the file given. This will render
58
		to SVG, PNG, and JPG."""
395 ira 59
 
397 ira 60
		if not have_yapgvb():
61
			raise ValueError # must have yapgvb installed
395 ira 62
 
377 ira 63
		dg = yapgvb.Graph (self.__name)
379 ira 64
		g = self.__graph
377 ira 65
 
66
		visited = []
67
 
397 ira 68
		for v in g.vertices.keys():
377 ira 69
			node = dg.add_node (str(v), label=str(v),
391 ira 70
					shape=g.get_vertex_shape(v), fontsize=14)
377 ira 71
 
72
			for c in g.get_children (v):
73
				k = Key (v, c)
74
 
75
				if k not in visited:
76
					node_c = dg.add_node (str(c), label=str(c),
391 ira 77
							shape=g.get_vertex_shape (c),
377 ira 78
							fontsize=14)
379 ira 79
					edge = dg.add_edge (node, node_c)
80
					edge.color = g.get_edge_color (v, c)
81
					edge.label = g.get_edge_label (v, c)
377 ira 82
					visited.append (k)
83
 
84
		# Do the rendering
387 ira 85
		dg.layout (layout_engine)
377 ira 86
		dg.render (filename)
87
 
88
 
89
def main ():
90
 
91
	vertices = ['a', 'b', 'c', 'd', 'e']
92
	edges = ( ['a', 'b'], ['a', 'c'], ['a', 'd'], ['b', 'a'], ['b', 'c'],
93
			['b', 'e'], ['e', 'b'], ['e', 'c'], ['e', 'd'],
94
			['d', 'a'], ['d', 'c'], ['d', 'e'], ['c', 'a'],
95
			['c', 'b'], ['c', 'd'], ['c', 'e'] )
96
 
97
	print 'Drawing graph g1.svg'
379 ira 98
	g1 = Graph.Graph (vertices, edges)
395 ira 99
	DrawGraph ('g1', g1).render_graphviz ('g1.svg')
377 ira 100
 
101
	vertices = range (11)
102
	edges = ( [0, 1], [0, 9], [0, 8], [8, 10], [8, 7], [9, 6], [9, 5],
103
			[6, 7], [1, 4], [1, 3], [1, 2], [2, 0] )
104
 
105
	print 'Drawing graph g2.svg'
379 ira 106
	g2 = Graph.Graph (vertices, edges)
395 ira 107
	DrawGraph ('g2', g2).render_graphviz ('g2.svg')
377 ira 108
 
109
	vertices = range (3)
110
	edges = ( [0, 1], [1, 2], [2, 0] )
111
 
112
	print 'Drawing graph g3.svg'
379 ira 113
	g3 = Graph.Graph (vertices, edges)
395 ira 114
	DrawGraph ('g3', g3).render_graphviz ('g3.svg')
377 ira 115
 
116
 
117
if __name__ == '__main__':
118
	main ()
119