'''
reverse a Graph as a separate function
( so not shown until all homework is in)
'''
from graph import Edge, Graph
def reverse(g):
'''Return a new Graph that is the reverse of g.'''
edges = {} # Dictionary will end up so edges[v] = v's neighbor list
for v in g.vertices:
edges[v] = [] # initially each list is empty
#dict comprehension for 3 lines above: edges = {v:[] for v in g.vertices}
for v in g.vertices:
for e in g.edges[v]:
endList = edges[e.end]
endList.append(Edge(e.end, v, e.wt))
return Graph(g.vertices, edges, g.isDirected)
# The version above is an awkward implementation designed originally to hide
# the homework code. The cleaner version would replace the Graph
# instance version with the code below, which is the same as the code above,
# except with all references to g replaced by self to follow Python convention:
## def reverse(self):
## '''Return a new Graph that is the reverse of self.'''
## edges = {} # Dictionary will end up so edges[v] = v's neighbor list
## for v in self.vertices:
## edges[v] = [] # initially each list is empty
## #dict comprehension for 3 lines above: edges = {v:[] for v in g.vertices}
## for v in self.vertices:
## for e in self.edges[v]:
## endList = edges[e.end]
## endList.append(Edge(e.end, v, e.wt))
## return Graph(self.vertices, edges, self.isDirected)