CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.

Slides:



Advertisements
Similar presentations
Graph Algorithms - 3 Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 13Feb 12, 2014Carnegie Mellon University.
Advertisements

0 Course Outline n Introduction and Algorithm Analysis (Ch. 2) n Hash Tables: dictionary data structure (Ch. 5) n Heaps: priority queue data structures.
Single Source Shortest Paths
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
The Greedy Approach Chapter 8. The Greedy Approach It’s a design technique for solving optimization problems Based on finding optimal local solutions.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
November 14, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Aalborg University
CSE 101- Winter ‘15 Discussion Section January 26th 2015.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
Introduction to Algorithms 6.046J/18.401J/SMA5503
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
CSC 331: Algorithm Analysis Paths in Graphs. The DFS algorithm we gave is recursive. DFS generates a search tree showing paths from one vertex to all.
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
Shortest Paths and Dijkstra's Algorithm CS 110: Data Structures and Algorithms First Semester,
1 Shortest Path Algorithms Given a graph G = (V, E) and a distinguished vertex s, find the shortest weighted path from s to every other vertex in G. weighted.
Tirgul 12 Algorithm for Single-Source-Shortest-Paths (s-s-s-p) Problem Application of s-s-s-p for Solving a System of Difference Constraints.
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
Shortest Path Algorithms
Shortest Paths Definitions Single Source Algorithms
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
DAST 2005 Tirgul 12 (and more) sample questions. DAST 2005 Q.We’ve seen that solving the shortest paths problem requires O(VE) time using the Belman-Ford.
CSE 780 Algorithms Advanced Algorithms SSSP Dijkstra’s algorithm SSSP in DAGs.
Tirgul 13. Unweighted Graphs Wishful Thinking – you decide to go to work on your sun-tan in ‘ Hatzuk ’ beach in Tel-Aviv. Therefore, you take your swimming.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Shortest Path Problem For weighted graphs it is often useful to find the shortest path between two vertices Here, the “shortest path” is the path that.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
CSC 2300 Data Structures & Algorithms April 3, 2007 Chapter 9. Graph Algorithms.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
Data Structures and Algorithms1 Graphs 3 Adapted From: Data Structures and Their Algorithms, by Harry R. Lewis and Larry Denenberg (Harvard University:
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Dijkstra's algorithm.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
1 Shortest Path Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
Jim Anderson Comp 122, Fall 2003 Single-source SPs - 1 Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph.
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
1 WEEK 9-10 Graphs II Unweighted Shortest Paths Dijkstra’s Algorithm Graphs with negative costs Acyclic Graphs Izmir University of Economics.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Minimum Spanning Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
All-Pairs Shortest Paths & Essential Subgraph 01/25/2005 Jinil Han.
Chapter 24: Single-Source Shortest Paths Given: A single source vertex in a weighted, directed graph. Want to compute a shortest path for each possible.
Introduction to Algorithms Jiafen Liu Sept
Shortest Path in Weighted Graph : Dijkstra’s Algorithm.
D IJKSTRA ' S ALGORITHM. S INGLE -S OURCE S HORTEST P ATH P ROBLEM Single-Source Shortest Path Problem - The problem of finding shortest paths from a.
Shortest Path Graph Theory Basics Anil Kishore.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
Shortest Path Algorithms. Definitions Variants  Single-source shortest-paths problem: Given a graph, finding a shortest path from a given source.
D IJKSTRA ’ S S INGLE S OURCE S HORTEST P ATH Informatics Department Parahyangan Catholic University.
Shortest Paths CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
1 Introduction to Algorithms L ECTURE 17 (Chap. 24) Shortest paths I 24.2 Single-source shortest paths 24.3 Dijkstra’s algorithm Edsger Wybe Dijkstra
Lecture 13 Algorithm Analysis
Length of a Path The weight (length) of a path is the sum of the weights of its edges. adcbe Path p: Edge weights: w(a, b) = 7, w(b, c) = 2, w(c,
Single-Source Shortest Paths (25/24) HW: 25-2 and 25-3 p. 546/24-2 and 24-3 p.615 Given a graph G=(V,E) and w: E   weight of is w(p) =  w(v[i],v[i+1])
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Unweighted Shortest Path Neil Tang 3/11/2010
Short paths and spanning trees
Greedy Algorithms / Dijkstra’s Algorithm Yin Tat Lee
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Slide Courtesy: Uwash, UT
CSE 373 Data Structures and Algorithms
Slide Courtesy: Uwash, UT
Graph Algorithms: Shortest Path
CSE 417: Algorithms and Computational Complexity
Presentation transcript:

CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin

Outline Graph Concepts Single-source shortest path problem Breadth-first search – for unweighted graphs Dijkstra’s algorithm – for non-negative weights

Graph G = (V, E) Simple graph: unweighted, undirected graph, containing no loops and multiple edges o |E| = O(|V| 2 )

Graph Adjacency list o Space complexity: O(|V|+|E|) / 6/ 1/ 15/ 147/ 2/ 5/

Single-source shortest path What is the optimal path from one vertex to another? o Suppose each edge is of unit length o Store the minimum distances in an array dist[] o Example: if source vertex s = “3” udist[u] 11 2∞ ∞ 73

Breadth-first search (BFS) Intuition: find the neighbors of the neighbors Additional structure: a queue Q Pseudocode: o Initialize: dist[s] = 0; dist[u] = ∞ for all other vertices u o Q = [ s ] o While Q is not empty Dequeue the top element u of Q For all neighbors v of u, if dist[v] = ∞, o Put v in Q o Set dist[v] = dist[u] + 1

Dry run 1: Initialize udist[u] 1∞ 2∞ 30 4∞ 5∞ 6∞ 7∞ Source s

Dry run 2: Q = [ s ] udist[u] 1∞ 2∞ 30 4∞ 5∞ 6∞ 7∞ Q

Dry run 3: Dequeue udist[u] 1∞ 2∞ 30 4∞ 5∞ 6∞ 7∞ Q u = 3

Dry run 4: Find neighbors udist[u] 1∞ 2∞ 30 4∞ 5∞ 6∞ 7∞ Q u = 3

Dry run 5: Update distance and enqueue udist[u] 11 2∞ 30 4∞ 5∞ 6∞ 7∞ Q u = 3 dist[3] + 1 = = 1

Dry run 6: Dequeue udist[u] 11 2∞ 30 4∞ 5∞ 6∞ 7∞ Q u = 1

Dry run 7: Find neighbors udist[u] 11 2∞ 30 4∞ 5∞ 6∞ 7∞ Q u = 1

Dry run 8: Update distance and enqueue udist[u] 11 2∞ ∞ 7∞ Q u = 1 dist[1] + 1 = = 2

Dry run 9: Dequeue udist[u] 11 2∞ ∞ 7∞ Q u = 4

Dry run 10: Find neighbors udist[u] 11 2∞ ∞ 7∞ Q u = 4

Dry run 11: udist[u] 11 2∞ ∞ 7∞ Q u = 4

Dry run 12: Dequeue udist[u] 11 2∞ ∞ 7∞ Q u = 5

Dry run 13: Find neighbors udist[u] 11 2∞ ∞ 7∞ Q u = 5

Dry run 14: udist[u] 11 2∞ ∞ Q u = 5 dist[5] + 1 = = 3

Dry run 15: Dequeue udist[u] 11 2∞ ∞ Q u = 7

Dry run 16: Find neighbors udist[u] 11 2∞ ∞ Q u = 7

Dry run 17: udist[u] 11 2∞ ∞ Q u = 7

Dry run 18: Q is now empty! udist[u] 11 2∞ ∞ Q

Dry run 19: Final result udist[u] 11 2∞ ∞ Q

Analysis Correctness: by induction o Vertices are processed in ascending order of distance from s o Subpaths of shortest paths are shortest paths Size of Q = O(|V|) o Each vertex is enqueued at most once Time complexity = O(|V|+|E|) o Initialization: O(|V|) operations o Each edge is considered O(1) times o Enqueue/dequeue: O(1) operations

Weighted graphs Suppose now that each edge has its non- negative length l ( u, v ) o Need something more than BFS udist[u] 13 2∞ ∞ / 1 (3, 3) (4, 4) (5, 2)

Dijkstra’s Algorithm Intuition: Identify those vertices whose distances are tight Additional structure: a priority queue Q Pseudocode: o Initialize: dist[s] = 0, and dist[u] = ∞ for all other u o Let Q contain all vertices o while Q is not empty find a vertex u in Q with dist[u] being the minimum delete u from Q for each neighbor v o if dist[v] > dist[u] + l ( u, v ), set dist[v] = dist[u] + l ( u, v )

Dry run 1: Initialize Source s udist[u] 1∞ 2∞ 30 4∞ 5∞ 6∞ 7∞

Dry run 2: Let Q contain all vertices o Let us not care about what a priority queue is for the time being. udist[u] 1∞ 2∞ 30 4∞ 5∞ 6∞ 7∞ Q

Dry run 3: Find minimum udist[u] 1∞ 2∞ 30 4∞ 5∞ 6∞ 7∞ Q u = 3

Dry run 4: Delete u from Q udist[u] 1∞ 2∞ 30 4∞ 5∞ 6∞ 7∞ Q u = 3

Dry run 5: Relaxation udist[u] 13 2∞ 30 4∞ 5∞ 6∞ 7∞ Q u = 3 dist[3] + l(3, 1) = = 3

Dry run 6: Find minimum udist[u] 13 2∞ 30 4∞ 5∞ 6∞ 7∞ Q u = 1

Dry run 7: Delete u from Q udist[u] 13 2∞ 30 4∞ 5∞ 6∞ 7∞ Q u = 1

Dry run 8: Relaxation udist[u] 13 2∞ ∞ 7∞ Q u = 1 dist[1] + l(1, 4) = = 7 dist[1] + l(1, 5) = = 5

Dry run 9: Find minimum udist[u] 13 2∞ ∞ 7∞ Q u = 5

Dry run 10: Delete u from Q udist[u] 13 2∞ ∞ 7∞ Q u =

Dry run 11: Relaxation udist[u] 13 2∞ ∞ Q u = 5 dist[5] + l(5, 4) = = 6 dist[5] + l(5, 7) = =

Dry run 12: Find minimum Q u = 4 udist[u] 13 2∞ ∞ 78

Dry run 13: Q u = 4 udist[u] 13 2∞ ∞

Dry run 14: Q u = udist[u] 13 2∞ ∞ 78

Dry run 15: Q u = udist[u] 13 2∞ ∞ 78 dist[2] + l(2, 6) = ∞ + 2 = ∞

Dry run 16: Q u = udist[u] 13 2∞ ∞ 78

Dry run 17: Q is now empty! Q udist[u] 13 2∞ ∞ 78

Dry run 18: Final result Q udist[u] 13 2∞ ∞ 78

Analysis Correctness: same as before Size of Q = O(|V|) A naive implementation has time complexity O(|V| 2 ) o A vertex is removed from Q in each iteration of the while loop o Finding a minimum: O(|V|) operations o Deletion / relaxation: O(1) operations We can achieve O(|V|log|V|+|E|) with a binary heap o Finding a minimum: O(1) operations o Deletion: O(log|V|) operations, to maintain the heap property that parent’s value ≤ children’s values

Priority queue It could be implemented using a heap. o parent’s value ≤ children’s values (3, 0) (1, ∞)(2, ∞) (4, ∞) (6, ∞) (5, ∞)(7, ∞)

Priority queue Delete (3, 0) (3, 0) (1, ∞)(2, ∞) (4, ∞) (6, ∞) (5, ∞)(7, ∞)

Priority queue Delete (3, 0) o Heap property NOT violated; OK! (1, ∞)(2, ∞) (4, ∞) (6, ∞) (5, ∞) (7, ∞)

Priority queue Update (1, ∞) → (1, 3) o dist[ 3 ] + l (3, 1) = = 3 o Heap property violated; DecreaseKey triggered (1, 3)(2, ∞) (4, ∞) (6, ∞) (5, ∞) (7, ∞)

Priority queue Update (1, ∞) → (1, 3) o Heap property NOT violated; OK! (7, ∞)(2, ∞) (4, ∞) (6, ∞) (5, ∞) (1, 3)

End Questions

End o I now know the distances dist[u], but what are the shortest paths? o How do we locate the heap entries quickly?