Minimum Spanning Tree Todays announcements: PA3 due 5 April 11:59p - - PowerPoint PPT Presentation

minimum spanning tree
SMART_READER_LITE
LIVE PREVIEW

Minimum Spanning Tree Todays announcements: PA3 due 5 April 11:59p - - PowerPoint PPT Presentation

Minimum Spanning Tree Todays announcements: PA3 due 5 April 11:59p Final Exam, 12 April 7:00p, SRC A & B 5 Todays Plan: 6 Prims algorithm 2 Dijkstras shortest path algorithm 7 2 Partition Property A cut is a


slide-1
SLIDE 1

Minimum Spanning Tree

Today’s announcements:

◮ PA3 due 5 April 11:59p ◮ Final Exam, 12 April 7:00p, SRC A & B

Today’s Plan:

◮ Prim’s algorithm ◮ Dijkstra’s shortest path algorithm

Partition Property A cut is a set of edges whose removal disconnects G. Any edge that is a smallest weight edge in a cut is in some minimum spanning tree.

1 / 8

2 7 2 5 6

slide-2
SLIDE 2

Prim’s Minimum Spanning Tree Algorithm

(i.e., Breadth-first search with a priority queue) Suppose I have an MST for a connected subgraph of G. What edge (and vertex) should I add next?

a b c d e f g h 5 15 5 2 16 17 12 13 16 10 11 2 8 9 4 12

2 / 8

slide-3
SLIDE 3

Prim’s Minimum Spanning Tree Algorithm

(i.e., Breadth-first search with a priority queue)

a b c d e f 4 8 5 2 7 3 6 9 6 8

v’s priority is edge weight to closest tree vertex from v

PQ

  • Adj. Matrix
  • Adj. List

array Θ(n2) Θ(n2) heap Θ(n2 + m log n) Θ(m log n)

Prim(G) for each vertex v unmark v; d[v]=infinity PQ.insert(v,d[v]) while PQ is not empty v = PQ.removeMin() mark v // add v to MST for each edge (v,w) if (w is unmarked && weight(v,w) < d[w]) d[w]=weight(v,w) parent[w] = v decreasePrior(w,d[w]) Which is best?

3 / 8

slide-4
SLIDE 4

Dijkstra’s Single-Source Shortest Path

(another breadth-first search with a priority queue) Assume edge weights are non-negative.

A E C D B 1 2

  • 5

3 10

Dijkstra’s algorithm is a greedy algorithm (it makes the current best choice without considering future consequences). Intuition: Find shortest paths from source in order of length.

◮ Start at the source vertex (shortest path length = 0) ◮ The next shortest path extends some already discovered

shortest path by one edge. Why?

◮ Find it (by considering all one-edge extensions) and repeat.

4 / 8

slide-5
SLIDE 5

Dijkstra’s Algorithm Pseudocode

  • 1. Unmark all vertices
  • 2. Initialize the dist to each vertex to ∞
  • 3. Initialize the dist to the source to 0
  • 4. While there are unmarked vertices left in the graph

◮ Select the unmarked vertex v with the lowest dist ◮ Mark v with SPlength dist ◮ For each edge (v, w) ◮ dist(w) = min {dist(w), dist(v) + weight of (v, w)}

C E A D B H F G 2 7 4 10 8 1 1 9 2 2 4 1 2 3 1

vertex A B C D E F G H dist SPlength

5 / 8

slide-6
SLIDE 6

Correctness: The Cloud Proof

s y u cloud P Q

◮ Assume Dijkstra’s algorithm finds the correct shortest path to

the first k vertices it visits (the cloud).

◮ But it fails on the (k + 1)st vertex u. ◮ So there is some shorter path, P, from s to u. ◮ Path P must contain a first vertex y not in the cloud. ◮ But since the path, Q, to u is the shortest path out of the

cloud, the path on P upto y must be at least as long as Q.

◮ Thus the whole path P is at least as long as Q. Contradiction

(What did I use in that last step?)

6 / 8

slide-7
SLIDE 7

Data Structures for Dijkstra’s Algorithm

n times: Select the unmarked vertex with the lowest dist. removeMin m times: dist(w) = min{dist(w), dist(v) + weight of (v, w)} decreasePrior Runtime: (adjacency matrix or adjacency list?)

7 / 8

slide-8
SLIDE 8

Fibonacci Heaps

◮ Another implementation of a Priority Queue ◮ Amortized O(1) time for decreasePrior. ◮ O(log n) time for removeMin

Dijkstra’s uses n removeMins and m decreasePriors Runtime with Fibonacci heaps:

8 / 8