from base_graph import getg1 from priority_queue1 import PriorityQueue import sys def prim(g, s): for x in g: x.distance = sys.maxsize x.parent = None x.color = "White" s.distance = 0 s.parent = 0 pq = PriorityQueue() pq.buildQueue([[x.distance, x] for x in g]) while pq.isEmpty() == False: cur = pq.extractMin()[1] cur.color = "Black" for x in cur.getNeighbours(): if x.distance > cur.getNeighbourWeight(x) and x.color == "White": x.parent = cur x.distance = cur.getNeighbourWeight(x) pq.decreaseKey(x, x.distance) if __name__ == "__main__": g = getg1() prim(g, g.getVertex('a')) for x in g: print x.key, x.distance, x.parent
from union_find2 import UnionFind from base_graph import getg1 def kruskal(g): v = [x for x in g] edges = sorted( [[u,y,u.getNeighbourWeight(y)] for u in v for y in u.getNeighbours()] , key=lambda x: x[2]) uf = UnionFind() uf.make_set(v) for x in edges: if uf.find(x[0]) != uf.find(x[1]): print 'Adding Edge from {0} to {1} with weight {2}'.format(x[0].key, x[1].key, x[2]) uf.union(x[0],x[1]) if __name__ == "__main__": kruskal(getg1())
from base_graph import getg1 from priority_queue1 import PriorityQueue import sys def prim(g,s): for x in g: x.distance = sys.maxsize x.parent = None x.color = "White" s.distance = 0 s.parent = 0 pq = PriorityQueue() pq.buildQueue([[x.distance,x] for x in g]) while pq.isEmpty() == False: cur = pq.extractMin()[1] cur.color = "Black" for x in cur.getNeighbours(): if x.distance > cur.getNeighbourWeight(x) and x.color == "White": x.parent = cur x.distance = cur.getNeighbourWeight(x) pq.decreaseKey(x,x.distance) if __name__ == "__main__": g = getg1() prim(g,g.getVertex('a')) for x in g: print x.key, x.distance, x.parent