Poscat seminar 10
-
Upload
hyungyu-shin -
Category
Education
-
view
122 -
download
0
Transcript of Poscat seminar 10
Topic
Topic today
− Shortest Path
• Dijkstra Algorithm
• Floyd Algorithm
• Bellman-Ford Algorithm
− Disjoint Set
• Union & Find
• Path Compression
POSCAT Seminar 1-215 July 2014
yougatup
Shortest Path
Problem
Given a graph, find a shortest path from start vertex to end vertex
POSCAT Seminar 1-315 July 2014
yougatup
1 2
3
4
6
5
7
8
3
1 1
2 3
1 2
9
3
Shortest Path
Problem
Given a graph, find a shortest path from start vertex to end vertex
1. Greedy Approach
2. Iterative Approach
3. Dynamic Programming Approach
POSCAT Seminar 1-415 July 2014
yougatup
1 2
3
4
6
5
7
8
3
1 1
2 3
1 2
9
3
Dijkstra Algorithm
Approach
− Greedy approach
− Let 𝑆 𝑖 = 𝑡ℎ𝑒 𝑙𝑒𝑛𝑔𝑡ℎ 𝑜𝑓 𝑠ℎ𝑜𝑟𝑡𝑒𝑠𝑡 𝑝𝑎𝑡ℎ 𝑓𝑟𝑜𝑚 𝑡ℎ𝑒 𝑠𝑡𝑎𝑟𝑡 𝑣𝑒𝑟𝑡𝑒𝑥 𝑡𝑜 𝑣𝑒𝑟𝑡𝑒𝑥 𝑖
POSCAT Seminar 1-515 July 2014
yougatup
1 2
3
4
6
5
7
8
3
1 1
2 3
1 2
9
3
Dijkstra Algorithm
Approach
− Greedy approach
− Let 𝑆 𝑖 = 𝑡ℎ𝑒 𝑙𝑒𝑛𝑔𝑡ℎ 𝑜𝑓 𝑠ℎ𝑜𝑟𝑡𝑒𝑠𝑡 𝑝𝑎𝑡ℎ 𝑓𝑟𝑜𝑚 𝑡ℎ𝑒 𝑠𝑡𝑎𝑟𝑡 𝑣𝑒𝑟𝑡𝑒𝑥 𝑡𝑜 𝑣𝑒𝑟𝑡𝑒𝑥 𝑖
POSCAT Seminar 1-615 July 2014
yougatup
1 2
3
4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1
∞
2
∞
3
∞
4
∞
5
∞
6
∞
7
∞
8
We don’t know the shortest path length of these
Dijkstra Algorithm
Approach
Currently, we only know that the shortest path length from the
start vertex to start vertex 0
POSCAT Seminar 1-715 July 2014
yougatup
1 2
3
4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1
∞
2
∞
3
∞
4
∞
5
∞
6
∞
7
∞
8
Dijkstra Algorithm
Approach
Also, we can fill another cell of 𝑆 by using this information !
POSCAT Seminar 1-815 July 2014
yougatup
1 2
3
4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1
∞
2
∞
3
∞
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
Dijkstra Algorithm
Approach
Also, we can fill another cell of 𝑆 by using this information !
POSCAT Seminar 1-915 July 2014
yougatup
1 2
3
4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1
3
2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
Dijkstra Algorithm
Approach
Also, we can fill another cell of 𝑆 by using this information !
POSCAT Seminar 1-1015 July 2014
yougatup
1 2
3
4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1
3
2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
By the way, is it correct value by definition ?
Dijkstra Algorithm
Approach
Also, we can fill another cell of 𝑆 by using this information !
POSCAT Seminar 1-1115 July 2014
yougatup
1 2
3
4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1
3
2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
By the way, is it correct value by definition ? NO !
Dijkstra Algorithm
Approach
Also, we can fill another cell of 𝑆 by using this information !
POSCAT Seminar 1-1215 July 2014
yougatup
1 2
3
4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1
3
2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
Then, which one is the correct value ?
Dijkstra Algorithm
Approach
Also, we can fill another cell of 𝑆 by using this information !
POSCAT Seminar 1-1315 July 2014
yougatup
1 2
3
4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1
3
2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
Then, which one is the correct value ?
Dijkstra Algorithm
Approach
Also, we can fill another cell of 𝑆 by using this information !
POSCAT Seminar 1-1415 July 2014
yougatup
1 2
3
4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1
3
2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
Okay, keep going
Dijkstra Algorithm
Approach
Because 𝑆(3) is the correct value, also we can fill another cell
POSCAT Seminar 1-1515 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1
3
2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
1
3
Dijkstra Algorithm
Approach
Because 𝑆(3) is the correct value, also we can fill another cell
POSCAT Seminar 1-1615 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1
3
2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
1
3
Dijkstra Algorithm
Approach
Because 𝑆(3) is the correct value, also we can fill another cell
POSCAT Seminar 1-1715 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1
3
2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
How can we calculate the path length ?
1
3
Dijkstra Algorithm
Approach
Because 𝑆(3) is the correct value, also we can fill another cell
POSCAT Seminar 1-1815 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1
3
2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
First, we move from 1 to 3 via a shortest pathSecond, we move from 3 to 2 via a edge
1
3
Dijkstra Algorithm
Approach
Because 𝑆(3) is the correct value, also we can fill another cell
POSCAT Seminar 1-1915 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1
3
2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
First, we move from 1 to 3 via a shortest path 𝑆(3)Second, we move from 3 to 2 via a edge 𝑐𝑜𝑠𝑡(3, 2)
1
3
Dijkstra Algorithm
Approach
Because 𝑆(3) is the correct value, also we can fill another cell
POSCAT Seminar 1-2015 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1
2
2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
We can update this value !
1
3
Dijkstra Algorithm
Approach
Because 𝑆(3) is the correct value, also we can fill another cell
POSCAT Seminar 1-2115 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
Then, which is the correct shortest path length ? WHY ?
2
1
3
Dijkstra Algorithm
Approach
Because 𝑆(3) is the correct value, also we can fill another cell
POSCAT Seminar 1-2215 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
We already use the information of 𝑆(1) and 𝑆(3)
2
1
3
Dijkstra Algorithm
Approach
Because 𝑆(3) is the correct value, also we can fill another cell
POSCAT Seminar 1-2315 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
We already use the information of 𝑆(1) and 𝑆 3It means that we already consider paths whichcontains the vertex 1 or 3
2
1
3
Dijkstra Algorithm
Approach
Because 𝑆(3) is the correct value, also we can fill another cell
POSCAT Seminar 1-2415 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
We already use the information of 𝑆(1) and 𝑆 3It means that we already consider paths whichcontains the vertex 1 or 3The remaining case is to consider paths which contains the another vertices ( white boxes )
2
1
3
We already use the information of 𝑆(1) and 𝑆 3It means that we already consider paths whichcontains the vertex 1 or 3The remaining case is to consider paths which contains the another vertices ( white boxes )Can we update the value of 𝑆(2) with the value of white boxes ?
Dijkstra Algorithm
Approach
Because 𝑆(3) is the correct value, also we can fill another cell
POSCAT Seminar 1-2515 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
2
1
3
We already use the information of 𝑆(1) and 𝑆 3It means that we already consider paths whichcontains the vertex 1 or 3The remaining case is to consider paths which contains the another vertices ( white boxes )Can we update the value of 𝑆(2) with the value of white boxes ? NEVER !!
Dijkstra Algorithm
Approach
Because 𝑆(3) is the correct value, also we can fill another cell
POSCAT Seminar 1-2615 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
2
1
3
Dijkstra Algorithm
Approach
Blue boxes contain the correct value of shortest path length
We NEVER update the minimum value of white boxes !
POSCAT Seminar 1-2715 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
2
1
3
Dijkstra Algorithm
Approach
Blue boxes contain the correct value of shortest path length
We NEVER update the minimum value of white boxes !
Therefore, the minimum value of white boxes is the correct value
POSCAT Seminar 1-2815 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
2
1
3
Dijkstra Algorithm
Overall algorithm
1. Choose the white box which contains minimum value
2. Update another cell
3. Repeat !
POSCAT Seminar 1-2915 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
2
1
3
Dijkstra Algorithm
Overall algorithm
1. Choose the white box which contains minimum value
2. Update another cell
3. Repeat !
POSCAT Seminar 1-3015 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
2
1
3
Dijkstra Algorithm
Overall algorithm
1. Choose the white box which contains minimum value
2. Update another cell
3. Repeat !
POSCAT Seminar 1-3115 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
4
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
2
1
3
Dijkstra Algorithm
Overall algorithm
1. Choose the white box which contains minimum value
2. Update another cell
3. Repeat !
POSCAT Seminar 1-3215 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
4
4
∞
5
∞
6
∞
7
∞
8
We can move from 1 to 2 and 3
2
1
3
Dijkstra Algorithm
Overall algorithm
1. Choose the white box which contains minimum value
2. Update another cell
3. Repeat !
POSCAT Seminar 1-3315 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
4
4
7
5
5
6
∞
7
∞
8
We can move from 1 to 2 and 3
2
1
3
Dijkstra Algorithm
Overall algorithm
1. Choose the white box which contains minimum value
2. Update another cell
3. Repeat !
POSCAT Seminar 1-3415 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
4
4
7
5
5
6
∞
7
∞
8
We can move from 1 to 2 and 3
2
1
3
Dijkstra Algorithm
Overall algorithm
1. Choose the white box which contains minimum value
2. Update another cell
3. Repeat !
POSCAT Seminar 1-3515 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
4
4
7
5
5
6
14
7
∞
8
We can move from 1 to 2 and 3
2
1
3
Dijkstra Algorithm
Overall algorithm
1. Choose the white box which contains minimum value
2. Update another cell
3. Repeat !
POSCAT Seminar 1-3615 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
4
4
7
5
5
6
14
7
∞
8
We can move from 1 to 2 and 3
2
1
3
Dijkstra Algorithm
Overall algorithm
1. Choose the white box which contains minimum value
2. Update another cell
3. Repeat !
POSCAT Seminar 1-3715 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
4
4
7
5
5
6
9
7
∞
8
We can move from 1 to 2 and 3
2
1
3
Dijkstra Algorithm
Overall algorithm
1. Choose the white box which contains minimum value
2. Update another cell
3. Repeat !
POSCAT Seminar 1-3815 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
4
4
7
5
5
6
9
7
∞
8
We can move from 1 to 2 and 3
2
1
3
Dijkstra Algorithm
Overall algorithm
1. Choose the white box which contains minimum value
2. Update another cell
3. Repeat !
POSCAT Seminar 1-3915 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
4
4
7
5
5
6
9
7
12
8
We can move from 1 to 2 and 3
2
1
3
Dijkstra Algorithm
Overall algorithm
1. Choose the white box which contains minimum value
2. Update another cell
3. Repeat !
POSCAT Seminar 1-4015 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
4
4
7
5
5
6
9
7
12
8
We can move from 1 to 2 and 3
2
1
3
Dijkstra Algorithm
Overall algorithm
1. Choose the white box which contains minimum value
2. Update another cell
3. Repeat !
POSCAT Seminar 1-4115 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
4
4
7
5
5
6
9
7
12
8
We can move from 1 to 2 and 3
2
1
3
No one is updated
Dijkstra Algorithm
Overall algorithm
1. Choose the white box which contains minimum value
2. Update another cell
3. Repeat !
POSCAT Seminar 1-4215 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
4
4
7
5
5
6
9
7
12
8
We can move from 1 to 2 and 3
2
1
3
Done! Because all the values in 𝑆 is correct value !
Dijkstra Algorithm
Analysis
1. Choose the white box which contains minimum value
2. Update another cell
3. Repeat !
POSCAT Seminar 1-4315 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
4
4
7
5
5
6
9
7
12
8
We can move from 1 to 2 and 3
2
1
3
Done! Because all the values in 𝑆 is correct value !
Dijkstra Algorithm
Analysis
1. Choose the white box which contains minimum value O(𝑉)
2. Update another cell O(𝑉)
3. Repeat !
POSCAT Seminar 1-4415 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
4
4
7
5
5
6
9
7
12
8
We can move from 1 to 2 and 3
2
1
3
Done! Because all the values in 𝑆 is correct value !
Dijkstra Algorithm
Analysis
1. Choose the white box which contains minimum value O(𝑉)
2. Update another cell O(𝑉)
3. Repeat !
POSCAT Seminar 1-4515 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
4
4
7
5
5
6
9
7
12
8
We can move from 1 to 2 and 3
2
1
3
Done! Because all the values in 𝑆 is correct value !
O(𝑽𝟐)
Dijkstra Algorithm
Analysis
1. Choose the white box which contains minimum value O(𝑉)
2. Update another cell O(𝑉)
3. Repeat !
POSCAT Seminar 1-4615 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
4
4
7
5
5
6
9
7
12
8
We can move from 1 to 2 and 3
2
1
3
Done! Because all the values in 𝑆 is correct value !
O(𝑽𝟐)
We can improve this time complexity as O(𝑬 𝐥𝐨𝐠𝑽) by using priority queue
Dijkstra Algorithm
Pitfall
However, does our logic clear ?
In other words, is it really true that white cell with minimum value
never be updated ?
POSCAT Seminar 1-4715 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
4
4
7
5
5
6
9
7
12
8
2
1
3
Dijkstra Algorithm
Pitfall
However, does our logic clear ?
In other words, is it really true that white cell with minimum value
never be updated ?
What if we have negative cost ?
POSCAT Seminar 1-4815 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
4
4
7
5
5
6
9
7
12
8
2
1
3
Dijkstra Algorithm
Pitfall
However, does our logic clear ?
In other words, is it really true that white cell with minimum value
never be updated ?
What if we have negative cost ? Fail !
POSCAT Seminar 1-4915 July 2014
yougatup
2 4
6
5
7
8
3
1 1
2 3
1 2
9
3
0𝑆
1 2
1
3
4
4
7
5
5
6
9
7
12
8
2
1
3
Bellman Ford Algorithm
Approach
Iterative Approach
Let me use 𝑆 𝑖 one more time
POSCAT Seminar 1-5015 July 2014
yougatup
2 4 5
7
8
3
1 1
2 3
1 2
9
3
1
3 6
0𝑆
1 2
∞
3
∞
4
∞
5
∞
6
∞
7
∞
8
∞
Bellman Ford Algorithm
Approach
Just update one time by using 𝑆
POSCAT Seminar 1-5115 July 2014
yougatup
2 4 5
7
8
3
1 1
2 3
1 2
9
3
1
3 6
0𝑆
1 2
∞
3
∞
4
∞
5
∞
6
∞
7
∞
8
∞
Bellman Ford Algorithm
Approach
Just update one time by using 𝑆
POSCAT Seminar 1-5215 July 2014
yougatup
2 4 5
7
8
3
1 1
2 3
1 2
9
3
1
3 6
0𝑆
1 2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
3
Bellman Ford Algorithm
Approach
Just update one time by using 𝑆
POSCAT Seminar 1-5315 July 2014
yougatup
2 4 5
7
8
3
1 1
2 3
1 2
9
3
1
3 6
0𝑆
1 2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
3
We can guarantee that we successfully calculate the length of shortest path which consists of 1 edge.The shortest path consists of 1 edge for vertex 3. Therefore, we calculate the shortest path length of vertex 3 !
Bellman Ford Algorithm
Approach
Just update one time by using 𝑆
POSCAT Seminar 1-5415 July 2014
yougatup
2 4 5
7
8
3
1 1
2 3
1 2
9
3
1
3 6
0𝑆
1 2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
3
However, we don’t know which one is the shortest path.In other words, we don’t know whether the value of 𝑆 3 is correct or not
Bellman Ford Algorithm
Approach
Update one more time !
POSCAT Seminar 1-5515 July 2014
yougatup
2 4 5
7
8
3
1 1
2 3
1 2
9
3
1
3 6
0𝑆
1 2
1
3
∞
4
∞
5
∞
6
∞
7
∞
8
3
Bellman Ford Algorithm
Approach
Update one more time !
POSCAT Seminar 1-5615 July 2014
yougatup
2 4 5
7
8
3
1 1
2 3
1 2
9
3
1
3 6
0𝑆
1 2
1
3
5
4
∞
5
∞
6
∞
7
∞
8
2
We can calculate the length of shortest path with 2 edges correctly !Also, we don’t know whether 𝑆(2) is correct value or not
Bellman Ford Algorithm
Approach
If we update n times, then we can guarantee that
We successfully calculate the length of shortest path with n edges !
POSCAT Seminar 1-5715 July 2014
yougatup
2 4 5
7
8
3
1 1
2 3
1 2
9
3
1
3 6
0𝑆
1 2
1
3
5
4
∞
5
∞
6
∞
7
∞
8
2
Bellman Ford Algorithm
Approach
However, shortest path have to consist of at most (V-1) edges
Therefore, (V-1) iteration is enough to get the correct value
POSCAT Seminar 1-5815 July 2014
yougatup
2 4 5
7
8
3
1 1
2 3
1 2
9
3
1
3 6
0𝑆
1 2
1
3
5
4
∞
5
∞
6
∞
7
∞
8
2
Bellman Ford Algorithm
Analysis
Each iteration needs 𝑂(𝐸)
We need O(𝑉) iterations
POSCAT Seminar 1-5915 July 2014
yougatup
2 4 5
7
8
3
1 1
2 3
1 2
9
3
1
3 6
0𝑆
1 2
1
3
5
4
∞
5
∞
6
∞
7
∞
8
2
Bellman Ford Algorithm
Analysis
Each iteration needs 𝑂(𝐸)
We need O(𝑉) iterations
O(𝑽𝑬)
POSCAT Seminar 1-6015 July 2014
yougatup
2 4 5
7
8
3
1 1
2 3
1 2
9
3
1
3 6
0𝑆
1 2
1
3
5
4
∞
5
∞
6
∞
7
∞
8
2
Floyd Algorithm
Approach
Dynamic Programming Approach
Remember 3 steps
POSCAT Seminar 1-6115 July 2014
yougatup
2 4 5
7
8
3
1 1
2 3
1 2
9
3
1
3 6
Floyd Algorithm
Approach
Let T(𝑘, 𝑖, 𝑗) = the shortest path length from 𝑖 to 𝑗
when we use vertices from 1 to 𝑘 only
POSCAT Seminar 1-6215 July 2014
yougatup
𝑖 𝑗
1 2 3 … 𝑘
Floyd Algorithm
Approach
Let T(𝑘, 𝑖, 𝑗) = the shortest path length from 𝑖 to 𝑗
when we use vertices from 1 to 𝑘 only
Case 1. we use Vertex 𝑘
POSCAT Seminar 1-6315 July 2014
yougatup
𝑖 𝑗
𝑘
Floyd Algorithm
Approach
Let T(𝑘, 𝑖, 𝑗) = the shortest path length from 𝑖 to 𝑗
when we use vertices from 1 to 𝑘 only
Case 1. we use Vertex 𝑘
POSCAT Seminar 1-6415 July 2014
yougatup
𝑖 𝑗
𝑘
Then we can use vertex 1 ~ (𝑘 − 1) to move from 𝑖 to 𝑘Also, we can use same vertices to move from 𝑘 to 𝑗
1 … (𝑘 − 1) 1 … (𝑘 − 1)
Floyd Algorithm
Approach
Let T(𝑘, 𝑖, 𝑗) = the shortest path length from 𝑖 to 𝑗
when we use vertices from 1 to 𝑘 only
Case 1. we use Vertex 𝑘
POSCAT Seminar 1-6515 July 2014
yougatup
𝑖 𝑗
𝑘
∴ T(𝑘, 𝑖, 𝑗) = T(𝑘 − 1, 𝑖, 𝑘) + T(𝑘 − 1, 𝑘, 𝑗)
1 … (𝑘 − 1) 1 … (𝑘 − 1)
Floyd Algorithm
Approach
Let T(𝑘, 𝑖, 𝑗) = the shortest path length from 𝑖 to 𝑗
when we use vertices from 1 to 𝑘 only
Case 2. we don’t use vertex 𝑘
POSCAT Seminar 1-6615 July 2014
yougatup
𝑖 𝑗
Floyd Algorithm
Approach
Let T(𝑘, 𝑖, 𝑗) = the shortest path length from 𝑖 to 𝑗
when we use vertices from 1 to 𝑘 only
Case 2. we don’t use vertex 𝑘
POSCAT Seminar 1-6715 July 2014
yougatup
𝑖 𝑗
1 … (𝑘 − 1)
T(𝑘, 𝑖, 𝑗) = T(𝑘 − 1, 𝑖, 𝑗)
Floyd Algorithm
Approach
Let T(𝑘, 𝑖, 𝑗) = the shortest path length from 𝑖 to 𝑗
when we use vertices from 1 to 𝑘 only
∴ T k, i, j = min ( T k − 1, i, j , T k − 1, i, k + T k − 1, k, j )
POSCAT Seminar 1-6815 July 2014
yougatup
Floyd Algorithm
Analysis
Let T(𝑘, 𝑖, 𝑗) = the shortest path length from 𝑖 to 𝑗
when we use vertices from 1 to 𝑘 only
∴ T k, i, j = min ( T k − 1, i, j , T k − 1, i, k + T k − 1, k, j )
POSCAT Seminar 1-6915 July 2014
yougatup
Floyd Algorithm
Analysis
Let T(𝑘, 𝑖, 𝑗) = the shortest path length from 𝑖 to 𝑗
when we use vertices from 1 to 𝑘 only
∴ T k, i, j = min ( T k − 1, i, j , T k − 1, i, k + T k − 1, k, j )
POSCAT Seminar 1-7015 July 2014
yougatup
O(𝒏𝟑)
Disjoint Set
Problem
Initially, there are n groups. You can merge two group into one.
Determine whether two elements is in the same group or not
POSCAT Seminar 1-7115 July 2014
yougatup
1 2 3 4 5 6 7 8
Disjoint Set
Problem
Initially, there are n groups. You can merge two group into one.
Determine whether two elements is in the same group or not
POSCAT Seminar 1-7215 July 2014
yougatup
1 2 3 4 5 6 7 8
Merge !
Disjoint Set
Problem
Initially, there are n groups. You can merge two group into one.
Determine whether two elements is in the same group or not
POSCAT Seminar 1-7315 July 2014
yougatup
1 2 3 4 5 6 7 8
Merge ! Merge !
Disjoint Set
Problem
Initially, there are n groups. You can merge two group into one.
Determine whether two elements is in the same group or not
POSCAT Seminar 1-7415 July 2014
yougatup
1 2 3
4 5
6 7 8
Disjoint Set
Problem
Initially, there are n groups. You can merge two group into one.
Determine whether two elements is in the same group or not
POSCAT Seminar 1-7515 July 2014
yougatup
1 2 3
4 5
6 78
Disjoint Set
Problem
Initially, there are n groups. You can merge two group into one.
Determine whether two elements is in the same group or not
POSCAT Seminar 1-7615 July 2014
yougatup
1 2 3
4 5
6 78
Determine whether vertex 1 and 3 is in the same group
Disjoint Set
Problem
Initially, there are n groups. You can merge two group into one.
Determine whether two elements is in the same group or not
POSCAT Seminar 1-7715 July 2014
yougatup
1 2 3
4 5
6 78
Determine whether vertex 1 and 3 is in the same group NO
Disjoint Set
Approach
How can we represent a group ?
POSCAT Seminar 1-7815 July 2014
yougatup
1 2 3
4 5
6 78
Disjoint Set
Approach
How can we represent a group ? Using array is bad
POSCAT Seminar 1-7915 July 2014
yougatup
1 2 3
4 5
6 78
Disjoint Set
Approach
How can we represent a group ? Using array is bad
We use tree structure !
POSCAT Seminar 1-8015 July 2014
yougatup
1 2 3
4 5
6 78
Disjoint Set
Approach
How can we represent a group ? Using array is bad
We use tree structure !
POSCAT Seminar 1-8115 July 2014
yougatup
1
2
34 56 7
8
Disjoint Set
Approach
How can we determine that two vertices are in the same group ?
POSCAT Seminar 1-8215 July 2014
yougatup
1
2
34 56 7
8
Disjoint Set
Approach
How can we determine that two vertices are in the same group ?
by comparing their root node !
POSCAT Seminar 1-8315 July 2014
yougatup
1
2
34 56 7
8
Disjoint Set
Approach
How can we determine that two vertices are in the same group ?
by comparing their root node !
POSCAT Seminar 1-8415 July 2014
yougatup
1
2
34 56 7
8
Disjoint Set
Approach
How can we determine that two vertices are in the same group ?
by comparing their root node !
POSCAT Seminar 1-8515 July 2014
yougatup
1
2
34 56 7
8
Same !
Disjoint Set
Approach
How can we determine that two vertices are in the same group ?
by comparing their root node !
POSCAT Seminar 1-8615 July 2014
yougatup
1
2
34 56 7
8
Disjoint Set
Approach
How can we determine that two vertices are in the same group ?
by comparing their root node !
POSCAT Seminar 1-8715 July 2014
yougatup
1
2
34 56 7
8
No !
Disjoint Set
Approach
Finding their root is quite simple. Just follow their parent node.
POSCAT Seminar 1-8815 July 2014
yougatup
1
2
34 56 7
8
Disjoint Set
Approach
Finding their root is quite simple. Just follow their parent node.
Is it efficient ?
POSCAT Seminar 1-8915 July 2014
yougatup
1
2
34 56 7
8
Disjoint Set
Approach
Finding their root is quite simple. Just follow their parent node.
Is it efficient ?
POSCAT Seminar 1-9015 July 2014
yougatup
1
2
3
4
5
6
7
8
Disjoint Set
Approach
Finding their root is quite simple. Just follow their parent node.
Is it efficient ? If the shape of tree is bad, it takes O(𝑛)
POSCAT Seminar 1-9115 July 2014
yougatup
1
2
3
4
5
6
7
8
Path Compression
Approach
However, we can “Compress” tree when we find the root node !
The idea is that the only requirement is to know root node
POSCAT Seminar 1-9215 July 2014
yougatup
1
2
3
4
5
6
7
8
Path Compression
Approach
However, we can “Compress” tree when we find the root node !
The idea is that the only requirement is to know root node
POSCAT Seminar 1-9315 July 2014
yougatup
1
2
3
4
5
6
7
8
Path Compression
Approach
However, we can “Compress” tree when we find the root node !
The idea is that the only requirement is to know root node
POSCAT Seminar 1-9415 July 2014
yougatup
1
2
3
4
5
6
7
8
Path Compression
Approach
However, we can “Compress” tree when we find the root node !
The idea is that the only requirement is to know root node
POSCAT Seminar 1-9515 July 2014
yougatup
1
2
3
4
5
6
7
8
Path Compression
Approach
However, we can “Compress” tree when we find the root node !
The idea is that the only requirement is to know root node
POSCAT Seminar 1-9615 July 2014
yougatup
1
2
3
4
5
6
7
8
Path Compression
Approach
However, we can “Compress” tree when we find the root node !
The idea is that the only requirement is to know root node
POSCAT Seminar 1-9715 July 2014
yougatup
1
2
3
4
5
6
7
8
Hey! 1 is the root !
Path Compression
Approach
However, we can “Compress” tree when we find the root node !
The idea is that the only requirement is to know root node
POSCAT Seminar 1-9815 July 2014
yougatup
1
2
3
4
5
6
7
8
Hey! 1 is the root !
Path Compression
Approach
However, we can “Compress” tree when we find the root node !
The idea is that the only requirement is to know root node
POSCAT Seminar 1-9915 July 2014
yougatup
1
2
3
4
5
6
7
8
Really? then I’ll cut this link and make a new link to the root directly !
Path Compression
Approach
However, we can “Compress” tree when we find the root node !
The idea is that the only requirement is to know root node
POSCAT Seminar 1-10015 July 2014
yougatup
1
2
3
4
5
6
7
8
Really? then I’ll cut this link and make a new link to the root directly !
Path Compression
Approach
However, we can “Compress” tree when we find the root node !
The idea is that the only requirement is to know root node
POSCAT Seminar 1-10115 July 2014
yougatup
1
2
3
4
5
6
7
8
Path Compression
Approach
However, we can “Compress” tree when we find the root node !
The idea is that the only requirement is to know root node
POSCAT Seminar 1-10215 July 2014
yougatup
1
3
4
5
6
7
8
2
Hey! 1 is the root !
Path Compression
Approach
However, we can “Compress” tree when we find the root node !
The idea is that the only requirement is to know root node
POSCAT Seminar 1-10315 July 2014
yougatup
1
3
4
5
6
7
8
2
Really? then I’ll cut this link and make a new link to the root directly !
Path Compression
Approach
However, we can “Compress” tree when we find the root node !
The idea is that the only requirement is to know root node
POSCAT Seminar 1-10415 July 2014
yougatup
1
3
4
5
6
7
8
2
Really? then I’ll cut this link and make a new link to the root directly !
Path Compression
Approach
However, we can “Compress” tree when we find the root node !
The idea is that the only requirement is to know root node
POSCAT Seminar 1-10515 July 2014
yougatup
1
3
5
6
7
8
2 4
Path Compression
Approach
However, we can “Compress” tree when we find the root node !
The idea is that the only requirement is to know root node
POSCAT Seminar 1-10615 July 2014
yougatup
1
3
5
6
7
8
2 4
Path Compression
Approach
We perform two things simultaneously
1. Find a root
2. Compress the tree !
POSCAT Seminar 1-10715 July 2014
yougatup
1
3
5
6
7
8
2 4
Path Compression
Approach
Then, How long does it take ?
POSCAT Seminar 1-10815 July 2014
yougatup
1
3
5
6
7
8
2 4