-
Notifications
You must be signed in to change notification settings - Fork 65
/
quick_find.py
68 lines (54 loc) · 1.8 KB
/
quick_find.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
""" Quick Find is a Eager Approach.
*Limitations:*
- Union is too expensive (N array accesses)
- Trees are flat, but too expensive to keep them flat
"""
class QuickFind:
""" elem1 and elem2 are connected iff they have the same id """
def __init__(self, n):
""" Initializing list of size n where value is same as index
Time Complexity: O(n)
:param n: number of elements
"""
self.data = []
for i in range(n):
self.data.append(i)
def connected(self, elem1, elem2):
""" elem1 and elem2 are connected iff they have the same id
Time Complexity: O(1)
:param elem1: element 1
:param elem2: element 2
:return: returns true iff two elem1 and elem2 are connected, else
false
:rtype: bool
"""
return self.data[elem1] == self.data[elem2]
def union(self, elem1, elem2):
""" When connecting two objects elem1 and elem2, change the id of all
objects that have the id of elem1 to that of elem2, or vice-versa.
Time Complexity is O(n), which is too expensive.
:param elem1: element 1
:param elem2: element 2
"""
pid = self.data[elem1]
qid = self.data[elem2]
for i in range(len(self.data)):
if self.data[i] == pid:
self.data[i] = qid
def main():
""" operational function """
maze = QuickFind(10)
maze.union(4, 3)
maze.union(3, 8)
maze.union(6, 5)
maze.union(9, 4)
maze.union(2, 1)
print("is 0-7 connected: ", maze.connected(0, 7))
print("is 8-9 connected: ", maze.connected(8, 9))
maze.union(5, 0)
maze.union(7, 2)
maze.union(6, 1)
maze.union(1, 0)
print("is 0-7 connected: ", maze.connected(0, 7))
if __name__ == "__main__":
main()