-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ford.py
51 lines (40 loc) · 1.48 KB
/
Ford.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from graph_copy import *
def Ford(graph, start, end):
'''we'llhave a list of minimum costs from start to all the possible paths'''
'''this returns a list of all the accessible vertices'''
distances = graph.iterate()
distances[start] = 0
for v in range (1, graph.nrVertices()):
distances[v] = 999999
edges = graph.getEdges()
for index in range (graph.nrVertices()):
print("\n step number: ", index )
print("Before:", distances)
for edg in edges:
'''the distance from starting point to the end
point of a given vertex will be saved as the minimum cost from the start to that point'''
frm = edg[0]
to = edg[1]
distances[to] = min(distances[to], distances[frm] + graph.getCost(frm,to))
print("After: ", distances)
# print(lst)
'''check for possible negative cost loops'''
previous_distances = deepcopy(distances)
print("\nThe final cost from ", start, "to", end, "is: ", distances[end])
def another_test():
'''Create a graph with 7 edges'''
graph = DirectedGraph("input1", 7)
graph.add_edge(0,1,6)
graph.add_edge(0,2,5)
graph.add_edge(0,3,5)
graph.add_edge(2,1,-2)
graph.add_edge(3,2,-2)
graph.add_edge(3,5,-1)
graph.add_edge(5,6,3)
graph.add_edge(4,6,3)
graph.add_edge(2,4,1)
graph.add_edge(1,4,-1)
print(graph)
print("\n")
Ford(graph, 0, 6)
another_test()