0% found this document useful (0 votes)
59 views20 pages

Design and Analysis of Algorithm: Graphs

The document discusses shortest path algorithms for graphs. It begins with an overview of the shortest path problem and the all-pairs shortest paths problem. It then describes the Floyd-Warshall algorithm for finding shortest paths between all pairs of vertices in a weighted graph. The algorithm uses matrix representation and iterates through intermediates to find new shorter paths. It runs in O(n3) time and also tracks predecessors to reconstruct actual shortest paths. An example run on a sample graph is provided.

Uploaded by

Rj Khan
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)
59 views20 pages

Design and Analysis of Algorithm: Graphs

The document discusses shortest path algorithms for graphs. It begins with an overview of the shortest path problem and the all-pairs shortest paths problem. It then describes the Floyd-Warshall algorithm for finding shortest paths between all pairs of vertices in a weighted graph. The algorithm uses matrix representation and iterates through intermediates to find new shorter paths. It runs in O(n3) time and also tracks predecessors to reconstruct actual shortest paths. An example run on a sample graph is provided.

Uploaded by

Rj Khan
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/ 20

Design and Analysis of Algorithm

Lecture 32
Graphs

Sahar Ajmal
Lecturer
Department of Computer Science
1
Today’s Lecture

Shortest Path
All-Pairs Shortest Paths Problem

2
Shortest path
In general, in graph theory the shortest path between
two vertices in a graph is a path between those two
vertices in such way that the sum of weights of edges
in that path is the smallest than the sum of weights of
edges in any path between those two vertices provided
the graph is a weighted graph.

So the shortest path problem is to find such a path


between the given vertices.

3
All-Pairs Shortest Paths Problem

Given a weighted digraph G=(V,E),determine the length of


the shortest path (i.e., distance) between all pairs of vertices
in G.

It aims to compute shortest path from each vertex v to every


other vertex u.

4
Floyd-Warshall algorithm

Given a weighted graph, we want to know the shortest path from


one vertex in the graph to another.
The Floyd-Warshall algorithm determines the shortest path
between all pairs of vertices in a graph, if there is no negative
cycle.

5
Main idea

a path exists between two vertices i, j, if


there is an edge from i to j; or
there is a path from i to j going through intermediate vertices
{1,….. k};

These are two situations:


k is an intermediate vertex on the shortest path.
k is not an intermediate vertex on the shortest path.

6
Matrix Representation

The graph is represented by an n x n matrix with the weights


of the edges

Output Format: an n x n distance D=[ di,j ] where di,j is the


distance from vertex i to j.

7
Floyd Warshall Algorithm
1. Floyd_Warshall (W) {
2. for i = 1 to n do { / / initialize
3. for j = 1 to n do{
4. d[i,j] = W[i,j]
5. pred[i,j] = null
6. }
7. }
8. for k = 1 to ndo / / use intermediates {1..k}
9. for i = 1 to n do / / ...from i
10. for j = 1 to n do / / ...to j
11. if (d[i,k] + d[k,j]) < d[i,j]) {
12. d[i,j] = d[i,k] + d[k,j] / / new shorter path length
13. pred[i,j] = k } / / new path is through k
14. return d / / matrix of final distances
15. }
8
Compute shortest path using
predecessor information
The pred[i, j] can be used to extract the final path.
Here is the idea, whenever we discover that the shortest path from
i to j passes through an intermediate vertex k, we set pred[i,j] = k.
If the shortest path does not pass through any intermediate vertex,
then pred[i, j] = null.
To find the shortest path from i to j, we consult pred[i,j]. If it is
null, then the shortest path is just the edge (i, j). Otherwise, we
recursively compute the shortest path from i to pred[i, j] and the
shortest path from pred[i, j] to j.

9
Example

10
Initially

P(0) =

11
if (d[i,k] + d[k,j]) < d[i,j]) {

Ist Iteration: k=1


d[i,j] = d[i,k] + d[k,j]
pred[i,j] = k }

P(0) =

P(1) =

12
if (d[i,k] + d[k,j]) < d[i,j]) {
2nd Iteration: k=2 d[i,j] = d[i,k] + d[k,j]
pred[i,j] = k }

P(1) =

P(2) =

13
if (d[i,k] + d[k,j]) < d[i,j]) {

3rd Iteration: k=3


d[i,j] = d[i,k] + d[k,j]
pred[i,j] = k }

P(2) =

P(3) =

14
if (d[i,k] + d[k,j]) < d[i,j]) {
d[i,j] = d[i,k] + d[k,j]
4th Iteration: k=4 pred[i,j] = k }

P(3) =

P(4) =

15
Shortest Path from 1 to 4
1->4 i-e 1->4
Shortest Path from 4 to 3
4->3 i-e 4->2->3
Shortest Path from 3 to 2
3->1->4->2

16
Analysis of Floyd Warshall Algorithm

O(n3)

17
Task

18
Initially

19
Questions?
Question in my Should I ask
mind is ? this ?

hmmmmmmmmm?
Sorry I was
sleeping sir !

20 Design and Analysis ofAlgorithm

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy