Introduction to Algorithms

Similar documents
Introduction to Algorithms

Introduction to Algorithms

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CMPS 2200 Fall Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk. 10/8/12 CMPS 2200 Intro.

Single Source Shortest Paths

Introduction to Algorithms

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

Single Source Shortest Paths

CS 4407 Algorithms Lecture: Shortest Path Algorithms

Design and Analysis of Algorithms

Introduction to Algorithms

Discrete Wiskunde II. Lecture 5: Shortest Paths & Spanning Trees

Analysis of Algorithms. Outline. Single Source Shortest Path. Andres Mendez-Vazquez. November 9, Notes. Notes

Shortest Path Algorithms

Data Structures and and Algorithm Xiaoqing Zheng

CS 253: Algorithms. Chapter 24. Shortest Paths. Credit: Dr. George Bebis

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

Algorithm Design and Analysis

Lecture 11. Single-Source Shortest Paths All-Pairs Shortest Paths

Algorithm Design and Analysis

BFS Dijkstra. Oct abhi shelat

COMP251: Bipartite graphs

Slides credited from Hsueh-I Lu & Hsu-Chun Hsiao

Breadth First Search, Dijkstra s Algorithm for Shortest Paths

Discrete Optimization 2010 Lecture 2 Matroids & Shortest Paths

Lecture Notes for Chapter 25: All-Pairs Shortest Paths

CSE101: Design and Analysis of Algorithms. Ragesh Jaiswal, CSE, UCSD

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Myriad of applications

COMP251: Bipartite graphs

Contents Lecture 4. Greedy graph algorithms Dijkstra s algorithm Prim s algorithm Kruskal s algorithm Union-find data structure with path compression

Discrete Optimization 2010 Lecture 3 Maximum Flows

Shortest Paths. CS 320, Fall Dr. Geri Georg, Instructor 320 ShortestPaths 3

Algorithms Booklet. 1 Graph Searching. 1.1 Breadth First Search

Algorithms: Lecture 12. Chalmers University of Technology

Query Processing in Spatial Network Databases

Design and Analysis of Algorithms

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

A faster algorithm for the single source shortest path problem with few distinct positive lengths

Lecture Notes Discrete Optimization

CSCE 750 Final Exam Answer Key Wednesday December 7, 2005

Scribes: Po-Hsuan Wei, William Kuzmaul Editor: Kevin Wu Date: October 18, 2016

Breadth-First Search of Graphs

Partha Sarathi Mandal

L feb abhi shelat

Discrete Mathematics, Spring 2004 Homework 9 Sample Solutions

Matching Residents to Hospitals

CS781 Lecture 3 January 27, 2011

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm

Ngày 20 tháng 7 năm Discrete Optimization Graphs

ICS 252 Introduction to Computer Design

16.1 Min-Cut as an LP

Dynamic Programming: Shortest Paths and DFA to Reg Exps

CS 580: Algorithm Design and Analysis

Dynamic Programming: Shortest Paths and DFA to Reg Exps

IS 709/809: Computational Methods in IS Research Fall Exam Review

Solutions to Exercises

Another way of saying this is that amortized analysis guarantees the average case performance of each operation in the worst case.

CS 410/584, Algorithm Design & Analysis, Lecture Notes 4

1. The graph of a function f is given above. Answer the question: a. Find the value(s) of x where f is not differentiable. Ans: x = 4, x = 3, x = 2,

CSE 591 Foundations of Algorithms Homework 4 Sample Solution Outlines. Problem 1

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

CS 6301 PROGRAMMING AND DATA STRUCTURE II Dept of CSE/IT UNIT V GRAPHS

Worksheets for GCSE Mathematics. Quadratics. mr-mathematics.com Maths Resources for Teachers. Algebra

Analysis of Algorithms I: All-Pairs Shortest Paths

The Matching Polytope: General graphs

Algorithms and Data Structures (COMP 251) Midterm Solutions

Examination paper for TDT4120 Algorithms and Data Structures

Part IA Algorithms Notes

arxiv: v1 [cs.cg] 29 Jun 2012

All-Pairs Shortest Paths

25. Minimum Spanning Trees

25. Minimum Spanning Trees

Discrete Mathematics

Question Paper Code :

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

Algorithm Design and Analysis

Observation 4.1 G has a proper separation of order 0 if and only if G is disconnected.

Observation 4.1 G has a proper separation of order 0 if and only if G is disconnected.

Connectivity of addable graph classes

Dynamic Programming. Data Structures and Algorithms Andrei Bulatov

Lecture 7: Shortest Paths in Graphs with Negative Arc Lengths. Reading: AM&O Chapter 5

from notes written mostly by Dr. Matt Stallmann: All Rights Reserved

CMPSCI 611: Advanced Algorithms

Greedy Alg: Huffman abhi shelat

Scheduling with AND/OR Precedence Constraints

arxiv: v1 [cs.ds] 20 Nov 2016

Impossibility of Distributed Consensus with One Faulty Process

arxiv: v1 [math.co] 12 Jul 2017

MA015: Graph Algorithms

Chapter 6. Properties of Regular Languages

6.889 Lecture 4: Single-Source Shortest Paths

University of Toronto Department of Electrical and Computer Engineering. Final Examination. ECE 345 Algorithms and Data Structures Fall 2016

Algorithm Design and Analysis

CMU Lecture 4: Informed Search. Teacher: Gianni A. Di Caro

Greedy Algorithms. CSE 101: Design and Analysis of Algorithms Lecture 10

Graph G = (V, E). V ={vertices}, E={edges}. V={a,b,c,d,e,f,g,h,k} E={(a,b),(a,g),( a,h),(a,k),(b,c),(b,k),...,(h,k)}

INVERSE SPANNING TREE PROBLEMS: FORMULATIONS AND ALGORITHMS

7.4 DO (uniqueness of minimum-cost spanning tree) Prove: if all edge weights are distinct then the minimum-cost spanning tree is unique.

ne a priority queue for the nodes of G; ile (! PQ.is empty( )) select u PQ with d(u) minimal and remove it; I Informatik 1 Kurt Mehlhorn

Transcription:

Introduction to Algorithms 6.046J/8.40J/SMA550 Lecture 7 Prof. Erik Demaine

Paths in graphs Consider a digraph G = (V, E) with edge-weight function w : E R. The weight of path p = v v L v k is defined to be Example: k i= w ( p) = w( v i, v i + ). v v v v 5 v v 4 4 5 w(p) = Introduction to Algorithms Day 9 L7.

Shortest paths A shortest path from u to v is a path of minimum weight from u to v. The shortestpath weight from u to v is defined as δ(u, v) = min{w(p) : p is a path from u to v}. Note: δ(u, v) = if no path from u to v exists. Introduction to Algorithms Day 9 L7.

Optimal substructure Theorem. A subpath of a shortest path is a shortest path. Proof. Cut and paste: Introduction to Algorithms Day 9 L7.4

Triangle inequality Theorem. For all u, v, x V, we have δ(u, v) δ(u, x) + δ(x, v). Proof. uu δ(u, v) vv δ(u, x) δ(x, v) xx Introduction to Algorithms Day 9 L7.5

Well-definedness of shortest paths If a graph G contains a negative-weight cycle, then some shortest paths may not exist. Example: < 0 uu vv Introduction to Algorithms Day 9 L7.6

Single-source shortest paths Problem. From a given source vertex s V, find the shortest-path weights δ(s, v) for all v V. If all edge weights w(u, v) are nonnegative, all shortest-path weights must exist. IDEA: Greedy.. Maintain a set S of vertices whose shortestpath distances from s are known.. At each step add to S the vertex v V S whose distance estimate from s is minimal.. Update the distance estimates of vertices adjacent to v. Introduction to Algorithms Day 9 L7.7

Dijkstra s algorithm d[s] 0 for each v V {s} do d[v] S Q V while Q do u EXTRACT-MIN(Q) S S {u} for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) Q is a priority queue maintaining V S relaxation step Implicit DECREASE-KEY Introduction to Algorithms Day 9 L7.8

Example of Dijkstra s algorithm Graph with nonnegative edge weights: AA 0 BB D 8 4 7 9 CC EE Introduction to Algorithms Day 9 L7.9

Example of Dijkstra s algorithm Initialize: 0 BB D 0 AA 8 4 7 9 Q: A B C D E 0 CC EE S: {} Introduction to Algorithms Day 9 L7.0

Example of Dijkstra s algorithm A EXTRACT-MIN(Q): 0 AA Q: A B C D E 0 0 BB CC D 8 4 7 9 EE S: { A } Introduction to Algorithms Day 9 L7.

Example of Dijkstra s algorithm Relax all edges leaving A: Q: 0 A B C D E 0 0 AA 0 0 BB CC S: { A } D 8 4 7 9 EE Introduction to Algorithms Day 9 L7.

Example of Dijkstra s algorithm C EXTRACT-MIN(Q): Q: 0 A B C D E 0 0 AA 0 0 BB CC D 8 4 7 9 S: { A, C } EE Introduction to Algorithms Day 9 L7.

Example of Dijkstra s algorithm Relax all edges leaving C: Q: 0 A B C D E 0 0 7 5 AA 0 7 BB CC D 8 4 7 9 S: { A, C } EE 5 Introduction to Algorithms Day 9 L7.4

Example of Dijkstra s algorithm E EXTRACT-MIN(Q): Q: 0 A B C D E 0 0 7 5 AA 0 7 BB CC D 8 4 7 9 S: { A, C, E } EE 5 Introduction to Algorithms Day 9 L7.5

Example of Dijkstra s algorithm Relax all edges leaving E: Q: 0 A B C D E 0 0 7 5 7 AA 0 7 BB CC D 8 4 7 9 S: { A, C, E } EE 5 Introduction to Algorithms Day 9 L7.6

Example of Dijkstra s algorithm B EXTRACT-MIN(Q): Q: 0 A B C D E 0 0 7 5 7 AA 0 7 BB CC D 8 4 7 9 S: { A, C, E, B } EE 5 Introduction to Algorithms Day 9 L7.7

Example of Dijkstra s algorithm Relax all edges leaving B: Q: 0 A B C D E 0 0 7 5 7 9 AA 0 7 BB CC 9 D 8 4 7 9 S: { A, C, E, B } EE 5 Introduction to Algorithms Day 9 L7.8

Example of Dijkstra s algorithm D EXTRACT-MIN(Q): Q: 0 A B C D E 0 0 7 5 7 9 AA 0 7 BB CC 9 D 8 4 7 9 EE 5 S: { A, C, E, B, D } Introduction to Algorithms Day 9 L7.9

Correctness Part I Lemma. Initializing d[s] 0 and d[v] for all v V {s} establishes d[v] δ(s, v) for all v V, and this invariant is maintained over any sequence of relaxation steps. Proof. Suppose not. Let v be the first vertex for which d[v] < δ(s, v), and let u be the vertex that caused d[v] to change: d[v] = d[u] + w(u, v). Then, d[v] < δ(s, v) supposition δ(s, u) + δ(u, v) triangle inequality δ(s,u) + w(u, v) sh. path specific path d[u] + w(u, v) v is first violation Contradiction. Introduction to Algorithms Day 9 L7.0

Correctness Part II Theorem. Dijkstra s algorithm terminates with d[v] = δ(s, v) for all v V. Proof. It suffices to show that d[v] = δ(s, v) for every v V when v is added to S. Suppose u is the first vertex added to S for which d[u] δ(s, u). Let y be the first vertex in V S along a shortest path from s to u, and let x be its predecessor: S, just before adding u. ss xx yy uu Introduction to Algorithms Day 9 L7.

Correctness Part II (continued) ss S xx yy uu Since u is the first vertex violating the claimed invariant, we have d[x] = δ(s, x). Since subpaths of shortest paths are shortest paths, it follows that d[y] was set to δ(s, x) + w(x, y) = δ(s, y) when (x, y) was relaxed just after x was added to S. Consequently, we have d[y] = δ(s, y) δ(s, u) d[u]. But, d[u] d[y] by our choice of u, and hence d[y] = δ(s, y) =δ(s, u) = d[u]. Contradiction. Introduction to Algorithms Day 9 L7.

Analysis of Dijkstra V times degree(u) times while Q do u EXTRACT-MIN(Q) S S {u} for each v Adj[u] do if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) Handshaking Lemma Θ(E) implicit DECREASE-KEY s. Time = Θ(V) T EXTRACT -MIN + Θ(E) T DECREASE-KEY Note: Same formula as in the analysis of Prim s minimum spanning tree algorithm. Introduction to Algorithms Day 9 L7.

Analysis of Dijkstra (continued) Time = Θ(V) T EXTRACT -MIN + Θ(E) T DECREASE-KEY Q T EXTRACT -MIN T DECREASE-KEY Total array O(V) O() O(V ) binary heap Fibonacci heap O(lg V) O(lg V) O(E lg V) O(lg V) amortized O() amortized O(E + V lg V) worst case Introduction to Algorithms Day 9 L7.4

Unweighted graphs Suppose w(u, v) = for all (u, v) E. Can the code for Dijkstra be improved? Use a simple FIFO queue instead of a priority queue. Breadth-first search while Q do u DEQUEUE(Q) for each v Adj[u] do if d[v] = then d[v] d[u] + ENQUEUE(Q, v) Analysis: Time = O(V + E). Introduction to Algorithms Day 9 L7.5

Example of breadth-first search aa bb cc dd ee f gg hh ii Q: Introduction to Algorithms Day 9 L7.6

Example of breadth-first search 0 aa bb cc dd ee f gg hh ii 0 Q: a Introduction to Algorithms Day 9 L7.7

Example of breadth-first search 0 aa bb cc dd ee Q: a b d f gg hh ii Introduction to Algorithms Day 9 L7.8

Example of breadth-first search 0 aa bb cc dd ee Q: a b d c e f gg hh ii Introduction to Algorithms Day 9 L7.9

Example of breadth-first search 0 aa bb cc dd ee Q: a b dc e f gg hh ii Introduction to Algorithms Day 9 L7.0

Example of breadth-first search 0 aa bb cc dd ee Q: a b dce f gg hh ii Introduction to Algorithms Day 9 L7.

Example of breadth-first search 0 aa bb cc dd ee Q: a b d c e g i f gg hh ii Introduction to Algorithms Day 9 L7.

Example of breadth-first search 0 aa bb cc dd ee 4 f Q: a b d c e g i f gg 4 hh ii Introduction to Algorithms Day 9 L7.

0 Example of breadth-first search aa bb cc dd ee f Q: a b d c e g i f h 4 4 gg 4 4 hh ii Introduction to Algorithms Day 9 L7.4

0 Example of breadth-first search aa bb cc dd ee f Q: a b d c e g i f h 4 4 gg 4 hh ii Introduction to Algorithms Day 9 L7.5

Example of breadth-first search 0 aa bb cc dd ee 4 4 f gg hh ii Q: a b d c e g i f h Introduction to Algorithms Day 9 L7.6

Example of breadth-first search 0 aa bb cc dd ee 4 4 f gg hh ii Q: a b d c e g i f h Introduction to Algorithms Day 9 L7.7

Correctness of BFS while Q do u DEQUEUE(Q) for each v Adj[u] do if d[v] = then d[v] d[u] + ENQUEUE(Q, v) Key idea: The FIFO Q in breadth-first search mimics the priority queue Q in Dijkstra. Invariant: v comes after u in Q implies that d[v] = d[u] or d[v] = d[u] +. Introduction to Algorithms Day 9 L7.8