Professional Documents
Culture Documents
Graph Theory and Applications: Lecture 3: Shortest Path Ta Viet Cuong, PH.D
Graph Theory and Applications: Lecture 3: Shortest Path Ta Viet Cuong, PH.D
Applications
Lecture 3: Shortest Path
2
Review (1)
3
Review (2)
4
More applications
Related to the usage of DFS:
5
Homework 1
Given the following tasks, explain which algorithms to choose between (DFS or BFS or
others)
6
Homework 2a
How many connected components of the following graphs. Given each connected
components, draw its BFS tree, expanding follow the alphabetical order
7
Homework 2b
Draw the DFS tree with the backedges
8
Homework 3:
You are asked to find the bridges of a graph G = <V, E> given it is a dynamic graph:
There is a total N vertices and M edges, the graph is simple and undirected, all of the
operations are valid. Find an feasible algorithm and analyzing its complexity.
9
Web structures
Only linear and sub-linear algorithms can work properly
10
Today
I. Review
II. Finding Shortest Path in Weighted Graph
III. Other approaches
Homework
11
Weighted Graph
Given a graph G = <V, E>, each is associated with
a number l:
Notes:
12
Single Source Shortest Path
Input:
Output:
13
Dijkstra Algorithm
History notes:
14
Dijkstra Algorithm
15
Quiz
A 5 B
2 2
3 C 6 D
4
1 1
E 3 F 4 G 2 H
Dijkstra Algorithm
Verify the correctness of the Dijkstra Algorithm
Proof Sketch:
- Start with s
- Every steps, we add one vertice to
processed list
- The remaining ones always has longer path
length
17
Complexity
Given G have N vertices, M edges
Line 1: O(1)
Line 2: O(N)
Line 3-6: Iterate each edge one time
Line 4: find w*
- There almost N vertices
- O(logN) with priority queue
Total: O((M+N)logN) 18
Shortest Path Variant
We have several variant of shortest path:
- Infinitive graphs:
- When edge weight a small: (using bucket queue)
- We have an algorithm run in:
Brodal, G. S. (2013). A survey on priority queues. In Space-Efficient Data Structures, Streams, and Algorithms (pp. 150-163). Springer, Berlin,
Heidelberg.
19
Graph with Negative Edges
Assume G have negative edges, what wrong with the proof of Dijkstra Algorithm?
20
Today
I. Review
II. Finding Shortest Path in Weighted Graph
III. Other approaches
Homework
21
Negative Cycles
If we allow negative edges, we arrive two options:
22
The Bellman-Ford Algorithm
Find shortest path and detect negative cycles in parallel:
23
Sub-problem
Start from the d(v) in dijkstra, add another dimension: i
24
Sub-problem and update rules
Prove the optimal of the following update rules:
25
Stopping condition and negatives cycles
Given the update rule:
- Init condition:
- We can stop when:
26
Stopping condition and negatives cycles
Given the update rule:
- Init condition:
- We can stop when:
Theorem:
27
Examples
28
Examples
29
Quiz
Which is the running time of The Bellman-Ford Algorithm (choose strongest true
statement):
a) O(n2)
b) O(nm)
c) O(n3)
d) O(mn2)
30
The Floyd-Warshall Algorithm
Given a directed graph G = <V, E>:
31
Sub-problem
Compute : the shortest path length from v to w and used only vertices from 1..k
32
Pseudo-code
Compute : the shortest path length from v to w and used only vertices from 1..k
Quiz:
- Complexity?
- How to detect negative cycle
33
Homework
1. Modify the Dijkstra algorithms to work with the following problem:
Consider a directed graph G = (V, E) with non- negative edge lengths and a starting vertex s.
Define the bottleneck of a path to be the maximum length of one of its edges (as opposed to
the sum of the lengths of its edges). Show how to modify Dijkstra’s algorithm to compute,
for each vertex v in V , the smallest bottleneck of any s-v path. Your algorithm should run in
O(mn) time, where m and n denote the number of edges and vertices, respectively.
34
Homework
2. Given a graph G= <V, E> with negative edges but no negative cycles, and two vertices
s, t. We want to find the shortest path from s to t.
To make the Dijkstra algorithm work with G, we add an large amount C to make negative
edges to positive. Then, apply Dijkstra to find the shortest path.
a) Compare the founded shortest path with the real shortest path. Give an example
b) Which conditions should be hold to make the founded shortest path is the real
shortest path.
35
Homework
3.
In this lectures, we have 2 different algorithms to detect negative cycles in the graph:
36