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.

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
 2004 SDU Lecture9- Single-Source Shortest Paths 1.Related Notions and Variants of Shortest Paths Problems 2.Properties of Shortest Paths and Relaxation.
Sequential Timing Optimization. Long path timing constraints Data must not reach destination FF too late s i + d(i,j) + T setup  s j + P s i s j d(i,j)
1 Greedy 2 Jose Rolim University of Geneva. Algorithmique Greedy 2Jose Rolim2 Examples Greedy  Minimum Spanning Trees  Shortest Paths Dijkstra.
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 Path Problems
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
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.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Graph Algorithms: Shortest Path We are given a weighted, directed graph G = (V, E), with weight function w: E R mapping.
Shortest path algorithm. Introduction 4 The graphs we have seen so far have edges that are unweighted. 4 Many graph situations involve weighted edges.
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.
Analysis of Algorithms CS 477/677 Shortest Paths Instructor: George Bebis Chapter 24.
CS 253: Algorithms Chapter 24 Shortest Paths Credit: Dr. George Bebis.
1 Shortest Path Algorithms. 2 Routing Algorithms Shortest path routing What is a shortest path? –Minimum number of hops? –Minimum distance? There is a.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 15 Shortest paths algorithms Properties of shortest paths Bellman-Ford algorithm.
CS 473 All Pairs Shortest Paths1 CS473 – Algorithms I All Pairs Shortest Paths.
TECH Computer Science Graph Optimization Problems and Greedy Algorithms Greedy Algorithms  // Make the best choice now! Optimization Problems  Minimizing.
Shortest Paths Introduction to Algorithms Shortest Paths CSE 680 Prof. Roger Crawfis.
Dijkstra's algorithm.
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.
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 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 Shortest Path Problems How can we find the shortest route between two points on a road map? Model the problem as a graph problem: –Road map is a weighted.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
COSC 2007 Data Structures II Chapter 14 Graphs III.
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
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.
Dijkstra’s Algorithm Supervisor: Dr.Franek Ritu Kamboj
Introduction to Algorithms Jiafen Liu Sept
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.
Data Structures & Algorithms Shortest Paths Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Lecture 13 Algorithm Analysis
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
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])
TIRGUL 10 Dijkstra’s algorithm Bellman-Ford Algorithm 1.
CSE 373: Data Structures and Algorithms Lecture 21: Graphs V 1.
Shortest Paths.
CSC317 Shortest path algorithms
Introduction to Graphs
Shortest Path Problems
Shortest Path Problems
Analysis of Algorithms
Shortest Paths.
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Advanced Algorithms Analysis and Design
Lecture 13 Algorithm Analysis
Lecture 13 Algorithm Analysis
Shortest Path Problems
Minimum Spanning Tree Algorithms
Lecture 13 Algorithm Analysis
Slide Courtesy: Uwash, UT
Lecture 13 Algorithm Analysis
Fundamental Data Structures and Algorithms
Shortest Path Problems
Shortest Path Problems
Slide Courtesy: Uwash, UT
Shortest Path Problems
Graph Algorithms: Shortest Path
Shortest Paths.
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:

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 suit and Tel-Aviv ’ s bus trails map, and go on bus 405 to the central station of Tel-Aviv. The bus ride inside the city costs 1 nis per station. How will you find the cheapest way from the central station to the beach?

Unweighted Graphs- cont. BFS finds shortest paths from a single source (i.e – the central station) in an unweighted graph. How much will you pay? n nis, when n is the number of stations you passed in your way. Can you think of an algorithm that finds a single shortest path, and always works better then BFS? No such algorithm is known.

Weighted Graphs Now assume that you pay for the bus ride between stations according to the distance between the stations. That is – every ‘edge’ is the bus trails map has a different price ( = weight). Total payment = sum over the costs between the stations on the way. Will BFS work? No – BFS counts the number of edges on the path, but does not refer to the edges weights.

Other Versions of Shortest Paths If we know how to find shortest paths from a single source, we can also find: A shortest path between a pair of vertices The shortest paths from all vertices to a single source. How? By reversing the direction of the edges in the graph Shortest paths between every pair of vertices. How? By running the sssp from every vertex (there are more efficient solutions)

Observations Observation 1:When can negative weights become a problem? When there is a circle with negative weight, reduce the weight of the path by repeating the circle over and over. Solution: either require non-negative weights, or identify and report circles with negative weights

Observations – cont. Observation 2: Let u ~ y ~ z ~ v be the shortest path between u and v. Is y ~ z optimal, too? Yes! If there was a shorter path between y and z, we could use it to shorten the path between u and v  contradiction This property suggests that we can use greedy algorithms to find the shortest path

Relaxation The Idea: Build a shortest paths tree rooted in S. For every vertex, v, keep a value, d[v], of the shortest path from s that is currently known. The general algorithm scheme: -Initialization: d[v] = ∞, d[s] = 0 -In every iteration of the algorithm we check if we can do relaxation – that is, find a shorter path from s to a vertex v then the path currently known. We will learn two algorithms: -Dijkstra – all weights are non-negative -Belman-Ford – identifies circles with negative weight

Dijkstra The idea: Maintain a set of vertices whose final shortest path weights from s have already been determined How will we choose the next vertex to add to the set? We will take the vertex u with the minimal d value. This is also the ‘real’ value of the shortest path. What relaxation can be done? We can check all the edges leaving u.

Dijkstra Run Time How can we keep the vertices, so we can easily find the next vertex to insert? We need to extract the minimal d value in each iteration, so a binary heap is a good choice. Run time alalysis : –Build heap takes O(V). –Extract-Min O(logV). –Altogether O(VlogV). –Going over the adjacent list O(E). –Relaxation of values in the heap O(logV). –Altogether O(ElogV). Total run-time compexity: O(ElogV+VlogV) = O(ElogV)

Bellman-Ford’s Algorithm Now we want to identify negative circles. Assume that every iteration we do relaxation from all the edges. What edges might be relaxed on iteration i? The edges that have a path with I edges from s (shorter paths – updated in previous iterations) What is the maximum number of edges in any shortest path from s? If there are no circles – the number of edges cannot exceed |V| - 1. The longest path without circles goes through every vertex exactly once, therefore contained of |V| - 1 edges.

Bellman-Ford’s Algorithm So – how many iterations are needed if there are no negative circles? |V| - 1. After the |V| - 1 iteration, all d values will be the lengths of the shortest paths. And how can we identify negative circles? By running the |V| iteration – if we can find a relaxation, then there is a negative circle in the graph

Bellman-Ford’s Algorithm

Bellman-Ford Run Time The algorithm run time is: –Goes over v-1 vertexes, O(V) –For each vertex relaxation over E, O(E) –Altogether O(VE)

Application of Bellman-Ford Linear programming problems – find an optimal value (min / max) of a linear function of n variables, with linear contraints. A special case: Set of difference constraints

Application of Bellman-Ford There are many uses for a set of difference constraints, for instance: –The variables can represent the times of different events –The inequalities are the constraints over there synchronization. The set of linear inequalities can also be expressed in matrix notation: A · x  b

Application of Bellman-Ford A ·x  b

Application of Bellman-Ford What is the connection between the Bellman-Ford algorithm and a set of linear inequalities? We can interpret the problem as a directed graph. The graph is called the constraint graph of the problem After constructing the graph, we could use the Bellman- Ford algorithm. The result of the Bellman-Ford algorithm is the vector x that solves the set of inequalities.

Application of Bellman-Ford Building the constraint graph out of matrix A: –Each variable represents a vertex (node) –Each constraint represents an edge –If edge i goes out of vertex j than A[i,j]= -1 –If edge i goes into vertex j than A[i,j]= 1 –Otherwise A[i,j]= 0 –Each row contains a single ‘1’, a single ‘-1’ and zeros.

Application of Bellman-Ford The problem is represented by the graph: v1v1 v5v5 v4v4 v3v3 v2v2

Application of Bellman-Ford How can we extend the constraint graph to a single- source-shortest-paths problem? By adding vertex v 0 that directs at all the other vertices. Weight all edges from v 0 as zero. The weight of the other edges is determined by the inequality constraints.

Application of Bellman-Ford Formally: Each node v i corresponds to a variable x i in the original problem, and an extra node - v 0 (will be s). Each edge is a constraint, except for edges (v 0,v i ) that were added. Assign weights:

Application of Bellman-Ford The extended constraint graph: v0v0 v1v1 v5v5 v4v4 v3v3 v2v

Application of Bellman-Ford The solution: v1v1 v5v5 v3v3 v2v v0v0 v4v

Application of Bellman-Ford The Bellman-Ford solution for the extended constraint graph is a set of values which meets the constraints. Formally: Why is this correct?

Application of Bellman-Ford Because

Application of Bellman-Ford If there is a negative cycle reachable from v 0 – there are no feasible solutions: Suppose the cycle is where v k =v 1 v 0 cannot be on it ( it has no incoming edges) This cycle corresponds to :

Application of Bellman-Ford The left side sums to 0. The right side sums to the cycle’s weight w(c) We get But we assumed the cycle was negative…