Spanning Trees 0 3 Review 4 1 2 Graphs o Vertices, edges, - - PowerPoint PPT Presentation

spanning trees
SMART_READER_LITE
LIVE PREVIEW

Spanning Trees 0 3 Review 4 1 2 Graphs o Vertices, edges, - - PowerPoint PPT Presentation

Spanning Trees 0 3 Review 4 1 2 Graphs o Vertices, edges, neighbors, paths, o Dense, sparse Adjacency matrix 0 1 2 3 4 0 1 implementation 2 3 4 Adjacency list 0


slide-1
SLIDE 1

Spanning Trees

slide-2
SLIDE 2

Review

 Graphs

  • Vertices, edges, neighbors, paths, …
  • Dense, sparse

 Adjacency matrix implementation  Adjacency list implementation

1 3 4 2

1 2 3 4 1 4 4 2 4 1 4 3 1 2 1 2 3 4

 

1 

 

2

  

3

4   

1

slide-3
SLIDE 3

Review

 Graph search

  • Determine whether two vertices are connected
  • and possibly report a path that connects them

 Explore the graph by expanding the frontier

  • Depth-first search
  • Charge ahead until we find the target vertex or hit a dead-end
  • then backtrack
  • Breadth-first search
  • Explore the graph level-by level

 Complexity

1 3 4 2

DFS BFS Adjacency list O(v + e) O(v + e) Adjacency matrix O(min(v2,ev)) O(min(v2,ev))

Remember the vertices to visit next a work list in a stack in a queue O(e) in practice O(v2) in practice

2

slide-4
SLIDE 4

Trees

3

slide-5
SLIDE 5

Cycles

 A cycle is a path from a vertex to itself

  • 0–1–4–0 is a cycle
  • 0–1–0

is a cycle

is a cycle too

 A simple cycle is a cycle with at least one edge and without repeated edges

  • 0–1–4–0 is a simple cycle
  • 0–1–0

is not a simple cycle

is not a simple cycle either

1 3 4 2

these are trivial cycles these are trivial cycles

4

slide-6
SLIDE 6

Simple Cycles

A cycle without repeated edges

  • and at least one edge

 Simple cycles are what forces us to use a mark array in DFS and BFS

  • After following edge (0,1) to go from 0 to 1,

it is easy to avoid using (0,1) to go back to 0

  • remembering where we come from is trivial
  • After following (0,1) and (1,4) to go from 0 to 4,

it is hard to know we shouldn’t use (0,4)

  • unless we mark visited vertices

 Graphs without simple cycles are convenient to work with

  • no need for mark arrays

1 3 4 2

5

slide-7
SLIDE 7

Trees

 A connected graph without simple cycles is called a tree  The are many ways to define a tree

6

slide-8
SLIDE 8

A Recursive Definition

We can also define trees recursively A tree is  a vertex by itself  two trees connected by an edge

7

slide-9
SLIDE 9

Another Recursive Definition

We can define trees recursively in several ways A tree is  a vertex by itself  a tree connected to a vertex by an edge

8

slide-10
SLIDE 10

The Edges of a Tree

 A tree is a connected graph with v vertices and v-1 edges

11 vertices 10 edges

9

slide-11
SLIDE 11

The Paths of a Tree

 A tree is a connected graph with exactly one path between any two vertices

10

slide-12
SLIDE 12

The Edges of a Tree

 We can prove that these definitions are equivalent

  • For example,

if we define a tree as a vertex by itself or a tree connected to a vertex by an edge, then if such a graph has v vertices it has v-1 edges

A vertex by itself This graph has

  • 1 vertex
  • 0 = 1-1 edges

A tree connected to a vertex by an edge Assume by induction hypothesis that T has v vertices and v-1 edge. Then, this graph has

  • v+1 vertices
  • v-1+1 = (v+1) - 1 edges

T

base case recursive case

11

slide-13
SLIDE 13

In Summary, a Tree is …

  • A. a connected graph with no simple cycles
  • B. (recursive definition #1)
  • a vertex
  • two trees connected by an edge
  • C. (recursive definition #2)
  • a vertex
  • a tree connected to a vertex by an edge
  • D. a connected graph with v vertices and v-1 edges
  • E. a connected graph with exactly 1 path between any two

vertices

12

slide-14
SLIDE 14

Forest

 A forest is a bunch of trees

  • a graph where each connected component is a tree

 Other definitions

  • a forest is a connected graph with no simple cycles
  • a graph with at most one path between any two vertices

 A forest with v vertices has at most v-1 edges

that was the definition of a tree

13

slide-15
SLIDE 15

Reachability Problem on a Tree

 What is the cost of DFS or BSF on a tree?

  • assuming an adjacency list implementation

O(v) — always

  • DFS and BFS cost O(v + e) in general
  • in a tree, e = v-1
  • definition D
  • so, the cost reduces to O(v)
  • A. a connected graph with no simple cycles
  • B. (recursive definition #1)
  • a vertex
  • two trees connected by an edge
  • C. (recursive definition #2)
  • a vertex
  • a tree connected to a vertex by an edge
  • D. a connected graph with v vertices and v-1 edges
  • E. a connected graph with exactly 1 path between

any two vertices

14

slide-16
SLIDE 16

Are BSTs Trees?

 A binary search tree is a tree where every vertex has at most 3 edges

  • two children
  • one parent

(plus there is the ordering invariant)  Which node is the root?

  • any vertex with at most 2 edges
  • the root does not have a parent

Simply hoist the graph by that node

15

slide-17
SLIDE 17

Spanning Trees

16

slide-18
SLIDE 18

Reaching Nodes Over and Over

 Some applications need to frequently reach a connected vertex in a graph

  • diagnosis in communication networks
  • billing in power networks, ..

 We can use DFS or BFS

  • but this is expensive: O(e) each query
  • it may go through a different path for the same query each time

 We can remember the paths

  • but this requires a lot of space
  • O(v2) in each vertex

 each vertex needs to remember v-1 paths  each of these paths can contain up to v-1 vertices

  • O(v3) for the whole graph

1 3 4 2

17

slide-19
SLIDE 19

Reaching Nodes Over and Over

 Some applications need to frequently reach a connected vertex in a graph

  • using DFS or BFS is too expensive
  • remembering paths to all vertices is O(v3) for the whole graph

Idea  Factor out the common subpaths by superimposing a tree on the graph

  • provides a path from every vertex to every other vertex
  • requires O(v) space in each vertex
  • O(v) total if each vertex is connected to

a “path server” vertex

 This is a spanning tree

1 3 4 2

If the graph has more than one connected component, we superimpose one spanning tree on each connected component — this is a spanning forest

18

slide-20
SLIDE 20

Spanning Tree

 Factor out the common subpaths by superimposing a tree (or forest) on the graph Formally,  A subgraph of a graph G is a graph with the same vertices and a subset of its edges  A spanning tree for G is a subgraph that

  • has the same connectivity as G
  • and is a tree

 A spanning forest for G is a subgraph that

  • has the same connectivity as G
  • and is a forest

1 3 4 2

a bunch of spanning trees A graph has a spanning trees

  • nly if it consists of a

single connected component

19

slide-21
SLIDE 21

The Spanning Trees of a Graph

 Most graphs have multiple spanning trees  Here are some  In general, any spanning tree will do

1 3 4 2 1 3 4 2 1 3 4 2 1 3 4 2 1 3 4 2

20

slide-22
SLIDE 22

How to Compute a Spanning Tree?

Two classic algorithms  The edge-centric algorithm

Start with a spanning forest of singleton trees and add edges from the graph as long as they don’t form a cycle

 The vertex-centric algorithm

Start with a single vertex in the tree and add edges to vertices not in the tree

This leverages definition B

A tree is

  • a vertex, or
  • two trees connected by an edge

This leverages definition C

A tree is

  • a vertex, or
  • a trees connected to a vertex

by an edge 21

slide-23
SLIDE 23

Edge-centric Algorithm

22

slide-24
SLIDE 24

The Edge-centric Algorithm

Start with a spanning forest of singleton trees and add edges from the graph as long as they don’t form a cycle

 Let’s run it on the example graph

1 3 4 2 1 3 4 2 1 3 4 2 1 3 4 2 1 3 4 2 1 3 4 2 1 3 4 2 (0,1)? (0,4)? (1,4)? (1,2)? (1,2)? (2,3)? 1 3 4 2 (2,4)?

A spanning forest

  • f singleton nodes

The resulting spanning tree

23

slide-25
SLIDE 25

Towards an Actual Algorithm

Start with a spanning forest of singleton trees and add edges from the graph as long as they don’t form a cycle

Given a graph G, construct a spanning tree T for it

  • 1. Start T with the isolated vertices of G
  • 2. For each edge (u,v) in G
  • are u and v already connected in T?
  • yes: discard the edge
  • no: add it to T

If G has more than 1 connected component, this will produce a spanning forest

24

slide-26
SLIDE 26

Towards an Actual Algorithm

Given a graph G, construct a spanning tree T for it

  • 1. Start T with the isolated vertices of G
  • 2. For each edge (u,v) in G
  • are u and v already connected in T?
  • yes: discard the edge
  • no: add it to T

 Is there room for improvement?

  • Stop as soon as we added v-1 edges in T

By definition D

A tree is a connected graph v vertices and v-1 edges 25

slide-27
SLIDE 27

The Edge-centric Algorithm

Given a graph G, construct a spanning tree T for it

  • 1. Start T with the isolated vertices of G
  • 2. For each edge (u,v) in G
  • are u and v already connected in T?
  • yes: discard the edge
  • no: add it to T
  • Stop once T has v-1 edges

 What is its complexity?

This won’t apply if G has more than 1 connected component

26

slide-28
SLIDE 28

Complexity

Given a graph G, construct a spanning tree T for it

  • 1. Start T with the isolated vertices of G
  • 2. For each edge (u,v) in G
  • are u and v already connected in T?
  • yes: discard the edge
  • no: add it to T
  • Stop once T has v-1 edges

O(1)

This is just graph_new Use DFS or BFS

  • n T for this

e times O(v) O(1)

This is graph_addedge

27

slide-29
SLIDE 29

Complexity

Given a graph G, construct a spanning tree T for it

  • 1. Start T with the isolated vertices of G
  • 2. For each edge (u,v) in G
  • are u and v already connected in T?
  • yes: discard the edge
  • no: add it to T
  • Stop once T has v-1 edges

 We run DFS/BFS on T

  • at most v-1 edges
  • the cost is O(v)
  • not O(e)

O(1)

Use DFS or BFS

  • n T for this

e times O(v) O(1)

28

slide-30
SLIDE 30

Complexity

Given a graph G, construct a spanning tree T for it

  • 1. Start T with the isolated vertices of G
  • 2. For each edge (u,v) in G
  • are u and v already connected in T?
  • yes: discard the edge
  • no: add it to T
  • Stop once T has v-1 edges

 Even if we end up adding at most v-1 edges, we may need to go through all the edges in e

O(1) e times O(v) O(1)

29

slide-31
SLIDE 31

Complexity

Given a graph G, construct a spanning tree T for it

  • 1. Start T with the isolated vertices of G
  • 2. For each edge (u,v) in G
  • are u and v already connected in T?
  • yes: discard the edge
  • no: add it to T
  • Stop once T has v-1 edges

 The edge-centric algorithm has complexity O(ev)

  • that’s O(v2) for sparse graphs

O(1) e times O(v) O(1)

30

slide-32
SLIDE 32

Greedy Algorithms

 At each step, we choose a candidate edge to add to the tree  Which edge does not matter

  • we will get a spanning tree in the end
  • possibly a different one for each choice

 Algorithms where we have to make a choice but the actual choice does not matter are called greedy

31

slide-33
SLIDE 33

Greedy Algorithms

 Algorithms where we have to make a choice but the actual choice does not matter are called greedy  DFS and BFS also involve making a choice

  • which vertex to examine next
  • but if we don’t pick the right one we may not compute the correct

answer

  • we need to remember the alternative choices
  • in a work list

DFS and BFS are not greedy  Greedy algorithms are great

  • no need to remember alternatives
  • but few problems have greedy algorithms that solve them

32

slide-34
SLIDE 34

Intermission

33

slide-35
SLIDE 35

How are maze screen- savers generated?

34

slide-36
SLIDE 36

How to Create a Screensaver Maze?

Start with an n * m grid of cells Place a node in every cell and an edge between adjacent cells

35

slide-37
SLIDE 37

How to Create a Screensaver Maze?

Build a spanning tree for this graph Dissolve the cell walls where its edges cross

36

slide-38
SLIDE 38

How to Create a Screensaver Maze?

Pick a start and an end along the perimeter Get rid of the graph end end start start

37

slide-39
SLIDE 39

How to Solve a Screensaver Maze?

end start end start Run DFS on the spanning tree

The animation is DFS exploring vertices and backtracking

38

slide-40
SLIDE 40

What about Pipe Screensavers?

Same thing in 3 dimensions

39

slide-41
SLIDE 41

Vertex-centric Algorithm

40

slide-42
SLIDE 42

The Vertex-centric Algorithm

Start with a single vertex in the tree and add edges to vertices not in the tree

 Let’s run it on the example graph

1 3 4 2 1 3 4 2 1 3 4 2 1 3 4 2 1 3 4 2 1 3 4 2 (0,1) (1,4) (1,2) (2,3)

Start with 0 The resulting spanning tree

41

slide-43
SLIDE 43

Towards an Algorithm

Start with a single vertex in the tree and add edges to vertices not in the tree

Given a graph G, construct a spanning tree T for it

  • 1. Pick an arbitrary vertex start in G and put it in T
  • 2. Repeat until all vertices are in T
  • find an edge (u,v) in G between a vertex u in T and

a vertex v not in T

  • add (u,v) to T

 How do we find (u,v)?

  • Consider the neighbors of the vertices we added in T

Assume G has a single connected component

42

slide-44
SLIDE 44

Towards an Actual Algorithm

Given a graph G, construct a spanning tree T for it

  • 1. Pick an arbitrary vertex start in G and put it in T
  • mark start
  • add all edges (start,w) in G to a work list
  • 2. Repeat until the work list is empty
  • pick an edge (u,v) from the work list
  • if v is marked, discard it
  • add (u,v) to T
  • mark v
  • add to the work list all edges (v,w) in G such that w is unmarked
  • stop once T has v-1 edges

Consider the neighbors of the vertices added to T Consider the neighbors of the vertices added to T Assume G has a single connected component This is our early exit condition

43

slide-45
SLIDE 45

Towards an Actual Algorithm

 This looks just like BSF and DFS

  • depending on the work list

 The edges followed by BFS and DFS form a spanning tree!

  • 1. Pick an arbitrary vertex start in G and put it in T
  • mark start
  • add all edges (start,w) in G to a work list
  • 2. Repeat until the work list is empty
  • pick an edge (u,v) from the work list
  • if v is marked, discard it
  • add (u,v) to T
  • mark v
  • add to the work list all edges (v,w) in G

such that w is unmarked

  • stop once T has v-1 edges

44

slide-46
SLIDE 46

Disconnected Graphs

 If G has more than one connected component, this will find a spanning tree only for start’s component  We need to repeat with a start vertex from each connected component

  • 1. Pick an arbitrary vertex start in G and put it in T
  • mark start
  • add all edges (start,w) in G to a work list
  • 2. Repeat until the work list is empty
  • pick an edge (u,v) from the work list
  • if v is marked, discard it
  • add (u,v) to T
  • mark v
  • add to the work list all edges (v,w) in G

such that w is unmarked

  • stop once T has v-1 edges

45

slide-47
SLIDE 47

Disconnected Graphs

Given a graph G, construct a spanning tree T for it

  • 1. Pick an arbitrary vertex start in G and put it in T
  • mark start
  • add all edges (start,w) in G to a work list
  • 2. Repeat until the work list is empty
  • pick an edge (u,v) from the work list
  • if v is marked, discard it
  • add (u,v) to T
  • mark v
  • add to the work list all edges (v,w) in G such that w is unmarked
  • stop once T has v-1 edges
  • 3. If T has fewer than v-1 edges
  • add an arbitrary unmarked vertex and continue with (1)

46

slide-48
SLIDE 48

Complexity

 The vertex-centric algorithm has the same complexity as DFS and BFS

  • if we use a stack or a queue as

the work list

O(e)

  • 1. Pick an arbitrary vertex start in G and put it in T
  • mark start
  • add all edges (start,w) in G to a work list
  • 2. Repeat until the work list is empty
  • pick an edge (u,v) from the work list
  • if v is marked, discard it
  • add (u,v) to T
  • mark v
  • add to the work list all edges (v,w) in G

such that w is unmarked

  • stop once T has v-1 edges
  • 3. If T has fewer than v-1 edges
  • add an arbitrary unmarked vertex and

continue with (1)

This too is a greedy algorithm

47

slide-49
SLIDE 49

Minimum Spanning Trees

48

slide-50
SLIDE 50

Weighted Graphs

 A graph with measures associated with the edges is a weighted graph

  • the measures are called weights
  • for us, they will be integers

 The weights represent some kind of cost or value of using that edge

  • time
  • distance
  • power, …

1 3 4 2 11 7 23 17 13 19

49

slide-51
SLIDE 51

50

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

The weight of an edge is the driving distance between the cities, rounded to the next 100 miles

slide-52
SLIDE 52

Minimum Spanning Tree

 Of all the spanning trees for a weighted graph, one with the least total weight

 the sum of weights of all its edges

is called a minimum spanning tree  A graph may have several minimum spanning trees

  • if all the weights are the same,

every spanning tree is a minimum spanning tree

They should be called minimal spanning trees

51

slide-53
SLIDE 53

Computing MSTs

 The algorithms for computing spanning trees are easily adapted to minimum spanning trees

  • The edge-centric algorithm for MSTs is called

Kruskal’s algorithm

  • The vertex-centric algorithm for MSTs is called

Prim’s algorithm

52

slide-54
SLIDE 54

Kruskal’s Algorithm

53

slide-55
SLIDE 55

The Cycle Property

If C is a simple cycle in graph G, and e is an edge of maximal weight in C, then there is some MST of G that does not contain e Proof  Assume e is the edge (u,v) and T is a spanning tree

  • either e is not in T, and we are done
  • or e is in T
  • if we remove e, we obtain two spanning trees T1 and T2
  • because e is part of a cycle in G, there is another

edge e’ we can add to connect T1 and T2

  • let T’ be the resulting tree
  • since e had maximal weight, the total weight of T’

is ≤ the total weight of T

T1 T2

u v

e’ e

54

slide-56
SLIDE 56

The Cycle Property

If C is a simple cycle in graph G, and e is an edge of maximal weight in C, then there is some MST of G that does not contain e  If we construct a spanning tree by adding the edges of lowest weight that won’t create a simple cycle first, we will

  • btain a minimum spanning tree
  • This is the basic insight of Kruskal’s algorithm

55

slide-57
SLIDE 57

Kruskal’s Algorithm

 Add a preliminary step to the edge-centric algorithm: sort the edges in increasing weight order Given a graph G, construct a minimum spanning tree T for it

  • 0. Sort the edges of G by increasing weight
  • 1. Start T with the isolated vertices of G
  • 2. For each edge (u,v) in G
  • are u and v already connected in T?
  • yes: discard the edge
  • no: add it to T
  • Stop once T has v-1 edges

Joseph Kruskal 56

slide-58
SLIDE 58

Complexity of Kruskal’s Algorithm

Given a graph G, construct a minimum spanning tree T for it

  • 0. Sort the edges of G by increasing weight
  • 1. Start T with the isolated vertices of G
  • 2. For each edge (u,v) in G
  • are u and v already connected in T?
  • yes: discard the edge
  • no: add it to T
  • Stop once T has v-1 edges

 Kruskal’s algorithm has complexity O(ev)

  • That’s O(e log e + ev) above
  • but log e  O(v)
  • because e  O(v2), so log e  O(log v)
  • and log v  O(v)

O(1) e times O(v) O(1) O(e log e)

Using mergesort for example

57

slide-59
SLIDE 59

58

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Sorted edges G–H

(1)

C–E

(2)

C–I

(2)

D–E

(2)

C–D

(2)

D–I

(3)

F–H

(3)

B-E

(5)

A–I

(5)

F–J

(6)

A–C

(6)

B–C

(7)

H–J

(7)

A–H

(8)

F–I

(9)

C–H

(11)

A–B

(11) from smallest to largest weight

slide-60
SLIDE 60

59

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Sorted edges G–H

(1)

C–E

(2)

C–I

(2)

D–E

(2)

C–D

(2)

D–I

(3)

F–H

(3)

B-E

(5)

A–I

(5)

F–J

(6)

A–C

(6)

B–C

(7)

H–J

(7)

A–H

(8)

F–I

(9)

C–H

(11)

A–B

(11) Next edge we examine

slide-61
SLIDE 61

60

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Sorted edges G–H

(1)

 C–E

(2)

C–I

(2)

D–E

(2)

C–D

(2)

D–I

(3)

F–H

(3)

B-E

(5)

A–I

(5)

F–J

(6)

A–C

(6)

B–C

(7)

H–J

(7)

A–H

(8)

F–I

(9)

C–H

(11)

A–B

(11)

slide-62
SLIDE 62

61

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Sorted edges G–H

(1)

 C–E

(2)

 C–I

(2)

D–E

(2)

C–D

(2)

D–I

(3)

F–H

(3)

B-E

(5)

A–I

(5)

F–J

(6)

A–C

(6)

B–C

(7)

H–J

(7)

A–H

(8)

F–I

(9)

C–H

(11)

A–B

(11)

slide-63
SLIDE 63

62

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Sorted edges G–H

(1)

 C–E

(2)

 C–I

(2)

 D–E

(2)

C–D

(2)

D–I

(3)

F–H

(3)

B-E

(5)

A–I

(5)

F–J

(6)

A–C

(6)

B–C

(7)

H–J

(7)

A–H

(8)

F–I

(9)

C–H

(11)

A–B

(11)

slide-64
SLIDE 64

63

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Sorted edges G–H

(1)

 C–E

(2)

 C–I

(2)

 D–E

(2)

 C–D

(2)

D–I

(3)

F–H

(3)

B-E

(5)

A–I

(5)

F–J

(6)

A–C

(6)

B–C

(7)

H–J

(7)

A–H

(8)

F–I

(9)

C–H

(11)

A–B

(11)

slide-65
SLIDE 65

64

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Sorted edges G–H

(1)

 C–E

(2)

 C–I

(2)

 D–E

(2)

 C–D

(2)

 D–I

(3)

F–H

(3)

B-E

(5)

A–I

(5)

F–J

(6)

A–C

(6)

B–C

(7)

H–J

(7)

A–H

(8)

F–I

(9)

C–H

(11)

A–B

(11)

slide-66
SLIDE 66

65

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Sorted edges G–H

(1)

 C–E

(2)

 C–I

(2)

 D–E

(2)

 C–D

(2)

 D–I

(3)

 F–H

(3)

B-E

(5)

A–I

(5)

F–J

(6)

A–C

(6)

B–C

(7)

H–J

(7)

A–H

(8)

F–I

(9)

C–H

(11)

A–B

(11)

slide-67
SLIDE 67

66

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Sorted edges G–H

(1)

 C–E

(2)

 C–I

(2)

 D–E

(2)

 C–D

(2)

 D–I

(3)

 F–H

(3)

 B-E

(5)

A–I

(5)

F–J

(6)

A–C

(6)

B–C

(7)

H–J

(7)

A–H

(8)

F–I

(9)

C–H

(11)

A–B

(11)

slide-68
SLIDE 68

67

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Sorted edges G–H

(1)

 C–E

(2)

 C–I

(2)

 D–E

(2)

 C–D

(2)

 D–I

(3)

 F–H

(3)

 B-E

(5)

 A–I

(5)

F–J

(6)

A–C

(6)

B–C

(7)

H–J

(7)

A–H

(8)

F–I

(9)

C–H

(11)

A–B

(11)

slide-69
SLIDE 69

68

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Sorted edges G–H

(1)

 C–E

(2)

 C–I

(2)

 D–E

(2)

 C–D

(2)

 D–I

(3)

 F–H

(3)

 B-E

(5)

 A–I

(5)

 F–J

(6)

A–C

(6)

B–C

(7)

H–J

(7)

A–H

(8)

F–I

(9)

C–H

(11)

A–B

(11)

slide-70
SLIDE 70

69

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Sorted edges G–H

(1)

 C–E

(2)

 C–I

(2)

 D–E

(2)

 C–D

(2)

 D–I

(3)

 F–H

(3)

 B-E

(5)

 A–I

(5)

 F–J

(6)

 A–C

(6)

B–C

(7)

H–J

(7)

A–H

(8)

F–I

(9)

C–H

(11)

A–B

(11)

slide-71
SLIDE 71

70

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Sorted edges G–H

(1)

 C–E

(2)

 C–I

(2)

 D–E

(2)

 C–D

(2)

 D–I

(3)

 F–H

(3)

 B-E

(5)

 A–I

(5)

 F–J

(6)

 A–C

(6)

 B–C

(7)

H–J

(7)

A–H

(8)

F–I

(9)

C–H

(11)

A–B

(11)

slide-72
SLIDE 72

71

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Sorted edges G–H

(1)

 C–E

(2)

 C–I

(2)

 D–E

(2)

 C–D

(2)

 D–I

(3)

 F–H

(3)

 B-E

(5)

 A–I

(5)

 F–J

(6)

 A–C

(6)

 B–C

(7)

 H–J

(7)

A–H

(8)

F–I

(9)

C–H

(11)

A–B

(11)

slide-73
SLIDE 73

72

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Sorted edges G–H

(1)

 C–E

(2)

 C–I

(2)

 D–E

(2)

 C–D

(2)

 D–I

(3)

 F–H

(3)

 B-E

(5)

 A–I

(5)

 F–J

(6)

 A–C

(6)

 B–C

(7)

 H–J

(7)

 A–H

(8)

F–I

(9)

C–H

(11)

A–B

(11)

slide-74
SLIDE 74

73

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Sorted edges G–H

(1)

 C–E

(2)

 C–I

(2)

 D–E

(2)

 C–D

(2)

 D–I

(3)

 F–H

(3)

 B-E

(5)

 A–I

(5)

 F–J

(6)

 A–C

(6)

 B–C

(7)

 H–J

(7)

 A–H

(8)

 F–I

(9)

C–H

(11)

A–B

(11) At this point we are done: we have vertices 10 and 9 edges We do not examine the remaining edges

slide-75
SLIDE 75

Prim’s Algorithm

74

slide-76
SLIDE 76

Prim’s Algorithm

 In the vertex-centric algorithm, use a priority queue with lower-weight edges having higher priority

Given a graph G, construct a spanning tree T for it

  • 1. Pick an arbitrary vertex start in G and put it in T
  • mark start
  • add all edges (start,w) in G to a priority queue
  • 2. Repeat until the priority queue is empty
  • pick an edge (u,v) from the priority queue
  • if v is marked, discard it
  • add (u,v) to T
  • mark v
  • add to the priority queue all edges (v,w) in G such that w is unmarked
  • stop once T has v-1 edges
  • 3. If T has fewer than v-1 edges
  • add an arbitrary unmarked vertex and continue with (1)

Robert Prim 75

slide-77
SLIDE 77

76

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Priority Queue

Lower-weight edges having higher priority Start vertex

slide-78
SLIDE 78

77

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Priority Queue A–I

(5)

A–C

(6)

A–H

(8)

A–B

(11)

Adding the edges going out of A

Lower-weight edges having higher priority

slide-79
SLIDE 79

78

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Priority Queue C–I

(2)

D–I

(3)

A–C

(6)

A–H

(8)

I–F

(9)

A–B

(11) Lower-weight edges having higher priority

Adding the edges going out of I

We skip the edges to marked vertices

slide-80
SLIDE 80

79

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Priority Queue C–D

(2)

C–E

(2)

D–I

(3)

A–C

(6)

B–C

(7)

A–H

(8)

I–F

(9)

A–B

(11)

C–H

(11) Lower-weight edges having higher priority

Adding the edges going out of C

slide-81
SLIDE 81

80

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Priority Queue C–E

(2)

D–E

(2)

D–I

(3)

A–C

(6)

B–C

(7)

A–H

(8)

I–F

(9)

A–B

(11)

C–H

(11) Lower-weight edges having higher priority

Adding the edges going out of D

slide-82
SLIDE 82

81

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Priority Queue D–E

(2)

D–I

(3)

B–E

(5)

A–C

(6)

B–C

(7)

A–H

(8)

I–F

(9)

A–B

(11)

C–H

(11) Lower-weight edges having higher priority

Adding the edges going out of E

The next 2 edges have marked endpoints; we skip them

slide-83
SLIDE 83

82

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Priority Queue B–E

(5)

A–C

(6)

B–C

(7)

A–H

(8)

I–F

(9)

A–B

(11)

C–H

(11) Lower-weight edges having higher priority

slide-84
SLIDE 84

83

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Priority Queue A–C

(6)

B–C

(7)

A–H

(8)

I–F

(9)

A–B

(11)

C–H

(11) Lower-weight edges having higher priority

Adding the edges going out of B

The next 2 edges have marked endpoints; we skip them

slide-85
SLIDE 85

84

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Priority Queue H–G

(1)

F–H

(3)

H–J

(7)

I–F

(9)

A–B

(11)

C–H

(11) Lower-weight edges having higher priority

Adding the edges going out of H

slide-86
SLIDE 86

85

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Priority Queue F–H

(3)

H–J

(7)

I–F

(9)

A–B

(11)

C–H

(11) Lower-weight edges having higher priority

Adding the edges going out of G

slide-87
SLIDE 87

86

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Priority Queue F–J

(5)

H–J

(7)

I–F

(9)

A–B

(11)

C–H

(11) Lower-weight edges having higher priority

Adding the edges going out of F

slide-88
SLIDE 88

87

Juarez Fort Worth Columbus Erie Boston Indianapolis Detroit Atlanta Houston Galveston

6 7 3 9 11 1 8 5 6 11 2 2 2 3 7 5 2

Priority Queue H–J

(7)

I–F

(9)

A–B

(11)

C–H

(11) Lower-weight edges having higher priority

At this point we are done: there are 10 vertices and 9 edges

slide-89
SLIDE 89

Complexity

 At most, Prim’s algorithm puts every edge of G in the priority queue

  • once from each endpoint

that’s 2e steps  At each step, the most expensive operation is adding/removing edges to/from the priority queue

  • O(log e)

 The complexity of Prim’s algorithm is O(e log e)

  • 1. Pick an arbitrary vertex start in G and put it in T
  • mark start
  • add all edges (start,w) in G to a priority queue
  • 2. Repeat until the priority queue is empty
  • pick an edge (u,v) from the priority queue
  • if v is marked, discard it
  • add (u,v) to T
  • mark v
  • add to the priority queue all edges (v,w) in G

such that w is unmarked

  • stop once T has v-1 edges
  • 3. If T has fewer than v-1 edges
  • add an arbitrary unmarked vertex and

continue with (1)

88

slide-90
SLIDE 90

Summary

 Spanning trees

  • Edge-centric algorithm:

O(ev)

  • Vertex-centric algorithm: O(e)

 Minimum spanning trees

  • Kruskal’s algorithm:

O(ev)

  • Prim’s algorithm:

O(e log e)

Clear winner Clear winner But can we improve Kruskal’s algorithm?

89