Subversion Repositories programming

Rev

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

Rev 396 Rev 397
Line 2... Line 2...
2
 
2
 
3
__author__    = "Ira W. Snyder (devel@irasnyder.com)"
3
__author__    = "Ira W. Snyder (devel@irasnyder.com)"
4
__copyright__ = "Copyright (c) 2006 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)"
5
__license__   = "GNU GPL v2 (or, at your option, any later version)"
6
 
6
 
7
# Fix for the lame version of python on the school's computers (v2.1)
7
# Fix for old version on school computers
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
8
from PyCompat import *
19
 
9
 
20
import Graph
10
import Graph
-
 
11
if have_yapgvb():
-
 
12
	import yapgvb
21
 
13
 
22
class Key:
14
class Key (object):
23
	"""A class that makes a unique key for each pair of vertices."""
15
	"""A class that makes a unique key for each pair of vertices."""
24
 
16
 
25
	def __init__ (self, v1, v2):
17
	def __init__ (self, v1, v2):
26
		self.__key = [v1, v2]
18
		self.__key = [v1, v2]
27
		self.__key.sort ()
19
		self.__key.sort ()
28
 
20
 
29
	def __eq__ (self, rhs):
21
	def __eq__ (self, rhs):
30
		return (self.__key[0] == rhs.__key[0]) and (self.__key[1] == rhs.__key[1])
22
		return (self.__key[0] == rhs.__key[0]) and (self.__key[1] == rhs.__key[1])
31
 
23
 
32
 
24
 
33
class DrawGraph:
25
class DrawGraph (object):
34
	"""A class that will draw an unweighted, undirected graph given the vertices and
26
	"""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."""
27
	edges in the same form that is used for the Graph class."""
36
 
28
 
37
	def __init__ (self, name, graph):
29
	def __init__ (self, name, graph):
38
		"""Constructor"""
30
		"""Constructor"""
Line 60... Line 52...
60
			count += 1
52
			count += 1
61
 
53
 
62
		print # nothing, to end the getattr() print above
54
		print # nothing, to end the getattr() print above
63
		print '%d nodes in the solution' % len(g.vertices)
55
		print '%d nodes in the solution' % len(g.vertices)
64
 
56
 
65
	def render_graphviz (self, filename, layout_engine='neato'):
57
	def render_graphviz (self, filename, layout_engine=yapgvb.engines.neato):
66
		"""Draw the graph given into the file given. This will render
58
		"""Draw the graph given into the file given. This will render
67
		to SVG, PNG, and JPG."""
59
		to SVG, PNG, and JPG."""
68
 
60
 
69
		if no_graphviz:
61
		if not have_yapgvb():
70
			raise ValueError # no yapgvb / graphviz installed!
62
			raise ValueError # must have yapgvb installed
71
 
63
 
72
		dg = yapgvb.Graph (self.__name)
64
		dg = yapgvb.Graph (self.__name)
73
		g = self.__graph
65
		g = self.__graph
74
 
66
 
75
		visited = []
67
		visited = []
76
 
68
 
77
		for v in g.vertices:
69
		for v in g.vertices.keys():
78
			node = dg.add_node (str(v), label=str(v),
70
			node = dg.add_node (str(v), label=str(v),
79
					shape=g.get_vertex_shape(v), fontsize=14)
71
					shape=g.get_vertex_shape(v), fontsize=14)
80
 
72
 
81
			for c in g.get_children (v):
73
			for c in g.get_children (v):
82
				k = Key (v, c)
74
				k = Key (v, c)