CS210-Data Structures-Module-38-Shortest-Path

CS210-Data Structures-Module-38-Shortest-Path

Data Structures and Algorithms (CS210A) Lecture 38 • An interesting problem: shortest path from a source to destination • Sorting Integers 1 SHORTEST PATHS IN A GRAPH A fundamental problem 2 Notations and Terminologies A directed graph 푮 = (푽, 푬) • 흎: 푬 → 푹+ • Represented as Adjacency lists or Adjacency matrix • 풏 = |푽| , 풎 = |푬| Question: what is a path in 푮? Answer: A sequence 풗ퟏ, 풗ퟐ,…, 풗풌 such that (풗풊,풗풊+ퟏ) ∈ 푬 for all ퟏ ≤ 풊 < 풌. 12 3 … 29 풗ퟏ 풗ퟐ 풗ퟑ 풗풌−ퟏ 풗풌 Length of a path 푷 = 풆∈푷 흎(풆) 3 Notations and Terminologies Definition: The path from 풖 to 풗 of ? minimum length is called the shortest path from 풖 to 풗 Definition: Distance from 풖 to 풗 is the length of the shortest path from 풖 to 풗. Notations: 휹(풖, 풗) : distance from 풖 to 풗. 푷(풖, 풗) : The shortest path from 풖 to 풗. 4 Problem Definition Input: A directed graph 푮 = (푽, 푬) with 흎: 푬 → 푹+ and a source vertex 풔 ∈ 푽 Aim: • Compute 휹(풔, 풗) for all 풗 ∈ 푽\*풔+ • Compute 푷(풔, 풗) for all 풗 ∈ 푽\*풔+ What if 흎(풆) = 1 ? Do BFS from 풔 5 Problem Definition Input: A directed graph 푮 = (푽, 푬) with 흎: 푬 → 푹+ and a source vertex 풔 ∈ 푽 Aim: • Compute 휹(풔, 풗) for all 풗 ∈ 푽\*풔+ • Compute 푷(풔, 풗) for all 풗 ∈ 푽\*풔+ First algorithm : by Edsger Dijkstra in 1956 And still the best … I am sure you will be able to re-invent it yourself if you are asked right questions So get ready ! 6 An Example 풛 10 풗 12 7 G 풙 풔 3 5 풖 풚 Can you spot any vertex for which you are certain about its distance from 풔 ? Give Answer: vertex 풖. reasons. 7 An Example 풛 10 풗 12 7 G 풙 풔 3 5 풖 풚 > ퟎ 8 An Example 풛 10 풗 12 7 G 풙 풔 3 5 풖 풚 Yes, the edge (풔, 풖) is indeed the shortest path to 풖. To Greedy form a Strategy.smaller instance Does it remind you How to use it to design an Right,of the so problem.what something from recent algorithm for shortest paths ? should be your Butpast how ? ? next step ? 9 Pondering over the problem Idea 1 : Remove 풖 since we have computed distance to 풖. & so its job is done. So now there will be 풏 − ퟏ vertices. The new graph will preserve those shortest paths from 풔 in which 풖 is not present. But what about those shortest paths from 풔 that pass through 풖 ? We lost them with the removal of 풖. So we can’t afford to remove 풖. How can then we get a smaller instance ? Merging 풔 and 풖 But how ? 10 An Example 풛 10 풗 12 7 2 21 G 풙 풔 풕 3 5 풖 3 8 풚 풓 Let us look carefully around 풖 ? 11 An Example 풛 10 풗 12 7 2+3 21 +3 G’ 풙 풕 풔 5 3 +3 8 +3 풚 풓 12 An Example 풛 10 풗 12 7 5 24 G’ 풙 풕 풔 5 6 11 풚 풓 13 How to compute instance 푮′ Let (풔,풖) be the least weight edge from 풔 in 푮=(푽, 푬). Transform 푮 into 푮′ as follows. 1. For each edge (풖,풙)ϵ 푬, add edge (풔,풙); 흎(풔,풙) ??흎 (풔,풖) + 흎(풖,풙); 2. In case of two edges from 풔 to any vertex 풙, keep only the lighter edge. 3. Remove vertex 풖. Theorem: For each 풗 ∈ 푽\*풔, 풖+, 휹푮 풔, 풗 = 휹푮′ 풔, 풗 How efficient an algorithm for shortest paths can you design ? 14 No. of vertices (푮, 풔) 풏 O(풏) time Building 푮′ (푮′, 풔) 풏 − ퟏ ퟐ an algorithm for distances from 풔 with 푶(풏 ) time complexity. 15 Integer sorting Algorithms for Sorting 풏 elements • Insertion sort: O(풏ퟐ) • Selection sort: O (풏ퟐ) • Bubble sort: O (풏ퟐ) • Merge sort: O (풏 log 풏) • Quick sort: worst case O(풏ퟐ), average case O(풏 log 풏) • Heap sort: O(풏 log 풏) Question: What is common among these algorithms ? Answer: All of them use only comparison operation to perform sorting. Theorem (to be proved in CS345): Every comparison based sorting algorithm must perform at least 풏 log 풏 comparisons in the worst case. Question: Can we sort in O(풏) time ? The answer depends upon • the model of computation. • the domain of input. Integer sorting Counting sort: algorithm for sorting integers Input: An array A storing 풏 integers in the range [0…풌 − ퟏ]. 풌 = O(풏) Output: Sorted array A. Running time: O(풏 + 풌) in word RAM model of computation. Extra space: O(풌) Motivating example: Indian railways There are 13 lacs employees. Aim : To sort them list according to DOB (date of birth) Observation: There are only 14600 different date of births possible. Counting sort: algorithm for sorting integers 0 1 2 3 4 5 6 7 A 2 5 3 0 2 3 0 3 If A[풊]=풋, where should A[풊] be placed in B ? 0 1 2 3 4 5 Count 2 0 2 3 0 1 Certainly after all those elements in A which are smaller than 풋 0 1 2 3 4 5 Place 2 2 4 7 7 8 0 1 2 3 4 5 6 7 B 3 Final sorted output Counting sort: algorithm for sorting integers 0 1 2 3 4 5 6 7 A 2 5 3 0 2 3 0 3 0 1 2 3 4 5 Count 2 0 2 3 0 1 0 1 2 3 4 5 Place 2 2 4 6 7 8 0 1 2 3 4 5 6 7 B 0 3 Counting sort: algorithm for sorting integers 0 1 2 3 4 5 6 7 A 2 5 3 0 2 3 0 3 0 1 2 3 4 5 Count 2 0 2 3 0 1 0 1 2 3 4 5 Place 1 2 4 6 7 8 0 1 2 3 4 5 6 7 B 0 3 3 Counting sort: algorithm for sorting integers Algorithm (A[ퟎ...풏 − ퟏ], 풌) For 풋=0 to 풌 − ퟏ do Count[풋] 0; For 풊=0 to 풏 − ퟏ do Count[ A[풊] ] Count[ A[풊] ] +1; For 풋=0 to 풌 − ퟏ do Place[풋] Count[풋]; For 풋=1 to 풌 − ퟏ do Place[풋] Place [ 풋 −? ퟏ ] + Count [ 풋 ] ; For 풊=풏 − ퟏ to ퟎ do { B[ Place ?[A[ 풊 ]] - 1 ] A[풊]; Place [A[ ? 풊 ]] Place [A[ 풊 ]] - 1 ; ; What is the time complexity } of this algorithm in word RAM model ? return B; .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    24 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us