cot 5407: introduction to algorithmsgiri/teach/5407/s19/lecs/lx9-graphs.pdffibonacci heaps o(1)*...

19
CAP 5510 / CGS 5166 COT 5407: Introduction to Algorithms Giri NARASIMHAN www.cs.fiu.edu/~giri/teach/5407S19.html 4/9/19 1

Upload: others

Post on 09-Oct-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COT 5407: Introduction to Algorithmsgiri/teach/5407/S19/Lecs/LX9-Graphs.pdfFibonacci Heaps O(1)* O(1)* O(log n)* O(m + n log n)* * Amortized Time Complexity. CAP 5510 / CGS 5166 SSSP

CAP 5510 / CGS 5166

COT 5407: Introduction to AlgorithmsGiri NARASIMHAN

www.cs.fiu.edu/~giri/teach/5407S19.html 4/9/19

!1

Page 2: COT 5407: Introduction to Algorithmsgiri/teach/5407/S19/Lecs/LX9-Graphs.pdfFibonacci Heaps O(1)* O(1)* O(log n)* O(m + n log n)* * Amortized Time Complexity. CAP 5510 / CGS 5166 SSSP

CAP 5510 / CGS 5166

Correctness of Dijkstra’s Alg

4/9/19

!2

Page 3: COT 5407: Introduction to Algorithmsgiri/teach/5407/S19/Lecs/LX9-Graphs.pdfFibonacci Heaps O(1)* O(1)* O(log n)* O(m + n log n)* * Amortized Time Complexity. CAP 5510 / CGS 5166 SSSP

CAP 5510 / CGS 5166

Analysis of Dijkstra’s Algorithm

! O(n) calls to INSERT, EXTRACT-MIN ! O(m) calls to DECREASE-KEY

4/9/19

!3

Approach Insert Dec-Key Extract-Min Total

PQ in Arrays O(1) O(1) O(n) O(n2)

Heaps O(log n) O(log n) O(log n) O((m+n)log n)

Fibonacci Heaps O(1)* O(1)* O(log n)* O(m + n log n)*

* Amortized Time Complexity

Page 4: COT 5407: Introduction to Algorithmsgiri/teach/5407/S19/Lecs/LX9-Graphs.pdfFibonacci Heaps O(1)* O(1)* O(log n)* O(m + n log n)* * Amortized Time Complexity. CAP 5510 / CGS 5166 SSSP

CAP 5510 / CGS 5166

SSSP Algorithms

! Dijkstra’s algorithm (only non-negative edges allowed) ! Best: O(m + n log n)

! Bellman-Ford algorithm (allows non-negative edges, but less efficient) ! Repeated RELAX steps until we have answers ! O(mn) time complexity

4/9/19

!4

Page 5: COT 5407: Introduction to Algorithmsgiri/teach/5407/S19/Lecs/LX9-Graphs.pdfFibonacci Heaps O(1)* O(1)* O(log n)* O(m + n log n)* * Amortized Time Complexity. CAP 5510 / CGS 5166 SSSP

COT 5407 2/23/17

!5 All Pairs Shortest Path Algorithm

Need to find shortest paths (or length) between every pair of vertices ! Invoke Dijkstra’s SSSP algorithm n times. ! Or an alternative approach …

Page 6: COT 5407: Introduction to Algorithmsgiri/teach/5407/S19/Lecs/LX9-Graphs.pdfFibonacci Heaps O(1)* O(1)* O(log n)* O(m + n log n)* * Amortized Time Complexity. CAP 5510 / CGS 5166 SSSP

CAP 5510 / CGS 5166

Structure of a Shortest Path

! Optimal substructure property ! Every subpath of a shortest path is “optimal”, i.e.,

it is a shortest path between the relevant vertices. ! Can we use DP?

! Invent appropriate subproblems to cast it as a DP

4/9/19

!6

Page 7: COT 5407: Introduction to Algorithmsgiri/teach/5407/S19/Lecs/LX9-Graphs.pdfFibonacci Heaps O(1)* O(1)* O(log n)* O(m + n log n)* * Amortized Time Complexity. CAP 5510 / CGS 5166 SSSP

CAP 5510 / CGS 5166

Idea!

! In iteration k, find SPk(u,v), shortest paths between u and v that use at most k edges

! Iteration 1: find all shortest paths that use at most 1 edge ! Every edge (u,v) is a SP between u and v ! Every non-edge (u,v) means no SP exists between u

and v using at most one edge

4/9/19

!7

Page 8: COT 5407: Introduction to Algorithmsgiri/teach/5407/S19/Lecs/LX9-Graphs.pdfFibonacci Heaps O(1)* O(1)* O(log n)* O(m + n log n)* * Amortized Time Complexity. CAP 5510 / CGS 5166 SSSP

CAP 5510 / CGS 5166

Iteration k

! We already have SPk-1(u,v) from iteration k-1 ! If path of length k between u & v exists, then path

of length k-1 exists between u and a neighbor of v ! SPk(u,v) can be computed as follows:

! SPk(u,v) = min ( SPk-1(u,v), minw {SPk-1(u,w) + SP1(w,v)}) ! This is our recurrence relation

4/9/19

!8

Page 9: COT 5407: Introduction to Algorithmsgiri/teach/5407/S19/Lecs/LX9-Graphs.pdfFibonacci Heaps O(1)* O(1)* O(log n)* O(m + n log n)* * Amortized Time Complexity. CAP 5510 / CGS 5166 SSSP

CAP 5510 / CGS 5166

Toward APSP

4/9/19

!9

SPk(i,j) = lij(i,j)

Extend SP to use one extra

edge.

Page 10: COT 5407: Introduction to Algorithmsgiri/teach/5407/S19/Lecs/LX9-Graphs.pdfFibonacci Heaps O(1)* O(1)* O(log n)* O(m + n log n)* * Amortized Time Complexity. CAP 5510 / CGS 5166 SSSP

CAP 5510 / CGS 5166

APSP Algorithm

4/9/19

!10

O(n4) time complexity

Page 11: COT 5407: Introduction to Algorithmsgiri/teach/5407/S19/Lecs/LX9-Graphs.pdfFibonacci Heaps O(1)* O(1)* O(log n)* O(m + n log n)* * Amortized Time Complexity. CAP 5510 / CGS 5166 SSSP

CAP 5510 / CGS 5166

APSP Algorithm – Matrix Mult

4/9/19

!11

Page 12: COT 5407: Introduction to Algorithmsgiri/teach/5407/S19/Lecs/LX9-Graphs.pdfFibonacci Heaps O(1)* O(1)* O(log n)* O(m + n log n)* * Amortized Time Complexity. CAP 5510 / CGS 5166 SSSP

CAP 5510 / CGS 5166

Intermediate Matrices …

4/9/19

!12

Page 13: COT 5407: Introduction to Algorithmsgiri/teach/5407/S19/Lecs/LX9-Graphs.pdfFibonacci Heaps O(1)* O(1)* O(log n)* O(m + n log n)* * Amortized Time Complexity. CAP 5510 / CGS 5166 SSSP

CAP 5510 / CGS 5166

Improved Idea

4/9/19

!13

O(n3log n) time complexity

Page 14: COT 5407: Introduction to Algorithmsgiri/teach/5407/S19/Lecs/LX9-Graphs.pdfFibonacci Heaps O(1)* O(1)* O(log n)* O(m + n log n)* * Amortized Time Complexity. CAP 5510 / CGS 5166 SSSP

CAP 5510 / CGS 5166

Repeating Squaring Idea

4/9/19

!14

Page 15: COT 5407: Introduction to Algorithmsgiri/teach/5407/S19/Lecs/LX9-Graphs.pdfFibonacci Heaps O(1)* O(1)* O(log n)* O(m + n log n)* * Amortized Time Complexity. CAP 5510 / CGS 5166 SSSP

CAP 5510 / CGS 5166

Floyd-Warshall’s Algorithm

! SPk(u,v), shortest paths between u and v that use at most k edges

! Old definition

! SPk(u,v), shortest paths between u and v that uses intermediate vertices from {1,2,…,k}

! New definition

4/9/19

!15

Page 16: COT 5407: Introduction to Algorithmsgiri/teach/5407/S19/Lecs/LX9-Graphs.pdfFibonacci Heaps O(1)* O(1)* O(log n)* O(m + n log n)* * Amortized Time Complexity. CAP 5510 / CGS 5166 SSSP

CAP 5510 / CGS 5166

Recurrence Relation

! Old Relation ! SPk(u,v) = min ( SPk-1(u,v), minw {SPk-1(u,w) +

SP1(w,v)})

! New Relation ! SPk(u,v) = min ( SPk-1(u,v), SPk-1(u,k) + SPk-1(k,v)})

4/9/19

!16

Page 17: COT 5407: Introduction to Algorithmsgiri/teach/5407/S19/Lecs/LX9-Graphs.pdfFibonacci Heaps O(1)* O(1)* O(log n)* O(m + n log n)* * Amortized Time Complexity. CAP 5510 / CGS 5166 SSSP

CAP 5510 / CGS 5166

Floyd-Warshall: Improved APSP

4/9/19

!17

O(n3) time complexity

Page 18: COT 5407: Introduction to Algorithmsgiri/teach/5407/S19/Lecs/LX9-Graphs.pdfFibonacci Heaps O(1)* O(1)* O(log n)* O(m + n log n)* * Amortized Time Complexity. CAP 5510 / CGS 5166 SSSP

COT 5407 2/23/17

!18

Page 19: COT 5407: Introduction to Algorithmsgiri/teach/5407/S19/Lecs/LX9-Graphs.pdfFibonacci Heaps O(1)* O(1)* O(log n)* O(m + n log n)* * Amortized Time Complexity. CAP 5510 / CGS 5166 SSSP

COT 5407 2/23/17

!19

Figure 14.38 Worst-case running times of various graph algorithms

Data Structures & Problem Solving using JAVA/2E Mark Allen Weiss © 2002 Addison Wesley