Introduction to Algorithms

Similar documents
SIDDHARTH INSTITUTE OF ENGINEERING & TECHNOLOGY :: PUTTUR (AUTONOMOUS) Siddharth Nagar, Narayanavanam Road QUESTION BANK (DESCRIPTIVE)

Uninformed search methods II.

Uninformed search methods II.

EE582 Physical Design Automation of VLSI Circuits and Systems

Topics Ch 3 Test A. A) 14 minutes B) 15 minutes C) 16 minutes minutes

CSE 3401: Intro to AI & LP Uninformed Search II

Uninformed search methods

Uninformed Search (Ch )

Uninformed Search (Ch )

Algorithms and Data Structures

Area, Volume, and Center of Mass

Uninformed search methods

Real World Search Problems. CS 331: Artificial Intelligence Uninformed Search. Simpler Search Problems. Example: Oregon. Search Problem Formulation

Pre-Kindergarten 2017 Summer Packet. Robert F Woodall Elementary

Maximum CPU utilization is obtained by multiprogramming. CPU I/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait

DESIGN AND ANALYSIS OF ALGORITHMS (DAA 2017)

Imperfectly Shared Randomness in Communication

Princess Nora University Faculty of Computer & Information Systems ARTIFICIAL INTELLIGENCE (CS 370D) Computer Science Department

Better Search Improved Uninformed Search CIS 32

Lecture 10. Support Vector Machines (cont.)

Uninformed Search Strategies

Representation. Representation. Representation. Representation. 8 puzzle.

Solving Problems by Searching chap3 1. Problem-Solving Agents

Abstract In this paper, the author deals with the properties of circumscribed ellipses of convex quadrilaterals, using tools of parallel projective tr

NATIONAL FEDERATION RULES B. National Federation Rules Apply with the following TOP GUN EXCEPTIONS

Algebra I: Strand 3. Quadratic and Nonlinear Functions; Topic 1. Pythagorean Theorem; Task 3.1.2

CS 4649/7649 Robot Intelligence: Planning

Midterm Exam 1, section 2. Thursday, September hour, 15 minutes

Unit 4. Triangle Relationships. Oct 3 8:20 AM. Oct 3 8:21 AM. Oct 3 8:26 AM. Oct 3 8:28 AM. Oct 3 8:27 AM. Oct 3 8:27 AM

Problem Solving Agents

Chapter. Similar Triangles. Copyright Cengage Learning. All rights reserved.

CS 4649/7649 Robot Intelligence: Planning

Addition and Subtraction of Rational Expressions

CENG 466 Artificial Intelligence. Lecture 4 Solving Problems by Searching (II)

Conservation of Energy. Chapter 7 of Essential University Physics, Richard Wolfson, 3 rd Edition

CK-12 Geometry: Special Right Triangles

Minimum Mean-Square Error (MMSE) and Linear MMSE (LMMSE) Estimation

Problem Solving as Search - I

Functions of Random Variables & Expectation, Mean and Variance

Name: Grade: LESSON ONE: Home Row

Write these equations in your notes if they re not already there. You will want them for Exam 1 & the Final.

Operational Risk Management: Preventive vs. Corrective Control

ANNEX 18. RESOLUTION MSC.362(92) (Adopted on 14 June 2013) REVISED RECOMMENDATION ON A STANDARD METHOD FOR EVALUATING CROSS-FLOODING ARRANGEMENTS

Uninformed search strategies

Inverting a Batting Average - an Application of Continued Fractions (Preliminary Version)

Mapping Diving Locations on Bali Island Based Mobile

First-Server Advantage in Tennis Matches

Coaches, Parents, Players and Fans

Paul Burkhardt. May 19, 2016

OPTIMAL FLOWSHOP SCHEDULING WITH DUE DATES AND PENALTY COSTS

INTRODUCTION Microfilm copy of the Draper Collection of manuscripts. Originals located at the State Historical Society of Wisconsin.

Knots and their projections I

8-1. The Pythagorean Theorem and Its Converse. Vocabulary. Review. Vocabulary Builder. Use Your Vocabulary

CSE 3402: Intro to Artificial Intelligence Uninformed Search II

SEARCH SEARCH TREE. Node: State in state tree. Root node: Top of state tree

Polynomial DC decompositions

5.5 Use Inequalities in a Triangle

CS Lecture 5. Vidroha debroy. Material adapted courtesy of Prof. Xiangnan Kong and Prof. Carolina Ruiz at Worcester Polytechnic Institute

Simplifying Radical Expressions and the Distance Formula

2.6 Related Rates Worksheet Calculus AB. dy /dt!when!x=8

SEARCH TREE. Generating the children of a node

11.4 Apply the Pythagorean

Urban OR: Quiz 2 Solutions (2003) ( 1 ρ 1 )( 1 ρ 1 ρ 2 ) ( 1 12 )( ) σ S ] 24 [ 2 = 60, 2 2 ] ( 2 ) 3

CT PET-2018 Part - B Phd COMPUTER APPLICATION Sample Question Paper

Growth alternative for Hecke-Kiselman monoids

arxiv: v1 [math.co] 16 Sep 2016

9.3 Altitude-on-Hypotenuse Theorems

Nationaal Lucht- en Ruimtevaartlaboratorium National Aerospace Laboratory NLR

Tie Breaking Procedure

Geometry Chapter 5 Review

Decision Trees. Nicholas Ruozzi University of Texas at Dallas. Based on the slides of Vibhav Gogate and David Sontag

(a) (First lets try to design the set of toy s the easy way.) The easiest thing to do would be to pick integer lengths for the lengths of the sticks.

Name: Period: Unit 5 Test Review. Multiple Choice Identify the choice that best completes the statement or answers the question.

TSP at isolated intersections: Some advances under simulation environment

My ABC Insect Discovery Book

Vectors in the City Learning Task

8-1. The Pythagorean Theorem and Its Converse. Vocabulary. Review. Vocabulary Builder. Use Your Vocabulary

Course 495: Advanced Statistical Machine Learning/Pattern Recognition

Artificial Intelligence. Uninformed Search Strategies

Section 8: Right Triangles

GaitAnalysisofEightLegedRobot

arxiv: v1 [math.co] 11 Apr 2018

Intro to Graph Theory

The purpose of this experiment is to find this acceleration for a puck moving on an inclined air table.

Full Name: Period: Heredity EOC Review

Lecture 5. Optimisation. Regularisation

COMP Intro to Logic for Computer Scientists. Lecture 13

1ACE Exercise 4. Name Date Class

Internet Algorithms. Lecture 10 06/24/11

Edexcel GCSE. Mathematics A 1387 Paper 5523/03. November Mark Scheme. Mathematics A 1387

Lesson 3: Using the Pythagorean Theorem. The Pythagorean Theorem only applies to triangles. The Pythagorean Theorem + = Example 1

x 2 = (60 m) 2 + (60 m) 2 x 2 = 3600 m m 2 x = m

UAB MATH-BY-MAIL CONTEST, 2004

ISyE 3103 Introduction to Supply Chain Modeling: Logistics Summer 2012 Homework 8 Issued: July 11, 2012 Due: July 18, 2012

Grade K-1 WRITING Traffic Safety Cross-Curriculum Activity Workbook

Holly Burns. Publisher Mary D. Smith, M.S. Ed. Author

Combining Experimental and Non-Experimental Design in Causal Inference

World Eight Ball Pool Federation Rules Unabridged Version Issued January 2009 An abridged version of the latest rules may be downloaded here (pdf)

Physical Design of CMOS Integrated Circuits

SQUARE ROOTS. Pythagoras theorem has been a perennially interesting. Drawing A SPIRAL OF. ClassRoom

Transcription:

Introduction to Algorithms 6.46J/.4J LECTURE 7 Shortest Paths I Properties o shortest paths Dijkstra s Correctness Analysis Breadth-irst Paths in graphs Consider a digraph G = (V, E) with edge-weight unction w : EoR. The weight o path p = v o v o ov k is deined to be k w( v i, v i i w ( p) ). Pro. Erik Demaine November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7. November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7. Paths in graphs Consider a digraph G = (V, E) with edge-weight unction w : EoR. The weight o path p = v o v o ov k is deined to be Example: k w( v i, v i i w ( p) ). Shortest paths A shortest path rom u to v is a path o minimum weight rom u to v. The shortestpath weight rom u to v is deined as G(u, v) = min{w(p) : p is a path rom u to v}. Note:G(u, v) = i no path rom u to v exists. v 4 v 5 v v 4 v 5 v 4 v 5 w(p) = November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7. November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.4

Optimal substructure Theorem. A subpath o a shortest path is a shortest path. Optimal substructure Theorem. A subpath o a shortest path is a shortest path. Proo. Cut and paste: November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.5 November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.6 Optimal substructure Theorem. A subpath o a shortest path is a shortest path. Proo. Cut and paste: Triangle inequality Theorem. For all u, v, x V, we have G(u, v) dg(u, x) + G(x, v). November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.7 November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.

Triangle inequality Theorem. For all u, v, x V, we have G(u, v) dg(u, x) + G(x, v). Well-deinedness o shortest paths I a graph G contains a negative-weight cycle, then some shortest paths may not exist. Proo. uu G(u, v) G(u, x) G(x, v) xx vv November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.9 November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7. Well-deinedness o shortest paths I a graph G contains a negative-weight cycle, then some shortest paths may not exist. Example: uu < November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7. vv Single-source shortest paths Problem. From a given source vertex s V, ind the shortest-path weights G(s, v) or all v V. I all edge weights w(u, v) are nonnegative, all shortest-path weights must exist. IDEA: Grdy.. Maintain a set S o vertices whose shortestpath distances rom s are known.. At each step a to S the vertex v V S whose distance estimate rom s is minimal.. Update the distance estimates o vertices adjacent to v. November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.

Dijkstra s d[s] m or each v V {s} do d[v] m Sm QmV Q is a priority queue maintaining V S Dijkstra s d[s] m or each v V {s} do d[v] m Sm QmV Q is a priority queue maintaining V S do u m EXTRACT-MIN(Q) SmS {u} or each v Adj[u] [MXLZJVSQ5 do i d[v] > d[u] + w(u, v) then d[v] m d[u] + w(u, v) MJH?YA!MRJMRMX]FVIEO )EPPVIQEMRMRK ZIVXMGIWEVIRSX EGGIWWMFPIJVSQ WSYVGI November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7. November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.4 Dijkstra s d[s] m or each v V {s} do d[v] m Sm QmV Q is a priority queue maintaining V S do u m EXTRACT-MIN(Q) SmS {u} or each v Adj[u] [MXLZJVSQ5 do i d[v] > d[u] + w(u, v) then d[v] m d[u] + w(u, v) TVIZ?ZA!Y Implicit DECREASE-KEY MJH?YA!MRJMRMX]FVIEO )EPPVIQEMRMRK ZIVXMGIWEVIRSX EGGIWWMFPIJVSQ WSYVGI relaxation step November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.5 Example o Dijkstra s Graph with nonnegative edge weights: November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.6 BB D 4 7 9 EE

Initialize: Example o Dijkstra s BB D 4 7 9 Q: A B C D E EE S: {} Example o Dijkstra s A mextract-min(q): BB D 4 7 9 Q: A B C D E EE S: { A } November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.7 November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7. Example o Dijkstra s Relax all edges leaving A: Q: A B C D E S: { A } 4 7 9 EE Example o Dijkstra s C mextract-min(q): Q: A B C D E 4 7 9 S: { A, C } EE November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.9 November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.

Q: Example o Dijkstra s Relax all edges leaving C: A B C D E 7 5 7 4 7 9 S: { A, C } EE 5 Q: Example o Dijkstra s E mextract-min(q): A B C D E 7 5 7 4 7 9 EE 5 S: { A, C, E } November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7. November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7. Q: Example o Dijkstra s Relax all edges leaving E: A B C D E 7 5 7 7 4 7 9 EE 5 S: { A, C, E } Example o Dijkstra s B mextract-min(q): Q: A B C D E 7 5 7 7 4 7 9 EE 5 S: { A, C, E, B } November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7. November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.4

Q: Example o Dijkstra s Relax all edges leaving B: A B C D E 7 5 7 9 7 9 November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.5 4 7 9 EE 5 S: { A, C, E, B } Q: Example o Dijkstra s D mextract-min(q): A B C D E 7 5 7 9 7 9 November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.6 4 7 9 EE 5 S: { A, C, E, B, D } Correctness Part I Lemma. Initializing d[s] m and d[v] m or all v V {s} establishes d[v] tg(s, v) or all v V, and this invariant is maintained over any sequence o relaxation steps. November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.7 Correctness Part I Lemma. Initializing d[s] m and d[v] m or all v V {s} establishes d[v] tg(s, v) or all v V, and this invariant is maintained over any sequence o relaxation steps. Proo. Suppose not. Let v be the irst vertex or which d[v] < G(s, v), and let u be the vertex that caused d[v] to change: d[v] = d[u] + w(u, v). Then, d[v] < G(s, v) supposition dg(s, u) + G(u, v) triangle inequality dg(s,u) + w(u, v) sh. path d specc path d d[u] + w(u, v) v is irst violation Contradiction. November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.

Correctness Part II Lemma. Let u be v s predecessor on a shortest path rom s to v. Then, i d[u] = G(s, u) and edge (u, v) is relaxed, we have d[v] G(s, v) ater the relaxation. Correctness Part II Lemma. Let u be v s predecessor on a shortest path rom s to v. Then, i d[u] = G(s, u) and edge (u, v) is relaxed, we have d[v] G(s, v) ater the relaxation. Proo. Observe that G(s, v) = G(s, u)+ w(u, v). Suppose that d[v] > G(s, v) beore the relaxation. (Otherwise, we re done.) Then, the test d[v] > d[u] + w(u, v) suds, because d[v] > G(s, v) = G(s, u)+ w(u, v) = d[u] + w(u, v), and the sets d[v] = d[u] + w(u, v) = G(s, v). November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.9 November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7. Correctness Part III Theorem. Dijkstra s terminates with d[v] = G(s, v) or all v V. November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7. Correctness Part III Theorem. Dijkstra s terminates with d[v] = G(s, v) or all v V. Proo. It suices to show that d[v] = G(s, v) or every v V when v is aed to S. Suppose u is the irst vertex aed to S or which d[u]!g(s, u). Let y be the irst vertex in V S along a shortest path rom s to u, and let x be its predecessor: S, just beore aing u. ss November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7. xx yy uu

Correctness Part III (continued) ss S Since u is the irst vertex violating the claimed invariant, we have d[x] = G(s, x). When x was aed to S, the edge (x, y) was relaxed, which implies that d[y] =G(s, y) dg(s, u) d[u]. But, d[u] d d[y] by our choice o u. Contradiction. xx yy uu Analysis o Dijkstra do u m EXTRACT-MIN(Q) SmS {u} or each v Adj[u] do i d[v] > d[u] + w(u, v) then d[v] m d[u] + w(u, v) November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7. November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.4 Analysis o Dijkstra Analysis o Dijkstra V times do u m EXTRACT-MIN(Q) SmS {u} or each v Adj[u] do i d[v] > d[u] + w(u, v) then d[v] m d[u] + w(u, v) V times degr(u) times do u m EXTRACT-MIN(Q) SmS {u} or each v Adj[u] do i d[v] > d[u] + w(u, v) then d[v] m d[u] + w(u, v) November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.5 November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.6

Analysis o Dijkstra Analysis o Dijkstra V times degr(u) times do u m EXTRACT-MIN(Q) SmS {u} or each v Adj[u] do i d[v] > d[u] + w(u, v) then d[v] m d[u] + w(u, v) V times degr(u) times do u m EXTRACT-MIN(Q) SmS {u} or each v Adj[u] do i d[v] > d[u] + w(u, v) then d[v] m d[u] + w(u, v) Handshaking Lemma Ÿ4(E) implicit DECREASE-KEY s. November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.7 Handshaking Lemma Ÿ4(E) implicit DECREASE-KEY s. Time = 4(V T EXTRACT-MIN + E T DECREASE-KEY ) Note: Same ormula as in the analysis o Prim s minimum spanning tr. November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7. Analysis o Dijkstra (continued) Time = 4(V) T EXTRACT-MIN + 4(E) T DECREASE-KEY Q T EXTRACT-MIN T DECREASE-KEY Total Analysis o Dijkstra (continued) Time = 4(V) T EXTRACT-MIN + 4(E) T DECREASE-KEY Q T EXTRACT-MIN T DECREASE-KEY Total array O(V) O() O(V ) November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.9 November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.4

Analysis o Dijkstra (continued) Time = 4(V) T EXTRACT-MIN + 4(E) T DECREASE-KEY Q T EXTRACT-MIN T DECREASE-KEY Total array O(V) O() O(V ) binary heap O(lg V) O(lg V) O(E lg V) Analysis o Dijkstra (continued) Time = 4(V) T EXTRACT-MIN + 4(E) T DECREASE-KEY Q T EXTRACT-MIN T DECREASE-KEY Total array O(V) O() O(V ) binary heap Fibonai heap O(lg V) O(lg V) O(E lg V) O(lg V) amortized O() amortized O(E + V lg V) worst case November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.4 November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.4 Unweighted graphs Suppose that w(u, v) = or all (u, v) E. Can Dijkstra s be improved? Unweighted graphs Suppose that w(u, v) = or all (u, v) E. Can Dijkstra s be improved? Use a simple FIFO queue instead o a priority queue. November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.4 November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.44

Unweighted graphs Suppose that w(u, v) = or all (u, v) E. Can Dijkstra s be improved? Use a simple FIFO queue instead o a priority queue. Breadth-irst do umdequeue(q) or each v Adj[u] do i d[v] = then d[v] m d[u] + ENQUEUE(Q, v) November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.45 Unweighted graphs Suppose that w(u, v) = or all (u, v) E. Can Dijkstra s be improved? Use a simple FIFO queue instead o a priority queue. Breadth-irst do umdequeue(q) or each v Adj[u] do i d[v] = then d[v] m d[u] + ENQUEUE(Q, v) Analysis: Time = O(V + E). November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.46 Example o breadth-irst Example o breadth-irst aa aa Q: Q: a November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.47 November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.4

Example o breadth-irst Example o breadth-irst aa Q: a b d aa Q: a b d c e November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.49 November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.5 Example o breadth-irst Example o breadth-irst aa Q: a bdc e aa Q: a bdce November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.5 November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.5

Example o breadth-irst aa Q: a b d c e g i Example o breadth-irst aa 4 4 Q: a b d c e g i November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.5 November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.54 Example o breadth-irst aa 4 4 4 4 Q: a b d c e g i h Example o breadth-irst aa 4 4 4 Q: a b d c e g i h November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.55 November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.56

Example o breadth-irst aa 4 4 Q: a b d c e g i h Example o breadth-irst aa 4 4 Q: a b d c e g i h November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.57 November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.5 Correctness o BFS do umdequeue(q) or each v Adj[u] do i d[v] = then d[v] m d[u] + ENQUEUE(Q, v) Key idea: The FIFO Q in breadth-irst mimics the priority queue Q in Dijkstra. Invariant: v comes ater u in Q implies that d[v] = d[u] or d[v] = d[u] +. November 4, 5 Copyright -5 by Erik D. Demaine and Charles E. Leiserson L7.59