0% found this document useful (0 votes)
45 views29 pages

Design and Analysis of Algorithms Week 11 Lecture 19: Dr. Husnain Mansoor Ali

This document summarizes key algorithms for finding shortest paths in graphs: Dijkstra's algorithm, Bellman-Ford algorithm, and Floyd-Warshall algorithm. It provides pseudocode and analysis of the time complexity of Dijkstra's and Bellman-Ford algorithms. The document also lists common references and sources used for algorithms and data structures.

Uploaded by

Muhammad Ammar
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)
45 views29 pages

Design and Analysis of Algorithms Week 11 Lecture 19: Dr. Husnain Mansoor Ali

This document summarizes key algorithms for finding shortest paths in graphs: Dijkstra's algorithm, Bellman-Ford algorithm, and Floyd-Warshall algorithm. It provides pseudocode and analysis of the time complexity of Dijkstra's and Bellman-Ford algorithms. The document also lists common references and sources used for algorithms and data structures.

Uploaded by

Muhammad Ammar
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/ 29

Design and Analysis of Algorithms

Week 11 Lecture 19

Dr. Husnain Mansoor Ali


Recap
• What is the running time of Floyd-Warshall
Design and Analysis of Algorithms

algorithm?
• What is the type of Floyd-Warshall algorithm?
• What is predecessor matrix?
• Differentiate between single-source shortest
path and all-pair shortest path algorithms

2
Reminder!
• Quiz 2 next week
Design and Analysis of Algorithms

– Wednesday / Thursday

– Please bring 4 empty pages for your solution

– All topics after mid-term exam will be covered


till todays lecture

3
Shortest Paths
• The problem: Given a source vertex s, find the
Design and Analysis of Algorithms

shortest path to all other vertices.


• Data structures needed:
– Graph representation:
• Adjacency lists / adjacency matrix

– Distance table:
• distances from source vertex

• paths from source vertex


4
Design and Analysis of Algorithms
Shortest Paths: Dijkstra

5
Shortest Paths: Dijkstra
• Take a vertex and examine all adjacent vertices
Design and Analysis of Algorithms

• Do the same with each of the adjacent vertices

• For directed weighted graph, adjacency is identified


based on direction

• Weights are included in the graph representation

6
Shortest Paths: Dijkstra
• Distance is not any more the number of edges,
Design and Analysis of Algorithms

instead it is the sum of weights


• Priority queue: the node with the smallest
distance is chosen for processing
• Distance table is updated if newly computed
distance is smaller

7
Design and Analysis of Algorithms
Pseudocode

8
Design and Analysis of Algorithms
Pseudocode

9
Design and Analysis of Algorithms

10
Design and Analysis of Algorithms

11
Design and Analysis of Algorithms

12
Design and Analysis of Algorithms

13
Design and Analysis of Algorithms

14
Design and Analysis of Algorithms

15
Design and Analysis of Algorithms

16
Shortest Paths:
Dijkstra Time Complexity
• Each vertex is stored only once in the queue
Design and Analysis of Algorithms

– O(V)
• DeleteMin operation is
– O( V logV )
• Updating the priority queue
– search and insert: O(log V)
• Performed at most for each edge
– O(E logV)

• O(E logV + V logV) = O((E + V) log(V)) 17


Shortest Paths
• One can also make use of Breadth First Search
Design and Analysis of Algorithms

for Shortest Paths calculation on Undirected


Unweighted graph

– For Undirected weighted graph, use weights for


distance instead of 1

18
Design and Analysis of Algorithms
Relaxation

19
Apply Dijkstra Shortest Path Algo.
Design and Analysis of Algorithms

20
Bellman-ford algorithm
Design and Analysis of Algorithms

• The Bellman-Ford algorithm runs in time O(VE), since


the initialization in line 1 takes θ(V) time, each of the
|V| - 1 passes over the edges in lines 2–4 takes θ (E)
time, and the for loop of lines 5–7 takes O(E) time.
21
Design and Analysis of Algorithms
Bellman-ford algorithm

22
Design and Analysis of Algorithms
Bellman-ford algorithm

23
Design and Analysis of Algorithms
Bellman-ford algorithm

24
Design and Analysis of Algorithms
Floyd-Warshall algorithm

25
Sources used: Books
• Introduction to algorithms
Design and Analysis of Algorithms

– Thomas H. Cormen, Charles E. Leiserson, Ronald L.


Rivest, Clifford Stein

• Introduction to Algorithms
– Jon Kleinberg, Eva Tardos

• Algorithms
– S. Dasgupta, C. H. Papadimitriou, and U. V. Vazirani

26
Sources used: Books
• Algorithms and Data Structures
Design and Analysis of Algorithms

– Kurt Mehlhorn and Peter Sanders

• Analysis of Computer Algorithms


– Aho, Hopcroft, and Ullman

• Art of Computer Programming


– Donald E. Knuth

27
Sources used: Online Links
• Algorithms: Design and Analysis, Part 1
Design and Analysis of Algorithms

– https://www.coursera.org/course/algo
• Algorithms: Design and Analysis, Part 2
– https://www.coursera.org/course/algo2
• Lecture Slides for Algorithm Design
– http://www.cs.princeton.edu/~wayne/kleinberg-
tardos/
• Course Notes - CS 161 - Design and Analysis of
Algorithms
– http://www.ics.uci.edu/~goodrich/teach/cs161/note
s/ 28
Design and Analysis of Algorithms
Sources used:

29

You might also like