Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

Graph Theory and

Applications
Lecture 3: Shortest Path

Ta Viet Cuong, Ph.D


HMI laboratory, FIT-UET
Today
I. Review
II. Finding Shortest Path in Weighted Graph
III. Open problems
Homework

2
Review (1)

3
Review (2)

4
More applications
Related to the usage of DFS:

- Finding Cut-Vertex and Biconnected-graph


- Finding Strongly Connected Component in Directed Graph
- Finding topology-sort in DAG

5
Homework 1
Given the following tasks, explain which algorithms to choose between (DFS or BFS or
others)

1. Find the order of learning subjects between each semester


2. It’s heavy raining, you want to go to school
3. Playing chess

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 three operators in total:

1. Add v: add a vertex with name v


2. Add u v: add an edge between u and v
3. Query: return how many bridges in the 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:

- u, v: could be directed or undirected


- : however, we work with l non-negative
- G could be stored by matrix or adjacency lists

12
Single Source Shortest Path
Input:

- a graph G = <V, E>, each edge is associated with


a non-negative length l
- a source vertex: s

Output:

Applications: many, for example OSPF of Internet


Protocol

13
Dijkstra Algorithm
History notes:

- Founded by Edsger W.Dijkstra


- Paper: DIJKSTRA, E W. A note on two problems in
connexion with graphs. Numer. Math. 1 (1959), 269-
271.
- Citations: 31113. One of the most popular paper in
Computer Science

14
Dijkstra Algorithm

15
Quiz

❖ Find shortest path form A to H

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?

Could you find graph-based applications which contain negative edges

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:

- Allow the repeating vertices: we could have a -INFINITY length path


- Not Allow repeating vertices: there should exist a valid path
- However, if there are negative cycles, it’s a NP-hard problem

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

L(v, i) - the shortest path from s to v with at most i edges

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>:

- Find the shortest path dist(v, w), for each v, w in V


- Or G contain a negative cycle

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:

- Compare the 2 versions.


- Could you find any more efficient algorithms to detect negative cycles ?

36

You might also like