CS3000: Algorithms & Data Jonathan Ullman Lecture 13: Minimum - - PowerPoint PPT Presentation

cs3000 algorithms data jonathan ullman
SMART_READER_LITE
LIVE PREVIEW

CS3000: Algorithms & Data Jonathan Ullman Lecture 13: Minimum - - PowerPoint PPT Presentation

CS3000: Algorithms & Data Jonathan Ullman Lecture 13: Minimum Spanning Trees Mar 9, 2020 Midterm II In Class Wednesday March 25 th Working on a backup plan Exactly the same format/rules as Midterm I Topics: Graph


slide-1
SLIDE 1

CS3000: Algorithms & Data Jonathan Ullman

Lecture 13:

  • Minimum Spanning Trees

Mar 9, 2020

slide-2
SLIDE 2

Midterm II

  • In Class Wednesday March 25th
  • Working on a backup plan
  • Exactly the same format/rules as Midterm I
  • Topics: Graph Algorithms
  • Key definitions, properties
  • Representing graphs
  • DFS and topological sort
  • Shortest Paths: BFS, Dijkstra, Bellman-Ford
  • Minimum spanning trees
  • Network flow

} this week

slide-3
SLIDE 3

Minimum Spanning Trees

slide-4
SLIDE 4

Network Design

  • Build a cheap, well connected network
  • We are given
  • a set of nodes ! = #$, … , #'
  • a set of potential edges ( ⊆ !×!
  • Want to build a network to connect these locations
  • Every #+, #, must be well connected
  • Must be as cheap as possible
  • Many variants of network design
  • Recall the bus routes problem from HW2
slide-5
SLIDE 5

Minimum Spanning Trees (MST)

  • Input: a weighted graph - = !, (, ./
  • Undirected, connected, weights may be negative
  • All edge weights are distinct (makes life simpler)
  • Output: a minimum weight spanning tree 0
  • A spanning tree of - is a subset of 0 ⊆ ( of the edges

such that !, 0 forms a tree

  • Weight of a tree 0 is the sum of the edge weights
  • We’ll use 0∗ to denote “the” minimum spanning tree

nodes

potentialedges

edge

costs

O

Eet't

Henesinter IT

we

slide-6
SLIDE 6

Minimum Spanning Trees (MST)

6 12 5 14 3 8 10 15 9 7

3 53

6

7 18

9

15

I

I

slide-7
SLIDE 7

Minimum Spanning Trees (MST)

6 12 5 14 3 8 10 15 9 7

slide-8
SLIDE 8

MST Algorithms

  • There are at least four reasonable MST algorithms
  • Borůvka’s Algorithm: start with 0 = ∅, in each round

add cheapest edge out of each connected component

  • Prim’s Algorithm: start with some 3, at each step add

cheapest edge that grows the connected component

  • Kruskal’s Algorithm: start with 0 = ∅, consider edges in

ascending order, adding edges unless they create a cycle

  • Reverse-Kruskal: start with 0 = (, consider edges in

descending order, deleting edges unless it disconnects

slide-9
SLIDE 9

Cycles and Cuts

  • Cycle: a set of edges #$, #4 , #4, #5 , … , #6, #$

Cycle C = (1,2),(2,3),(3,4),(4,5),(5,6),(6,1)

1 3 8 2 6 7 4 5

  • Cut: a partition of the nodes into 7, 7̅

1 3 8 2 6 7 4 5

Cut S = {4, 5, 8} Cutset = (5,6), (5,7), (3,4), (3,5), (7,8)

S

slide-10
SLIDE 10

Cycles and Cuts

  • Fact: a cycle and a cutset intersect in an even

number of edges

slide-11
SLIDE 11

Cycles and Cuts

  • Fact: removing an edge from a cycle doesn’t

disconnect any nodes

O

O

slide-12
SLIDE 12

Properties of MSTs

  • Cut Property: Let 7 be a cut. Let 9 be the minimum

weight edge cut by 7. Then the MST 0∗ contains 9

  • We call such an 9 a safe edge
  • Cycle Property: Let : be a cycle. Let ; be the

maximum weight edge in :. Then the MST 0∗ does not contain ;.

  • We call such an ; a useless edge
slide-13
SLIDE 13

Proof of Cut Property

  • Cut Property: Let 7 be a cut. Let 9 be the minimum

weight edge cut by 7. Then the MST 0∗ contains 9

; 0∗ 9 7

remove f

Proof bycontradet

Assume T

is the MST and

it doesnt

contain

e

N

__

If we add

e

to

T

there must

be a

cycle C

C contains

72 edges

crossing the

ut

se f3

w f

sole If we

remove f from

T uEe3 the the total we

is lower than T

Tau Edl Ef

is still a tree is

slide-14
SLIDE 14

Proof of Cycle Property

  • Cycle Property: Let : be a cycle. Let ; be the max

weight edge in :. The MST 0∗ does not contain ;.

; 0∗ 9 7

Proof by

contradiction

Assume T

is the

MST and

J

µ

contains

f

If

we remove f

the graph THEM

has two

components 5,5

There is

some

edge etc

et by

S

utle

cut Cf

Thus

T 173 use

is

a spanning hee 4

low

weight

a

slide-15
SLIDE 15

Ask the Audience

  • Assume - has distinct edge weights
  • True/False? If 9 is the edge with the smallest

weight, then 9 is always in the MST 0∗

  • True/False? If ; is the edge with the largest

weight, then ; is never in the MST 0∗

slide-16
SLIDE 16

The “Only” MST Algorithm

  • GenericMST:
  • Let 0 = ∅
  • Repeat until 0 is connected:
  • Find one or more safe edges not in 0
  • Add safe edges to 0
  • Theorem: GenericMST outputs an MST

Suppose

T

is

not

connected

Then it has multiple

ied

crossing the art

is a safeedge

slide-17
SLIDE 17

Borůvka’s Algorithm

  • Borůvka:
  • Let 0 = ∅
  • Repeat until 0 is connected:
  • Let :$, … , :6 be the connected components of !, 0
  • Let 9$, … , 96 be the safe edge for the cuts :$, … , :<
  • Add 9$, … , 96 to 0
  • Correctness: every edge we add is safe

if

I

5

iii

i

Br Ce

Will contain duplicates

slide-18
SLIDE 18

Borůvka’s Algorithm

1 2 6 3 5 4 7 8

6 12 5 14 3 8 10 15 9 7

Label Connected Components

I

slide-19
SLIDE 19

Borůvka’s Algorithm

1 2 6 3 5 4 7 8

6 12 5 14 3 8 10 15 9 7

Add Safe Edges

slide-20
SLIDE 20

Borůvka’s Algorithm

1 1 1 2 1 2 1 1

6 12 5 14 3 8 10 15 9 7

Label Connected Components

slide-21
SLIDE 21

Borůvka’s Algorithm

1 1 1 2 1 2 1 1

6 12 5 14 3 8 10 15 9 7

Add Safe Edges

slide-22
SLIDE 22

Borůvka’s Algorithm

1 1 1 1 1 1 1 1

6 12 5 14 3 8 10 15 9 7

Done!

slide-23
SLIDE 23

Borůvka’s Algorithm (Running Time)

  • Borůvka
  • Let 0 = ∅
  • Repeat until 0 is connected:
  • Let :$, … , :6 be the connected components of !, 0
  • Let 9$, … , 96 be the safe edge for the cuts :$, … , :<
  • Add 9$, … , 96 to 0
  • Running time
  • How long to find safe edges?
  • How many times through the main loop?

ntm

BFS thegraph tofind components

Loopthrough edges keeptrack of

m n ut edgefor

each component

slide-24
SLIDE 24

Borůvka’s Algorithm (Running Time)

FindSafeEdges(G,T): find connected components =>, … , =? let L[v] be the component of node @ Let S[i] be the safe edge of =A for each edge (u,v) in E: If L[u] ≠ L[v]: If w(u,v) < w(S[L[u]]): S[L[u]] = (u,v) If w(u,v) < w(S[L[v]]): S[L[v]] = (u,v) Return {S[1],…,S[k]}

HusingBFS Dfs

Hln I

rally 4

May have duplicates

slide-25
SLIDE 25

Borůvka’s Algorithm (Running Time)

  • Claim: every iteration of the main loop halves the

number of connected components.

atleast

If the

claim

is

true

then

  • f

iterations

I Llogzln

slide-26
SLIDE 26

Borůvka’s Algorithm (Running Time)

  • Borůvka
  • Let 0 = ∅
  • Repeat until 0 is connected:
  • Let :$, … , :6 be the connected components of !, 0
  • Let 9$, … , 96 be the safe edge for the cuts :$, … , :<
  • Add 9$, … , 96 to 0
  • Running Time:
  • How long to find safe edges?
  • How many times through the main loop?

V E

is connected

so

Ms

n

l

htm E 2am11 04

ntm

per iteration

OClogh

T.me

OmLoglnl

slide-27
SLIDE 27

Prim’s Algorithm

  • Prim Informal
  • Let 0 = ∅
  • Let 3 be some arbitrary node and 7 = 3
  • Repeat until 7 = !
  • Find the cheapest edge 9 = C, # cut by 7. Add 9 to 0 and

add # to 7

  • Correctness: every edge we add is safe

s

So Eos

slide-28
SLIDE 28

Prim’s Algorithm

iii

i

slide-29
SLIDE 29

Prim’s Algorithm

Prim(G=(V,E)) let Q be a priority queue storing V value[v] ← ∞, last[v] ←⊥ value[s] ← G for some arbitrary H while (Q ≠ ∅): u ← ExtractMin(Q) for each edge (u,v) in E: if v ∈ Q and w(u,v) < value[v]: DecreaseKey(v,w(u,v)) last[v] ← u T = {(1,last[1]),…,(n,last[n])} (excluding s) return T

Time

Okhtm tog

n

0cmlog

m

n

Extract nm

m

Decreasekey

slide-30
SLIDE 30

Kruskal’s Algorithm

  • Kruskal’s Informal
  • Let 0 = ∅
  • For each edge e in ascending order of weight:
  • If adding 9 would decrease the number of connected

components add 9 to 0

  • Correctness: every edge we add is safe
slide-31
SLIDE 31

Kruskal’s Algorithm

slide-32
SLIDE 32

Implementing Kruskal’s Algorithm

  • Union-Find: group items into components so that

we can efficiently perform two operations:

  • Find(u): lookup which component contains u
  • Union(u,v): merge connected components of u,v
  • Can implement Union-Find so that
  • Find takes J 1 time
  • Any L Union operations takes J L log L time
slide-33
SLIDE 33

Kruskal’s Algorithm (Running Time)

  • Kruskal’s Informal
  • Let 0 = ∅
  • For each edge e in ascending order of weight:
  • If adding 9 would decrease the number of connected

components add 9 to 0

  • Time to sort:
  • Time to test edges:
  • Time to add edges:
slide-34
SLIDE 34

Comparison

  • Can compute MST in time P Q RST Q
  • Boruvka’s Algorithm:
  • Only algorithm worth implementing
  • Low overhead, can be easily parallelized
  • Each iteration takes J U , very few iterations in practice
  • Prim’s/Kruskal’s Algorithms:
  • Reveal useful structure of MSTs
  • Templates for other algorithms