Data+Structures+and+Algorithms+Bootcamp+in+Python+slides+Remaster (1) - Part-5
Data+Structures+and+Algorithms+Bootcamp+in+Python+slides+Remaster (1) - Part-5
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
V1 V3
V5
V2 V4
AppMillers
www.appmillers.com
Graph Terminology
V1 V3
8 12 V5
V2 V4
AppMillers
www.appmillers.com
Graph Terminology
V1 V3
V5
V2 V4
AppMillers
www.appmillers.com
Graph Terminology
V1 V3
V5
V2
AppMillers
www.appmillers.com
Graph Terminology
V1
V2 V3
V5 AppMillers
www.appmillers.com
Graph Types
Graph
Directed Undirected
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
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
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
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
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
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
Unweighted - undirected OK
Unweighted - directed OK
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 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
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
Unweighted - undirected OK OK OK
Unweighted - directed OK 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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
AppMillers
www.appmillers.com
Edge Weight
A->C 6
Distance Matrix
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)
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
Unweighted - undirected OK OK OK
Unweighted - directed OK OK OK
Negative Cycle X X OK
AppMillers
www.appmillers.com
BFS vs Dijkstra vs Bellman Ford
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.
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
20 30 20
Berlin Berlin Berlin
10 10
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
E - E N/A
Dijkstra , BFS and Bellmand Ford
C - C B->A->C C A->D->C
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
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
∞ > 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
C 4 12 0 5 C 4 12 0 5
D ∞ 2 2+1=3 0 D 3+4=7 2 3 0
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
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
AppMillers
www.appmillers.com
Which algorithm to use for APSP?
Unweighted - undirected OK OK OK OK
Unweighted - directed OK OK OK OK
Negative Cycle X X OK X
AppMillers
www.appmillers.com
Which algorithm to use for APSP?
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
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
5 5
10 10
15 20 15
10
AppMillers
www.appmillers.com