CS 401 Dijkstras Algorithm / Minimum Spanning Tree Xiaorui Sun 1 - - PowerPoint PPT Presentation

cs 401
SMART_READER_LITE
LIVE PREVIEW

CS 401 Dijkstras Algorithm / Minimum Spanning Tree Xiaorui Sun 1 - - PowerPoint PPT Presentation

CS 401 Dijkstras Algorithm / Minimum Spanning Tree Xiaorui Sun 1 Single Source Shortest Path Given an (un)directed connected graph ! = ($, &) with non- negative edge weights ( ) 0 and a start vertex , . Find length of shortest paths


slide-1
SLIDE 1

CS 401

Dijkstra’s Algorithm / Minimum Spanning Tree

Xiaorui Sun

1

slide-2
SLIDE 2

Single Source Shortest Path

Given an (un)directed connected graph ! = ($, &) with non- negative edge weights () ≥ 0 and a start vertex ,. Find length of shortest paths from , to each vertex in !

Cost of path s-2-3-4-t = 9 + 23 + 6 + 6 = 44.

s 3 t 2 6 7 4 5 23 18 2 9 14 15 5 30 20 44 16 11 6 19 6 length of path = sum of edge weights in path

slide-3
SLIDE 3

Dijkstra(!, #, $) { Initialize set of explored nodes % ← {$} // Maintain distance from $ to each vertices in % ) $ ← * while (% ≠ ,) { Pick an edge (., /) such that . ∈ % and / ∉ % and )[.] + #(.,/) is as small as possible. Add / to % and define )[/] = )[.] + #(.,/). 789:;< / ← .. }

Dijkstra’s Algorithm

Theorem: ) / is the length of shortest path from $ to /

slide-4
SLIDE 4

Remarks on Dijkstra’s Algorithm

  • Algorithm produces a tree of shortest paths to ! following

Parent links (for undirected graph)

  • Algorithm works on directed graph (with nonnegative weights)
  • The algorithm fails with negative edge weights.
  • Why does it fail?
slide-5
SLIDE 5

Implementing Dijkstra’s Algorithm

Trivial implementation

Dijkstra(!, #, $) { Initialize set of explored nodes % ← {$} // Maintain distance from $ to each vertices in % ) $ ← * while (% ≠ ,) { Pick an edge (., /) such that . ∈ % and / ∉ % and )[.] + #(.,/) is as small as possible. Add / to % and define )[/] = )[.] + #(.,/). 789:;< / ← .. }

O(m) time O(n) iterations Overall: O(nm) time O(1) time

slide-6
SLIDE 6

Implementing Dijkstra’s Algorithm

Better idea: for each ! ∉ # , maintain min

'∈): ',, ∈- .[0] + 3(0,!) 2 4 16 8 9 15 ¥ 10 ¥ 7 5 13

4 2 5 10 1 8 4 3 4 3 1 5 8 2 5 6 7 9

s

slide-7
SLIDE 7

Implementing Dijkstra’s Algorithm

Better idea: for each ! ∉ # maintain min

'∈): ',, ∈- .[0] + 3(0,!)

Dijkstra(6, 3, 7) { Initialize set of explored nodes # ← {7} // Maintain distance from 7 to each vertices in # . 7 ← ; while (# ≠ =) { Pick an edge (0, !) such that 0 ∈ # and ! ∉ # and .[0] + 3(0,!) is as small as possible. Add ! to # and define .[!] = .[0] + 3(0,!). ?@ABCD ! ← 0. Maintain min

E∈): E,F ∈- .[@] + 3(@,G) for each G ∉ #

}

O(n) time O(n) iterations Overall: O(n2+m) time O(1) time O(deg(u)) time

slide-8
SLIDE 8

Implementing Dijkstra’s Algorithm

Priority Queue: Elements each with an associated key Operations

  • Insert
  • Find-min

– Return the element with the smallest key

  • Delete-min

– Return the element with the smallest key and delete it from the data structure

  • Decrease-key

– Decrease the key value of some element

Implementations

Arrays:

  • !(#) time find/delete-min,
  • !(1) time insert/decrease key

Binary Heaps:

  • !(log #) time insert/decrease-key/delete-min,
  • !(1) time find-min

Fibonacci heap:

  • !(1) time insert/decrease-key
  • !(log #) delete-min
  • O(1) time find-min

Read wiki!

maintain min

,∈.: ,,1 ∈2 3[5] + 8(5,9)

for each 9 ∉ ;

slide-9
SLIDE 9

Dijkstra(!, #, $) { Initialize set of explored nodes % ← {$} // Maintain distance from $ to each vertices in % ) $ ← * Insert all neighbors + of s into a priority queue with value #($,+). while (% ≠ /) { Pick an edge (0, +) such that 0 ∈ % and + ∉ % and )[0] + #(0,+) is as small as possible. v ¬ delete min element from 6 Add + to % and define )[+] = )[0] + #(0,+). 89:;<= + ← 0. foreach (edge ; = (+, >) incident to +) if (> ∉ %) if (> is not in the 6) Insert > into 6 with value ) + + #(+,>) else (the key of > > ) + + #(+,>)) Decrease key of ? to )[+] + #(+,>). }

@(A) of delete min, each in @(log A) @(A) of insert, each in @(1) @(F) of decrease/insert key, each runs in @(1) Overall: O((nlog n)+m) time

slide-10
SLIDE 10

10

Minimum Spanning Tree

slide-11
SLIDE 11

Spanning Tree

Given a connected undirected graph ! = #, % . We call & is a spanning tree of ! if

  • All edges in & are from %.
  • & includes all of the vertices of !.

11

& !

slide-12
SLIDE 12

Minimum Spanning Tree (MST)

Given a connected undirected graph ! = ($, &) with real- valued edge weights () ≥ 0. An MST , is a spanning tree whose sum of edge weights is minimized.

12

5 23 10 21 14 24 16 6 4 18 9 7 11 8

! = ($, &)

5 6 4 9 7 11 8

( , = -

)∈/

() = 50

slide-13
SLIDE 13

Kruskal’s Algorithm [1956]

Kruskal(G, c) { Sort edges weights so that !" ≤ !$ ≤ ⋯ ≤ !&. ' ← ∅ foreach (* ∈ ,) make a set containing singleton {*} for / = " to & Let *, 2 = 3/ if (* and 2 are in different sets) { ' ← ' ∪ {3/} merge the sets containing * and 2 } return ' }

Kruskal

Sort edges weight. Add edges whenever it does not create cycle.

slide-14
SLIDE 14

Cuts

In a graph ! = ($, &), a cut is a bipartition of V into disjoint sets (, $ − ( for some ( ⊆ $. We denote it by ((, $ − (). An edge , = {., /} is in the cut ((, $ − () if exactly one of ., / is in (.

14

S V-S u v x S V-S

slide-15
SLIDE 15

Properties of the OPT

Simplifying assumption: All edge costs !" are distinct. Cut property: Let # be any subset of nodes (called a cut), and let $ be the min cost edge with exactly one endpoint in #. Then every MST contains $. Cycle property. Let % be any cycle, and let & be the max cost edge belonging to %. Then no MST contains &.

15 10

S red edge is in the MST Green edge is not in the MST

5 7 2 3 5 4 7

V-S

slide-16
SLIDE 16

Cut Property: Proof

Simplifying assumption: All edge costs !" are distinct. Cut property. Let # be any subset of nodes, and let $ be the min cost edge with exactly one endpoint in #. Then any MST %∗ contains $.

  • Proof. By contradiction

Suppose $ = {), +} does not belong to %∗. There is a path from ) to + in %∗ Þ there exists another edge, say -, that leaves #. Adding $ to %∗ creates a cycle . in %∗. % = %∗ È {$} − {-} is also a spanning tree. Since !" < !1, ! % < !(%∗). This is a contradiction.

17 f T* e

S u v (coz all tree has 4 − 1 edges)

slide-17
SLIDE 17

Cycle Property: Proof

Simplifying assumption: All edge costs !" are distinct. Cycle property: Let # be any cycle in $, and let % be the max cost edge belonging to #. Then the MST &∗ does not contain %.

  • Proof. By contradiction

Suppose % belongs to &∗. Deleting % from &∗ cuts &∗ into two connected components. There exists another edge, say (, that is in the cycle and connects the components. & = &∗ È {(} − {%} is also a spanning tree. Since !" < !., ! & < !(&∗). This is a contradiction.

18 f T* e

S

Every connected graph has a spanning tree. Hence it has at least 1 − 1 edges.

slide-18
SLIDE 18

Proof of Correctness (Kruskal)

Consider edges in ascending order of weight. Case 1: adding ! to " creates a cycle, ! is the maximum weight edge in that cycle. cycle property show ! is not in any minimum spanning tree. Case 2: ! = (%, ') is the minimum weight edge in the cut ) where ) is the set of nodes in %’s connected component. So, ! is in all minimum spanning tree.

v u

Case 2

e

S Case 1

! This proves MST is unique if weights are distinct.

slide-19
SLIDE 19

20

Summary

  • Greedy algorithm: ‘Best’ current partial solution at each

step

  • Design greedy algorithm:

How to order your input Strategy for every step

  • Greedy Analysis Strategies

Greedy algorithm stays ahead Structural Exchange argument