31
CSE 421 Union Find DS Dijkstra’s Algorithm, Shayan Oveis Gharan 1

CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

CSE 421

Union Find DS Dijkstra’s Algorithm,

Shayan Oveis Gharan

1

Page 2: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Union Find Data StructureEach set is represented as a tree of pointers, where every vertex is labeled with longest path ending at the vertex

To check whether A,Q are in same connected component, follow pointers and check if root is the same.

vv

D,2

vv

A,1vv

B,0

vv

C,0

vv

W,1

vv

P,0vv

Q,0

{A,B,C,D} {W,P,Q}

Page 3: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Union Find Data StructureMerge: To merge two connected components, make the root with the smaller label point to the root with the bigger label (adjusting labels if necessary). Runs in O(1) time

vv

D,2

vv

A,1vv

B,0

vv

C,0

vv

W,1

vv

P,0vv

Q,0

vv

D,2

vv

A,1 vv

B,0

vv

C,0

vv

W,1

vv

P,0vv

Q,0

vv

W,1

vv

Q,0

vv

D,1

vv

A,0

vv

W,2

vv

Q,0vv

D,1

vv

A,0

At most one labelmust be adjusted

Page 4: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Depth vs SizeClaim: If the label of a root is k, there are at least 2! elements in the set. Therefore the depth of any tree in algorithm is at most log n

So, we can check if 𝑢, 𝑣 are in the same component in time 𝑂(log 𝑛)

vv

D,2

vv

A,1 vv

B,0

vv

C,0

vv

W,1

vv

P,0vv

Q,0

Page 5: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Depth vs Size: CorrectnessClaim: If the label of a root is k, there are at least 2! elements in the set.

Pf: By induction on k. Base Case (k = 0): this is true. The set has size 1.IH: Suppose the claim is true until some time tIS: If we merge roots with labels 𝑘" > 𝑘#, the number of vertices only increases while the label stays the same. If 𝑘" = 𝑘#, the merged tree has label 𝑘" + 1, and by induction, it has at least

2!! + 2!" = 2!!$"

elements.

Page 6: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Kruskal’s Algorithm with Union FindImplementation. Use the union-find data structure.

• Build set 𝑇 of edges in the MST.• Maintain a set for each connected component.• O(m log n) for sorting and O(m log n) for union-find

Kruskal(G, c) {Sort edges weights so that c1 £ c2 £ ... £ cm.𝑻 ← ∅

foreach (𝒖 ∈ 𝑽) make a set containing singleton {𝒖}

for i = 1 to mLet (u,v) = eiif (u and v are in different sets) {

𝑻 ← 𝑻È {𝒆𝒊}merge the sets containing 𝒖 and 𝒗

}return 𝑻

}

Find roots and compare

Merge at the roots

Page 7: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Removing weight Distinction AssumptionSuppose edge weights are not distinct, and Kruskal’s algorithm sorts edges so

𝑐/! ≤ 𝑐/" ≤ ⋯ ≤ 𝑐/#Suppose Kruskal finds tree 𝑇 of weight 𝑐(𝑇), but the optimal solution 𝑇∗ has cost 𝑐 𝑇∗ < 𝑐 𝑇 .

Perturb each of the weights by a very small amount so that𝑐!!" < 𝑐!"

" < ⋯ < 𝑐!#"

where 𝑐/$1 = 𝑐/$ + 𝑖. 𝜖

If 𝜖 is small enough, 𝑐1 𝑇∗ < 𝑐(𝑇). However, this contradicts the correctness of Kruskal’s algorithm, since the algorithm will still find 𝑇, and Kruskal’s algorithm is correct if all weights are distinct.

Page 8: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Single Source Shortest Path

Given an (un)directed graph G=(V,E) with non-negativeedge weights 𝑐! ≥ 0and a start vertex s

Find length of shortest paths from s to each vertex in G

UW

Amazon

Page 9: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Dijkstra’s AlgorithmMaintain a set S of vertices whose shortest paths are known• initially S={s}

Maintaining current best lengths of paths that only go through S to each of the vertices in G

• Path-lengths to elements of S will be right, to V-S they might not be right

Repeatedly add vertex v to S that has the shortest path-length of any vertex in V-S

• Update path lengths based on new paths through v

Page 10: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Dijkstra’s Algorithm: Example

0

¥¥

¥

¥

¥

¥

¥

¥

¥

¥¥

¥

425

10

1

8

43

4

3 1

5

8

2

5

6

7

9

s

Page 11: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Dijkstra’s Algorithm: Example

0

24

¥

¥

¥

¥

¥

¥

¥

¥¥

¥

425

10

1

8

43

4

3 1

5

8

2

5

6

7

9

s

Page 12: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Dijkstra’s Algorithm: Example

0

24

¥

¥

¥

¥

¥

¥

¥

¥¥

¥

425

10

1

8

43

4

3 1

5

8

2

5

6

7

9

s

Page 13: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Dijkstra’s Algorithm: Example

0

24

¥

¥

9

¥

¥

¥

¥

¥¥

¥

425

10

1

8

43

4

3 1

5

8

2

5

6

7

9

s

Page 14: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Dijkstra’s Algorithm: Example

0

24

¥

¥

9

¥

¥

¥

¥

¥¥

¥

425

10

1

8

43

4

3 1

5

8

2

5

6

7

9

s

Page 15: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Dijkstra’s Algorithm: Example

0

24

¥

8

9

¥

¥

¥

¥

75

¥

425

10

1

8

43

4

3 1

5

8

2

5

6

7

9

s

Page 16: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Dijkstra’s Algorithm: Example

0

24

¥

8

9

¥

¥

¥

¥

75

¥

425

10

1

8

43

4

3 1

5

8

2

5

6

7

9

s

Page 17: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Dijkstra’s Algorithm: Example

0

24

¥

8

9

¥

¥

¥

¥

75

13

425

10

1

8

43

4

3 1

5

8

2

5

6

7

9

s

Page 18: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Dijkstra’s Algorithm: Example

0

24

¥

8

9

¥

¥

¥

¥

75

13

425

10

1

8

43

4

3 1

5

8

2

5

6

7

9

s

Page 19: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Dijkstra’s Algorithm: Example

0

24

¥

8

9

¥

¥

¥

¥

75

13

425

10

1

8

43

4

3 1

5

8

2

5

6

7

9

s

Page 20: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Dijkstra’s Algorithm: Example

0

24

16

8

9

¥

¥

¥

¥

75

13

425

10

1

8

43

4

3 1

5

8

2

5

6

7

9

s

Page 21: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Dijkstra’s Algorithm: Example

0

24

16

8

9

¥

¥

¥

¥

75

13

425

10

1

8

43

4

3 1

5

8

2

5

6

7

9

s

Page 22: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Dijkstra’s Algorithm: Example

0

24

16

8

9

15

¥

10

¥

75

13

425

10

1

8

43

4

3 1

5

8

2

5

6

7

9

s

Page 23: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Dijkstra’s Algorithm: Example

0

24

16

8

9

15

¥

10

¥

75

13

425

10

1

8

43

4

3 1

5

8

2

5

6

7

9

s

Page 24: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Dijkstra’s Algorithm: Example

0

24

16

8

9

15

14

10

20

75

13

425

10

1

8

43

4

3 1

5

8

2

5

6

7

9

s

Page 25: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Dijkstra’s Algorithm: Example

0

24

16

8

9

15

14

10

20

75

13

425

10

1

8

43

4

3 1

5

8

2

5

6

7

9

s

Page 26: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Dijkstra’s Algorithm: Example

0

24

16

8

9

15

14

10

19

75

13

425

10

1

8

43

4

3 1

5

8

2

5

6

7

9

s

Page 27: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Dijkstra’s Algorithm: Example

0

24

16

8

9

15

14

10

19

75

13

425

10

1

8

43

4

3 1

5

8

2

5

6

7

9

s

Page 28: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Dijkstra’s Algorithm: Example

0

24

16

8

9

15

14

10

18

75

13

425

10

1

8

43

4

3 1

5

8

2

5

6

7

9

s

Page 29: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Dijkstra’s Algorithm: Example

0

24

16

8

9

15

14

10

18

75

13

425

10

1

8

43

4

3 1

5

8

2

5

6

7

9

s

Page 30: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Dijkstra’s Algorithm

Dijkstra(G, c, s) {foreach (v Î V) d[v] ¬ ¥ //This is the key of node v𝒅 𝒔 ← 𝟎foreach (v Î V) insert v onto a priority queue QInitialize set of explored nodes S ¬ {s}

while (Q is not empty) {u ¬ delete min element from QS ¬ S È { u }foreach (edge e = (u, v) incident to u)

if ((v Ï S) and (d[u]+ce < d[v]))𝒅 𝒗 ← 𝒅 𝒖 + 𝒄𝒆Decrease key of v to d[v].𝑷𝒂𝒓𝒆𝒏𝒕 𝒗 ← 𝒖

}

Page 31: CSE 421 - courses.cs.washington.edu › courses › cse421 › ... · If 8is small enough, 211∗

Disjkstra’s Algorithm: CorrectnessProve by induction that throughout the algorithm, for any 𝑢 ∈ 𝑆, the path 𝑃< in the shortest from s to u.Base Case: This is always true when 𝑆 = 𝑠 .IH: Suppose |𝑆| = 𝑘 and the claim holds for S

IS: Say 𝑣 is the k+1-st vertex that we add to S. Let {u,v} be last edge on 𝑃= .If 𝑃= is not the shortest path there is a path 𝑃 to s which is shorter. Consider the first time that P leaves S(with edge {x,y}). S -> x has weight (at least) d(x)So, 𝑐 𝑃 ≥ 𝑑 𝑥 + 𝑐>,@ ≥ 𝑑 𝑣 = 𝑐 𝑃= .A contradiction.

v

y

u

sx

𝑃!

𝑃