comparison Graph.py @ 4:9d0cf96b6a3b

Updates to Graph.py after looking at Prof. Downey's code.
author Brian Neal <bgneal@gmail.com>
date Thu, 29 Nov 2012 20:37:03 -0600
parents de3ae15ebbfa
children 8e44660965ef
comparison
equal deleted inserted replaced
3:e3d0a85354b3 4:9d0cf96b6a3b
86 return None 86 return None
87 87
88 def remove_edge(self, e): 88 def remove_edge(self, e):
89 """Removes the edge e from the graph.""" 89 """Removes the edge e from the graph."""
90 90
91 for x in self.iterkeys(): 91 v, w = e
92 remove = [k for k, v in self[x].iteritems() if v is e] 92 del self[v][w]
93 for k in remove: 93 del self[w][v]
94 del self[x][k]
95 94
96 def vertices(self): 95 def vertices(self):
97 """Returns a list of the vertices in the graph.""" 96 """Returns a list of the vertices in the graph."""
98 97
99 return self.keys() 98 return self.keys()
100 99
101 def edges(self): 100 def edges(self):
102 """"Returns a list of the edges in the graph.""" 101 """"Returns a list of the edges in the graph."""
103 102
104 edge_set = set() 103 edge_set = set()
105 for x in self.iterkeys(): 104 for d in self.itervalues():
106 for e in self[x].itervalues(): 105 edge_set.update(d.itervalues())
107 edge_set.add(e)
108 106
109 return list(edge_set) 107 return list(edge_set)
110 108
111 def out_vertices(self, v): 109 def out_vertices(self, v):
112 """Returns a list of vertices that are adjacent to the given vertex v. 110 """Returns a list of vertices that are adjacent to the given vertex v.