0% found this document useful (0 votes)
131 views16 pages

Single Source Shortest Path

Uploaded by

co22352
Copyright
© © All Rights Reserved
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)
131 views16 pages

Single Source Shortest Path

Uploaded by

co22352
Copyright
© © All Rights Reserved
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/ 16

Single-Source Shortest Path Problem

Single-Source Shortest Path Problem - The


problem of finding shortest paths from a source
vertex v to all other vertices in the graph.
Dijkstra's algorithm
Dijkstra's algorithm - is a solution to the single-source
shortest path problem in graph theory.

Works on both directed and undirected graphs. However,


all edges must have nonnegative weights.

Input: Weighted graph G={E,V} and source vertex v∈V,


such that all edge weights are nonnegative

Output: Lengths of shortest paths (or the shortest paths


themselves) from a given source vertex v∈V to all other
vertices
Approach
• The algorithm computes for each vertex u the distance to u from
the start vertex v, that is, the weight of a shortest path between v
and u.

• The algorithm keeps track of the set of vertices for which the
distance has been computed, called the cloud C

• Every vertex has a label D associated with it. For any vertex u, D[u]
stores an approximation of the distance between v and u. The
algorithm will update a D[u] value when it finds a shorter path from
v to u.

• When a vertex u is added to the cloud, its label D[u] is equal to the
actual (final) distance between the starting vertex v and vertex u.
3
Example
Example
Example
Example
Example
Example
Example
Example
Example
Example

Distance(source) = 0 ∞ Distance (all vertices


0 A
2
B but source) = ∞

4 1 3 10

2 2 ∞
∞ C D E

5 8 ∞ 4 6

1
F G

∞ ∞

Pick vertex in List with minimum distance.

13
Dijkstra’s Algorithm
• Graph G, weight function w, root s

relaxing
edges

14
Time Complexity: Using List
The simplest implementation of the Dijkstra's algorithm
stores vertices in an ordinary linked list or array
– Good for dense graphs (many edges)

• |V| vertices and |E| edges


• Initialization O(|V|)
• While loop O(|V|)
– Find and remove min distance vertices O(|V|)
• Potentially |E| updates
• Update costs O(1)

Total time O(|V2| + |E|) = O(|V2| )


Time Complexity: Priority Queue
For sparse graphs, (i.e. graphs with much less than |V2| edges)
Dijkstra's implemented more efficiently by priority queue

• Initialization O(|V|) using O(|V|) buildHeap


• While loop O(|V|)
• Find and remove min distance vertices O(log |V|) using O(log |V|)
deleteMin

• Potentially |E| updates


• Update costs O(log |V|) using decreaseKey

Total time O(|V|log|V| + |E|log|V|) = O(|E|log|V|)


• |V| = O(|E|) assuming a connected graph
16

You might also like