def prim(G, root): MST = weighted_graph.Graph(G.num_nodes()) inMST = [False] * G.num_nodes() graph_PQ = graph_priority_queue.MinHeap(G.num_nodes()) root_node = NodeData(root, root, 0) graph_PQ.insert(root_node) MSTwt = 0 while not graph_PQ.is_empty(): element = graph_PQ.pop() MST.set_vertex(element.vertex, G.get_node(element.vertex).data) inMST[element.vertex] = True if element.vertex != element.MST_vertex: MST.add_edge(element.vertex, element.MST_vertex, element.weight) MSTwt = MSTwt + element.weight adjlist = G.get_node(element.vertex).get_edges() for MST_vertex, candidate_vertex, wt in adjlist: if not inMST[candidate_vertex]: newelt = NodeData(candidate_vertex, MST_vertex, wt) graph_PQ.insert(newelt) print('MST weight: ' + str(MSTwt)) return MST
def kruskal(G): set_roots = initialize_set(G.num_nodes()) MST = weighted_graph.Graph(G.num_nodes()) for i in range(G.num_nodes()): MST.set_vertex(i, G.get_node(i).data) E = G.sort_edges() MSTwt = 0 for i in range(len(E)): u, v, w = E[i] rootu = find_root(set_roots, u) rootv = find_root(set_roots, v) if rootu != rootv: union(set_roots, rootu, rootv) print('Adding edge: ' + str((u, v, w))) MST.add_edge(u, v, w) MSTwt = MSTwt + w print('MST weight: ' + str(MSTwt)) return MST
import weighted_graph import kruskal node_numbers = { 'N1': 0, 'N2': 1, 'N3': 2, 'N4': 3, 'N5': 4, 'N6': 5, 'N7': 6, 'N8': 7 } G = weighted_graph.Graph(len(node_numbers)) for node in node_numbers: G.set_vertex(node_numbers[node], node) G.add_edge(node_numbers['N1'], node_numbers['N2'], 3) G.add_edge(node_numbers['N1'], node_numbers['N4'], 2) G.add_edge(node_numbers['N1'], node_numbers['N3'], 3) G.add_edge(node_numbers['N2'], node_numbers['N4'], 9) G.add_edge(node_numbers['N2'], node_numbers['N5'], 4) G.add_edge(node_numbers['N2'], node_numbers['N7'], 2) G.add_edge(node_numbers['N3'], node_numbers['N4'], 4) G.add_edge(node_numbers['N3'], node_numbers['N5'], 1) G.add_edge(node_numbers['N3'], node_numbers['N7'], 6) G.add_edge(node_numbers['N3'], node_numbers['N8'], 6)
def dijkstra(G,s): span_tree = weighted_graph.Graph(G.num_nodes()) in_span_tree = [False]*G.num_nodes() distances = [float('inf')]*G.num_nodes() distances[s] = 0 predecessors = [-1]*G.num_nodes() E = [] graph_PQ = graph_priority_queue.MinHeap(G.num_nodes()) for v in range(G.num_nodes()): E.extend(G.get_node(v).get_edges()) source = NodeData(s,-1,0) graph_PQ.insert(source) while not graph_PQ.is_empty(): element = graph_PQ.pop() span_tree.set_vertex(element.vertex, G.get_node(element.vertex).data) in_span_tree[element.vertex] = True if element.Svertex != -1: span_tree.add_edge(element.vertex, element.Svertex, element.weight) adjlist = G.get_node(element.vertex).get_edges() for Svertex, candidate_vertex, wt in adjlist: if in_span_tree[candidate_vertex] == False: if (distances[candidate_vertex] > (distances[Svertex] + wt)): predecessors[candidate_vertex] = Svertex distances[candidate_vertex] = distances[Svertex]+wt newelt = NodeData(candidate_vertex, Svertex, distances[candidate_vertex]) graph_PQ.insert(newelt) for v in range(G.num_nodes()): if v == s: continue if (predecessors[v]==-1 or distances[v] == float('inf')): print('NO PATH from node '+ str(s) + ' to node '+ str(v)) continue else: print('Shortest path (cost = '+ str(distances[v])+ ') from node '+str(s)+' to node '+str(v)+': ') stack = [v] done = False current_node = v while not done: current_node = predecessors[current_node] if current_node != -1: stack.append(current_node) else: done = True stack.reverse() print(stack)
import weighted_graph g = weighted_graph.Graph() g.add_edge("A", "B", 2) g.add_edge("A", "C", 2) g.add_edge("A", "E", 0) g.add_edge("B", "D", 0) g.add_edge("B", "F", 0) g.add_edge("C", "G", 2) g.add_edge("G", "B", 1) g.add_edge("F", "E") g.add_edge("E", "A", 0) g.add_node("I") pathFW = g.FloydWarshall('A', 'I') pathD = g.dijkstra('A', 'I') print(pathFW) print(pathD)