Skip to content

Commit d118a4e

Browse files
author
Hamid Gasmi
committed
Issue 87 (Finding an Exit from a Maze): solved with a Disjoint Set DS: Rank Heuristic and Path Compression Heuristic
1 parent a832945 commit d118a4e

File tree

3 files changed

+180
-0
lines changed

3 files changed

+180
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import sys
2+
from abc import ABC, abstractmethod
3+
4+
class DisjointSets(ABC):
5+
def __init__(self, n, edges):
6+
self.parent = [i for i in range(n)]
7+
self.rank = [0] * n
8+
self.buildDisjointSets(edges)
9+
10+
# Time Complexity: O(|E|)
11+
# Space Complexity: O(1)
12+
def buildDisjointSets(self, edges):
13+
for i in range(0, len(edges)):
14+
self.union(edges[i][0] - 1, edges[i][1] - 1)
15+
16+
@abstractmethod
17+
def find(self, v):
18+
pass
19+
20+
# Time Complexity: O(log |V|)
21+
# Space Complexity: O(1)
22+
def union(self, u, v):
23+
ru = self.find(u)
24+
rv = self.find(v)
25+
26+
if ru == rv:
27+
return
28+
29+
if self.rank[ru] >= self.rank[rv]:
30+
self.parent[rv] = ru
31+
32+
if self.rank[ru] == self.rank[rv]:
33+
self.rank[ru] += 1
34+
else:
35+
self.parent[ru] = rv
36+
37+
class MazeDisjointSetRankHeuristic(DisjointSets):
38+
# Time Complexity: O(log |V|)
39+
# Space Complexity: O(1)
40+
def find(self, v):
41+
while self.parent[v] != v:
42+
v = self.parent[v]
43+
44+
return v
45+
46+
class MazeDisjointSetCompressPathHeuristic(DisjointSets):
47+
def find(self, v):
48+
if self.parent[v] != v:
49+
self.parent[v] = self.find(self.parent[v])
50+
51+
return self.parent[v]
52+
53+
def reach(edges, n, u, v, solution):
54+
55+
if solution == 3:
56+
# Good job! (Max time used: 0.04/5.00, max memory used: 10145792/536870912.)
57+
aMazeSets = MazeDisjointSetRankHeuristic(n, edges)
58+
return 1 if aMazeSets.find(u) == aMazeSets.find(v) else 0
59+
60+
elif solution == 4:
61+
# Good job! (Time used: 0.04/5.00, memory used: 9732096/536870912.)
62+
aMazeSets = MazeDisjointSetCompressPathHeuristic(n, edges)
63+
return 1 if aMazeSets.find(u) == aMazeSets.find(v) else 0
64+
65+
else:
66+
return 0
67+
68+
if __name__ == '__main__':
69+
input = sys.stdin.read()
70+
data = list(map(int, input.split()))
71+
n, m = data[0:2]
72+
data = data[2:]
73+
edges = list(zip(data[0:(2 * m):2], data[1:(2 * m):2]))
74+
x, y = data[2 * m:]
75+
x, y = x - 1, y - 1
76+
77+
print(reach(edges, n, x, y, 3))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
100 100
2+
27 96
3+
6 9
4+
81 98
5+
21 94
6+
22 68
7+
76 100
8+
8 50
9+
38 86
10+
71 75
11+
32 93
12+
16 50
13+
71 84
14+
6 72
15+
22 58
16+
7 19
17+
19 76
18+
44 75
19+
24 76
20+
31 35
21+
11 89
22+
42 98
23+
63 92
24+
37 38
25+
20 98
26+
45 91
27+
23 53
28+
37 91
29+
76 93
30+
67 90
31+
12 22
32+
43 52
33+
23 56
34+
67 68
35+
1 21
36+
17 83
37+
63 72
38+
30 32
39+
7 91
40+
50 69
41+
38 44
42+
55 89
43+
15 23
44+
11 72
45+
28 42
46+
22 69
47+
56 79
48+
5 83
49+
55 73
50+
13 72
51+
7 93
52+
20 54
53+
21 55
54+
66 89
55+
2 91
56+
18 88
57+
26 64
58+
11 61
59+
28 59
60+
12 86
61+
42 95
62+
17 82
63+
50 66
64+
66 99
65+
40 71
66+
20 40
67+
5 66
68+
92 95
69+
32 46
70+
7 36
71+
44 94
72+
6 31
73+
19 67
74+
26 57
75+
53 84
76+
10 68
77+
28 74
78+
34 94
79+
25 61
80+
71 88
81+
10 89
82+
28 52
83+
72 79
84+
39 73
85+
11 80
86+
44 79
87+
13 77
88+
30 96
89+
30 53
90+
10 39
91+
1 90
92+
40 91
93+
62 71
94+
44 54
95+
15 17
96+
69 74
97+
13 67
98+
24 69
99+
34 96
100+
21 50
101+
20 91
102+
42 46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1

0 commit comments

Comments
 (0)