0% found this document useful (0 votes)
36 views17 pages

Unweighted Shortest Paths Unweighted Shortest Paths Dijkstra's Algorithm

This document discusses algorithms for finding shortest paths in graphs, including: - Dijkstra's algorithm for finding single-source shortest paths in weighted graphs. It runs in O(|E|+|V|log|V|) time using a priority queue. - Breadth-first search for finding shortest unweighted paths from a source, with an improved O(|E|+|V|) time using a queue. - Negative edge costs can cause problems for Dijkstra's algorithm. A combination of weighted and unweighted approaches is needed. - Shortest paths in acyclic graphs can be found in O(|V|+|E|) time by processing vertices topologically

Uploaded by

ilia360
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views17 pages

Unweighted Shortest Paths Unweighted Shortest Paths Dijkstra's Algorithm

This document discusses algorithms for finding shortest paths in graphs, including: - Dijkstra's algorithm for finding single-source shortest paths in weighted graphs. It runs in O(|E|+|V|log|V|) time using a priority queue. - Breadth-first search for finding shortest unweighted paths from a source, with an improved O(|E|+|V|) time using a queue. - Negative edge costs can cause problems for Dijkstra's algorithm. A combination of weighted and unweighted approaches is needed. - Shortest paths in acyclic graphs can be found in O(|V|+|E|) time by processing vertices topologically

Uploaded by

ilia360
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Unweighted Shortest Paths Dijkstras Algorithm

CS 202 Data Structures and Algorithms

Izmir University of Economics

Announcement
Lab exam will take place on May 17 2011 17, Tuesday, at 6:30pm

Izmir University of Economics

Shortest-Path Algorithms
Th i The input i a weighted graph: associated t is i ht d h i t d with each edge (vi, vj) is a cost ci,j. The cost of a path v1v2...vN is ci,i+1 for i in v [1..N-1]. This is weighted path length, the unweighted path length on the other hand is merely the number of edges on p , y, the path, namely, N-1. Single-source Shortest-Path Problem: p g graph ( , ), Given as input a weighted g p G=(V, E), and a distinguished vertex, s, find the shortest weighted path from s to every other vertex in G th t i G.
Izmir University of Economics 3

Negative Cost Cycles


I the graph to the l ft th shortest path from v1 t v6 h a In th h t th left, the h t t th f to has cost of 6 and the path itself is v1v4v7v6. The shortest unweighted p g path has 2 edges. g In the graph to the right, we have a negative cost. The path from v5 to v4 has cost 1, but a shorter path exists by following the loop v5v4v2v5v4 which has cost -5. This path is still not the 5 shortest, because we could stay in the loop arbitrarily long.

Izmir University of Economics

Unweighted Shortest Paths g


Using some vertex, s, which is an input parameter, find the shortest path from s to all other vertices i an unweighted graph. A th ti in i ht d h Assume s=v3.

Izmir University of Economics

Unweighted Shortest Paths


Algorithm: find vertices that are at distance 1, 2, ... N-1 by processing vertices in layers (breadth-first search)

Izmir University of Economics

Unweighted Shortest Paths

Izmir University of Economics

Unweighted Shortest Paths

Complexity O(|V|2)
Izmir University of Economics 8

Unweighted Shortest Paths - Improvement At any point in time there y yp are only two types of unknown vertices that have dv. Some have dv = currDist and the rest have dv = currDist +1. We can make use of a queue data structure structure. O(|E|+|V|)
Izmir University of Economics 9

Weighted Shortest Path Dijkstras Algorithm


With weighted shortest path,distance dv is tentative. It turns out to be the shortest path length from s to v using only known vertices as intermediates. Greedy algorithm: proceeds in stages doing the best at each stage. Dijkstras algorithm selects a vertex v with smallest dv among all unknown vertices and declares it known Remainder of the known. stage consists of updating the values dw for all edges (v w) (v, w).
Izmir University of Economics 10

Dijkstras Algorithm - Example

Izmir University of Economics

11

Dijkstra s Dijkstras Algorithm - Example

A proof by contradiction will show that this algorithm always works as long as no edge has a negative cost cost.
Izmir University of Economics 12

Dijkstras Algorithm - Pseudocode


If the vertices a e t e e t ces are sequentially scanned to find minimum dv, each phase will take O(|V|) to find the minimum, thus O(|V|2) over the course of the algorithm. The time for updates is constant and at most one update per edge for a t t l of O(|E|). total f O(|E|) Therefore the total time spent is O(|V|2+|E|). If the graph is dense, OPTIMAL.

Izmir University of Economics

13

Dijkstras Algorithm-What if the graph is sparse?


If the graph is sparse |E|=(|V|), algorithm is too |E| (|V|), slow. The distances of vertices need to be kept in a priority queue. Selection of vertex with minimum distance via deleteMin, and updates via decreaseKey operation. Hence; O(|E|log|V|+|V|log|V|) find operations are not supported, so you need to b able to maintain l be bl i i locations of di i the h i f in h heap and update them as they change. Alt Alternative: i ti insert w and dw with every update. t d ith d t
Izmir University of Economics 14

Graphs with negative edge costs


Dijkstras algorithm does not work with Dijkstra s negative edge costs. Once a vertex u is known, known it is possible that from some other unknown vertex v, there is a path back to u that is very negative negative. Algorithm: A combination of weighted and unweighted algorithms Forget about the algorithms. concept of known vertices.
Izmir University of Economics 15

Graphs with negative edge costs - I


O(|E|*|V|) Each vertex O(|E| |V|). can dequeue at most O(|V|) times. (Why? (| |) ( y Algorithm computes shortest paths with at most 0, 1, ..., |V|-1 edges in this order). Hence, the result! If negative cost cycles, then each vertex should be checked to have been dequeued at most | | q |V| times.

Izmir University of Economics

16

Acyclic Graphs
If the graph is known to be acyclic, the order in which vertices are declared known, can be set to be the topological order. Running time = O(|V|+|E|) This selection rule works because when a vertex is selected, its distance can no longer be lowered, since by topological lowered ordering rule it has no incoming edges emanating from unknown nodes nodes.
Izmir University of Economics 17

You might also like