Exemple #1
0
		weight = weighted_graph.W[edge]
		u, v = edge
		if dist[v] > (dist[u] + weight):
			dist[v] = dist[u] + weight
			parent[v] = u
			pq.update({v: dist[v]})
	topo_sorted_vertexes = topological_sort(weighted_graph)
	for v in topo_sorted_vertexes:
		for e in weighted_graph.get_set_of_children(v):
			relax((v,e))

	print(dist)





if __name__ == "__main__":
	print("====== Testing Weighted Graph =========")
	WG = WeightedGraph(directed = True)

	Vertices = ["a","b", "c", "d", "e"]
	Edges = [(("a", "b"), 19), (("a", "c"), 7), (("b", "c"), 11), (("b", "d"), 4), (("d", "c"), 15), (("d", "e"), 13), (("c", "e"), 5)]
	WG.make_graph(Vertices, Edges)
	print(WG.W)
	djikstra(WG, "a")
	DAG_shortest_path(WG, "a")



        current_dict = memo[i]
        previous_dict = memo[i - 1]
        for u in graph.get_vertexes():
            if u == source:
                current_dict[u] = 0
            for v in graph.get_set_of_children(u):
                if graph.W[(u, v)] + previous_dict[u] < current_dict[v]:
                    current_dict[v] = graph.W[(u, v)] + previous_dict[u]
                    parent[v] = u
    return parent, memo


if __name__ == "__main__":
    s = "s"
    a = "a"
    b = "b"
    c = "c"
    d = "d"
    e = "e"
    f = "f"
    g = "g"
    V = [s, a, b, c, d, e, f, g]
    E = [((s, a), 10), ((s, g), 8), ((a, e), 2), ((b, a), 1), ((b, c), 1),
         ((c, d), 3), ((d, e), -1), ((e, b), -2), ((f, e), -1), ((f, a), -4),
         ((g, f), 1)]
    WG = WeightedGraph()
    WG.make_graph(V, E)
    a, b = shortest_path(WG, s, [])
    print(a)
    print(b[len(WG.get_vertexes()) - 1])