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

Data+Structures+and+Algorithms+Bootcamp+in+Python+slides+Remaster (1) - Part-5

Uploaded by

n22dcpt095
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
23 views70 pages

Data+Structures+and+Algorithms+Bootcamp+in+Python+slides+Remaster (1) - Part-5

Uploaded by

n22dcpt095
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 70

What is Graph?

Graph consists of a nite set of Vertices(or nodes) and a set of Edges which
connect a pair of nodes.

Node

A D

Edge
B F
C
K

AppMillers
www.appmillers.com
fi
Why Graph?

AppMillers
www.appmillers.com
Graph Terminology

- Vertices (vertex) : Vertices are the nodes of the graph


- Edge : The edge is the line that connects pairs of vertices

V1 V3

V5

V2 V4
AppMillers
www.appmillers.com
Graph Terminology

- Vertices : Vertices are the nodes of the graph


- Edge : The edge is the line that connects pairs of vertices
- Unweighted graph : A graph which does not have a weight associated with any edge
- Weighted graph : A graph which has a weight associated with any edge
- Undirected graph : In case the edges of the graph do not have a direction associated with them
- Directed graph : If the edges in a graph have a direction associated with them

V1 V3

8 12 V5

V2 V4
AppMillers
www.appmillers.com
Graph Terminology

- Vertices : Vertices are the nodes of the graph


- Edge : The edge is the line that connects pairs of vertices
- Unweighted graph : A graph which does not have a weight associated with any edge
- Weighted graph : A graph which has a weight associated with any edge
- Undirected graph : In case the edges of the graph do not have a direction associated with them
- Directed graph : If the edges in a graph have a direction associated with them
- Cyclic graph : A graph which has at least one loop

V1 V3

V5

V2 V4
AppMillers
www.appmillers.com
Graph Terminology

- Vertices : Vertices are the nodes of the graph


- Edge : The edge is the line that connects pairs of vertices
- Unweighted graph : A graph which does not have a weight associated with any edge
- Weighted graph : A graph which has a weight associated with any edge
- Undirected graph : In case the edges of the graph do not have a direction associated with them
- Directed graph : If the edges in a graph have a direction associated with them
- Cyclic graph : A graph which has at least one loop
- Acyclic graph : A graph with no loop

V1 V3

V5

V2
AppMillers
www.appmillers.com
Graph Terminology

- Vertices : Vertices are the nodes of the graph


- Edge : The edge is the line that connects pairs of vertices
- Unweighted graph : A graph which does not have a weight associated with any edge
- Weighted graph : A graph which has a weight associated with any edge
- Undirected graph : In case the edges of the graph do not have a direction associated with them
- Directed graph : If the edges in a graph have a direction associated with them
- Cyclic graph : A graph which has at least one loop
- Acyclic graph : A graph with no loop
- Tree: It is a special case of directed acyclic graphs

V1

V2 V3

V5 AppMillers
www.appmillers.com
Graph Types
Graph

Directed Undirected

Weighted Unweighted Weighted Unweighted

Positive Negative Positive Negative

AppMillers
www.appmillers.com
Graph Types

1. Unweighted - undirected

V1 V3

V5

V2 V4

AppMillers
www.appmillers.com
Graph Types

1. Unweighted - undirected
2. Unweighted - directed

V1 V3

V5

V2 V4

AppMillers
www.appmillers.com
Graph Types

1. Unweighted - undirected
2. Unweighted - directed
3. Positive - weighted - undirected

3
V1 V3

2
5
4 V5

3
V2 V4
3

AppMillers
www.appmillers.com
Graph Types

1. Unweighted - undirected
2. Unweighted - directed
3. Positive - weighted - undirected
4. Positive - weighted - directed
3
V1 V3

2
5
4 V5

3
V2 V4
3

AppMillers
www.appmillers.com
Graph Types

1. Unweighted - undirected
2. Unweighted - directed
3. Positive - weighted - undirected
4. Positive - weighted - directed
5. Negative - weighted - undirected
-3
V1 V3

2
-5
4 V5

3
V2 V4
3
AppMillers
www.appmillers.com
Graph Types

1. Unweighted - undirected
2. Unweighted - directed
3. Positive - weighted - undirected
4. Positive - weighted - directed
5. Negative - weighted - undirected

6. Negative - weighted - directed


-3
V1 V3

2
-5
4 V5

3
V2 V4
3 AppMillers
www.appmillers.com
Graph Representation

Adjacency Matrix : an adjacency matrix is a square matrix or you can say it is a 2D array. And
the elements of the matrix indicate whether pairs of vertices are adjacent or not in the graph

A B C D E
A B
A 0 1
0 1
0 1
0 0

B 1
0 0 0 0 1
0
E
0
C 1 0 0 0
1 0

C D
0
D 1 0 0
1 0 0
1

E 0 1
0 0 1
0 0

AppMillers
www.appmillers.com
Graph Representation

Adjacency List : an adjacency list is a collection of unordered list used to represent a graph.
Each list describes the set of neighbors of a vertex in the graph.

A B C D
A B
B A E

E C A D
D A C E
C D B D
E

AppMillers
www.appmillers.com
Graph Representation
If a graph is complete or almost complete we should use Adjacency Matrix

If the number of edges are few then we should use Adjacency List

A B C D E
A B C D
A 0 1
0 1
0 1
0 0
B A E
B 1
0 0 0 0 1
0
C A D
0
C 1 0 0 0
1 0
D A C E
0
D 1 0 0
1 0 0
1
E B D
E 0 1
0 0 1
0 0

A B

AppMillers
www.appmillers.com
C D
Graph Representation
Python Dictionary implementation

A B { A : [B, C, D],
B: [A, E],
C: [A,D],
E
D: [A,C,E],
E: [B,D] }
C D

AppMillers
www.appmillers.com
Graph in Python

Dictionary implementation

{ A : [B, C],
B: [A, D, E],
B C
C: [A,E],
D: [B,E,F],
E: [C,D,F],
F: [D, E]}
D E

AppMillers
www.appmillers.com
Graph Traversal
It is a process of visiting all vertices in a given Graph

A B - Breadth First Search


- Depth First Search

C D G

E F

AppMillers
www.appmillers.com
Breadth First Search
BFS is an algorithm for traversing Graph data structure. It starts at some arbitrary node of a
graph and explores the neighbor nodes (which are at current level) rst, before moving to the
next level neighbors.

Level 1 A B

Level 2 C D G

Level 3 E F

AppMillers
www.appmillers.com
fi
Breadth First Search Algorithm

BFS
A B
enqueue any starting vertex
while queue is not empty
p = dequeue()
if p is unvisited
mark it visited
enqueue all adjacent C D G
unvisited vertices of p

A B C D G E F
E F

Queue A B C D G E F F F
AppMillers
www.appmillers.com
Depth First Search
DFS is an algorithm for traversing a graph data structure which starts selecting some arbitrary
node and explores as far as possible along each edge before backtracking

A B

C D G

E F

AppMillers
www.appmillers.com
Depth First Search Algorithm
Stack
DFS A B
G
push any starting vertex
while stack is not empty
B
p = pop()
if p is unvisited
mark it visited D
Push all adjacent C D G
unvisited vertices of p G

A C E F D B G E
E F

A
AppMillers
www.appmillers.com
BFS vs DFS

Level 1 A B A B

G C D G
Level 2 C D

Level 3 E F E F

BFS DFS

AppMillers
www.appmillers.com
BFS vs DFS

BFS DFS

How does it work internally? It goes in breath rst It goes in depth rst

Which DS does it use internally? Queue Stack

Time Complexity O(V+E) O(V+E)

Space Complexity O(V+E) O(V+E)

If we know that the target is close to the If we already know that the
When to use? staring point target vertex is buried very
deep

AppMillers
www.appmillers.com
fi
fi
Topological Sort

Topological Sort: Sorts given actions in such a way that if there is a dependency of one action
on another, then the dependent action always comes later than its parent action.

Buy
breakfast
Exercise fruits

Prepare
Breakfast
Bath

Wash
dishes
Breakfast

Work

AppMillers
www.appmillers.com
Topological Sort Algorithm
Stack

if a vertex depends on currentVertex: A B


Go to that vertex and
then come back to currentVertex
else B
Push currentVertex to Stack
C D
D

ABCDEHFG A
E
BACDEHFG C

E H F G
BDACEFGH
F

H
AppMillers
www.appmillers.com
Single Source Shortest Path Problem
A single source problem is about nding a path between a given vertex (called source) to all
other vertices in a graph such that the total distance between them (source and destination) is
minimum.
The problem:
- Five o ces in di erent cities.
- Travel costs between these cities are known.
- Find the cheapest way from head o ce to branches Paris
10 Rome
in di erent cities
Paris
Rome 5
10

5
London Barcelona 10
35
London Barcelona 10

Berlin
30
20 10
Berlin

10
AppMillers
www.appmillers.com
ff
ffi
ff
fi
ffi
Single Source Shortest Path Problem
- BFS
- Dijkstra’s Algorithm
- Bellman Ford

BFS - Breadth First Search

A
Level 1 A B

B C
Level 2 C D G

Level 3 E F

AppMillers
www.appmillers.com
BFS for SSSP

BFS
null
A
enqueue any starting vertex
A
while queue is not empty B
p = dequeue()
if p is unvisited
mark it visited B B
A
enqueue all adjacent unvisited vertices of p C D G
update parent of adjacent vertices to curVertex

A B C D G E F C D
E F

Queue A B C D G E F F F
AppMillers
www.appmillers.com
Why BFS not work with weighted graph

Graph Type BFS

Unweighted - undirected OK

Unweighted - directed OK

Positive - weighted - undirected X

Positive - weighted - directed X

Negative - weighted - undirected X

Negative - weighted - directed X

AppMillers
www.appmillers.com
Why BFS not work with weighted graph

10 50

A B C A

30 5 9 30
B 6 B
D E F G

7 20

H I

AppMillers
www.appmillers.com
Why does DFS not work with SSSP?

DFS has the tendency to go “as far as possible” from source, hence it can never nd “Shortest Path”

B C

D E F

AppMillers
www.appmillers.com

fi
Dijkstra’s Algorithm for SSSP

min min
B∞
Bv2
Bv 3 E∞
Ev5
Ev

2 1 4 9
min
A
Av0 6 D∞
Dv3
Dv G∞
Gv14
Gv

5 7

C∞
Cv5
Cv 8 F13

Fv13
Fv
min min

AppMillers
www.appmillers.com
Dijkstra’s Algorithm with negative cycle

A path is called a negative cycle if:


There is a cycle (a cycle is a path of edges or vertices wherein a vertex is reachable from itself)
Total weight of cycle is a negative number

A 3
B
4
-6 1
6
E
1 2
C D

1 + 3 + (-6) = -2

AppMillers
www.appmillers.com
Dijkstra’s Algorithm with negative cycle

We cannot never nd a negative cycle in a graph

3 Path from A to B = 6 + 1 = -5
A B
= -5+3+(-6)+1 = -7
4
-6 = -7+3+(-6)+1 = -9
6 1
E = -9+3+(-6)+1 = -11
1 2
C D

AppMillers
www.appmillers.com
fi
Bellman Ford Algorithm

Single Source Shortest Path Algorithm

Graph Type BFS Dijkstra Bellman Ford

Unweighted - undirected OK OK OK

Unweighted - directed OK OK OK

Positive - weighted - undirected X OK OK

Positive - weighted - directed X OK OK

Negative - weighted - undirected X OK OK

Negative - weighted - directed X OK OK

Negative Cycle X X OK

AppMillers
www.appmillers.com
Bellman Ford Algorithm

Bellman Ford algorithm is used to nd single source shortest path problem. If there is a
negative cycle it catches it and report its existence.

A 3
B
4
6 1
6
E
1 2
C D

AppMillers
www.appmillers.com
fi
Bellman Ford Algorithm
∞ ∞
A 3 B
If the distance of destination vertex > (distance of
4
6 0 source vertex + weight between source and destination
6 1 vertex):
E
Update distance of destination vertex to (distance of
1 2
C D source vertex + weight between source and destination
∞ 2 ∞ vertex)

Edge Weight Distance Matrix


A->C 6
Vertex Distance
A->D 6
B->A 3 A ∞
C->D 1 B ∞
D->C 2 C ∞
D->B 1
D ∞
E->B 4
E->D 2 E 0

AppMillers
www.appmillers.com
Bellman Ford Algorithm
∞ 4∞E
A 3 B
If the distance of destination vertex > (distance of
4
6 0 source vertex + weight between source and destination
6 1 vertex):
E
Update distance of destination vertex to (distance of
1 2
C D source vertex + weight between source and destination
∞ 2 2∞E vertex)

Edge Weight Distance


Distance Matrix Matrix
A->C 6 Iteration 1
Vertex Distanc
Distance
A->D 6 Vertex
e Distanc Parent
B->A 3 A ∞
A ∞ e
∞ -
C->D 1 B ∞
B ∞ 4 E
D->C 2 C ∞
D->B 1 C ∞ ∞ -
E->B 4 DD ∞ ∞ 2 E
E->D 2 EE 0 0 0 -

AppMillers
www.appmillers.com
Bellman Ford Algorithm
7∞B 3
4DE
A 3 B
If the distance of destination vertex > (distance of
4
6 0 source vertex + weight between source and destination
6 1 vertex):
E
Update distance of destination vertex to (distance of
1 2
C D source vertex + weight between source and destination
4∞D 2 2E vertex)

Edge Weight Distance Distance


Matrix Matrix
A->C 6 Iteration
Iteration11 Iteration 2
Distanc
Distanc
A->D 6 Vertex
ee Distanc
Distanc Parent
Parent Distanc Parent
B->A 3 e∞e e
A ∞
∞ ∞ -- 4+3=7 B
C->D 1
B ∞
∞ 44 EE 2+1=3 D
D->C 2
D->B 1 C ∞
∞ ∞∞ -- 2+1=4 D
E->B 4 D ∞
∞ 22 EE 2 E
E->D 2 E 00 00 -- 0 -

AppMillers
www.appmillers.com
Bellman Ford Algorithm
6B
7 3D
A 3 B
If the distance of destination vertex > (distance of
4
6 0 source vertex + weight between source and destination
6 1 vertex):
E
Update distance of destination vertex to (distance of
1 2
C D source vertex + weight between source and destination
4D 2 2E vertex)

Edge Weight Distance Matrix


Distance Matrix
A->C 6 Iteration 1 Iteration 2 Iteration 3
Distanc
A->D 6 Vertex
e Distanc Parent Distanc Parent
Parent Distanc Parent
B->A 3 e e e
A ∞ ∞ - 4+3=7 B
B 3+3=6 B
C->D 1
B ∞ 4 E 2+1=3 D
D 3 D
D->C 2
D->B 1 C ∞ ∞ - 2+1=4
2+1=3 D
D 4 D
E->B 4 D ∞ 2 E 2 E
E 2 E
E->D 2 E 0 0 - 0 -- 0

AppMillers
www.appmillers.com
Bellman Ford Algorithm
6B 3D
A 3 B
If the distance of destination vertex > (distance of
4
6 0 source vertex + weight between source and destination
6 1 vertex):
E
Update distance of destination vertex to (distance of
1 2
C D source vertex + weight between source and destination
3D 2 2E vertex)

Edge Weight Distance Matrix


A->C 6 Iteration 1 Iteration 2 Iteration 3 Iteration 4
Distanc
A->D 6 Vertex
e Distanc Parent Distanc Parent Distanc Parent Distanc Parent
B->A 3 e e e e
A ∞ ∞ - 4+3=7 B 3+3=6 B 6 B
C->D 1
B ∞ 4 E 2+1=3 D 3 D 3 D
D->C 2
D->B 1 C ∞ ∞ - 2+2=4 D 4 D 4 D
E->B 4 D ∞ 2 E 2 E 2 E 2 E
E->D 2 E 0 0 - 0 - 0 - 0 -

AppMillers
www.appmillers.com
Bellman Ford Algorithm
6B 3D
A 3 B
If the distance of destination vertex > (distance of
4
6 0 source vertex + weight between source and destination
6 1 vertex):
E
Update distance of destination vertex to (distance of
1 2
C D source vertex + weight between source and destination
3D 2 2E vertex)

Edge Weight Final Solution


A->C 6
Vertex Distance from E Path from E
A->D 6
B->A 3 A 6 E -> D -> B -> A
C->D 1 B 3 E -> D -> B
D->C 2 C 4 E -> D -> C
D->B 1
D 2 E -> D
E->B 4
E->D 2 E 0 0

AppMillers
www.appmillers.com
Bellman Ford Algorithm with negative cycle
∞ ∞
A 3 B
If the distance of destination vertex > (distance of
4
-6 0 source vertex + weight between source and destination
6 1 vertex):
E
Update distance of destination vertex to (distance of
1 2
C D source vertex + weight between source and destination
∞ 2 ∞ vertex)

Edge Weight Distance Matrix


A->C 6
Vertex Distance
A->D -6
B->A 3 A ∞
C->D 1 B ∞
D->C 2 C ∞
D->B 1
D ∞
E->B 4
E->D 2 E 0

AppMillers
www.appmillers.com
Bellman Ford Algorithm with negative cycle
∞ 4E
A 3 B
If the distance of destination vertex > (distance of
4
-6 0 source vertex + weight between source and destination
6 1 vertex):
E
Update distance of destination vertex to (distance of
1 2
C D source vertex + weight between source and destination
∞ 2 2E vertex)

Edge Weight Distance Matrix


A->C 6 Iteration 1
Distanc
A->D -6 Vertex
e Distanc Parent
B->A 3 e
A ∞ ∞ -
C->D 1
B ∞ 4 E
D->C 2
D->B 1 C ∞ ∞ -
E->B 4 D ∞ 2 E
E->D 2 E 0 0 -

AppMillers
www.appmillers.com
Bellman Ford Algorithm with negative cycle
7B 3D
A 3 B
If the distance of destination vertex > (distance of
4
-6 0 source vertex + weight between source and destination
6 1 vertex):
E
Update distance of destination vertex to (distance of
1 2
C D source vertex + weight between source and destination
4D 2 2E vertex)

Edge Weight Distance Matrix


A->C 6 Iteration 1 Iteration 2
Distanc
A->D -6 Vertex
e Distanc Parent Distanc Parent
B->A 3 e e
A ∞ ∞ - 4+3=7 B
C->D 1
B ∞ 4 E 2+1=3 D
D->C 2
D->B 1 C ∞ ∞ - 2+2=4 D
E->B 4 D ∞ 2 E 2 E
E->D 2 E 0 0 - 0 -

AppMillers
www.appmillers.com
Bellman Ford Algorithm with negative cycle
6B 3D
A 3 B
If the distance of destination vertex > (distance of
4
-6 0 source vertex + weight between source and destination
6 1 vertex):
E
Update distance of destination vertex to (distance of
1 2
C D source vertex + weight between source and destination
4D 2 1A vertex)

Edge Weight Distance Matrix


A->C 6 Iteration 1 Iteration 2 Iteration 3
Distanc
A->D -6 Vertex
e Distanc Parent Distanc Parent Distanc Parent
B->A 3 e e e
A ∞ ∞ - 4+3=7 B 3+3=6 B
C->D 1
B ∞ 4 E 2+1=3 D 3 D
D->C 2
D->B 1 C ∞ ∞ - 2+2=4 D 4 D
E->B 4 D ∞ 2 E 2 E 7+ A
E 0 0 - 0 - (-6)=1
0
E->D 2

AppMillers
www.appmillers.com
Bellman Ford Algorithm with negative cycle
6B 3D
A 3 B
If the distance of destination vertex > (distance of
4
-6 0 source vertex + weight between source and destination
6 1 vertex):
E
Update distance of destination vertex to (distance of
1 2
C D source vertex + weight between source and destination
4D 2 0A vertex)

Edge Weight Distance Matrix


A->C 6 Iteration 1 Iteration 2 Iteration 3 Iteration 4
Distanc
A->D -6 Vertex
e Distanc Parent Distanc Parent Distanc Parent Distanc Parent
B->A 3 e e e e
A ∞ ∞ - 4+3=7 B 3+3=6 B 6 B
C->D 1
B ∞ 4 E 2+1=3 D 3 D 3 D
D->C 2
D->B 1 C ∞ ∞ - 2+2=4 D 4 D 4 D
E->B 4 D ∞ 2 E 2 E 7+ A 6+ A
E 0 0 - 0 - (-6)=1
0 - (-6)=0
0 -
E->D 2

AppMillers
www.appmillers.com
Edge Weight
A->C 6

Bellman Ford Algorithm with negative cycle A->D -6


B->A 3
6B 3D C->D 1
A 3 B D->C 2
D->B 1
4
-6 0 E->B 4
6 1
E E->D 2
1 2 If the distance of destination vertex > (distance of
C D
4D 2 source vertex + weight between source and destination
0A
vertex):
Update distance of destination vertex to (distance of
source vertex + weight between source and destination
vertex)

Distance Matrix

Distanc Iteration 1 Iteration 2 Iteration 3 Iteration 4 Iteration 5


Vertex
e Distanc Parent Distanc Parent Distanc Parent Distanc Parent Distanc Parent
A ∞ e
∞ - e
4+3=7 B e
3+3=6 B e
6 B e
4 B
B ∞ 4 E 2+1=3 D 3 D 3 D 0 D
C ∞ ∞ - 2+2=4 D 4 D 4 D 0 D
D ∞ 2 E 2 E 7+(-6)=1 A 6+(-6)=0 A 1 A
E 0 0 - 0 - 0 - 0 -

AppMillers
www.appmillers.com
Bellman Ford Algorithm with negative cycle
6B 3D
A 3 B
If the distance of destination vertex > (distance of
4
-6 0 source vertex + weight between source and destination
6 1 vertex):
E
Update distance of destination vertex to (distance of
1 2
C D source vertex + weight between source and destination
4D 2 2E vertex)

Edge Weight Final Solution


A->C 6
Vertex Distance from E Path from E
A->D 6
B->A 3 A 6 E -> D -> B -> A
C->D 1 B 3 E -> D -> B
D->C 2 C 4 E -> D -> C
D->B 1
D 2 E -> D
E->B 4
E->D 2 E 0 0

AppMillers
www.appmillers.com
Why does Bellman Ford run V-1 times?
- If any node is achieved better distance in previous iteration, then that better distance is used to
improve distance of other vertices
- Identify worst case graph that can be given to us


76 ∞
43
A 3 B
A
10 20 30 40
B C D E
4
-6 0
6 1
E
60
∞ 50
∞ 30
∞ 0
1 2 10 20 30
C D A B C D
∞ 2 ∞

A->B
B->C
C->D

AppMillers
www.appmillers.com
BFS vs Dijkstra vs Bellman Ford

Graph Type BFS Dijkstra Bellman Ford

Unweighted - undirected OK OK OK

Unweighted - directed OK OK OK

Positive - weighted - undirected X OK OK

Positive - weighted - directed X OK OK

Negative - weighted - undirected X OK OK

Negative - weighted - directed X OK OK

Negative Cycle X X OK

AppMillers
www.appmillers.com
BFS vs Dijkstra vs Bellman Ford

BFS Dijkstra Bellman Ford


Time complexity O(V2) O(V2) O(VE)
Space complexity O(E) O(V) O(V)
Implementation Easy Moderate Moderate
Limitation Not work for weighted graph Not work for negative cycle N/A
OK OK OK
Unweighted graph Use this as time complexity is good Not use as implementation not Not use as time complexity
and easy to implement easy is bad
X OK OK
Weighted graph Use as time complexity is Not use as time complexity
Not supported
better than Bellman is bad
X X OK
Negative Cycle Use this as others not
Not supported Not supported
support

AppMillers
www.appmillers.com
All pair shortest path problem
What is single source shortest path?
A single source problem is about nding a path between a given vertex (called source) to all
other vertices in a graph such that the total distance between them (source and destination) is
minimum.

- Five o ces in di erent cities.


- Travel costs between these cities are known.
- Find the cheapest way from head o ce to branches Paris
10 Rome
in di erent cities
Paris
Rome 5
10

5
London Barcelona 10
35
London Barcelona 10

Berlin
30
20 10
Berlin

10
AppMillers
www.appmillers.com
ff
ffi
ff
fi
ffi
All pair shortest path problem
All pair shortest path problem is about nding a path between every vertex to all other vertices
in a graph such that the total distance between them (source and destination) is minimum.

The problem:
- Five o ces in di erent cities.
- Travel costs between these cities are known.
- Find the cheapest way to reach each o ce from
every other o ce
Paris
10 Rome

5
35
London Barcelona 10

30
20
Berlin

10
AppMillers
www.appmillers.com
ffi
ffi
ff
ffi
fi
All pair shortest path problem
Paris Paris Paris
10 Rome 10 Rome 10 Rome

5 35 5 5

London Barcelona 10 London Barcelona 10 London Barcelona 10

20 30 20
Berlin Berlin Berlin

10 10

Paris Paris Paris


Rome Rome Rome

5 35 5 35 5 35
London Barcelona 10 London Barcelona 10 London Barcelona 10

20 20 20
Berlin Berlin Berlin

AppMillers
www.appmillers.com
Dry run for All pair shortest path problem
6
∞ ∞
3
4
3 Source Vertex Path
A B E
A E->D->B-
4 >A
6 0
6 1 B E->D->B
E
1 2 C E->D->C
C D

2 2 ∞
2 D E->D

E -
Dijkstra

AppMillers
www.appmillers.com
Dry run for All pair shortest path problem
4
∞ ∞
1
3 Source Vertex Path Source Vertex Path
A B E D
A E->D->B- A D->B->A
4 >A
6 ∞
6 1 B E->D->B B D->B
E
1 2 C E->D->C C D->C
C D

2 2 0 D E->D D -

E - E N/A
Dijkstra

AppMillers
www.appmillers.com
Dry run for All pair shortest path problem

3 Source Vertex Path Source Vertex Path


A B E D
A E->D->B- A D->B->A
4 >A
6
6 1 B E->D->B B D->B
E
1 2 C E->D->C C D->C
C D
2 D E->D D -

E - E N/A
Dijkstra , BFS and Bellmand Ford

Source Vertex Path Source Vertex Path Source Vertex Path


C B A
A C->D->B- A B->A A -
>A
B C->D->B B - B A->D->B

C - C B->A->C C A->D->C

D C->D D A->D D A->D

E N/A E N/A E N/A

AppMillers
www.appmillers.com
Floyd Warshall

A 8 Given Iteration 1
B
A B C D via A A B C D
4 A 0 8 ∞ 1 A 0 8 ∞ 1
1 1 B ∞ 0 1 ∞ B ∞ 0 1 ∞
2 C 4 ∞ 0 ∞ C 4 4+8=12 0 4+1=5

D C D ∞ 2 9 0 D ∞ 2 9 0
9

If D[u][v] > D[u][viaX] + D[viaX][v]:


D[u][v] = D[u][viaX] + D[viaX][v]

C -> B
If D[C][B] > D[C][A] + D[A][B]:
D[C][B] = D[C][A] + D[A][B]

∞ > 4 + 8 = 12

C -> D
If D[C][D] > D[C][A] + D[A][D]:
D[C][D] = D[C][A] + D[A][D]
AppMillers
www.appmillers.com
∞ > 4 + 1 = 5
Floyd Warshall

A 8 Given Iteration 1
B
A B C D via A A B C D
4 A 0 8 ∞ 1 A 0 8 ∞ 1
1 1 B ∞ 0 1 ∞ B ∞ 0 1 ∞
2 C 4 ∞ 0 ∞ C 4 4+8=12 0 4+1=5

D C D ∞ 2 9 0 D ∞ 2 9 0
9

If D[u][v] > D[u][viaX] + D[viaX][v]: Iteration 2


D[u][v] = D[u][viaX] + D[viaX][v]
via B A B C D
A 0 8 8+1=9 1
A -> C
B ∞ 0 1 ∞
If D[A][C] > D[A][B] + D[B][C]: C 4 12 0 5
D[A][C] = D[A][B] + D[B][C] D ∞ 2 2+1=3 0

∞ > 8 + 1 = 9

D -> C
If D[D][C] > D[D][B] + D[B][C]:
D[D][C] = D[D][B] + D[B][C]
AppMillers
www.appmillers.com
9 > 2 + 1 = 3
Floyd Warshall

A 8 Given Iteration 1
B
A B C D via A A B C D
4 A 0 8 ∞ 1 A 0 8 ∞ 1
1 1 B ∞ 0 1 ∞ B ∞ 0 1 ∞
2 C 4 ∞ 0 ∞ C 4 4+8=12 0 4+1=5

D C D ∞ 2 9 0 D ∞ 2 9 0
9

If D[u][v] > D[u][viaX] + D[viaX][v]: Iteration 2 Iteration 3


D[u][v] = D[u][viaX] + D[viaX][v]
via B A B C D via C A B C D
A 0 8 8+1=9 1 A 0 8 9 1
B ∞ 0 1 ∞ B 1+4=5 0 1 1+4+1
=6

C 4 12 0 5 C 4 12 0 5
D ∞ 2 2+1=3 0 D 3+4=7 2 3 0

Iteration 4 Final answer


via D A B C D A B C D
A 0 1+2=3 3+1=4 1 A 0 3 4 1
B 5 0 1 6 B 5 0 1 6
C 4 5+2=7 0 5 C 4 7 0 5
D 7 2 3 0 D 7 2 3 0 AppMillers
www.appmillers.com
Why Floyd Warshall algorithm?

A 8 Given Iteration 1 Iteration 2


B
A B C D via A A B C D via B A B C D
4 A 0 8 ∞ 1 A 0 8 ∞ 1 A 0 8 8+1=9 1
1 1 B ∞ 0 1 ∞ B ∞ 0 1 ∞ B ∞ 0 1 ∞
2 C 4 ∞ 0 ∞ C 4 0 4 12 0 5
4+8=12 4+1=5 C
D C D ∞ 2 9 0 D ∞ 2 9 0 D ∞ 2 2+1=3 0
9
Iteration 3 Iteration 4 Final answer
via C A B C D via D A B C D A B C D
A 0 8 9 1 A 0 1+2=3 3+1=4 1 A 0 3 4 1
E
B 1+4=5 0 1 1+4+1
B 5 0 1 6 B 5 0 1 6
=6

C 4 12 0 5 C 4 5+2=7 0 5 C 4 7 0 5
D 3+4=7 2 3 0 D 7 2 3 0 D 7 2 3 0

- The vertex is not reachable


- Two vertices are directly connected
This is the best solution
It can be improved via other vertex
- Two vertices are connected via other vertex
AppMillers
www.appmillers.com
Floyd Warshall negative cycle

A -8
B -8+1+4=-3

4
1 1
2
D C
9

- To go through cycle we need to go via negative cycle participating vertex at least twice

- FW never runs loop twice via same vertex

- Hence, FW can never detect a negative cycle

AppMillers
www.appmillers.com
Which algorithm to use for APSP?

Graph Type BFS Dijkstra Bellman Ford Floyd Warshall

Unweighted - undirected OK OK OK OK

Unweighted - directed OK OK OK OK

Positive - weighted - undirected X OK OK OK

Positive - weighted - directed X OK OK OK

Negative - weighted - undirected X OK OK OK

Negative - weighted - directed X OK OK OK

Negative Cycle X X OK X

AppMillers
www.appmillers.com
Which algorithm to use for APSP?

BFS Dijkstra Bellman Ford Floyd Warshall

Time complexity O(V3) O(V3) O(EV2) O(V3)

Space complexity O(EV) O(EV) O(V2) O(V2)

Implementation Easy Moderate Moderate Moderate

Not work for negative Not work for negative


Limitation Not work for weighted graph N/A
cycle cycle
OK OK OK OK
Unweighted graph Use this as time complexity Not use as priority Not use as time
is good and easy to Can be used
queue slows it complexity is bad
implement
X OK OK OK
Weighted graph Not use as time Can be preferred as
Not supported Can be used complexity is bad implementation easy
X X OK X
Negative Cycle Use this as others not
Not supported Not supported No supported
support

AppMillers
www.appmillers.com
Minimum Spanning Tree
A Minimum Spanning Tree (MST) is a subset of the edges of connected, weighted and undirected graph which :
- Connects all vertices together
- No cycle
- Minimum total edge

Real life problem


- Connect ve island with bridges
- The cost of bridges between island varies based on di erent factors
- Which bridge should be constructed so that all islands are accessible and the cost is minimum

5 5

10 10

20 15 15
35
25
10 10

AppMillers
www.appmillers.com
fi
ff
Minimum Spanning Tree
Real life problem

10

35 20 15

25
10

Minimum spanning Tree Single source shortest path

5 5

10 10

15 20 15

10

AppMillers
www.appmillers.com

You might also like