Subversion Repositories programming

Rev

Rev 378 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 378 Rev 379
Line 20... Line 20...
20
 
20
 
21
class DrawGraph (object):
21
class DrawGraph (object):
22
	"""A class that will draw an unweighted, undirected graph given the vertices and
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."""
23
	edges in the same form that is used for the Graph class."""
24
 
24
 
25
	def __init__ (self, name, vertices, edges):
25
	def __init__ (self, name, graph):
26
		"""Constructor"""
26
		"""Constructor"""
27
		self.__name = str(name)
27
		self.__name = str(name)
28
		self.__vertices = vertices
-
 
29
		self.__edges = edges
28
		self.__graph = graph
30
 
29
 
31
	def render_graph (self, filename):
30
	def render_graph (self, filename):
32
		"""Draw the graph given into the file given. This will render
31
		"""Draw the graph given into the file given. This will render
33
		to SVG, PNG, and JPG."""
32
		to SVG, PNG, and JPG."""
34
		dg = yapgvb.Graph (self.__name)
33
		dg = yapgvb.Graph (self.__name)
35
		g = Graph.Graph (self.__vertices, self.__edges)
34
		g = self.__graph
36
 
35
 
37
		visited = []
36
		visited = []
38
 
37
 
39
		for v in g.vertices:
38
		for v in g.vertices:
40
			node = dg.add_node (str(v), label=str(v),
39
			node = dg.add_node (str(v), label=str(v),
Line 45... Line 44...
45
 
44
 
46
				if k not in visited:
45
				if k not in visited:
47
					node_c = dg.add_node (str(c), label=str(c),
46
					node_c = dg.add_node (str(c), label=str(c),
48
							shape=yapgvb.shapes.circle,
47
							shape=yapgvb.shapes.circle,
49
							fontsize=14)
48
							fontsize=14)
50
					node >> node_c
49
					edge = dg.add_edge (node, node_c)
-
 
50
					edge.color = g.get_edge_color (v, c)
-
 
51
					edge.label = g.get_edge_label (v, c)
51
					visited.append (k)
52
					visited.append (k)
52
 
53
 
53
		# Do the rendering
54
		# Do the rendering
54
		dg.layout (yapgvb.engines.neato)
55
		dg.layout (yapgvb.engines.neato)
55
		dg.render (filename)
56
		dg.render (filename)
Line 62... Line 63...
62
			['b', 'e'], ['e', 'b'], ['e', 'c'], ['e', 'd'],
63
			['b', 'e'], ['e', 'b'], ['e', 'c'], ['e', 'd'],
63
			['d', 'a'], ['d', 'c'], ['d', 'e'], ['c', 'a'],
64
			['d', 'a'], ['d', 'c'], ['d', 'e'], ['c', 'a'],
64
			['c', 'b'], ['c', 'd'], ['c', 'e'] )
65
			['c', 'b'], ['c', 'd'], ['c', 'e'] )
65
 
66
 
66
	print 'Drawing graph g1.svg'
67
	print 'Drawing graph g1.svg'
-
 
68
	g1 = Graph.Graph (vertices, edges)
67
	DrawGraph ('g1', vertices, edges).render_graph ('g1.svg')
69
	DrawGraph ('g1', g1).render_graph ('g1.svg')
68
 
70
 
69
	vertices = range (11)
71
	vertices = range (11)
70
	edges = ( [0, 1], [0, 9], [0, 8], [8, 10], [8, 7], [9, 6], [9, 5],
72
	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] )
73
			[6, 7], [1, 4], [1, 3], [1, 2], [2, 0] )
72
 
74
 
73
	print 'Drawing graph g2.svg'
75
	print 'Drawing graph g2.svg'
-
 
76
	g2 = Graph.Graph (vertices, edges)
74
	DrawGraph ('g2', vertices, edges).render_graph ('g2.svg')
77
	DrawGraph ('g2', g2).render_graph ('g2.svg')
75
 
78
 
76
	vertices = range (3)
79
	vertices = range (3)
77
	edges = ( [0, 1], [1, 2], [2, 0] )
80
	edges = ( [0, 1], [1, 2], [2, 0] )
78
 
81
 
79
	print 'Drawing graph g3.svg'
82
	print 'Drawing graph g3.svg'
-
 
83
	g3 = Graph.Graph (vertices, edges)
80
	DrawGraph ('g3', vertices, edges).render_graph ('g3.svg')
84
	DrawGraph ('g3', g3).render_graph ('g3.svg')
81
 
85
 
82
 
86
 
83
if __name__ == '__main__':
87
if __name__ == '__main__':
84
	main ()
88
	main ()
85
 
89