Skip to content

Commit b515666

Browse files
committed
Algo tutoria
1 parent 2ff6fa4 commit b515666

File tree

6 files changed

+173
-0
lines changed

6 files changed

+173
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Quick Find:
3+
* Initialize O(N) - Linear
4+
* Union O(N) - Linear
5+
* Find O(1) - Constant
6+
7+
Cons:
8+
* Union too expensive (N array accesses)
9+
* Trees are flat, but too expensive to keep them flat
10+
11+
*/
12+
13+
14+
public class QuickFindUF {
15+
private int[] id;
16+
17+
public QuickFindUF(int N) { // O(N)
18+
id = new int[n];
19+
for (int i = 0; i < N; i++)
20+
id[i] = i;
21+
}
22+
23+
public boolean connected(int p, int q) { // O(1)
24+
return id[p] == id[q];
25+
}
26+
27+
public void union(int p, int q) { // O(N)
28+
int pid = id[p];
29+
int qid = id[q];
30+
31+
for (int i = 0; i < id.length; i ++)
32+
if (id[i] == pid) id[i] = qid;
33+
}
34+
35+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
Quick Find:
3+
* Initialize O(N) - Linear
4+
* Union O(N) - Linear
5+
* Find O(N) - Linear
6+
7+
Cons:
8+
* Trees can get too tal
9+
* Find too expensive (could be N array acceses)
10+
*/
11+
public class QuickUnionUF {
12+
private int[] id;
13+
14+
QuickUnionUF(int N) { // O(N)
15+
id = new int[N];
16+
for (int i = 0; i < N; i++)
17+
id[i] = i;
18+
}
19+
20+
private int root(int i) { // O(N)
21+
while (i != id[i])
22+
i = id[i];
23+
return i;
24+
}
25+
26+
public boolean connected(int p, int q) { // O(N)
27+
return root(p) == root(q);
28+
}
29+
30+
public void union(int p, int q) { // O(N)
31+
int i = root(p);
32+
int j = root(q);
33+
id[i] = j;
34+
}
35+
36+
}
37+
38+
public class QuickUnionWeightedUF {
39+
private int[] id;
40+
private int[] size;
41+
42+
QuickUnionWeightedUF(int N) { // O(N) - Linear
43+
id = new int[N];
44+
size = new size[N];
45+
for (int i = 0; i < N; i++)
46+
id[i] = i;
47+
}
48+
49+
private int root(int i) { // O(N) - Linear
50+
while (i != id[i])
51+
i = id[i];
52+
return i;
53+
}
54+
55+
public boolean connected(int p, int q) { // O(log N) - Logarithm
56+
return root(p) == root(q);
57+
}
58+
59+
public void union(int p, int q) { // O(log N) - Logarithm
60+
int i = root(p);
61+
int j = root(q);
62+
63+
if (i == j) return;
64+
65+
if (size[i] < size[j]) {
66+
id[i] = j;
67+
size[j] += size[i];
68+
} else {
69+
id[j] = i;
70+
size[i] += size[j];
71+
}
72+
73+
}
74+
75+
}
76+
77+
78+
public class QuickUnionWeightedPathComporessionUF {
79+
private int[] id;
80+
private int[] size;
81+
82+
QuickUnionWeightedUF(int N) { // O(N) - Linear
83+
id = new int[N];
84+
size = new size[N];
85+
for (int i = 0; i < N; i++)
86+
id[i] = i;
87+
}
88+
89+
private int root(int i) { // O(N) - Linear
90+
while (i != id[i])
91+
id[i] = id[id[i]]; // path compression
92+
i = id[i];
93+
return i;
94+
}
95+
96+
public boolean connected(int p, int q) { // O(log N) - Logarithm
97+
return root(p) == root(q);
98+
}
99+
100+
public void union(int p, int q) { // O(log N) - Logarithm
101+
int i = root(p);
102+
int j = root(q);
103+
104+
if (i == j) return;
105+
106+
if (size[i] < size[j]) {
107+
id[i] = j;
108+
size[j] += size[i];
109+
} else {
110+
id[j] = i;
111+
size[i] += size[j];
112+
}
113+
114+
}
115+
116+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
* [Algorithms part 1 complete](https://www.youtube.com/watch?v=9diDWV-fOnE&t=1374s)
2+
3+
4+
Quick Union Improvement
5+
-----------------------
6+
7+
### 1 Weighting
8+
9+
#### Weighted quick-union
10+
11+
Basically, when you union you connect the smaller --> bigger tree.
12+
13+
14+
![alt text](image.png)
15+
16+
17+
### 2 Path Compression
18+
19+
Basically, it will 'level up' or 'flatten' the tree while scanning the tree!
20+
21+
![alt text](image-1.png)
Loading
Loading

learn_resources.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Video Tutorials
2525
* [Professor Gerry Jenkins](https://www.youtube.com/c/GerryJenkins/playlists)
2626
* [Dynamic Programming - Learn to Solve Algorithmic Problems & Coding Challenges](https://www.youtube.com/watch?v=oBt53YbR9Kk)
2727
* [CS50 | Data structure Playlists](https://www.youtube.com/c/cs50/search?query=structure)
28+
* [Algorithms part 1 complete](https://www.youtube.com/watch?v=9diDWV-fOnE&t=1374s)
2829

2930

3031
### Udemy

0 commit comments

Comments
 (0)