109
POSCAT Seminar 10 : Graph 2 yougatup @ POSCAT 1

POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

POSCAT Seminar 10 :Graph 2

yougatup @ POSCAT

1

Page 2: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 3: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 4: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 5: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 6: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 7: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 8: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 9: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 10: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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 ?

Page 11: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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 !

Page 12: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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 ?

Page 13: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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 ?

Page 14: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 15: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 16: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 17: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 18: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 19: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 20: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 21: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 22: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 23: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 24: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 25: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 26: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 27: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 28: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 29: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 30: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 31: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 32: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 33: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 34: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 35: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 36: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 37: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 38: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 39: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 40: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 41: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 42: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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 !

Page 43: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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 !

Page 44: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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 !

Page 45: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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(𝑽𝟐)

Page 46: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 47: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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 ?

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

Page 48: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 49: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 50: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 51: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 52: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 53: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 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 !

Page 54: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

Page 55: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 56: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 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

Page 57: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 58: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 59: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 60: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 61: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 62: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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 … 𝑘

Page 63: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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

𝑖 𝑗

𝑘

Page 64: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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-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)

Page 65: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 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-6515 July 2014

yougatup

𝑖 𝑗

𝑘

∴ T(𝑘, 𝑖, 𝑗) = T(𝑘 − 1, 𝑖, 𝑘) + T(𝑘 − 1, 𝑘, 𝑗)

1 … (𝑘 − 1) 1 … (𝑘 − 1)

Page 66: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

𝑖 𝑗

Page 67: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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, 𝑖, 𝑗)

Page 68: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 69: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 70: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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(𝒏𝟑)

Page 71: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 72: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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 !

Page 73: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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 !

Page 74: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 75: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 76: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 77: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 78: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

Disjoint Set

Approach

How can we represent a group ?

POSCAT Seminar 1-7815 July 2014

yougatup

1 2 3

4 5

6 78

Page 79: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 80: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 81: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 82: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 83: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 84: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 85: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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 !

Page 86: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 87: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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 !

Page 88: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 89: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 90: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 91: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 92: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 93: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 94: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 95: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 96: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 97: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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 !

Page 98: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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 !

Page 99: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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 !

Page 100: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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 !

Page 101: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 102: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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 !

Page 103: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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 !

Page 104: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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 !

Page 105: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 106: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 107: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

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

Page 108: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

Path Compression

Approach

Then, How long does it take ?

POSCAT Seminar 1-10815 July 2014

yougatup

1

3

5

6

7

8

2 4

Page 109: POSCAT Seminar 10 : Graph 2 · Shortest Path Problem Given a graph, find a shortest path from start vertex to end vertex POSCAT Seminar 1-3 15 July 2014 yougatup 1 2 3 4 6 5 7 8 3

Path Compression

Approach

Then, How long does it take ?

Amazingly, it takes just constant time. i.e. O(1)

Analysis is very complex. We don’t discuss it.

POSCAT Seminar 1-10915 July 2014

yougatup

1

3

5

6

7

8

2 4