CS 225 Data Structures April 23 Dijkstras Algorithm Wad ade Fag - - PowerPoint PPT Presentation

cs 225
SMART_READER_LITE
LIVE PREVIEW

CS 225 Data Structures April 23 Dijkstras Algorithm Wad ade Fag - - PowerPoint PPT Presentation

CS 225 Data Structures April 23 Dijkstras Algorithm Wad ade Fag agen-Ulm lmschneid ider Partition Property Consider an arbitrary partition of the vertices on G into two subsets U and V . V U A 4 Let e be an edge of 8 2 B D


slide-1
SLIDE 1

CS 225

Data Structures

April 23 – Dijkstra’s Algorithm

Wad ade Fag agen-Ulm lmschneid ider

slide-2
SLIDE 2

Partition Property

Consider an arbitrary partition of the vertices on G into two subsets U and V.

A C D E B F 8 4 2 7 1 2 3 9 5

U V

Let e be an edge of minimum weight across the partition. Then e is part of some minimum spanning tree.

e

slide-3
SLIDE 3

Partition Property

The partition property suggests an algorithm:

A C D E B F G H 16 5 5 2 15 16 10 11 8 9 12 4 17 13 9

slide-4
SLIDE 4

Prim’s Algorithm

A C D E B F 16 5 2 15 11 8 12 17 13 9

PrimMST(G, s): Input: G, Graph; s, vertex in G, starting vertex Output: T, a minimum spanning tree (MST) of G foreach (Vertex v : G): d[v] = +inf p[v] = NULL d[s] = 0 PriorityQueue Q // min distance, defined by d[v] Q.buildHeap(G.vertices()) Graph T // "labeled set" repeat n times: Vertex m = Q.removeMin() T.add(m) foreach (Vertex v : neighbors of m not in T): if cost(v, m) < d[v]: d[v] = cost(v, m) p[v] = m return T 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

slide-5
SLIDE 5

Prim’s Algorithm

PrimMST(G, s): foreach (Vertex v : G): d[v] = +inf p[v] = NULL d[s] = 0 PriorityQueue Q // min distance, defined by d[v] Q.buildHeap(G.vertices()) Graph T // "labeled set" repeat n times: Vertex m = Q.removeMin() T.add(m) foreach (Vertex v : neighbors of m not in T): if cost(v, m) < d[v]: d[v] = cost(v, m) p[v] = m 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

  • Adj. Matrix
  • Adj. List

Heap Unsorted Array

slide-6
SLIDE 6

Prim’s Algorithm

repeat n times: Vertex m = Q.removeMin() T.add(m) foreach (Vertex v : neighbors of m not in T): if cost(v, m) < d[v]: d[v] = cost(v, m) p[v] = m 16 17 18 19 20 21 22 repeat n times: Vertex m = Q.removeMin() T.add(m) foreach (Vertex v : neighbors of m not in T): if cost(v, m) < d[v]: d[v] = cost(v, m) p[v] = m 16 17 18 19 20 21 22

slide-7
SLIDE 7

Prim’s Algorithm

PrimMST(G, s): foreach (Vertex v : G): d[v] = +inf p[v] = NULL d[s] = 0 PriorityQueue Q // min distance, defined by d[v] Q.buildHeap(G.vertices()) Graph T // "labeled set" repeat n times: Vertex m = Q.removeMin() T.add(m) foreach (Vertex v : neighbors of m not in T): if cost(v, m) < d[v]: d[v] = cost(v, m) p[v] = m 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

  • Adj. Matrix
  • Adj. List

Heap

O(n2 + m lg(n)) O(n lg(n) + m lg(n))

Unsorted Array

O(n2) O(n2)

Sparse Graph: Dense Graph:

slide-8
SLIDE 8

MST Algorithm Runtime:

  • Kruskal’s Algorithm:

O(n + m lg(n))

  • What must be true about the connectivity of a graph

when running an MST algorithm?

  • How does n and m relate?
  • Prim’s Algorithm:

O(n lg(n) + m lg(n))

slide-9
SLIDE 9

MST Algorithm Runtime:

  • Kruskal’s Algorithm:

O(n + m lg(n))

  • Prim’s Algorithm:

O(n lg(n) + m lg(n))

slide-10
SLIDE 10

MST Algorithm Runtime:

  • Upper bound on MST Algorithm Runtime:

O(m lg(n))

slide-11
SLIDE 11

Suppose I I have a new heap:

PrimMST(G, s): foreach (Vertex v : G): d[v] = +inf p[v] = NULL d[s] = 0 PriorityQueue Q // min distance, defined by d[v] Q.buildHeap(G.vertices()) Graph T // "labeled set" repeat n times: Vertex m = Q.removeMin() T.add(m) foreach (Vertex v : neighbors of m not in T): if cost(v, m) < d[v]: d[v] = cost(v, m) p[v] = m 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

Binary Heap Fibonacci Heap Remove Min O( lg(n) ) O( lg(n) ) Decrease Key O( lg(n) ) O(1)*

What’s the updated running time?

slide-12
SLIDE 12

End of f Semester Logistics

Lab: Your final CS 225 lab is this week.

  • No lab sections next week (partial week).

Final Exam: Final exams start on Reading Day (May 3)

  • Last day of office hours is Wednesday, May 2.
  • No office/lab hours once the first final exam is given.

Grades: There will be a “Pre-Final” grade update posted next week with all grades except your final.

  • MP7’s grace period extends until Tuesday, May 1
  • Goal: Have “Pre-Final” grade on Wednesday/Thursday
slide-13
SLIDE 13

Shortest Path

slide-14
SLIDE 14
slide-15
SLIDE 15

Dijkstra’s Algorithm (SSSP)

A C D E B F G H 7 5 4 10 7 5 3 6 2 5 4 3

DijkstraSSSP(G, s): foreach (Vertex v : G): d[v] = +inf p[v] = NULL d[s] = 0 PriorityQueue Q // min distance, defined by d[v] Q.buildHeap(G.vertices()) Graph T // "labeled set" repeat n times: Vertex u = Q.removeMin() T.add(u) foreach (Vertex v : neighbors of u not in T): if _______________ < d[v]: d[v] = __________________ p[v] = m 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

slide-16
SLIDE 16

Dijkstra’s Algorithm (SSSP)

A C D E B F G H 7 5 4 10 7

  • 5

3

  • 6

2 5 4 3

What about negative weight cycles?

slide-17
SLIDE 17

Dijkstra’s Algorithm (SSSP)

A C D E B F G H 7 5 4 10 7 5 3 6

  • 2

2 3 3

What about negative weight edges, without negative weight cycles?

slide-18
SLIDE 18

Dijkstra’s Algorithm (SSSP)

DijkstraSSSP(G, s): foreach (Vertex v : G): d[v] = +inf p[v] = NULL d[s] = 0 PriorityQueue Q // min distance, defined by d[v] Q.buildHeap(G.vertices()) Graph T // "labeled set" repeat n times: Vertex u = Q.removeMin() T.add(u) foreach (Vertex v : neighbors of u not in T): if _______________ < d[v]: d[v] = __________________ p[v] = m 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

What is the running time?