1 9.3 Shortest Path Algorithms Improvement –Use a queue to keep all vertices whose shortest distance to s is known. –Initialize d v to  for all vertices,

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.
Chapter 9: Graphs Shortest Paths
CS 206 Introduction to Computer Science II 04 / 01 / 2009 Instructor: Michael Eckmann.
Graph Theory Arnold Mesa. Basic Concepts n A graph G = (V,E) is defined by a set of vertices and edges v3 v1 v2Vertex (v1) Edge (e1) A Graph with 3 vertices.
§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.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
CSC 2300 Data Structures & Algorithms April 13, 2007 Chapter 9. Graph Algorithms.
Graph Algorithms. Jaruloj Chongstitvatana Chapter 3: Greedy Algorithms 2 Outline Graph Representation Shortest path Minimum spanning trees.
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
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.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
CMSC 341 Graphs 2. 2 Weighted Shortest Path Problem Single-source shortest-path problem: Given as input a weighted graph, G = (V,E), and a distinguished.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
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.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Chapter 9: Graphs Summary Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
CS 206 Introduction to Computer Science II 11 / 10 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Shortest Path Algorithm By Weston Vu CS 146. What is Shortest Paths? Shortest Paths is a part of the graph algorithm. It is used to calculate the shortest.
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.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
CSC 2300 Data Structures & Algorithms April 3, 2007 Chapter 9. Graph Algorithms.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
CS 146: Data Structures and Algorithms July 21 Class Meeting
Dijkstra’s Algorithm. 2 Shortest-path Suppose we want to find the shortest path from node X to node Y It turns out that, in order to do this, we need.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
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
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
ALG0183 Algorithms & Data Structures Lecture 21 d Dijkstra´s algorithm 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Chapter 14 Weiss.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 6 Shortest Path Algorithms.
Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine.
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.
CS 61B Data Structures and Programming Methodology Aug 5, 2008 David Sun.
CMSC 341 Graphs. 8/3/2007 UMBC CMSC 341 Graphs 2 Basic Graph Definitions A graph G = (V,E) consists of a finite set of vertices, V, and a finite set of.
Graphs and Paths Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 14 © 2002 Addison Wesley.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Graph Algorithm Hongfei Yan School of EECS, Peking University 5/21/2014.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
Shortest Paths.
C.Eng 213 Data Structures Graphs Fall Section 3.
Shortest Path Problems
Graphs.
CSC 172 DATA STRUCTURES.
MST - Prim’s Algorithm Greedy algorithm that “grows” an MST by repeatedly finding the lowest cost edge that will connect a new vertex to MST For every.
CSE 373: Data Structures and Algorithms
Shortest Path Algorithms
Shortest Path Problems
CMSC 341 Lecture 20.
Shortest Path Algorithms
Topological Sort Algorithm
Fundamental Structures of Computer Science II
CE 221 Data Structures and Algorithms
Fundamental Structures of Computer Science II
CSC 380: Design and Analysis of Algorithms
CSE 417: Algorithms and Computational Complexity
CMSC 341 Graphs 2.
CE 221 Data Structures and Algorithms
Richard Anderson Spring 2016
Graphs: Shortest path and mst
Chapter 9: Graphs Shortest Paths
GRAPH – Definitions A graph G = (V, E) consists of
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

1 9.3 Shortest Path Algorithms Improvement –Use a queue to keep all vertices whose shortest distance to s is known. –Initialize d v to  for all vertices, except s –Set d s to 0 and enqueue s. –While the queue is not empty remove the first vertex in the queue. enqueue all vertices which are adjacent to the vertex, and have a distance of infinity from s.

2 9.3 Shortest Path Algorithms –known not used –Running time is  (|E|+|V|). /* Fig Unweighted shortest path algorithm */ void Unweighted (Table T) /* Assume T is initialized */ { Queue Q; Vertex V, W; Q = CreateQueue (NumVertex);

3 9.3 Shortest Path Algorithms MakeEmpty (Q); /* Enqueue the start vertex S */ Enqueue (S, Q); while (!IsEmpty (Q)) { V = Dequeue {Q); T [V].Known = True; /* Not really needed */

4 9.3 Shortest Path Algorithms for each W adjacent to V if (T [W].Dist == Infinity) { T [W].Dist = T [V].Dist + 1; T [W].Path = V; Enqueue (W, Q); } DisposeQueue (Q); /* Free the memory */ }

5 9.3 Shortest Path Algorithms Result for Fig. 9.20, with weight=1 & start = v 3

6 9.3 Shortest Path Algorithms

7 Dijkstra’s Algorithm (for single-source weighted graphs) A greedy algorithm, solving a problem by stages by doing what appears to be the best thing at each stage. Select a vertex v, which has the smallest d v among all the unknown vertices, and declare that the shortest path from s to v is known.

8 9.3 Shortest Path Algorithms For each adjacent vertex, w, update d w = d v + c v,w if this new value for d w is an improvement.

9 9.3 Shortest Path Algorithms Application of Dijkstra’s algorithm for Fig. 9.20

Shortest Path Algorithms Details on Fig. 9.28

Shortest Path Algorithms /* Fig Declarations for Dijkstra’s Algorithm typedef int Vertex; struct TableEntry { List Header; /* Adjacency list */ int Known; DistType Dist; Vertex Path; } #defin NotAVertex (-1) tyepdef struct TableEntry Table [NumVertex];

Shortest Path Algorithms /* Fig Table initialization routine */ void InitTable (Vertex Start, Graph G, Table T) { int i; ReadGraph (G, T); for (i=0; i<NumVertex; i++) { T [i].Known = False; T [i].Dist = Infinity; T [i].Path = NotAVertex; } T [Start].Dist = 0; }

Shortest Path Algorithms /* Fig Pseudocode - Dijkstra’s Algorithm */ void Dijkstra (Table T) { Vertex V, W; for (;;;) { V = smallest unknown distance vertex; if (V == NotAVertex) break; T [V].Known = True; for each W adjacent to V

Shortest Path Algorithms if (!T [W].Known) if (T [V].Dist + Cvw < T [W].Dist) { /* update W */ Decrease (T [W].Dist to T [V].Dist + Cvw); T [W].Path = V; }

Shortest Path Algorithms /* Fig Routine to print actual shortest path */ /* Assume the path exists */ void PrintPath (Vertex V, Table T) { if (T [V].Path != NotAVertex) { PrintPath (T [V].Path, T) printf (" to"); } printf ("%v", V); /* %v is pseudocode */ }

Shortest Path Algorithms Total of the running time to find the minimum d v is  (|V| 2 ). Running time for updating d w is constant per update, for a total of  (|E|). Total running time is  (|E|+|V| 2 ). For a sparse graph, |E| =  (|V|), the algorithm is slow. It’s best to keep the distances in a priority queue.

Shortest Path Algorithms Critical Path Analysis (An Acyclic Graph) Given an activity-node graph The edges represent precedence relationships: (v, w) means that activity v must be completed before activity w may begin. Activities independent of each other can be performed in parallel.

Shortest Path Algorithms

Shortest Path Algorithms Convert the activity-node graph to an event-node graph. Each event corresponds to the completion of an activity and all its dependent activities. Events reachable from a node v in the event-node graph may not commence until after the event v is completed.

Shortest Path Algorithms Dummy edges and nodes may need to be inserted in the case where an activity depends on several others, to avoid introducing false dependencies (or false lack of dependencies).

Shortest Path Algorithms

Shortest Path Algorithms Earliest completion time –Length of the longest path from the first event to the last event –Let EC i be the earliest completion time for node i –Compute for all vertices by their topological order.

Shortest Path Algorithms

Shortest Path Algorithms Latest starting time –Latest starting time –Let LC i be the latest time event i must be completed without affecting the final completion time. –Computed by reverse topological order.

Shortest Path Algorithms

Shortest Path Algorithms Slack time for each edge is the amount of time that the completion of the corresponding activity can be delayed without affecting the overall completion. Slack (v,w) = LC w - EC v - c v,w

Shortest Path Algorithms A critical path consists entirely of zero-slack edges.