コード例 #1
0
ファイル: bellman_ford.py プロジェクト: BastianoNove/DSA
def bellman_ford(g, w, s):
    graph.init_single_source(g, s)
    for i in range(1, len(g.vertices)):
        for u,v in g.edges:
            graph.relax(g.vertices[u], g.vertices[v], w)
    for u,v in g.edges:
        if g.vertices[v].d > g.vertices[u].d + w[(u,v)]:
            return False
    return True
コード例 #2
0
ファイル: dijkstra.py プロジェクト: BastianoNove/DSA
def dijkstra(graph, weights, start_vertex):
    graph_utils.init_single_source(graph, start_vertex)
    queue = [vertex for vertex in graph.vertices.values()] # vertices to process
    heapq.heapify(queue)
    s = [] # set of vertices with shortest path already determined
    while queue:
        u = heapq.heappop(queue) # extract min
        s.append(u)
        for v in u.edges:
            graph_utils.relax(u, v, weights)
コード例 #3
0
ファイル: dijkstra.py プロジェクト: sebastianrmirz/DSA
def dijkstra(graph, weights, start_vertex):
    graph_utils.init_single_source(graph, start_vertex)
    queue = [vertex
             for vertex in graph.vertices.values()]  # vertices to process
    heapq.heapify(queue)
    s = []  # set of vertices with shortest path already determined
    while queue:
        u = heapq.heappop(queue)  # extract min
        s.append(u)
        for v in u.edges:
            graph_utils.relax(u, v, weights)