Advertisement
Guest User

Untitled

a guest
May 25th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. import math
  2.  
  3. class Vertex:
  4.     def __init__(self, data = None,data2 = None, ins=None,outs = None, parent = None):
  5.         self.data2 = data2
  6.         self.data = data
  7.         self.ins = ins
  8.         self.outs = outs
  9.         self.parent = parent
  10.  
  11. class Edge:
  12.     def __init__(self, source = None, destination = None, weight = None):
  13.         self.source = source
  14.         self.destination = destination
  15.         self.weight = weight
  16.  
  17. def initialize_single_source(G, s):
  18.     for vertex in G:
  19.         vertex.data2 = math.inf
  20.         vertex.parent = Vertex(data = 'Nothing')
  21.     s.data2 = 0
  22.  
  23.  
  24. def relax(u, v, w):
  25.     if v.data2 > (u.data2 + w(u, v)):
  26.         v.data2 = u.data2 + w(u, v)
  27.         v.parent = u
  28.  
  29. def w(u, v):
  30.     for i in u.outs:
  31.         if i.destination == v:
  32.             temp = i
  33.     return temp.weight
  34.  
  35. def Bellman_Ford(G, w, s):
  36.     initialize_single_source(G, s)
  37.     for i in range(0, len(G)):
  38.         for vertex in G:
  39.             for edge in vertex.outs:
  40.                 relax(edge.source, edge.destination, w)
  41.     for vertex in G:
  42.         for edge in vertex.outs:
  43.             if edge.destination.data2 > edge.source.data2 + w(edge.source, edge.destination):
  44.                 return 0
  45.         return 1
  46.  
  47. def MakeGraph():
  48.     a = Vertex(data = 'a', ins=[], outs=[])
  49.     b = Vertex(data='b',ins=[], outs=[])
  50.     c = Vertex(data='c', ins=[], outs=[])
  51.     d = Vertex(data='d',ins=[], outs=[])
  52.     e = Vertex(data='e', ins=[], outs=[])
  53.     f = Vertex(data='f', ins=[], outs=[])
  54.     g = Vertex(data='g', ins=[], outs=[])
  55.  
  56.     ab = Edge(source=a, destination=b, weight=8)
  57.     a.outs.append(ab)
  58.     b.ins.append(ab)
  59.     ac = Edge(source=a, destination=c, weight=6)
  60.     a.outs.append(ac)
  61.     c.ins.append(ac)
  62.     bd = Edge(source=b, destination=d, weight=10)
  63.     b.outs.append(bd)
  64.     d.ins.append(bd)
  65.     cd = Edge(source=c, destination=d, weight=15)
  66.     c.outs.append(cd)
  67.     d.ins.append(cd)
  68.     ce = Edge(source=c, destination=e, weight=9)
  69.     c.outs.append(ce)
  70.     e.ins.append(ce)
  71.     de = Edge(source=d, destination=e, weight=14)
  72.     d.outs.append(de)
  73.     e.ins.append(de)
  74.     df = Edge(source=d, destination=f, weight=4)
  75.     d.outs.append(df)
  76.     f.ins.append(df)
  77.     ef = Edge(source=e, destination=f, weight=13)
  78.     e.outs.append(ef)
  79.     f.ins.append(ef)
  80.     eg = Edge(source=e, destination=g, weight=17)
  81.     e.outs.append(eg)
  82.     g.ins.append(eg)
  83.     fg = Edge(source=f, destination=g, weight=7)
  84.     f.outs.append(fg)
  85.     g.ins.append(fg)
  86.  
  87.     list = [a,b,c,d,e,f,g]
  88.     return list
  89.  
  90. graf = MakeGraph()
  91. for item in graf:
  92.     print(item.data)
  93. print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement