I am creating a graph with 5 nodes in it (A,B,C,D,E) and edges/weights ("A","D",1),("D","B",3),("E","D",5),("C","B",4),("B","E",2)
I want to create a function that will create edges AB,AC,AE,BC,CE for me by summing the edges along the path from (for example) AB, which would be BD with weight 3 + AD with weight 1.
What I mean is if AD is an edge with weight 1 and DB is an edge with weight 3, I want the function to create edge AB with weight 4. I have a graph and vertex class but do not know where to go from here.
class Vertex:
def __init__(self, node):
self.id = node
self.adjacent = {}
def __str__(self):
return str(self.id) + ' adjacent: ' + str([x.id for x in self.adjacent])
def add_neighbor(self, neighbor, weight=0):
self.adjacent[neighbor] = weight
def get_connections(self):
return self.adjacent.keys()
def get_id(self):
return self.id
def get_weight(self, neighbor):
return self.adjacent[neighbor]
class Graph:
def __init__(self):
self.vert_dict = {}
self.num_vertices = 0
def __iter__(self):
return iter(self.vert_dict.values())
def add_vertex(self, node):
self.num_vertices = self.num_vertices + 1
new_vertex = Vertex(node)
self.vert_dict[node] = new_vertex
return new_vertex
def get_vertex(self, n):
if n in self.vert_dict:
return self.vert_dict[n]
else:
return None
def add_edge(self, frm, to, cost = 0):
if frm not in self.vert_dict:
self.add_vertex(frm)
if to not in self.vert_dict:
self.add_vertex(to)
self.vert_dict[frm].add_neighbor(self.vert_dict[to], cost)
self.vert_dict[to].add_neighbor(self.vert_dict[frm], cost)
def get_vertices(self):
return self.vert_dict.keys()
if __name__ == '__main__':
g = Graph()
g.add_vertex('a')
g.add_vertex('b')
g.add_vertex('c')
g.add_vertex('d')
g.add_vertex('e')
allEdges = [("A", "D"), ("D", "B"), ("E", "D"), ("C", "B"), ("B", "E")]
nodes = ["A", "B", "C", "D", "E"]
g.add_edge('a', 'd', 1)
g.add_edge('d', 'b', 3)
g.add_edge('e', 'd', 5)
g.add_edge('b', 'c', 4)
g.add_edge('b', 'e', 2)
for v in g:
for w in v.get_connections():
vid = v.get_id()
wid = w.get_id()
print '( %s , %s, %3d)' % ( vid, wid, v.get_weight(w))
for v in g:
print 'g.vert_dict[%s]=%s' %(v.get_id(), g.vert_dict[v.get_id()])