Skip to content

Commit 490d468

Browse files
author
Hamid Gasmi
committed
Issue #135 is started
1 parent f73195d commit 490d468

File tree

14 files changed

+214
-18
lines changed

14 files changed

+214
-18
lines changed

3-graph-algorithms/2_paths_in_graphs/dijkstra_priorityQueue.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def dijkstra(self, s, t):
2626
parents = [None] * len(self.adjacency_list)
2727
parents[s] = -1
2828

29-
q = queue.PriorityQueue()
29+
q = queue.PriorityQueue(n)
3030
for v in range(0, len(self.adjacency_list)):
3131
if v == s:
3232
continue
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,81 @@
11
#Uses python3
2-
32
import sys
43
import queue
4+
from collections import namedtuple
5+
6+
Edge = namedtuple('Edge', ['vertex', 'weight'])
7+
8+
class Graph:
9+
def __init__(self, n, edges):
10+
self.adjacency_list = [[] for _ in range(n)]
11+
self.build_adjacency_list(edges)
12+
13+
def v_size(self):
14+
return len(self.adjacency_list)
15+
16+
def build_adjacency_list(self, edges):
17+
for ((s, d), w) in edges:
18+
self.adjacency_list[s - 1].append(Edge(d - 1, w))
19+
20+
# Implementation: Bellman-Ford algorithm
21+
def shortest_paths(self, s, distances):
22+
n = self.v_size()
23+
parents = [-1] * n
24+
distances[s] = 0
25+
negative_cycle_reachable_nodes = []
526

27+
# Find shortest paths
28+
for i in range(n):
29+
for v in range(n):
30+
for a_tuple in self.adjacency_list[v]:
31+
a_candidate_distance = distances[v] + a_tuple.weight
32+
if distances[a_tuple.vertex] > a_candidate_distance:
33+
distances[a_tuple.vertex] = a_candidate_distance
34+
parents[a_tuple.vertex] = v
35+
36+
if i == n - 1:
37+
negative_cycle_reachable_nodes.append(a_tuple.vertex)
38+
distances[s] = - 10**19
39+
40+
# Find all negative cycles
41+
visited = [False] * n
42+
for v in negative_cycle_reachable_nodes:
43+
cycle_v = self.get_cycle(v, parents)
44+
# Visit all nodes reachable from a negative cycle
45+
self.dfs(cycle_v, distances, visited)
46+
47+
def get_cycle(self, v, parents):
48+
# Find a node in a cycle where v is reachable from
49+
cycle_v = v
50+
for _ in range(self.v_size()):
51+
cycle_v = parents[cycle_v]
652

7-
def shortet_paths(adj, cost, s, distance, reachable, shortest):
8-
#write your code here
9-
pass
53+
return cycle_v
1054

55+
# Find all nodes of the cycle
56+
#cycle = []
57+
#cycle.append(cycle_v)
58+
#p = parents[cycle_v]
59+
#while p != cycle_v:
60+
# cycle.append(p)
61+
# p = parents[p]
62+
# return cycle
63+
64+
def dfs(self, s, distances, visited):
65+
if visited[s]:
66+
return
67+
68+
visited[s] = True
69+
distances[s] = - 10**19
70+
71+
for a_tuple in self.adjacency_list[s]:
72+
self.dfs(a_tuple.vertex, distances, visited)
73+
74+
def shortet_paths(n, edges, s, distance):
75+
76+
aGraph = Graph(n, edges)
77+
78+
aGraph.shortest_paths(s, distances)
1179

1280
if __name__ == '__main__':
1381
input = sys.stdin.read()
@@ -16,22 +84,15 @@ def shortet_paths(adj, cost, s, distance, reachable, shortest):
1684
data = data[2:]
1785
edges = list(zip(zip(data[0:(3 * m):3], data[1:(3 * m):3]), data[2:(3 * m):3]))
1886
data = data[3 * m:]
19-
adj = [[] for _ in range(n)]
20-
cost = [[] for _ in range(n)]
21-
for ((a, b), w) in edges:
22-
adj[a - 1].append(b - 1)
23-
cost[a - 1].append(w)
87+
2488
s = data[0]
2589
s -= 1
26-
distance = [10**19] * n
27-
reachable = [0] * n
28-
shortest = [1] * n
29-
shortet_paths(adj, cost, s, distance, reachable, shortest)
90+
distances = [10**19] * n
91+
shortet_paths(n, edges, s, distances)
3092
for x in range(n):
31-
if reachable[x] == 0:
93+
if distances[x] == 10**19:
3294
print('*')
33-
elif shortest[x] == 0:
95+
elif distances[x] == - 10**19:
3496
print('-')
3597
else:
36-
print(distance[x])
37-
98+
print(distances[x])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
6 7
2+
1 2 10
3+
2 3 5
4+
1 3 100
5+
3 5 7
6+
5 4 10
7+
4 3 -18
8+
6 1 -1
9+
1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
0
2+
10
3+
-
4+
-
5+
-
6+
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
0
2+
10
3+
-
4+
-
5+
-
6+
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
5 4
2+
1 2 1
3+
4 1 2
4+
2 3 2
5+
3 1 -5
6+
4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-
2+
-
3+
-
4+
0
5+
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-
2+
-
3+
-
4+
0
5+
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
15 17
2+
1 2 10
3+
2 3 5
4+
1 3 100
5+
1 15 2
6+
3 5 7
7+
4 3 -18
8+
5 4 10
9+
5 7 2
10+
5 8 1
11+
7 9 1
12+
9 10 1
13+
6 1 -1
14+
2 11 1
15+
11 12 7
16+
12 13 10
17+
13 11 -18
18+
12 14 1
19+
1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
0
2+
10
3+
-
4+
-
5+
-
6+
*
7+
-
8+
-
9+
-
10+
-
11+
-
12+
-
13+
-
14+
-
15+
2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
0
2+
10
3+
-
4+
-
5+
-
6+
*
7+
-
8+
-
9+
-
10+
-
11+
-
12+
-
13+
-
14+
-
15+
2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
15 17
2+
1 2 10
3+
2 3 5
4+
1 3 100
5+
1 15 2
6+
3 5 7
7+
4 3 -18
8+
5 4 10
9+
5 7 2
10+
5 8 1
11+
7 9 1
12+
9 10 1
13+
6 1 -1
14+
2 11 1
15+
11 12 7
16+
12 13 10
17+
13 11 -18
18+
12 14 1
19+
6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-1
2+
9
3+
-
4+
-
5+
-
6+
0
7+
-
8+
-
9+
-
10+
-
11+
-
12+
-
13+
-
14+
-
15+
1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-1
2+
9
3+
-
4+
-
5+
-
6+
0
7+
-
8+
-
9+
-
10+
-
11+
-
12+
-
13+
-
14+
-
15+
1

0 commit comments

Comments
 (0)