def _dijkstra_adjacency_list(graph: Graph, start: str, target: str): V = len(graph.vertices) visited, dist, pred = {}, {}, {} for v in graph.vertices: visited[v] = False pred[v] = None if v != start: dist[v] = float('inf') dist[start] = 0 pq = PriorityQueue(implementation='binomial_heap') for vertex in dist: pq.push(vertex, dist[vertex]) for _ in range(V): u = pq.pop() visited[u] = True for v in graph.vertices: edge_str = u + '_' + v if (edge_str in graph.edge_weights and graph.edge_weights[edge_str].value > 0 and visited[v] is False and dist[v] > dist[u] + graph.edge_weights[edge_str].value): dist[v] = dist[u] + graph.edge_weights[edge_str].value pred[v] = u pq.push(v, dist[v]) if target != "": return (dist[target], pred) return dist, pred
def _minimum_spanning_tree_prim_adjacency_list(graph): q = PriorityQueue(implementation='binomial_heap') e = dict() mst = Graph(implementation='adjacency_list') q.push(next(iter(graph.vertices)), 0) while not q.is_empty: v = q.pop() if not hasattr(mst, v): mst.add_vertex(graph.__getattribute__(v)) if e.get(v, None) is not None: edge = e[v] mst.add_vertex(edge.target) mst.add_edge(edge.source.name, edge.target.name, edge.value) mst.add_edge(edge.target.name, edge.source.name, edge.value) for w_node in graph.neighbors(v): w = w_node.name vw = graph.edge_weights[v + '_' + w] q.push(w, vw.value) if e.get(w, None) is None or \ e[w].value > vw.value: e[w] = vw return mst