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

Single - Source - Shortest - Path

The document discusses Dijkstra's algorithm, which is a greedy algorithm for solving the single-source shortest path problem in a weighted graph. It finds the shortest paths between a source node and all other nodes in the graph. The algorithm uses a priority queue to keep track of tentative distances and iterates through the graph, relaxing edges and updating distances until it has explored the entire graph.

Uploaded by

Iftakhar Utsho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views16 pages

Single - Source - Shortest - Path

The document discusses Dijkstra's algorithm, which is a greedy algorithm for solving the single-source shortest path problem in a weighted graph. It finds the shortest paths between a source node and all other nodes in the graph. The algorithm uses a priority queue to keep track of tentative distances and iterates through the graph, relaxing edges and updating distances until it has explored the entire graph.

Uploaded by

Iftakhar Utsho
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 16

Shortest Paths Algorithms

 An algorithm that is designed essentially


to find a path of minimum length between
two specified vertices of a connected
weighted graph.

1
Shortest Paths Algorithms

2
Single source Shortest path algorithm

3
Dijkstra’s Algorithm

4
Introduction
• Dijkstra’s algorithm is a greedy
algorithm for solving single source
shortest path problem that provide us
with the shortest path from one particular
source node to all other nodes in the
given graph .

5
6
Rules of Dijkstra’s algorithm
• It can be applied on both directed and undirected graph.
• It is applied on weighted graph.
• For the algorithm to be applicable the graph must be connected.
• Dijkstra’s algorithm does not work for graphs with negative
weight edges. For graphs with negative weight edges,
Bellman_ford algorithm can be used.
• In Dijkstra’s algorithm always assign source vertex to zero
distance.
• Assign every other node a tentative distance.

7
Weight Matrix

Suppose G is a weighted and simple directed graph with m nodes. The weight

matrix W =(wij) of the graph G is the m x m matrix defined as follows:

wij = w(e) if there is a weighted edge from vi to vj

0 otherwise

Example:
7
4
R U R S T U
5 7 2 1 W= R 7 5 0 0
S 7 0 0 2
S T
3 T 0 3 0 0
U 4 0 1 0

Figure: Weighted Graph and Its Weighted Matrix 8


Algorithm: Dijkstra(G, w, s)
Here,
1. Initialize-Single-Source(G, s)
G = Given Graph
2. S := Ø
S = Source Vertex
3. Q := V[G]
4. While Q = Ø Adj[v] = Adjacent Vertices of u
5. do u = Extract-Min(Q) d[v] = Weight from s to v
6. S := S U u Π[v] = Predecessor Vertex of v
7. for each vertex v є Adj[u]
8. do Relax(u, v, w)
9. Exit

Initailize-Single-Source(G,s) Relax(u, v, w)
1. for each vertex v є V[G] 1. If d[v] > d[u] + w(u,v)
2. do d[v] := 2. then d[v] := d[u] + w(u,v)

3. Π[v] := NIL 3. Π[v] := u
4. d[s] := 0 4. exit
5. exit
9
Example:
u v u v
1 1
∞ ∞
s 10 9 s 10 9
2 3 2 3
4 6 0 4 6
7 7
5 5

x 2 y x ∞ 2 y
(a) Given weighted, directed graph (b) Initialize d[v] and S = Ø

u v u v
1 1
10 8 14

s 10 9 s 10 9
2 3 2 3
0 4 6 0 4 6
7 7
5 5 5 5 7

x 2 y x 2 y

(c) Consider vertex s with d[s]=0 and S={s} (d) Consider vertex x with d[x]=5 and S={s,x}

10
u v u v
1 1
8 13 8 9
s 10 9 s 10 9
2 3 2 3
0 4 6 0 4 6
7 7
5 5 7 5 5 7
x 2 y x 2 y

(e) Consider vertex y with d[y]=7 and S={s,x,y} (f) Consider vertex u with d[u]=8 and S={s,x,y,u}

u v u v
1 1
8 9 8 9
s 10 9 s 10
2 2 9
3 6 3
0 4 0 4 6
7 7
5 5 7 5 5 7
x 2 y x 2 y

(g) Consider vertex v with d[v]=9 and S={s,x,y,u,v} Figure: Result of Dijkstra’s Algorithm

11
12
13
14
15
END!!!

16

You might also like