Dijkstra’s Algorithm Supervisor: Dr.Franek Ritu Kamboj 0502560.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
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.
Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
CSCE 411H Design and Analysis of Algorithms Set 8: Greedy Algorithms Prof. Evdokia Nikolova* Spring 2013 CSCE 411H, Spring 2013: Set 8 1 * Slides adapted.
October 31, Algorithms and Data Structures Lecture XIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Data Structures and Algorithms Graphs Single-Source Shortest Paths (Dijkstra’s Algorithm) PLSD210(ii)
1 Theory I Algorithm Design and Analysis (10 - Shortest paths in graphs) T. Lauer.
chapter Single-Source Shortest Paths Problem Definition Shortest paths and Relaxation Dijkstra’s algorithm (can be viewed as a greedy algorithm)
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.
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.
Minimum Spanning Tree Algorithms
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.1 Data Structure and Algorithm Lecture 11 Application of BFS  Shortest Path Topics Reference: Introduction to Algorithm by Cormen Chapter 25: Single-Source.
1 8-ShortestPaths Shortest Paths in a Graph Fundamental Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
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.
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.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
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.
Shortest Paths Introduction to Algorithms Shortest Paths CSE 680 Prof. Roger Crawfis.
1 Shortest Path Calculations in Graphs Prof. S. M. Lee Department of Computer Science.
Theory of Computing Lecture 7 MAS 714 Hartmut Klauck.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
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.
Week -7-8 Topic - Graph Algorithms CSE – 5311 Prepared by:- Sushruth Puttaswamy Lekhendro Lisham.
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.
Data Structures Week 9 Towards Weighted BFS So, far we have measured d s (v) in terms of number of edges in the path from s to v. Equivalent to assuming.
Graph Algorithms. Definitions and Representation An undirected graph G is a pair (V,E), where V is a finite set of points called vertices and E is a finite.
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.
Shortest Path in Weighted Graph : Dijkstra’s Algorithm.
Kruskal’s and Dijkstra’s Algorithm.  Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connected weighted.
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)
CSCE 411 Design and Analysis of Algorithms Set 9: More Graph Algorithms Prof. Jennifer Welch Spring 2012 CSCE 411, Spring 2012: Set 9 1.
Single Source Shortest Paths Chapter 24 CSc 4520/6520 Fall 2013 Slides adapted from George Bebis, University of Reno, Nevada.
1 Chapter 6 : Graph – Part 2 교수 : 이상환 강의실 : 113,118 호, 324 호 연구실 : 과학관 204 호 Home :
Shortest Path -Prim’s -Djikstra’s. PRIM’s - Minimum Spanning Tree -A spanning tree of a graph is a tree that has all the vertices of the graph connected.
Graphs – Part III CS 367 – Introduction to Data Structures.
Algorithms and Data Structures Lecture XIII
CS 3343: Analysis of Algorithms
Shortest Path Problems
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Announcement 2: A 2 hour midterm (open book) will be given on March (Tuesday) during the lecture time. 2018/12/4.
Lecture 11 Topics Application of BFS Shortest Path
Algorithms and Data Structures Lecture XIII
Shortest Path Problems
Minimum Spanning Tree Algorithms
Algorithms Searching in a Graph.
Shortest Path Problems
Graph Algorithms: Shortest Path
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 .
More Graphs Lecture 19 CS2110 – Fall 2009.
Presentation transcript:

Dijkstra’s Algorithm Supervisor: Dr.Franek Ritu Kamboj

Dijkstra’s Algorithm (Introduction) It is named after a Dutch Computer Scientist Edsger Dijkstra. This algorithm is used for solving the single- source shortest path problem. The input of the algorithm consists of a weighted directed graph G and a source vertex s in G. G is a graph with nonnegative edge weights. We will denote V the set of all vertices in the graph G. Each edge of the graph is an ordered pair of vertices (u,v) representing a connection from vertex u to vertex v. The set of all edges is denoted E. Weights of edges are given by a weight function w: E -> [0, ∞]; therefore w(u,v) is the non-negative cost of moving from vertex ‘u’ to vertex ‘v’. The cost of an edge is the distance between those two vertices. For a given pair of vertices s and t in V, the algorithm finds the path from s to t with lowest cost (i.e. the shortest path).

This Algorithm is used to calculate the shortest path from a starting node to all other nodes of a directed graph. To do this the direct path from the starting node to the separate nodes is noted as the shortest path. In the following steps the cheapest not yet visited node is chosen and it is checked if there is a node that can be reached from there with lower costs than before. In the end one has the cheapest path from the starting node to all other as long as all edges have a positive weight. Brief Description … s t r p q 18

Application…  The algorithm works by keeping for each vertex ‘v’the cost ‘d[v]’ of the shortest path found so far between s and v. Initially, this value is 0 for the source vertex s (d[s]=0), and infinity for all other vertices, representing the fact that we do not know any path leading to those vertices (d[v]=∞ for every v in V, except s). When the algorithm finishes, d[v] will be the cost of the shortest path from s to v or infinity, if no such path exists.  The basic operation of Dijkstra's algorithm is edge relaxation: if there is an edge from u to v, then the shortest known path from s to u (d[u]) can be extended to a path from s to v by adding edge (u,v) at the end. This path will have length d[u] + w(u,v). If this is less than the current d[v], we can replace the current value of d[v] with the new value.

Edge relaxation is applied until all values d[v] represent the cost of the shortest path from s to v. The algorithm is organized so that each edge (u,v) is relaxed only once, when d[u] has reached its final value.  The algorithm maintains two sets of vertices S and Q. Set S contains all vertices for which we know that the value d[v] is already the cost of the shortest path and set Q contains all other vertices. Set S starts empty, and in each step one vertex is moved from Q to S. This vertex is chosen as the vertex with lowest value of d[u]. When a vertex u is moved to S, the algorithm relaxes every outgoing edge (u,v). Contd….

Algorithm : DIJKSTRA(G,w,s) 1. INITIALIZE-SINGLE-SOURCE (G,s) 2.S  Ǿ 3.Q  V[G] 4.While Q != Ǿ 5. do u  EXTRACT-MIN (Q) 6. S  S U {u} 7. for each vertex v € Adj[u] 8. do RELAX ( u,v,w)

INITIALIZE-SINGLE-SOURCE(G,s) 1. for each vertex v element V[G] 2. do d[v]  ∞ 3. Π [v]  NIL 4. d[s]  0 RELAX(u,v,w) 1. if d[v] > d[u] + w(u,v) 2. then d[v]  d[u] +w(u,v) 3. Π [v]  u Contd …

Running time: The Running time of Dijkstra’s algorithm depends on how the min-priority queue is implemented. The min-priority queue uses 3 operations : INSERT EXTRACT-MIN DECREASE KEY Each INSERT and DECREASE KEY operation takes O(1) time, and EXTRACT-MIN operation is simply a linear search through all vertices in Q. In this case, the running time is O(n 2 ). For sparse graphs, that is, graphs with much less than n 2 edges, Dijkstra’s algorithm can be implemented more efficiently by storing the graph in the form of adjacency lists and using a binary heap or Fibonacci heap as a priority queue to implement the Extract-Min function. With a binary heap, the algorithm requires O((m+n)log n) time, and the Fibonacci heap improves this to O(m + n log n).adjacency listsbinary heapFibonacci heappriority queue

Example: ∞ ∞ ∞∞ s y z x t ∞ ∞

Contd…

s y z x t Contd…

AdjSTXYZ S105 T 12 X 4 Y 39 2 Z7 6 For the example we considered,this will be the adjacency matrix (adj), this matrix stores the edge weights between two vertices(it stores –1 otherwise ): Backtracking: We have a table of direct distances from every node to each other, we will find the shortest path from the source node to every other node in the graph, regardless of how many intermediate nodes it encounters. NOTE : The values should be read as the distance from the node at the left side of the table to the value at the top of the table.

We will use the table adj to construct a new table, BT, one row at a time. Each successive row is constructed using values from the previously constructed row of BT and the values of adj. Since our source node is s, the initial row of BT, call it BT 0, is simply the same as the first row of adj. Successive rows of BT will be computed on each iteration,the k th iteration consists of selecting a node v from the candidate set (initially C = {txyz}) and moving it to the source set (initially S = {s}); then computing BT k from BT (k-1) and adj according to the equation : BT i (w) = min(BT i-1 (w), BT i-1 (v) + adj(v,w)) The selection of w involves finding the node still in C that is closest to s according to the current row of BT.

Initially, we have an S = {s} and C = {txyz}. BT 0 looks like (I'll use blue color to highlight values that correspond to nodes in C): We select the node w for iteration 1 by finding the element of C (blue in the table) with smallest value in the current table BT; here, w = Y : We next compute the second row of BT according to the equation given before, then remove w from C and add it to S, leaving C = {txz}, S = {sy}, and BT looking like: BTSTXYZ BT BTSTXYZ BT 0 105

BTSTXYZ BT BT Notice how the second and fifth entries have changed. We can now repeat the process, selecting w = Z (the smallest value in BT 1 that corresponds to a node still in C). Updating, we get C = {tx}, S = {syz} and BT: BT STXYZ BT BT BT Now w=T

S-{syzt} and C={x} The algorithm terminates here, since there is only one node remaining in C, and since it is the furthest from the source node, no path through this distant node to any other node could possibly be shorter than the best known path. BT STXYZ BT BT BT BT

The shortest path can be traced by starting at the desired node's value in the last row of BT and going up the column until the value changes. When the value changes, we check to see what w caused this value to change, and add w to the series. We trace the path backwards. For example : To trace the shortest path from S to X: 1.start with values in BT 3 and find the value for X. 2.the value is 9. 3.going up the column, we see that 9 changed from 8 when w = T. 4.tracing back from 2nd entry, we see that 8 changed from 10 when w = Y. 5.tracing back from the 2nd entry, we see its value was fixed in BT 0.

Important Aspects 1. The first thing I had to do was to programmatically create a graph with vertices connected by weighed edges. 2.I chose data structures that I have experience with, that I found easy to use, and have reasonable run times. 3.I had to decide how to input graph (I chose to input from a file rather than hard code it or do user defined input. This approach allowed me to save more time in the long run when testing) 4.I had to make some assumptions I assumed a maximum edge size of I assumed there was always at least one path to every vertex from the source. I assumed that edge lengths would not be zero I assumed there was no edge from a vertex to itself

S- list of vertices that we have already explored, and don’t want to explore again to avoid redundancy. V- it’s just the list of all vertices, once filled it doesn’t changes further. Adj – it’s a matrix that stores the weight of edges, and if there is no edge between 2 vertices it stores –1. Vlist – an array that stores distance from source vertex to all other vertices. BT- a matrix which will be used to find the paths from a node to a vertex. Brief description of methods & Data Structures: Data Structures :-

Main– It is the main function body, which calls other functions. ReadFile – It reads the file, which has the graph details. The file looks like this abcd / ab4 bc2 cd5 da5 Functions : -

LoadVertices (String)- It discovers all the vertices and stores them in ‘V’. LoadEdges(String)-It discovers all the edges between two vertices and stores them in ‘Adj’. Dijkstra(String)- Executes the minimum weighed path from source vertex to other vertices in the graph. printVList()- It simply prints the contents in Vlist(), which now contains the minimum weighed path. Relax (String)-It tests whether we can improve the shortest path to a node found so far, if yes it updates Vlist[] ExtractMin ()- it returns the minimum weight out of the two compared edges. Backtracking()- it will find out the Shortest path, as described just now. Continued….

Weak Points : Using an adjacency matrix, the implementation uses more memory than if I were to use an adjacency list. If a graph exists with an edge weight greater than , then my implementation fails. If there is no existing path from the source vertex to one of the other vertices, then my implementation fails. If there are negative length edges in the graph, then my implementation fails.

Thank you!!!