Shortest Paths (1/11)  In this section, we shall study the path problems such like  Is there a path from city A to city B?  If there is more than one.

Slides:



Advertisements
Similar presentations
Maximum flow Main goals of the lecture:
Advertisements

Bellman-Ford algorithm
The following 5 questions are about VOLTAGE DIVIDERS. You have 20 seconds for each question What is the voltage at the point X ? A9v B5v C0v D10v Question.
Unit-iv.
and 6.855J Cycle Canceling Algorithm. 2 A minimum cost flow problem , $4 20, $1 20, $2 25, $2 25, $5 20, $6 30, $
Introduction to Algorithms 6.046J/18.401J/SMA5503
CSE 211 Discrete Mathematics
Graph Algorithms - 3 Algorithm Design and Analysis Victor AdamchikCS Spring 2014 Lecture 13Feb 12, 2014Carnegie Mellon University.
Improved Shortest Path Algorithms for Nearly Acyclic Directed Graphs L. Tian and T. Takaoka University of Canterbury New Zealand 2007.
ALG0183 Algorithms & Data Structures Lecture 23 a acyclic with neg. weights (topological sort algorithm) 8/25/20091 ALG0183 Algorithms & Data Structures.
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
1 Linked List Demo Node third = new Node(); third.item = "Carol"; third.next = null; Node second = new Node(); second.item = "Bob"; second.next = third;
Traffic assignment.
Comp 122, Spring 2004 Graph Algorithms – 2. graphs Lin / Devi Comp 122, Fall 2004 Identification of Edges Edge type for edge (u, v) can be identified.
Graphs, representation, isomorphism, connectivity
Microsoft Office Grade 10 A / B Cahaya Bangsa Classical School (C) 2010 Digital Media Production Facility 14 Microsoft Excel – 05.
1 On c-Vertex Ranking of Graphs Yung-Ling Lai & Yi-Ming Chen National Chiayi University Taiwan.
§5 Minimum Spanning Tree 【 Definition 】 A spanning tree of a graph G is a tree which consists of V( G ) and a subset of E( G ) 〖 Example 〗 A complete.
Computer Science: A Structured Programming Approach Using C Stacks A stack is a linear list in which all additions and deletions are restricted to.
Epp, section 10.? CS 202 Aaron Bloomfield
 Theorem 5.9: Let G be a simple graph with n vertices, where n>2. G has a Hamilton circuit if for any two vertices u and v of G that are not adjacent,
CS 253: Algorithms Chapter 22 Graphs Credit: Dr. George Bebis.
Foundations of Data Structures Practical Session #11 Sort properties, Quicksort algorithm.
Single Source Shortest Paths
all-pairs shortest paths in undirected graphs
UNC Chapel Hill Lin/Foskey/Manocha Steps in DP: Step 1 Think what decision is the “last piece in the puzzle” –Where to place the outermost parentheses.
Analysis of Algorithms CS 477/677
Lecture 7 March 1, 11 discuss HW 2, Problem 3
Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Interval Graph Test.
1 Efficient algorithms on sets of permutations, dominance, and real-weighted APSP Raphael Yuster University of Haifa.
Graphs CSE 331 Section 2 James Daly. Reminders Homework 4 is out Due Thursday in class Project 3 is out Covers graphs (discussed today and Thursday) Due.
CSE Lectures 18 – Graphs Graphs & Characteristics
Section 3.4 The Traveling Salesperson Problem Tucker Applied Combinatorics By Aaron Desrochers and Ben Epstein.
§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.
CHAPTER 6 GRAPHS All the programs in this file are selected from
* Bellman-Ford: single-source shortest distance * O(VE) for graphs with negative edges * Detects negative weight cycles * Floyd-Warshall: All pairs shortest.
CSC 2300 Data Structures & Algorithms April 13, 2007 Chapter 9. Graph Algorithms.
Chapter 6. Konigsberg Bridge Problem A river Pregel flows around the island Keniphof and then divides into two. Four land areas A, B, C, D have this river.
1 Pertemuan 26 Activity Network Matakuliah: T0026/Struktur Data Tahun: 2005 Versi: 1/1.
Minimum cost spanning tree and activity networks Data structure 2002/12/2.
1 Theory I Algorithm Design and Analysis (10 - Shortest paths in graphs) T. Lauer.
Design and Analysis of Algorithms Single-source shortest paths, all-pairs shortest paths Haidong Xue Summer 2012, at GSU.
§2 Topological Sort 〖 Example 〗 Courses needed for a computer science degree at a hypothetical university How shall we convert this list into a graph?
Data Structures Using C++
Chapter 4 The Greedy Method.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Algorithms All pairs shortest path
The Shortest Path Problem
The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each decision is locally optimal. These.
Directed graphs Definition. A directed graph (or digraph) is a pair (V, E), where V is a finite non-empty set of vertices, and E is a set of ordered pairs.
Chapter 2 Graph Algorithms.
Runtime O(VE), for +/- edges, Detects existence of neg. loops
All-Pairs Shortest Paths
Graphs 2015, Fall Pusan National University Ki-Joune Li.
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.
Shortest Paths.
CS202 - Fundamental Structures of Computer Science II
More Graph Algorithms.
Graph Algorithm.
Shortest Paths.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Unit 4: Dynamic Programming
2018, Fall Pusan National University Ki-Joune Li
2017, Fall Pusan National University Ki-Joune Li
Activity Networks
All pairs shortest path problem
Shortest Paths.
Presentation transcript:

Shortest Paths (1/11)  In this section, we shall study the path problems such like  Is there a path from city A to city B?  If there is more than one path from A to B, which path is the shortest?

Shortest Paths (2/11)  Single source/All destinations: nonnegative edge cost  Problem: given a directed graph G = (V, E), a length function length(i, j), length(i, j)  0, for the edges of G, and a source vertex v.  Need to solve: determine a shortest path from v to each of the remaining vertices of G.  Let S denote the set of vertices  dist[w]: the length of shortest path starting from v, going through only the vertices that are in S, ending at w. V0V0 V4V4 V1V1 V5V5 V3V3 V2V path length 1) v0 v2 10 2) v0 v2 v3 25 3) v0 v2 v3 v1 45 4) v0 v4 45

Shortest Paths (3/11)  Dijkastra’s Algorithm  Find the min cost of the path from a given source node to every other node.  Given: the cost e(v i, v j ) of all edges; v 0 is the source node; e(v i, v j ) = , if v i and v j are not adjacent S  {v 0 }; dist[v 0 ]  0; for each v in V - {v 0 } do dist[v]  e(v 0, v); while S  V do choose a vertex w in V-S such that dist[w] is a minimum; add w to S; for each v in V-S do dist[v]  min(dist[v], dist[w]+e(w, v));

Shortest Paths (4/11)  Declarations for the Shortest Path Algorithm #define MAX_VERTICES 6 int cost[ ][MAX_VERTICES]= {{ 0, 50, 10, 1000, 45, 1000}, {1000, 0, 15, 1000, 10, 1000}, { 20, 1000, 0, 15, 1000, 1000}, {1000, 20, 1000, 0, 35, 1000}, {1000, 1000, 30, 1000, 0, 1000}, {1000, 1000, 1000, 3, 1000, 0}}; int distance[MAX_VERTICES]; short int found{MAX_VERTICES]; int n = MAX_VERTICES; V0V0 V4V4 V1V1 V5V5 V3V3 V2V

Shortest Paths (5/11)  Choosing the least cost edge

 Single Source Shortest Paths Program (v=0) [0][1][2][3][4][5] visited: distance: cost: i: u: w: V0V0 V4V4 V1V1 V5V5 V3V3 V2V

Shortest Paths (7/11)  All Pairs Shortest Paths  we could solve this problem using shortestpath with each of the vertices in V(G) as the source. (O(n 3 ))  we can obtain a conceptually simpler algorithm that works correctly even if some edges in G have negative weights, require G has no cycles with a negative length (still O(n 3 ))

Shortest Paths (8/11)  Another solution  Use dynamic programming method.  Represent the graph G by its cost adjacency matrix with cost[i][j].  If i = j, cost[i][j] = 0.  If is not in G, cost[i][j] is set to some sufficiently large number.  Let A k [i][j] be the cost of shortest path from i to j, using only those intermediate vertices with an index  k.  The shortest path from i to j is A n-1 [i][j] as no vertex in G has an index greater than n - 1.

Shortest Paths (9/11)  Algorithm concept  The basic idea in the all pairs algorithm is begin with the matrix A -1 and successively generated the matrices A -1, A 0, A 2, …, A n  A -1 [i][j]=cost[i][j]  Calculate the A 0, A 1, A 2,..., A n-1 from A -1 iteratively  A k [i][j]=min{A k-1 [i][j], A k-1 [i][k]+A k-1 [k][j]}, k>=0

Shortest Paths (10/11)  Graph with Negative Cycle   The length of the shortest path from vertex 0 to vertex 2 is -  0, 1, 0, 1, 0, 1, …, 0, 1, (a) Directed graph (b) A -1

Shortest Paths (11/11)  All pairs shortest paths program V0V0 V2V2 V cost: final distance: K I J

Topological Sorts (1/19)  Activity on vertex (AOV) networks  All but the simplest of projects can be divided into several subprojects called activities.  The successful completion of these activities results in the completion of the entire project  Example: A student working toward a degree in computer science must complete several courses successful

Topological Sorts (2/19)   Definition:   Activity On Vertex (AOV) Network: a directed graph in which the vertices represent tasks or activities and the edges represent precedence relations between tasks.   predecessor (successor): vertex i is a predecessor of vertex j iff there is a directed path from i to j. j is a successor of i.   partial order: a precedence relation which is both transitive (  i, j, k, i  j & j  k → i  k ) and irreflexive (  x  x  x)   acylic graph: a directed graph with no directed cycles

Topological Sorts (3/19)  Definition: Topological order  linear ordering of vertices of a graph   i, j if i is a predecessor of j, then i precedes j in the linear ordering C4, C5, C2, C1, C6, C3, C8, C15, C7, C9, C10, C11, C13, C12, C14 C1, C2, C4, C5, C3, C6, C8, C7, C10, C13, C12, C14, C15, C11, C9

Topological Sorts (4/19)  Topological sort Algorithm for (i = 0; i <n; i++) { if every vertex has a predecessor { fprintf (stderr, “Network has a cycle. \n “ ); exit(1);} pick a vertex v that has no predecessors; output v; delete v and all edges leading out of v from the network; } V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 Topological order generated: v0v0 v3v3 v2v2 v5v5 v1v1 v4v4

Topological Sorts (5/19)  Declarations used in topsort  data representation decide whether a vertex has any predecessors decide a vertex together with all its incident edges V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 the in-degree of that vertex

 Topological sort Program Time complexity: O(n+e) V5V5 V4V4 V3V3 V2V2 V1V1 V0V0 count link headnode node output: v0v3v2v5v1v4 top: 0 j:0k: ptr V0V0 V1V1 V2V2 V3V3 V4V4 V5V5 4

Topological Sorts (7/19)  Activity on Edge (AOE) Networks  AOE is related to the AOV network.  AOE networks have proved very useful for evaluating the performance of many types of projects.  What is the least amount of time in which the project may be complete (assuming there are no cycle in the network)?  Which activities should be speeded to reduce project length?

Topological Sorts (8/19)  An AOE network  directed edge: tasks or activities to be performed  vertex: events which signal the completion of certain activities  number: associated with each edge (activity) is the time required to perform the activity

Topological Sorts (9/19)  Application of AOE Network   Evaluate performance   minimum amount of time   activity whose duration time should be shortened   …   Critical path   a path that has the longest length   minimum time required to complete the project v 0, v 1, v 4, v 6, v 8 or v 0, v 1, v 4, v 7, v 8 (18)

Topological Sorts (10/19)  Critical-path analysis  The purpose of critical-path analysis is to identify critical activities so that resource may be concentrated on these activities in an attempt to reduce a project finish time.  Critical-path analysis can also be carried out with AOV network  Determine Critical Paths  Delete all noncritical activities  Generate all the paths from the start to finish vertex

Topological Sorts (11/19)  Various Factors  The earliest time of an event  The earliest time of an event v i   the length of the longest path from v 0 to v i (Ex. 7 for v 4 )   early(i): earliest event occurrence (Earliest activity) time of i   the earliest start time for all activities responded by edges leaving v i   early(6) = early(7)=7   late(i): latest event occurrence (latest activity) time of i   the latest time the activity may start without increasing the project duration   early(5)=5, late(5)=8   early(7)=7, late(7)=7 =

Topological Sorts (12/19)  Various Factors (cont’d)   late(i)-early(i)   measure of how critical an activity is (ex. late(5)-early(5)=8-5=3)   Critical activity   an activity for which early(i)=late(i) (ex. early(7)=late(7))  earliest[j]: earliest event occurrence time for all event j in the network  latest[j]: latest event occurrence time for all event j in the network  If activity a i is represented by edge  If activity a i is represented by edge  early(i) = earliest[k]  late(i) = latest[i] - duration of activity a i  We compute the times earliest[j] and latest[j] in two stages: a forward stage and a backward stage vkvk vivi aiai

Topological Sorts (13/19)  Calculation of Earliest Times  During the forwarding stage, we start with earliest[0] = 0 and compute the remaining start times using the formula: Where P(j) is the set of immediate predecessors of j Where P(j) is the set of immediate predecessors of j  We can easily obtain an algorithm that does this by inserting the following statement at the end of the else clause in topsort v i1 v i2 v in vjvj forward stage if (earliest[k] duration) earliest[k] = earliest[j] + ptr->duration;

for (ptr=graph[j].link;ptr;ptr=ptr->link){ k=ptr->vertex; graph[k].count--; if(!graph[k].count){ graph[k].count=top; top=k;} if(earliest[k] duration) earliest[k] = earliest[j]+ptr->duration; }  Calculation of Earliest Times (cont’d)

Topological Sorts (15/19)  Calculation of latest times  In the backward stage, we compute the values of latest[i] using a procedure analogous to that used in the forward stage.  We start with latest[n-1] = earliest[n-1] and use the formula: Where S(j) is the set of vertices adjacent from vertex j  Use inverse adjacency list  Insert the following statement at the end of the else clause in topsort backward stage v i1 v i2 v in vjvj if (latest[k] > latest[j] - ptr->duration) latest[k] = latest[j] - ptr->duration;

 Calculation of latest Times (cont’d) for (ptr=graph[j].link;ptr; ptr=ptr->link){ k=ptr->vertex; graph[k].count--; if(!graph[k].count){ graph[k].count=top; top=k;} if(latest[k]> latest[j]-ptr->duration) latest[k] = latest[j]-ptr->duration; }

 Calculation of latest Times (cont’d)

Topological Sorts (18/19)  Non-critical activities deleted  earliest and latest values  early(i) and late(i)  the degree of criticality for each task

Topological Sorts (19/19)  We note that the topsort detects only directed cycles in the network.  There may be other flaws in the network, including  vertices that are not reachable from the start vertex  We can also use critical path analysis to detect this kind of fault in project planning earliest[i] = 0 V0V0 V3V3 V1V1 V5V5 V2V2 V4V4 a0a0 a1a1 a2a2 a3a3 a5a5 a6a6 a4a4