0% found this document useful (0 votes)
23 views21 pages

L37 ShortestPath Dijkstra

Dijkstra's algorithm is used to find the shortest path between a source node and all other nodes in a graph. It works by maintaining two sets - S contains nodes whose shortest path from the source is known, and the other set contains the remaining nodes. It repeatedly selects the node with the lowest tentative distance from S and relaxes all edges outgoing from that node. This process continues until all nodes have been examined. The runtime is O(ElogV) where E is the number of edges and V is the number of vertices.

Uploaded by

Akash Sahu
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)
23 views21 pages

L37 ShortestPath Dijkstra

Dijkstra's algorithm is used to find the shortest path between a source node and all other nodes in a graph. It works by maintaining two sets - S contains nodes whose shortest path from the source is known, and the other set contains the remaining nodes. It repeatedly selects the node with the lowest tentative distance from S and relaxes all edges outgoing from that node. This process continues until all nodes have been examined. The runtime is O(ElogV) where E is the number of edges and V is the number of vertices.

Uploaded by

Akash Sahu
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/ 21

Single Source Shortest Path

Dijkstra’s Algorithm

Instructor: Ashok Singh Sairam


Lecture Plan
• Dijkstra’s Algorithm
▪ Algorithm
▪ Example
▪ Complexity
• Linear Programming
▪ System of difference constraints

MA512: Data Structures and Algorithms


2
Dijkstra’s Algorithm
• Single-source shortest path problem:
▪ No negative-weight edges: w(u, v) > 0,  (u, v)  E

• Each edge is relaxed only once! Lower running time


• Maintains two sets of vertices:
V

S V-S
d[v]=δ (s, v) d[v]>δ (s, v)
MA512: Data Structures and Algorithms
3
Dijkstra’s Algorithm: Data structure
• Vertices in V – S reside in a min-priority queue
▪ Keys in Q are estimates of shortest-path weights d[u]

• Repeatedly select a vertex u  V – S, with the


minimum shortest-path estimate d[u]
• Relax all edges leaving u

MA512: Data Structures and Algorithms


4
Dijkstra’s Algorithm

9 Update Q (DECREASE_KEY)

MA512: Data Structures and Algorithms


5
Example (1)
S=<> Q=<s,t,x,z,y> S=<s> Q=<y,t,x,z>
t 1 x t x
1
  
10 
10 9
10 9
2 3 4 6
s 0 2 3 4 6
s 0
5 7
5 7
   
2 5 2
y z y z

MA512: Data Structures and Algorithms


6
Example (2)
t 1 x t 1 x
8
10 
14 8 13
14
10 9 10 9
2 3 4 6 2 3 4 6
s 0 s 0
5 7 5 7
5 
7 5 7
2 2
y z y z

S=<s,y> Q=<z,t,x> S=<s,y,z> Q=<t,x>

MA512: Data Structures and Algorithms


7
Example (3)

S=<s,y,z,t> Q=<x> S=<s,y,z,t,x> Q=<>

t x t 1 x
1
8 13
9 8 9
10 9 10 9

2 4 2 3 4 6
s 0 3 6 s 0

7 5 7
5
5 7 5 7
2 2
y z y z

MA512: Data Structures and Algorithms


8
Dijkstra’s Algorithm: complexity

(V)

(V) O(VlgV)
O(lgV)
O(E) times
(total) O(ElgV)
9 Update Q (DECREASE_KEY) O(lgV)

Running time: O(VlgV + ElgV) = O(ElgV)


MA512: Data Structures and Algorithms
9 9
Exercise

MA512: Data Structures and Algorithms


10
Linear Programming
• Suppose you are given
▪ A matrix A with m rows and n columns
▪ A vector 𝑏 of length m
▪ A vector 𝑐Ԧ of length n
• Find a length-n vector 𝑥Ԧ such that
T
is as large (or small) as possible

such that

MA512: Data Structures and Algorithms


11
LP: Pictorial view
• The matrix inequality: 𝐴𝑥Ԧ ≤ 𝑏

• Each row of A gives coefficients of a linear expression:

• Each row of A along with an entry of b specifies a


linear inequality:
MA512: Data Structures and Algorithms
12
Ex:
• Given directed graph G = (V, E) with capacities ce and cost qe
on edge e. Sending 𝛼 units of flow on edge e costs 𝛼qe.
Find a flow that gets r units of flow from s to t, and minimizes
the cost

MA512: Data Structures and Algorithms


13
Feasibility Problem
• We wish to find any feasible solution
▪ That is any path from s to t that gets r flows. Not
concerned with the cost
• That is we wish to solve
𝐴𝑥Ԧ ≤ 𝑏

MA512: Data Structures and Algorithms


14
System of difference constraints
• Linear programming where each row of A contains
exactly one 1, one –1, and the rest 0’s.
▪ Example

MA512: Data Structures and Algorithms


15
Constraint graph
• Given 𝐴𝑥Ԧ ≤ 𝑏, the corresponding constraint graph is
(transpose of an incidence matrix) a weighted
directed graph G=(V,E) where
▪ Each vertex corresponds to one of the n unknown variables
▪ Each edge corresponds to the m inequalities

MA512: Data Structures and Algorithms


16
Ex: Constraint graph

One feasible solution: x = (-5, -3, 0, -1, -4)


MA512: Data Structures and Algorithms
17
Unsatisfiable constraints
• Theorem. If the constraint graph contains a negative-
weight cycle, then the system of differences is
unsatisfiable.

MA512: Data Structures and Algorithms


18
Satisfying the constraints

MA512: Data Structures and Algorithms


19
MA512: Data Structures and Algorithms
20
Acknowledgement
• Erik D. Demaine and Charles E. Leiserson

MA512: Data Structures and Algorithms


21

You might also like