CSE 101- Winter ‘15 Discussion Section January 26th 2015.

Slides:



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

Single Source Shortest Paths
§3 Shortest Path Algorithms Given a digraph G = ( V, E ), and a cost function c( e ) for e  E( G ). The length of a path P from source to destination.
Nattee Niparnan. Distance of nodes  The distance between two nodes is the length of the shortest path between them S  A 1 S  C 1 S  B 2.
CSC 2300 Data Structures & Algorithms April 13, 2007 Chapter 9. Graph Algorithms.
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
Introduction To Algorithms CS 445 Discussion Session 6 Instructor: Dr Alon Efrat TA : Pooja Vaswani 03/21/2005.
Applications Data Structures and Algorithms (60-254)
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.
The Shortest Path Problem. Shortest-Path Algorithms Find the “shortest” path from point A to point B “Shortest” in time, distance, cost, … Numerous.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
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.
CPSC 411, Fall 2008: Set 9 1 CPSC 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Fall 2008.
Shortest Path Problems
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Shortest Path Problems Directed weighted graph. Path length is sum of weights of edges on path. The vertex at which the path begins is the source vertex.
1 Advanced Algorithms All-pairs SPs DP algorithm Floyd-Warshall alg.
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
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.
All-Pairs Shortest Paths
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
CSC 2300 Data Structures & Algorithms April 3, 2007 Chapter 9. Graph Algorithms.
Dijkstra's algorithm.
Topological Sorting and Least-cost Path Algorithms.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
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.
5/27/03CSE Shortest Paths CSE Algorithms Shortest Paths Problems.
CS223 Advanced Data Structures and Algorithms 1 The Bellman-Ford Shortest Path Algorithm Neil Tang 03/11/2010.
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.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Algorithms: Design and Analysis Summer School 2013 at VIASM: Random Structures and Algorithms Lecture 3: Greedy algorithms Phan Th ị Hà D ươ ng 1.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
Graphs Data Structures and Algorithms A. G. Malamos Reference Algorithms, 2006, S. Dasgupta, C. H. Papadimitriou, and U. V. Vazirani Introduction to Algorithms,Third.
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.
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
Introduction to Algorithms Jiafen Liu Sept
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
The single-source shortest path problem (SSSP) input: a graph G = (V, E) with edge weights, and a specific source node s. goal: find a minimum weight (shortest)
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.
Nattee Niparnan. Dijkstra’s Algorithm Graph with Length.
Shortest Paths CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
1 Prim’s algorithm. 2 Minimum Spanning Tree Given a weighted undirected graph G, find a tree T that spans all the vertices of G and minimizes the sum.
Lecture 13 Algorithm Analysis
CSCE 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 9 1.
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
CSC317 Shortest path algorithms
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
Graph Algorithms: Shortest Path
CSE 417: Algorithms and Computational Complexity
Presentation transcript:

CSE 101- Winter ‘15 Discussion Section January 26th 2015

Shortest Paths Breadth-first search finds the shortest path in graphs whose edges have unit length. How do we adapt breadth-first search to find the shortest path in a more general graph G = (V,E) whose edge lengths l e are positive integers. We could break G’s long edges into unit-length pieces by introducing “dummy nodes”. Breadth-first search on this new graph should give the shortest path between the nodes in the original graph. For a more detailed explanation of the above procedure, please refer to slides 9-13 of lecture 6. (Lecture 6 link)(Lecture 6 link)

Dijsktra’s Algorithm The usage of the concept of alarm clock in the breadth-first search performed on the graph with dummy nodes results in Dijsktra’s algorithm. (refer to slide 12 of lecture 6 – link )link We explain the algorithm briefly through the below example : In the graph below, we are finding the shortest paths to all the nodes from the source node A. B A C D E

Dijsktra’s Algorithm The usage of the concept of alarm clock in the breadth-first search performed on the graph with dummy nodes results in Dijsktra’s algorithm. (refer to slide 12 of lecture 6 – link )link We explain the algorithm briefly through the below example : In the graph below, we are finding the shortest paths to all the nodes from the source node A. A B C D E Initially only node A is part of the known region NodesDistance from node A A0 B4 C2 D∞ E∞ Update the distances of the neighbors of A We maintain the nodes not in the known region in a priority queue with their current distance from node A as the key.

Dijsktra’s Algorithm The usage of the concept of alarm clock in the breadth-first search performed on the graph with dummy nodes results in Dijsktra’s algorithm. (refer to slide 12 of lecture 6 – link )link We explain the algorithm briefly through the below example : In the graph below, we are finding the shortest paths to all the nodes from the source node A. A B C D E NodesDistance from node A A0 B3 C2 D6 E7 Update the distances of the neighbors of C Delete-min – Returns C as the node with the shortest distance from the known region. C is added to the known region and deleted from the priority queue.

Dijsktra’s Algorithm The usage of the concept of alarm clock in the breadth-first search performed on the graph with dummy nodes results in Dijsktra’s algorithm. (refer to slide 12 of lecture 6 – link )link We explain the algorithm briefly through the below example : In the graph below, we are finding the shortest paths to all the nodes from the source node A. A B C D E NodesDistance from node A A0 B3 C2 D5 E6 Update the distances of the neighbors of B Delete-min – Returns B as the node with the shortest distance from the known region. B is added to the known region and deleted from the priority queue.

Dijsktra’s Algorithm The usage of the concept of alarm clock in the breadth-first search performed on the graph with dummy nodes results in Dijsktra’s algorithm. (refer to slide 12 of lecture 6 – link )link We explain the algorithm briefly through the below example : In the graph below, we are finding the shortest paths to all the nodes from the source node A. A B C D E NodesDistance from node A A0 B3 C2 D5 E6 Since D has no outgoing edges, we have nothing to update Delete-min – Returns D as the node with the shortest distance from the known region. D is added to the known region and deleted from the priority queue.

Dijsktra’s Algorithm The usage of the concept of alarm clock in the breadth-first search performed on the graph with dummy nodes results in Dijsktra’s algorithm. (refer to slide 12 of lecture 6 – link )link We explain the algorithm briefly through the below example : In the graph below, we are finding the shortest paths to all the nodes from the source node A. A B C D E We have the shortest path from A to all the other nodes NodesDistance from node A A0 B3 C2 D5 E6

Dijkstra’s Algorithm: Pseudo-Code We use a binary heap data structure for the priority queue. makequeue() takes O (|V| log |V|) Delete operation in a binary heap takes O(log|V|) time. Since we do a total of |V| deletions, total time taken = O (|V| log |V|) decreaseKey operation takes O( log |V|) time. Since this is done |E| times in total, total time taken = O(|E| log|V|) Hence, total time taken = O ((|V| + |E|) log |V|) Remember : If we use an array to implement the priority queue. 1)Time spent in makeQueue() = O(V). 2)Delete Operation takes O(|V|) time. Time taken for |V| deletions = O(|V| 2 ) time. 3)Time taken for decreaseKey operation = O(|1|). We do this operation |E] times. Total time taken = O(|E|). |E| = O(|V| 2 ). Hence, total time taken for this step = O(|V| 2 ). 4)Hence, total time taken = O (|V| 2 ).

Exercise 1 Give an algorithm that takes as input a directed graph with positive edge lengths, and returns the length of the shortest cycle in the graph (if the graph is acyclic, it should say so). Your algorithm should take time at most O(|V| 3 ).

Exercise 1 Give an algorithm that takes as input a directed graph with positive edge lengths, and returns the length of the shortest cycle in the graph (if the graph is acyclic, it should say so). Your algorithm should take time at most O(|V| 3 ). Solution : –If there is a cycle in the graph then we know there is a path from a vertex U to a vertex V and a path from V to U. –Finding the shortest cycle involves finding two vertices U and V such that above property is satisfied and the sum of the distances of the shortest path from U to V and the shortest path from V to U is the shortest amongst all the cycles in the graph. –We run Dijkstra’s once from each vertex in the graph. Implementing Dijkstra’s algorithm using an array as the priority queue takes O(|V| 2 ) time. Since we run Dijkstra’s with each of the vertices as the source vertex, time taken for this step is O(|V| 2 ) * |V| = O(|V| 3 ). If there are multiple pairs of vertices U and V which satisfy the above property, we an use any one of them since we just need the length of the shortest cycle. –We then check every combination of vertices (U,V) in order to find the shortest cycle. Since we have O(|V| 2 ) combinations, time taken for this check = O(|V| 2 ).

Shortest Path (negative edges) Dijkstra’s algorithm works in part because the shortest path from the starting node S to any node V must pass exclusively through nodes that are closer than V. This no longer holds when the edge lengths can be negative. In the figure below, shortest path from S to A passes through B, a node that is further away. S A B

Properties of Dijkstra’s algorithm U1U1 SU2U2 UkUk V

Bellman-Ford Algorithm If the shortest path from the source vertex S to a vertex V is as below: Dijkstra’s algorithm performs a sequence of safe updates.. Although Dijkstra’s performs safe updates, it does not ensure the sequence of updates happen in the order expected (order expected is explained in the previous slide). This problem can be solved by updating all the edges |V| - 1 times. This should ensure we get the shortest path even when the graph has negative edges. Why do we need to do this |V| -1 times? –The shortest path from the source vertex S to a vertex V has at most V-1 edges. This results in a O(|V||E| ) procedure called the Bellman-Ford algorithm. U1U1 SU2U2 UkUk V

Bellman-Ford Algorithm: Pseudo-Code

Comparion of Bellman-Ford and Dijkstra’s Dijkstra’s Bellman-Ford Time Complexity = O ( |V| + |E| ) log |V|Time Complexity = O( |V| |E| ) Solves the single-source shortest path problem for a graph with non-negative edge weights. Solves the single-source shortest path problem for a graph with negative edge weights. Cannot identify negative cycles.Can identify negative cycles.