Lecture 10
Lecture 10
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
Cartesian product A × B
{(a, b) | a ∈ A, b ∈ B}
Cartesian product A × B
{(a, b) | a ∈ A, b ∈ B}
A relation is a subset of A × B
Cartesian product A × B
{(a, b) | a ∈ A, b ∈ B}
A relation is a subset of A × B
Teachers and courses
T , set of teachers in a college
C , set of courses being offered
Cartesian product A × B
{(a, b) | a ∈ A, b ∈ B}
A relation is a subset of A × B
Teachers and courses
T , set of teachers in a college
C , set of courses being offered
A ⊆ T × C describes the allocation of
teachers to courses
A = {(t, c) | (t, c) ∈ T × C , t teaches c}
v8
v7
v9
v8
v7
v9
v8
v7
v9
Is v reachable from u?
v8
v7
v9
Is v reachable from u?
What is the shortest path from u to v ? v8
v7
v9
Is v reachable from u?
What is the shortest path from u to v ? v8
v7
What are the vertices reachable from u?
v9
Is v reachable from u?
What is the shortest path from u to v ? v8
v7
What are the vertices reachable from u?
Is the graph connected? Are all vertices v9
reachable from each other?
Is v reachable from u?
What is the shortest path from u to v ? v8
v7
What are the vertices reachable from u?
Is the graph connected? Are all vertices v9
reachable from each other?
Is v reachable from u?
What is the shortest path from u to v ? v8
v7
What are the vertices reachable from u?
Is the graph connected? Are all vertices v9
reachable from each other?
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
v8
v7
v9
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 2/8
Graphs
v8
v7
v9
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 2/8
Graphs
v9
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 2/8
Graphs
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 2/8
Map colouring
Assign each state a colour
States that share a border should be
coloured differently
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 3/8
Map colouring
Assign each state a colour
States that share a border should be
coloured differently
How many colours do we need?
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 3/8
Map colouring
Assign each state a colour
States that share a border should be
coloured differently
How many colours do we need?
Create a graph
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 3/8
Map colouring
Assign each state a colour
States that share a border should be
coloured differently
How many colours do we need?
Create a graph
Each state is a vertex
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 3/8
Map colouring
Assign each state a colour
States that share a border should be
coloured differently
How many colours do we need?
Create a graph
Each state is a vertex
Connect states that share a border
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 3/8
Map colouring
Assign each state a colour
States that share a border should be
coloured differently
How many colours do we need?
Create a graph
Each state is a vertex
Connect states that share a border
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 3/8
Map colouring
Assign each state a colour
States that share a border should be
coloured differently
How many colours do we need?
Create a graph
Each state is a vertex
Connect states that share a border
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 3/8
Map colouring
Assign each state a colour
States that share a border should be
coloured differently
How many colours do we need?
Create a graph
Each state is a vertex
Connect states that share a border
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 3/8
Map colouring
Assign each state a colour
States that share a border should be
coloured differently
How many colours do we need?
Create a graph
Each state is a vertex
Connect states that share a border
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 3/8
Map colouring
Assign each state a colour
States that share a border should be
coloured differently
How many colours do we need?
Create a graph
Each state is a vertex
Connect states that share a border
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 3/8
Map colouring
Assign each state a colour
States that share a border should be
coloured differently
How many colours do we need?
Create a graph
Each state is a vertex
Connect states that share a border
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 3/8
Map colouring
Assign each state a colour
States that share a border should be
coloured differently
How many colours do we need?
Create a graph
Each state is a vertex
Connect states that share a border
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 3/8
Map colouring
Assign each state a colour
States that share a border should be
coloured differently
How many colours do we need?
Create a graph
Each state is a vertex
Connect states that share a border
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 4/8
Graph colouring
Graph G = (V , E ), set of colours C
Colouring is a function c : V → C such
that (u, v ) ∈ E ⇒ c(u) 6= c(v )
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 4/8
Graph colouring
Graph G = (V , E ), set of colours C
Colouring is a function c : V → C such
that (u, v ) ∈ E ⇒ c(u) 6= c(v )
Given G = (V , E ), what is the smallest
set of colours need to colour G
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 4/8
Graph colouring
Graph G = (V , E ), set of colours C
Colouring is a function c : V → C such
that (u, v ) ∈ E ⇒ c(u) 6= c(v )
Given G = (V , E ), what is the smallest
set of colours need to colour G
Four Colour Theorem For graphs derived
from geographical maps, 4 colours suffice
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 4/8
Graph colouring
Graph G = (V , E ), set of colours C
Colouring is a function c : V → C such
that (u, v ) ∈ E ⇒ c(u) 6= c(v )
Given G = (V , E ), what is the smallest
set of colours need to colour G
Four Colour Theorem For graphs derived
from geographical maps, 4 colours suffice
Not all graphs are planar. General case?
Why do we care?
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 4/8
Graph colouring
Graph G = (V , E ), set of colours C
Colouring is a function c : V → C such
that (u, v ) ∈ E ⇒ c(u) 6= c(v )
Given G = (V , E ), what is the smallest
set of colours need to colour G
Four Colour Theorem For graphs derived
from geographical maps, 4 colours suffice
Not all graphs are planar. General case?
Why do we care?
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 4/8
Graph colouring
Graph G = (V , E ), set of colours C
Colouring is a function c : V → C such
that (u, v ) ∈ E ⇒ c(u) 6= c(v )
Given G = (V , E ), what is the smallest
set of colours need to colour G
Four Colour Theorem For graphs derived
from geographical maps, 4 colours suffice
Not all graphs are planar. General case?
Why do we care?
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 4/8
Graph colouring
Graph G = (V , E ), set of colours C
Colouring is a function c : V → C such
that (u, v ) ∈ E ⇒ c(u) 6= c(v )
Given G = (V , E ), what is the smallest
set of colours need to colour G
Four Colour Theorem For graphs derived
from geographical maps, 4 colours suffice
Not all graphs are planar. General case?
Why do we care?
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 4/8
Graph colouring
Graph G = (V , E ), set of colours C
Colouring is a function c : V → C such
that (u, v ) ∈ E ⇒ c(u) 6= c(v )
Given G = (V , E ), what is the smallest
set of colours need to colour G
Four Colour Theorem For graphs derived
from geographical maps, 4 colours suffice
Not all graphs are planar. General case?
Why do we care?
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 5/8
Vertex cover
A hotel wants to install security cameras
All corridors are straight lines
Camera at the intersection of corridors
can monitor all those corridor.
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 5/8
Vertex cover
A hotel wants to install security cameras
All corridors are straight lines
Camera at the intersection of corridors
can monitor all those corridor.
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 5/8
Vertex cover
A hotel wants to install security cameras
All corridors are straight lines
v5
Camera at the intersection of corridors
can monitor all those corridor.
v2
Minimum number of cameras needed?
Represent the floor plan as a graph v0 v4
V — intersections of corridors
v1
E — corridor segments connecting
intersections
v3
Vertex cover
Marking v covers all edges from v
Mark smallest subset of V to cover all
edges
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 5/8
Vertex cover
A hotel wants to install security cameras
All corridors are straight lines
v5
Camera at the intersection of corridors
can monitor all those corridor.
v2
Minimum number of cameras needed?
Represent the floor plan as a graph v0 v4
V — intersections of corridors
v1
E — corridor segments connecting
intersections
v3
Vertex cover
Marking v covers all edges from v
Mark smallest subset of V to cover all
edges
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 5/8
Vertex cover
A hotel wants to install security cameras
All corridors are straight lines
v5
Camera at the intersection of corridors
can monitor all those corridor.
v2
Minimum number of cameras needed?
Represent the floor plan as a graph v0 v4
V — intersections of corridors
v1
E — corridor segments connecting
intersections
v3
Vertex cover
Marking v covers all edges from v
Mark smallest subset of V to cover all
edges
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 5/8
Independent set
A dance school puts up group dances
Each dance has a set of dancers
Sets of dancers may overlap across
dances
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 6/8
Independent set
A dance school puts up group dances
Each dance has a set of dancers
Sets of dancers may overlap across
dances
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 6/8
Independent set
A dance school puts up group dances
Each dance has a set of dancers
Sets of dancers may overlap across
dances
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 6/8
Independent set
A dance school puts up group dances
Each dance has a set of dancers
v4 v3
Sets of dancers may overlap across
dances
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 7/8
Matching
Class project can be done by one or two
people
If two people, they must be friends
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 7/8
Matching
Class project can be done by one or two
people
If two people, they must be friends
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 7/8
Matching
Class project can be done by one or two
people
v2 v4 v5
If two people, they must be friends
Matching
G = (V , E ), an undirected graph
A matching is a subset M ⊆ E of
mutually disjoint edges
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 7/8
Matching
Class project can be done by one or two
people
v2 v4 v5
If two people, they must be friends
Matching
G = (V , E ), an undirected graph
A matching is a subset M ⊆ E of
mutually disjoint edges
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 7/8
Matching
Class project can be done by one or two
people
v2 v4 v5
If two people, they must be friends
Matching
G = (V , E ), an undirected graph
A matching is a subset M ⊆ E of
mutually disjoint edges
Madhavan Mukund More on Graphs Mathematics for Data Science 1, Week 10 8/8
Representing Graphs
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
v9
v8
v7
v9
v8
v7
v9
v9
v8
v7
v9
v8
v7
v9
0 {1,4} 5 {3,7}
1 {2} 6 {5} v8
v7
2 {0} 7 {4,8}
3 {4,6} 8 {5,9}
4 {0,3,7} 9 {8} v9
0 {1,4} 5 {3,7}
1 {2} 6 {5}
2 {0} 7 {4,8}
3 {4,6} 8 {5,9}
4 {0,3,7} 9 {8}
0 {1,4} 5 {3,7}
1 {2} 6 {5}
2 {0} 7 {4,8}
3 {4,6} 8 {5,9}
4 {0,3,7} 9 {8}
Adjacency list
For each vertex i, list of neighbours of i
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
v8
v7
v9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 2 / 11
Reachability in a graph
Mark source vertex as reachable 0 1 2 3 4 5 6 7 8 9
0 0 1 0 0 1 0 0 0 0 0
Systematically mark neighbours of marked
1 0 0 1 0 0 0 0 0 0 0
vertices 2 1 0 0 0 0 0 0 0 0 0
3 0 0 0 0 1 0 1 0 0 0
Stop when target becomes marked 4 1 0 0 1 0 0 0 1 0 0
5 0 0 0 1 0 0 0 1 0 0
Choose an appropriate representation
6 0 0 0 0 0 1 0 0 0 0
Adjacency matrix 7 0 0 0 0 1 0 0 0 1 0
Adjacency list 8 0 0 0 0 0 1 0 0 0 1
9 0 0 0 0 0 0 0 0 1 0
0 {1,4} 5 {3,7}
1 {2} 6 {5}
2 {0} 7 {4,8}
3 {4,6} 8 {5,9}
4 {0,3,7} 9 {8}
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 2 / 11
Reachability in a graph
Mark source vertex as reachable 0 1 2 3 4 5 6 7 8 9
0 0 1 0 0 1 0 0 0 0 0
Systematically mark neighbours of marked
1 0 0 1 0 0 0 0 0 0 0
vertices 2 1 0 0 0 0 0 0 0 0 0
3 0 0 0 0 1 0 1 0 0 0
Stop when target becomes marked 4 1 0 0 1 0 0 0 1 0 0
5 0 0 0 1 0 0 0 1 0 0
Choose an appropriate representation
6 0 0 0 0 0 1 0 0 0 0
Adjacency matrix 7 0 0 0 0 1 0 0 0 1 0
Adjacency list 8 0 0 0 0 0 1 0 0 0 1
9 0 0 0 0 0 0 0 0 1 0
Strategies for systematic exploration
0 {1,4} 5 {3,7}
Breadth first — propagate marks in 1 {2} 6 {5}
“layers” 2 {0} 7 {4,8}
Depth first — explore a path till it dies 3 {4,6} 8 {5,9}
out, then backtrack 4 {0,3,7} 9 {8}
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 2 / 11
Breadth first search (BFS)
8
7
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 3 / 11
Breadth first search (BFS)
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 3 / 11
Breadth first search (BFS)
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 3 / 11
Breadth first search (BFS) . . .
Assume V = {0, 1, . . . , n − 1}
0
1
2
3
4 6
5
8
7
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 4 / 11
Breadth first search (BFS) . . .
Assume V = {0, 1, . . . , n − 1}
0
visited : V → {True, False} tells us 1
whether v ∈ V has been visited 2
Initially, visited(v ) = False for all v ∈ V 3
4 6
5
8
7
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 4 / 11
Breadth first search (BFS) . . .
Assume V = {0, 1, . . . , n − 1}
0
visited : V → {True, False} tells us 1
whether v ∈ V has been visited 2
Initially, visited(v ) = False for all v ∈ V 3
4 6
Maintain a sequence of visited vertices yet 5
be explored
A queue — first in, first out
Initially empty
8
7
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 4 / 11
Breadth first search (BFS) . . .
Assume V = {0, 1, . . . , n − 1}
0
visited : V → {True, False} tells us 1
whether v ∈ V has been visited 2
Initially, visited(v ) = False for all v ∈ V 3
4 6
Maintain a sequence of visited vertices yet 5
be explored
A queue — first in, first out
Initially empty
8
7
Exploring a vertex i
9
For each edge (i, j), if visited(j) is False,
Set visited(j) to True
Append j to the queue
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 4 / 11
Breadth first search (BFS) . . .
Initially
visited(v ) = False for all v ∈ V
0
1
Queue of vertices to be explored is empty
2
3
4 6
5
8
7
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 5 / 11
Breadth first search (BFS) . . .
Initially
visited(v ) = False for all v ∈ V
0
1
Queue of vertices to be explored is empty
2
Start BFS from vertex j 3
Set visited(j) = True 4 6
Add j to the queue 5
8
7
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 5 / 11
Breadth first search (BFS) . . .
Initially
visited(v ) = False for all v ∈ V
0
1
Queue of vertices to be explored is empty
2
Start BFS from vertex j 3
Set visited(j) = True 4 6
Add j to the queue 5
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 5 / 11
Breadth first search (BFS) . . .
Initially
visited(v ) = False for all v ∈ V
0
1
Queue of vertices to be explored is empty
2
Start BFS from vertex j 3
Set visited(j) = True 4 6
Add j to the queue 5
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 5 / 11
BFS from vertex 7
Visited To explore queue
0 False 0
1 False 1
2 False 2
3 False 3
4 False 4 6
5 False 5
6 False
7 False 8
8 False 7
9 False
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 6 / 11
BFS from vertex 7
Visited To explore queue
0 False 7 0
1 False 1
Mark 7 and add to queue 2
2 False
3 False 3
4 False 4 6
5 False 5
6 False
7 True 8
8 False 7
9 False
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 6 / 11
BFS from vertex 7
Visited To explore queue
0 False 4 5 8 0
1 False 1
Mark 7 and add to queue 2
2 False
Explore 7, visit {4,5,8} 3
3 False
4 True 4 6
5 True 5
6 False
7 True 8
8 True 7
9 False
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 6 / 11
BFS from vertex 7
Visited To explore queue
0 True 5 8 0 3 0
1 False 1
Mark 7 and add to queue 2
2 False
Explore 7, visit {4,5,8} 3
3 True
4 True Explore 4, visit {0,3} 4 6
5 True 5
6 False
7 True 8
8 True 7
9 False
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 6 / 11
BFS from vertex 7
Visited To explore queue
0 True 8 0 3 6 0
1 False 1
Mark 7 and add to queue 2
2 False
Explore 7, visit {4,5,8} 3
3 True
4 True Explore 4, visit {0,3} 4 6
5 True Explore 5, visit {6} 5
6 True
7 True 8
8 True 7
9 False
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 6 / 11
BFS from vertex 7
Visited To explore queue
0 True 0 3 6 9 0
1 False 1
Mark 7 and add to queue 2
2 False
Explore 7, visit {4,5,8} 3
3 True
4 True Explore 4, visit {0,3} 4 6
5 True Explore 5, visit {6} 5
6 True Explore 8, visit {9}
7 True 8
8 True 7
9 True
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 6 / 11
BFS from vertex 7
Visited To explore queue
0 True 3 6 9 1 2 0
1 True 1
Mark 7 and add to queue 2
2 True
Explore 7, visit {4,5,8} 3
3 True
4 True Explore 4, visit {0,3} 4 6
5 True Explore 5, visit {6} 5
6 True Explore 8, visit {9}
7 True Explore 0, visit {1,2} 8
8 True 7
9 True
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 6 / 11
BFS from vertex 7
Visited To explore queue
0 True 6 9 1 2 0
1 True 1
Mark 7 and add to queue 2
2 True
Explore 7, visit {4,5,8} 3
3 True
4 True Explore 4, visit {0,3} 4 6
5 True Explore 5, visit {6} 5
6 True Explore 8, visit {9}
7 True Explore 0, visit {1,2} 8
8 True 7
Explore 3
9 True
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 6 / 11
BFS from vertex 7
Visited To explore queue
0 True 9 1 2 0
1 True 1
Mark 7 and add to queue 2
2 True
Explore 7, visit {4,5,8} 3
3 True
4 True Explore 4, visit {0,3} 4 6
5 True Explore 5, visit {6} 5
6 True Explore 8, visit {9}
7 True Explore 0, visit {1,2} 8
8 True 7
Explore 3
9 True
Explore 6 9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 6 / 11
BFS from vertex 7
Visited To explore queue
0 True 1 2 0
1 True 1
Mark 7 and add to queue 2
2 True
Explore 7, visit {4,5,8} 3
3 True
4 True Explore 4, visit {0,3} 4 6
5 True Explore 5, visit {6} 5
6 True Explore 8, visit {9}
7 True Explore 0, visit {1,2} 8
8 True 7
Explore 3
9 True
Explore 6 9
Explore 9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 6 / 11
BFS from vertex 7
Visited To explore queue
0 True 2 0
1 True 1
Mark 7 and add to queue 2
2 True
Explore 7, visit {4,5,8} 3
3 True
4 True Explore 4, visit {0,3} 4 6
5 True Explore 5, visit {6} 5
6 True Explore 8, visit {9}
7 True Explore 0, visit {1,2} 8
8 True 7
Explore 3
9 True
Explore 6 9
Explore 9
Explore 1
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 6 / 11
BFS from vertex 7
Visited To explore queue
0 True 0
1 True 1
Mark 7 and add to queue 2
2 True
Explore 7, visit {4,5,8} 3
3 True
4 True Explore 4, visit {0,3} 4 6
5 True Explore 5, visit {6} 5
6 True Explore 8, visit {9}
7 True Explore 0, visit {1,2} 8
8 True 7
Explore 3
9 True
Explore 6 9
Explore 9
Explore 1
Explore 2
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 6 / 11
Enhancing BFS to record paths
8
7
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 7 / 11
Enhancing BFS to record paths
8
7
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 7 / 11
Enhancing BFS to record paths
8
7
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 7 / 11
Enhancing BFS to record paths
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 7 / 11
Enhancing BFS to record paths
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 7 / 11
BFS from vertex 7 with parent information
Visited Parent To explore queue
0 False 0
1 False 1
2 False 2
3 False 3
4 False 4 6
5 False 5
6 False
7 False 8
8 False 7
9 False
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 8 / 11
BFS from vertex 7 with parent information
Visited Parent To explore queue
0 False 7 0
1 False 1
Mark 7, add to queue 2
2 False
3 False 3
4 False 4 6
5 False 5
6 False
7 True 8
8 False 7
9 False
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 8 / 11
BFS from vertex 7 with parent information
Visited Parent To explore queue
0 False 4 5 8 0
1 False 1
2 False Mark 7, add to queue 2
3 False Explore 7, visit {4,5,8} 3
4 True 7 4 6
5 True 7 5
6 False
7 True 8
8 True 7 7
9 False
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 8 / 11
BFS from vertex 7 with parent information
Visited Parent To explore queue
0 True 4 5 8 0 3 0
1 False 1
2 False Mark 7, add to queue 2
3 True 4 Explore 7, visit {4,5,8} 3
4 True 7 4 6
Explore 4, visit {0,3}
5 True 7 5
6 False
7 True 8
8 True 7 7
9 False
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 8 / 11
BFS from vertex 7 with parent information
Visited Parent To explore queue
0 True 4 8 0 3 6 0
1 False 1
2 False Mark 7, add to queue 2
3 True 4 Explore 7, visit {4,5,8} 3
4 True 7 4 6
Explore 4, visit {0,3}
5 True 7 5
Explore 5, visit {6}
6 True 5
7 True 8
8 True 7 7
9 False
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 8 / 11
BFS from vertex 7 with parent information
Visited Parent To explore queue
0 True 4 0 3 6 9 0
1 False 1
2 False Mark 7, add to queue 2
3 True 4 Explore 7, visit {4,5,8} 3
4 True 7 4 6
Explore 4, visit {0,3}
5 True 7 5
Explore 5, visit {6}
6 True 5
Explore 8, visit {9}
7 True 8
8 True 7 7
9 True 8
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 8 / 11
BFS from vertex 7 with parent information
Visited Parent To explore queue
0 True 4 3 6 9 1 2 0
1 True 0 1
2 True 0 Mark 7, add to queue 2
3 True 4 Explore 7, visit {4,5,8} 3
4 True 7 4 6
Explore 4, visit {0,3}
5 True 7 5
Explore 5, visit {6}
6 True 5
Explore 8, visit {9}
7 True 8
8 True 7 Explore 0, visit {1,2} 7
9 True 8
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 8 / 11
BFS from vertex 7 with parent information
Visited Parent To explore queue
0 True 4 6 9 1 2 0
1 True 0 1
2 True 0 Mark 7, add to queue 2
3 True 4 Explore 7, visit {4,5,8} 3
4 True 7 4 6
Explore 4, visit {0,3}
5 True 7 5
Explore 5, visit {6}
6 True 5
Explore 8, visit {9}
7 True 8
8 True 7 Explore 0, visit {1,2} 7
9 True 8 Explore 3
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 8 / 11
BFS from vertex 7 with parent information
Visited Parent To explore queue
0 True 4 9 1 2 0
1 True 0 1
2 True 0 Mark 7, add to queue 2
3 True 4 Explore 7, visit {4,5,8} 3
4 True 7 4 6
Explore 4, visit {0,3}
5 True 7 5
Explore 5, visit {6}
6 True 5
Explore 8, visit {9}
7 True 8
8 True 7 Explore 0, visit {1,2} 7
9 True 8 Explore 3
Explore 6 9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 8 / 11
BFS from vertex 7 with parent information
Visited Parent To explore queue
0 True 4 1 2 0
1 True 0 1
2 True 0 Mark 7, add to queue 2
3 True 4 Explore 7, visit {4,5,8} 3
4 True 7 4 6
Explore 4, visit {0,3}
5 True 7 5
Explore 5, visit {6}
6 True 5
Explore 8, visit {9}
7 True 8
8 True 7 Explore 0, visit {1,2} 7
9 True 8 Explore 3
Explore 6 9
Explore 9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 8 / 11
BFS from vertex 7 with parent information
Visited Parent To explore queue
0 True 4 2 0
1 True 0 1
Mark 7, add to queue 2
2 True 0
Explore 7, visit {4,5,8} 3
3 True 4
4 True 7 Explore 4, visit {0,3} 4 6
5 True 7 Explore 5, visit {6} 5
6 True 5 Explore 8, visit {9}
7 True Explore 0, visit {1,2} 8
8 True 7 7
Explore 3
9 True 8
Explore 6 9
Explore 9
Explore 1
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 8 / 11
BFS from vertex 7 with parent information
Visited Parent To explore queue
0 True 4 0
1 True 0 1
Mark 7, add to queue 2
2 True 0
Explore 7, visit {4,5,8} 3
3 True 4
4 True 7 Explore 4, visit {0,3} 4 6
5 True 7 Explore 5, visit {6} 5
6 True 5 Explore 8, visit {9}
7 True Explore 0, visit {1,2} 8
8 True 7 7
Explore 3
9 True 8
Explore 6 9
Explore 9
Explore 1
Explore 2
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 8 / 11
BFS from vertex 7 with parent information
Visited Parent To explore queue
0 True 4 0
1 True 0 1
Mark 7, add to queue 2
2 True 0
Explore 7, visit {4,5,8} 3
3 True 4
4 True 7 Explore 4, visit {0,3} 4 6
5 True 7 Explore 5, visit {6} 5
6 True 5 Explore 8, visit {9}
7 True Explore 0, visit {1,2} 8
8 True 7 7
Explore 3
9 True 8
Explore 6 9
Path from 7 to 6 is Explore 9
7–5–6 Explore 1
Explore 2
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 8 / 11
BFS from vertex 7 with parent information
Visited Parent To explore queue
0 True 4 0
1 True 0 1
Mark 7, add to queue 2
2 True 0
Explore 7, visit {4,5,8} 3
3 True 4
4 True 7 Explore 4, visit {0,3} 4 6
5 True 7 Explore 5, visit {6} 5
6 True 5 Explore 8, visit {9}
7 True Explore 0, visit {1,2} 8
8 True 7 7
Explore 3
9 True 8
Explore 6 9
Path from 7 to 2 is Explore 9
7–4–0–2 Explore 1
Explore 2
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 8 / 11
Enhancing BFS to record distance
8
7
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 9 / 11
Enhancing BFS to record distance
8
7
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 9 / 11
Enhancing BFS to record distance
8
7
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 9 / 11
Enhancing BFS to record distance
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 9 / 11
Enhancing BFS to record distance
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 9 / 11
Enhancing BFS to record distance
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 9 / 11
Enhancing BFS to record distance
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 9 / 11
BFS from vertex 7 with parent and distance information
Level Parent To explore queue
0 −1 0
1 −1 1
2 −1 2
3 −1 3
4 −1 4 6
5 −1 5
6 −1
7 −1 8
8 −1 7
9 −1
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 10 / 11
BFS from vertex 7 with parent and distance information
Level Parent To explore queue
0 −1 7 0
1 −1 1
Mark 7, add to queue 2
2 −1
3 −1 3
4 −1 4 6
5 −1 5
6 −1
7 0 8
8 −1 7
9 −1
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 10 / 11
BFS from vertex 7 with parent and distance information
Level Parent To explore queue
0 −1 4 5 8 0
1 −1 1
2 −1 Mark 7, add to queue 2
3 −1 Explore 7, visit {4,5,8} 3
4 1 7 4 6
5 1 7 5
6 −1
7 0 8
8 1 7 7
9 −1
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 10 / 11
BFS from vertex 7 with parent and distance information
Level Parent To explore queue
0 2 4 5 8 0 3 0
1 −1 1
2 −1 Mark 7, add to queue 2
3 2 4 Explore 7, visit {4,5,8} 3
4 1 7 4 6
Explore 4, visit {0,3}
5 1 7 5
6 −1
7 0 8
8 1 7 7
9 −1
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 10 / 11
BFS from vertex 7 with parent and distance information
Level Parent To explore queue
0 2 4 8 0 3 6 0
1 −1 1
2 −1 Mark 7, add to queue 2
3 2 4 Explore 7, visit {4,5,8} 3
4 1 7 4 6
Explore 4, visit {0,3}
5 1 7 5
Explore 5, visit {6}
6 2 5
7 0 8
8 1 7 7
9 −1
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 10 / 11
BFS from vertex 7 with parent and distance information
Level Parent To explore queue
0 2 4 0 3 6 9 0
1 −1 1
2 −1 Mark 7, add to queue 2
3 2 4 Explore 7, visit {4,5,8} 3
4 1 7 4 6
Explore 4, visit {0,3}
5 1 7 5
Explore 5, visit {6}
6 2 5
Explore 8, visit {9}
7 0 8
8 1 7 7
9 2 8
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 10 / 11
BFS from vertex 7 with parent and distance information
Level Parent To explore queue
0 2 4 3 6 9 1 2 0
1 3 0 1
2 3 0 Mark 7, add to queue 2
3 2 4 Explore 7, visit {4,5,8} 3
4 1 7 4 6
Explore 4, visit {0,3}
5 1 7 5
Explore 5, visit {6}
6 2 5
Explore 8, visit {9}
7 0 8
8 1 7 Explore 0, visit {1,2} 7
9 2 8
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 10 / 11
BFS from vertex 7 with parent and distance information
Level Parent To explore queue
0 2 4 6 9 1 2 0
1 3 0 1
2 3 0 Mark 7, add to queue 2
3 2 4 Explore 7, visit {4,5,8} 3
4 1 7 4 6
Explore 4, visit {0,3}
5 1 7 5
Explore 5, visit {6}
6 2 5
Explore 8, visit {9}
7 0 8
8 1 7 Explore 0, visit {1,2} 7
9 2 8 Explore 3
9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 10 / 11
BFS from vertex 7 with parent and distance information
Level Parent To explore queue
0 2 4 9 1 2 0
1 3 0 1
2 3 0 Mark 7, add to queue 2
3 2 4 Explore 7, visit {4,5,8} 3
4 1 7 4 6
Explore 4, visit {0,3}
5 1 7 5
Explore 5, visit {6}
6 2 5
Explore 8, visit {9}
7 0 8
8 1 7 Explore 0, visit {1,2} 7
9 2 8 Explore 3
Explore 6 9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 10 / 11
BFS from vertex 7 with parent and distance information
Level Parent To explore queue
0 2 4 1 2 0
1 3 0 1
2 3 0 Mark 7, add to queue 2
3 2 4 Explore 7, visit {4,5,8} 3
4 1 7 4 6
Explore 4, visit {0,3}
5 1 7 5
Explore 5, visit {6}
6 2 5
Explore 8, visit {9}
7 0 8
8 1 7 Explore 0, visit {1,2} 7
9 2 8 Explore 3
Explore 6 9
Explore 9
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 10 / 11
BFS from vertex 7 with parent and distance information
Level Parent To explore queue
0 2 4 2 0
1 3 0 1
Mark 7, add to queue 2
2 3 0
Explore 7, visit {4,5,8} 3
3 2 4
4 1 7 Explore 4, visit {0,3} 4 6
5 1 7 Explore 5, visit {6} 5
6 2 5 Explore 8, visit {9}
7 0 Explore 0, visit {1,2} 8
8 1 7 7
Explore 3
9 2 8
Explore 6 9
Explore 9
Explore 1
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 10 / 11
BFS from vertex 7 with parent and distance information
Level Parent To explore queue
0 2 4 0
1 3 0 1
Mark 7, add to queue 2
2 3 0
Explore 7, visit {4,5,8} 3
3 2 4
4 1 7 Explore 4, visit {0,3} 4 6
5 1 7 Explore 5, visit {6} 5
6 2 5 Explore 8, visit {9}
7 0 Explore 0, visit {1,2} 8
8 1 7 7
Explore 3
9 2 8
Explore 6 9
Explore 9
Explore 1
Explore 2
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 10 / 11
Summary
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 11 / 11
Summary
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 11 / 11
Summary
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 11 / 11
Summary
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 11 / 11
Summary
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 11 / 11
Summary
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 11 / 11
Summary
Madhavan Mukund Breadth First Search Mathematics for Data Science 1, Week 10 11 / 11
Depth First Search
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
8
7
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 2/5
Depth first search (DFS)
8
7
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 2/5
Depth first search (DFS)
8
7
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 2/5
Depth first search (DFS)
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 2/5
Depth first search (DFS)
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 2/5
DFS from vertex 4
Visited Stack of suspended vertices
0 False 0
1 False 1
2 False 2
3 False 3
4 False 4 6
5 False 5
6 False
7 False 8
8 False 7
9 False
9
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 3/5
DFS from vertex 4
Visited Stack of suspended vertices
0 False 0
1 False 1
Mark 4,
2 False 2
3 False 3
4 True 4 6
5 False 5
6 False
7 False 8
8 False 7
9 False
9
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 3/5
DFS from vertex 4
Visited Stack of suspended vertices
0 True 4 0
1 False 1
Mark 4, Suspend 4, explore 0
2 False 2
3 False 3
4 True 4 6
5 False 5
6 False
7 False 8
8 False 7
9 False
9
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 3/5
DFS from vertex 4
Visited Stack of suspended vertices
0 True 4 0 0
1 True 1
Mark 4, Suspend 4, explore 0
2 False 2
suspend 0, explore 1 3
3 False
4 True 4 6
5 False 5
6 False
7 False 8
8 False 7
9 False
9
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 3/5
DFS from vertex 4
Visited Stack of suspended vertices
0 True 4 0 1 0
1 True 1
Mark 4, Suspend 4, explore 0
2 True 2
suspend 0, explore 1 3
3 False
Suspend 1, explore 2 4 6
4 True
5 False 5
6 False
7 False 8
8 False 7
9 False
9
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 3/5
DFS from vertex 4
Visited Stack of suspended vertices
0 True 4 0 0
1 True 1
Mark 4, Suspend 4, explore 0
2 True 2
suspend 0, explore 1 3
3 False
Suspend 1, explore 2 4 6
4 True
Backtrack to 1, 5
5 False
6 False
7 False 8
8 False 7
9 False
9
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 3/5
DFS from vertex 4
Visited Stack of suspended vertices
0 True 4 0
1 True 1
Mark 4, Suspend 4, explore 0
2 True 2
suspend 0, explore 1 3
3 False
Suspend 1, explore 2 4 6
4 True
Backtrack to 1, 0, 5
5 False
6 False
7 False 8
8 False 7
9 False
9
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 3/5
DFS from vertex 4
Visited Stack of suspended vertices
0 True 0
1 True 1
Mark 4, Suspend 4, explore 0
2 True 2
suspend 0, explore 1 3
3 True
Suspend 1, explore 2 4 6
4 True
Backtrack to 1, 0, 4 5
5 False
6 False
7 False 8
8 False 7
9 False
9
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 3/5
DFS from vertex 4
Visited Stack of suspended vertices
0 True 4 0
1 True 1
Mark 4, Suspend 4, explore 0
2 True 2
suspend 0, explore 1 3
3 True
Suspend 1, explore 2 4 6
4 True
Backtrack to 1, 0, 4 5
5 False
Suspend 4, explore 3
6 False
7 False 8
8 False 7
9 False
9
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 3/5
DFS from vertex 4
Visited Stack of suspended vertices
0 True 4 3 0
1 True 1
Mark 4, Suspend 4, explore 0
2 True 2
suspend 0, explore 1 3
3 True
Suspend 1, explore 2 4 6
4 True
Backtrack to 1, 0, 4 5
5 True
Suspend 4, explore 3
6 False
7 False Suspend 3, explore 5
8
8 False 7
9 False
9
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 3/5
DFS from vertex 4
Visited Stack of suspended vertices
0 True 4 3 5 0
1 True 1
Mark 4, Suspend 4, explore 0
2 True 2
suspend 0, explore 1 3
3 True
Suspend 1, explore 2 4 6
4 True
Backtrack to 1, 0, 4 5
5 True
Suspend 4, explore 3
6 True
7 False Suspend 3, explore 5
8
8 False Suspend 5, explore 6 7
9 False
9
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 3/5
DFS from vertex 4
Visited Stack of suspended vertices
0 True 4 3 0
1 True 1
Mark 4, Suspend 4, explore 0
2 True 2
suspend 0, explore 1 3
3 True
Suspend 1, explore 2 4 6
4 True
Backtrack to 1, 0, 4 5
5 True
Suspend 4, explore 3
6 True
7 False Suspend 3, explore 5
8
8 False Suspend 5, explore 6 7
9 False Backtrack to 5,
9
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 3/5
DFS from vertex 4
Visited Stack of suspended vertices
0 True 4 3 5 0
1 True 1
Mark 4, Suspend 4, explore 0
2 True 2
suspend 0, explore 1 3
3 True
Suspend 1, explore 2 4 6
4 True
Backtrack to 1, 0, 4 5
5 True
Suspend 4, explore 3
6 True
7 True Suspend 3, explore 5
8
8 False Suspend 5, explore 6 7
9 False Backtrack to 5, suspend 5,
explore 7 9
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 3/5
DFS from vertex 4
Visited Stack of suspended vertices
0 True 4 3 5 7 0
1 True 1
Mark 4, Suspend 4, explore 0
2 True 2
suspend 0, explore 1 3
3 True
Suspend 1, explore 2 4 6
4 True
Backtrack to 1, 0, 4 5
5 True
Suspend 4, explore 3
6 True
7 True Suspend 3, explore 5
8
8 True Suspend 5, explore 6 7
9 False Backtrack to 5, suspend 5,
explore 7 9
Suspend 7, explore 8
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 3/5
DFS from vertex 4
Visited Stack of suspended vertices
0 True 4 3 5 7 8 0
1 True 1
Mark 4, Suspend 4, explore 0
2 True 2
suspend 0, explore 1 3
3 True
Suspend 1, explore 2 4 6
4 True
Backtrack to 1, 0, 4 5
5 True
Suspend 4, explore 3
6 True
7 True Suspend 3, explore 5
8
8 True Suspend 5, explore 6 7
9 True Backtrack to 5, suspend 5,
explore 7 9
Suspend 7, explore 8
Suspend 8, explore 9
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 3/5
DFS from vertex 4
Visited Stack of suspended vertices
0 True 4 3 5 7 0
1 True 1
Mark 4, Suspend 4, explore 0
2 True 2
suspend 0, explore 1 3
3 True
Suspend 1, explore 2 4 6
4 True
Backtrack to 1, 0, 4 5
5 True
Suspend 4, explore 3
6 True
7 True Suspend 3, explore 5
8
8 True Suspend 5, explore 6 7
9 True Backtrack to 5, suspend 5,
explore 7 9
Suspend 7, explore 8
Suspend 8, explore 9
Backtrack to 8,
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 3/5
DFS from vertex 4
Visited Stack of suspended vertices
0 True 4 3 5 0
1 True 1
Mark 4, Suspend 4, explore 0
2 True 2
suspend 0, explore 1 3
3 True
Suspend 1, explore 2 4 6
4 True
Backtrack to 1, 0, 4 5
5 True
Suspend 4, explore 3
6 True
7 True Suspend 3, explore 5
8
8 True Suspend 5, explore 6 7
9 True Backtrack to 5, suspend 5,
explore 7 9
Suspend 7, explore 8
Suspend 8, explore 9
Backtrack to 8, 7,
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 3/5
DFS from vertex 4
Visited Stack of suspended vertices
0 True 4 3 0
1 True 1
Mark 4, Suspend 4, explore 0
2 True 2
suspend 0, explore 1 3
3 True
Suspend 1, explore 2 4 6
4 True
Backtrack to 1, 0, 4 5
5 True
Suspend 4, explore 3
6 True
7 True Suspend 3, explore 5
8
8 True Suspend 5, explore 6 7
9 True Backtrack to 5, suspend 5,
explore 7 9
Suspend 7, explore 8
Suspend 8, explore 9
Backtrack to 8, 7, 5,
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 3/5
DFS from vertex 4
Visited Stack of suspended vertices
0 True 4 0
1 True 1
Mark 4, Suspend 4, explore 0
2 True 2
suspend 0, explore 1 3
3 True
Suspend 1, explore 2 4 6
4 True
Backtrack to 1, 0, 4 5
5 True
Suspend 4, explore 3
6 True
7 True Suspend 3, explore 5
8
8 True Suspend 5, explore 6 7
9 True Backtrack to 5, suspend 5,
explore 7 9
Suspend 7, explore 8
Suspend 8, explore 9
Backtrack to 8, 7, 5, 3,
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 3/5
DFS from vertex 4
Visited Stack of suspended vertices
0 True 0
1 True 1
Mark 4, Suspend 4, explore 0
2 True 2
suspend 0, explore 1 3
3 True
Suspend 1, explore 2 4 6
4 True
Backtrack to 1, 0, 4 5
5 True
Suspend 4, explore 3
6 True
7 True Suspend 3, explore 5
8
8 True Suspend 5, explore 6 7
9 True Backtrack to 5, suspend 5,
explore 7 9
Suspend 7, explore 8
Suspend 8, explore 9
Backtrack to 8, 7, 5, 3, 4
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 3/5
Depth first search (DFS)
Paths discovered by BFS are not shortest
paths, unlike BFS 0
1
2
3
4 6
5
8
7
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 4/5
Depth first search (DFS)
Paths discovered by BFS are not shortest
paths, unlike BFS 0
1
Useful features can be found by recording
2
the order in which DFS visits vertices
3
4 6
5
8
7
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 4/5
Depth first search (DFS)
Paths discovered by BFS are not shortest
paths, unlike BFS 0
1
Useful features can be found by recording
2
the order in which DFS visits vertices
3
DFS numbering — maintain a counter 4 6
Increment and record counter value each 5
time you start and finish exploring a
vertex
8
7
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 4/5
Depth first search (DFS)
Paths discovered by BFS are not shortest
paths, unlike BFS 0
1
Useful features can be found by recording
2
the order in which DFS visits vertices
3
DFS numbering — maintain a counter 4 6
Increment and record counter value each 5
time you start and finish exploring a
vertex
8
DFS numbering can be used to 7
Find cut vertices (deleting vertex
disconnects graph) 9
Find bridges (deleting edge disconnects
graph)
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 4/5
Summary
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 5/5
Summary
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 5/5
Summary
Madhavan Mukund Depth First Search Mathematics for Data Science 1, Week 10 5/5
Complexity of BFS and DFS
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
G = (V , E )
|V | = n
|E | = m
If G is connected, m can vary from
n − 1 to n(n − 1)/2
Madhavan Mukund Complexity of BFS and DFS Mathematics for Data Science 1, Week 10 3/6
Complexity BFS and DFS
G = (V , E )
|V | = n
|E | = m
If G is connected, m can vary from
n − 1 to n(n − 1)/2
In both BFS and DFS, each reachable
vertex is visited exactly once
Visit and explore at most n vertices
Madhavan Mukund Complexity of BFS and DFS Mathematics for Data Science 1, Week 10 3/6
Complexity BFS and DFS
G = (V , E )
|V | = n
|E | = m
If G is connected, m can vary from
n − 1 to n(n − 1)/2
In both BFS and DFS, each reachable
vertex is visited exactly once
Visit and explore at most n vertices
Each visited vertex is explored once
Check all outgoing edges
How long does this take?
Madhavan Mukund Complexity of BFS and DFS Mathematics for Data Science 1, Week 10 3/6
Complexity BFS and DFS
G = (V , E ) Adjacency matrix
|V | = n To explore i, scan row i
|E | = m Look up n entries, regardless of number
If G is connected, m can vary from of actual edges from i
n − 1 to n(n − 1)/2 Degree of i
In both BFS and DFS, each reachable Overall, n2 steps
vertex is visited exactly once
Visit and explore at most n vertices
Each visited vertex is explored once
Check all outgoing edges
How long does this take?
Madhavan Mukund Complexity of BFS and DFS Mathematics for Data Science 1, Week 10 3/6
Complexity BFS and DFS
G = (V , E ) Adjacency matrix
|V | = n To explore i, scan row i
|E | = m Look up n entries, regardless of number
If G is connected, m can vary from of actual edges from i
n − 1 to n(n − 1)/2 Degree of i
In both BFS and DFS, each reachable Overall, n2 steps
vertex is visited exactly once
Adjacency list
Visit and explore at most n vertices
To explore i, scan list of neighbours of i
Each visited vertex is explored once
Time to explore i is degree of i
Check all outgoing edges
How long does this take? Degree varies across vertices
Estimate overall time?
Madhavan Mukund Complexity of BFS and DFS Mathematics for Data Science 1, Week 10 3/6
Calculating with degrees
Adjacency list
To explore i, scan list of neighbours of i
Time to explore i is degree of i
Total time is the sum of the degrees
Madhavan Mukund Complexity of BFS and DFS Mathematics for Data Science 1, Week 10 4/6
Calculating with degrees
Adjacency list
To explore i, scan list of neighbours of i
Time to explore i is degree of i
Total time is the sum of the degrees
Sum of degrees
Each edge (i, j) contributes to degree of
both i and j
Sum of degrees is 2m
Madhavan Mukund Complexity of BFS and DFS Mathematics for Data Science 1, Week 10 4/6
Calculating with degrees
Madhavan Mukund Complexity of BFS and DFS Mathematics for Data Science 1, Week 10 4/6
Calculating with degrees
Madhavan Mukund Complexity of BFS and DFS Mathematics for Data Science 1, Week 10 4/6
Calculating with degrees
Degree of a vertex i is
Madhavan Mukund Complexity of BFS and DFS Mathematics for Data Science 1, Week 10 5/6
More about degrees
Degree of a vertex i is
Number of 1’s in row i of adjacency
matrix
Madhavan Mukund Complexity of BFS and DFS Mathematics for Data Science 1, Week 10 5/6
More about degrees
Degree of a vertex i is
Number of 1’s in row i of adjacency
matrix
Number of 1’s in column i of
adjacency matrix
Madhavan Mukund Complexity of BFS and DFS Mathematics for Data Science 1, Week 10 5/6
More about degrees
Degree of a vertex i is
Number of 1’s in row i of adjacency
matrix
Number of 1’s in column i of
adjacency matrix
Length of adjacency list for i
Madhavan Mukund Complexity of BFS and DFS Mathematics for Data Science 1, Week 10 5/6
More about degrees
Degree of a vertex i is
Number of 1’s in row i of adjacency
matrix
Number of 1’s in column i of
adjacency matrix
Length of adjacency list for i
Sum of degrees is 2m
Madhavan Mukund Complexity of BFS and DFS Mathematics for Data Science 1, Week 10 5/6
More about degrees
Degree of a vertex i is
Number of 1’s in row i of adjacency
matrix
Number of 1’s in column i of
adjacency matrix
Length of adjacency list for i
Sum of degrees is 2m
Sum is an even number
Madhavan Mukund Complexity of BFS and DFS Mathematics for Data Science 1, Week 10 5/6
More about degrees
Degree of a vertex i is
Number of 1’s in row i of adjacency
matrix
Number of 1’s in column i of
adjacency matrix
Length of adjacency list for i
Sum of degrees is 2m
Sum is an even number
For each vertex with odd degree, must
be another vertex of odd degree to
make the sum even
Madhavan Mukund Complexity of BFS and DFS Mathematics for Data Science 1, Week 10 5/6
More about degrees
Degree of a vertex i is
Number of 1’s in row i of adjacency
matrix
Number of 1’s in column i of
adjacency matrix
Length of adjacency list for i
Sum of degrees is 2m
Sum is an even number
For each vertex with odd degree, must
be another vertex of odd degree to
make the sum even
Number of vertices of odd degree is
even
Madhavan Mukund Complexity of BFS and DFS Mathematics for Data Science 1, Week 10 5/6
More about degrees
Madhavan Mukund Complexity of BFS and DFS Mathematics for Data Science 1, Week 10 5/6
More about degrees
Madhavan Mukund Complexity of BFS and DFS Mathematics for Data Science 1, Week 10 5/6
More about degrees
Madhavan Mukund Complexity of BFS and DFS Mathematics for Data Science 1, Week 10 5/6
More about degrees
Madhavan Mukund Complexity of BFS and DFS Mathematics for Data Science 1, Week 10 5/6
More about degrees
Madhavan Mukund Complexity of BFS and DFS Mathematics for Data Science 1, Week 10 6/6
Directed Acyclic Graphs (DAGs)
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
7 6
Madhavan Mukund Directed Acyclic Graphs (DAGs) Mathematics for Data Science 1, Week 11 2/7
Directed cycles
Madhavan Mukund Directed Acyclic Graphs (DAGs) Mathematics for Data Science 1, Week 11 2/7
Directed cycles
Madhavan Mukund Directed Acyclic Graphs (DAGs) Mathematics for Data Science 1, Week 11 2/7
Directed cycles
Madhavan Mukund Directed Acyclic Graphs (DAGs) Mathematics for Data Science 1, Week 11 2/7
Tasks and dependencies
Madhavan Mukund Directed Acyclic Graphs (DAGs) Mathematics for Data Science 1, Week 11 3/7
Tasks and dependencies
Madhavan Mukund Directed Acyclic Graphs (DAGs) Mathematics for Data Science 1, Week 11 3/7
Tasks and dependencies
Madhavan Mukund Directed Acyclic Graphs (DAGs) Mathematics for Data Science 1, Week 11 3/7
Tasks and dependencies
Madhavan Mukund Directed Acyclic Graphs (DAGs) Mathematics for Data Science 1, Week 11 3/7
Tasks and dependencies
Madhavan Mukund Directed Acyclic Graphs (DAGs) Mathematics for Data Science 1, Week 11 3/7
Tasks and dependencies
Madhavan Mukund Directed Acyclic Graphs (DAGs) Mathematics for Data Science 1, Week 11 3/7
Tasks and dependencies
Madhavan Mukund Directed Acyclic Graphs (DAGs) Mathematics for Data Science 1, Week 11 3/7
Tasks and dependencies
Madhavan Mukund Directed Acyclic Graphs (DAGs) Mathematics for Data Science 1, Week 11 3/7
Tasks and dependencies
Constraints on the sequence
Lay conduits before tiles and
plastering
Lay tiles, plaster wall before
painting
Finish painting before any
cabling/wiring work
Electrical wiring before installing
fittings
Tiling Plastering
Painting
Fittings (E)
Madhavan Mukund Directed Acyclic Graphs (DAGs) Mathematics for Data Science 1, Week 11 5/7
Typical questions
Fittings (E)
Madhavan Mukund Directed Acyclic Graphs (DAGs) Mathematics for Data Science 1, Week 11 5/7
Typical questions
Fittings (E)
Madhavan Mukund Directed Acyclic Graphs (DAGs) Mathematics for Data Science 1, Week 11 5/7
Typical questions
Madhavan Mukund Directed Acyclic Graphs (DAGs) Mathematics for Data Science 1, Week 11 5/7
Directed Acyclic Graphs
Painting
Fittings (E)
Madhavan Mukund Directed Acyclic Graphs (DAGs) Mathematics for Data Science 1, Week 11 6/7
Directed Acyclic Graphs
Fittings (E)
Madhavan Mukund Directed Acyclic Graphs (DAGs) Mathematics for Data Science 1, Week 11 6/7
Directed Acyclic Graphs
Fittings (E)
Madhavan Mukund Directed Acyclic Graphs (DAGs) Mathematics for Data Science 1, Week 11 6/7
Directed Acyclic Graphs
Madhavan Mukund Directed Acyclic Graphs (DAGs) Mathematics for Data Science 1, Week 11 6/7
Summary
Madhavan Mukund Directed Acyclic Graphs (DAGs) Mathematics for Data Science 1, Week 11 7/7
Topological Sorting
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
2
4
5 6 7
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 2/8
Directed Acyclic Graphs
5 6 7
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 2/8
Directed Acyclic Graphs
5 6 7
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 2/8
Topological Sort
2
4
5 6 7
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 3/8
Topological Sort
5 6 7
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 3/8
Topological Sort
5 6 7
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 3/8
Topological Sort
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 3/8
Topological Sort
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 3/8
How to topologically sort a DAG?
Strategy
0 1
2
4
5 6 7
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 4/8
How to topologically sort a DAG?
Strategy
First list vertices with no 0 1
dependencies
2
4
5 6 7
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 4/8
How to topologically sort a DAG?
Strategy
First list vertices with no 0 1
dependencies
As we proceed, list vertices whose
2
dependencies have already been listed
4
...
3
5 6 7
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 4/8
How to topologically sort a DAG?
Strategy
First list vertices with no 0 1
dependencies
As we proceed, list vertices whose
2
dependencies have already been listed
4
...
3
Questions
5 6 7
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 4/8
How to topologically sort a DAG?
Strategy
First list vertices with no 0 1
dependencies
As we proceed, list vertices whose
2
dependencies have already been listed
4
...
3
Questions
Why will there be a starting vertex 5 6 7
with no dependencies?
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 4/8
How to topologically sort a DAG?
Strategy
First list vertices with no 0 1
dependencies
As we proceed, list vertices whose
2
dependencies have already been listed
4
...
3
Questions
Why will there be a starting vertex 5 6 7
with no dependencies?
How do we guarantee we can keep
progressing with the listing?
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 4/8
Algorithm for topological sort
A vertex with no dependencies has no
incoming edges, indegree(v ) = 0
0 1
2
4
5 6 7
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 5/8
Algorithm for topological sort
A vertex with no dependencies has no
incoming edges, indegree(v ) = 0
0 1
Claim
Every DAG has a vertex with indegree 0
2
4
5 6 7
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 5/8
Algorithm for topological sort
A vertex with no dependencies has no
incoming edges, indegree(v ) = 0
0 1
Claim
Every DAG has a vertex with indegree 0
2
Start with any vertex with 4
indegree > 0
3
5 6 7
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 5/8
Algorithm for topological sort
A vertex with no dependencies has no
incoming edges, indegree(v ) = 0
0 1
Claim
Every DAG has a vertex with indegree 0
2
Start with any vertex with 4
indegree > 0
3
Follow edge back to one of its
predecessors
5 6 7
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 5/8
Algorithm for topological sort
A vertex with no dependencies has no
incoming edges, indegree(v ) = 0
0 1
Claim
Every DAG has a vertex with indegree 0
2
Start with any vertex with 4
indegree > 0
3
Follow edge back to one of its
predecessors
5 6 7
Repeat so long as indegree > 0
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 5/8
Algorithm for topological sort
A vertex with no dependencies has no
incoming edges, indegree(v ) = 0
0 1
Claim
Every DAG has a vertex with indegree 0
2
Start with any vertex with 4
indegree > 0
3
Follow edge back to one of its
predecessors
5 6 7
Repeat so long as indegree > 0
If we repeat n times, we must have a
cycle, which is impossible in a DAG
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 5/8
Topological sort algorithm
Fact
Every DAG has a vertex with indegree 0 0 1
2
4
5 6 7
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 6/8
Topological sort algorithm
Fact
Every DAG has a vertex with indegree 0 0 1
5 6 7
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 6/8
Topological sort algorithm
Fact
Every DAG has a vertex with indegree 0 0 1
5 6 7
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 6/8
Topological sort algorithm
Fact
Every DAG has a vertex with indegree 0 0 1
5 6 7
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 6/8
Topological sort algorithm
Fact
Every DAG has a vertex with indegree 0 0 1
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 6/8
Topological sort algorithm
Fact
Every DAG has a vertex with indegree 0 0 1
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 6/8
Topological sort algorithm
Compute indegree of each vertex
0 1
2
4
3
5 6 7
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 7/8
Topological sort algorithm
Compute indegree of each vertex Indegree
Scan each column of the adjacency 0 0
matrix 0 1
2
2 1
4
1
3
2 1 4
5 6 7
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 7/8
Topological sort algorithm
Compute indegree of each vertex Indegree
Scan each column of the adjacency 0
matrix 0
List a vertex with indegree 0 and
remove it from the DAG 2
2 1
4
1
3
2 1 4
5 6 7
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 7/8
Topological sort algorithm
Compute indegree of each vertex Indegree
Scan each column of the adjacency 0
matrix 0
List a vertex with indegree 0 and
remove it from the DAG 1
2 1
Update indegrees 4
1
3
2 1 3
5 6 7
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 7/8
Topological sort algorithm
Compute indegree of each vertex Indegree
Scan each column of the adjacency
matrix
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 7/8
Topological sort algorithm
Compute indegree of each vertex Indegree
Scan each column of the adjacency
matrix
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 7/8
Topological sort algorithm
Compute indegree of each vertex Indegree
Scan each column of the adjacency
matrix
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 7/8
Topological sort algorithm
Compute indegree of each vertex Indegree
Scan each column of the adjacency
matrix
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 7/8
Topological sort algorithm
Compute indegree of each vertex Indegree
Scan each column of the adjacency
matrix
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 7/8
Topological sort algorithm
Compute indegree of each vertex Indegree
Scan each column of the adjacency
matrix
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 7/8
Topological sort algorithm
Compute indegree of each vertex Indegree
Scan each column of the adjacency
matrix
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 7/8
Topological sort algorithm
Compute indegree of each vertex Indegree
Scan each column of the adjacency
matrix
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 7/8
Topological sort algorithm
Compute indegree of each vertex Indegree
Scan each column of the adjacency
matrix
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 7/8
Topological sort algorithm
Compute indegree of each vertex Indegree
Scan each column of the adjacency
matrix
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 7/8
Topological sort algorithm
Compute indegree of each vertex Indegree
Scan each column of the adjacency
matrix
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 7/8
Topological sort algorithm
Compute indegree of each vertex Indegree
Scan each column of the adjacency
matrix
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 7/8
Topological sort algorithm
Compute indegree of each vertex Indegree
Scan each column of the adjacency
matrix
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 7/8
Topological sort algorithm
Compute indegree of each vertex
Scan each column of the adjacency
matrix 0 1
List a vertex with indegree 0 and
remove it from the DAG
2
Update indegrees 4
Can find another vertex with 3
indegree = 0 to list and eliminate
5 6 7
Repeat till all vertices are listed
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 7/8
Topological sort algorithm
Compute indegree of each vertex
Scan each column of the adjacency
matrix 0 1
List a vertex with indegree 0 and
remove it from the DAG
2
Update indegrees 4
Can find another vertex with 3
indegree = 0 to list and eliminate
5 6 7
Repeat till all vertices are listed
Using adjacency lists? Topologically sorted sequence
Scan each list i → [j1 , j2 , . . . , jk ] 1, 0, 3, 2, 5, 6, 4, 7
Increment indegree(j` ) for each j`
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 7/8
Summary
Madhavan Mukund Topological Sorting Mathematics for Data Science 1, Week 11 8/8
Longest Paths in DAGs
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
2
4
5 6 7
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 2/7
Directed Acyclic Graphs
5 6 7
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 2/7
Directed Acyclic Graphs
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 2/7
Directed Acyclic Graphs
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 2/7
Directed Acyclic Graphs
2
4
5 6 7
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 3/7
Longest Path
2
4
5 6 7
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 3/7
Longest Path
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 3/7
Longest Path
longest-path-to(i) =
1 + max{longest-path-to(j) | (j, i) ∈ E } 0 1
2
4
5 6 7
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 4/7
Longest Path
longest-path-to(i) =
1 + max{longest-path-to(j) | (j, i) ∈ E } 0 1
To compute longest-path-to(i), need
longest-path-to(k), for each incoming
neighbour k 2
4
5 6 7
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 4/7
Longest Path
longest-path-to(i) =
1 + max{longest-path-to(j) | (j, i) ∈ E } 0 1
To compute longest-path-to(i), need
longest-path-to(k), for each incoming
neighbour k 2
4
If graph is topologically sorted, k is
listed before i 3
5 6 7
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 4/7
Longest Path
longest-path-to(i) =
1 + max{longest-path-to(j) | (j, i) ∈ E } 0 1
To compute longest-path-to(i), need
longest-path-to(k), for each incoming
neighbour k 2
4
If graph is topologically sorted, k is
listed before i 3
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 4/7
Longest Path
2
4
5 6 7
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 5/7
Longest Path
5 6 7
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 5/7
Longest Path
5 6 7
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 5/7
Longest Path
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 5/7
Longest path algorithm
Compute indegree of each vertex
0 1
2
4
3
5 6 7
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 6/7
Longest path algorithm
Compute indegree of each vertex Indegree, Longest path
Scan each column of the adjacency 0 0 0 0
matrix 0 1
Initialize textlongest − path − to to 0
for all vertices 2
2 1
0 4
0 1 0
3
2 1 4
5 6 7
0 0 0
Topological order
Longest path to
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 6/7
Longest path algorithm
Compute indegree of each vertex Indegree, Longest path
Scan each column of the adjacency 0 0
matrix 0
Initialize textlongest − path − to to 0
for all vertices 2
2 1
List a vertex with indegree 0 and 0 4
remove it from the DAG 0 1 0
3
2 1 4
5 6 7
0 0 0
Topological order 1
Longest path to 0
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 6/7
Longest path algorithm
Compute indegree of each vertex Indegree, Longest path
Scan each column of the adjacency 0 0
matrix 0
Initialize textlongest − path − to to 0
for all vertices 1
2 1
List a vertex with indegree 0 and 1 4
remove it from the DAG 0 1 0
3
Update indegrees, longest path 2 1 3
5 6 7
0 0 1
Topological order 1
Longest path to 0
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 6/7
Longest path algorithm
Compute indegree of each vertex Indegree, Longest path
Scan each column of the adjacency
matrix
Topological order 1 0
Longest path to 0 0
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 6/7
Longest path algorithm
Compute indegree of each vertex Indegree, Longest path
Scan each column of the adjacency
matrix
Topological order 1 0
Longest path to 0 0
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 6/7
Longest path algorithm
Compute indegree of each vertex Indegree, Longest path
Scan each column of the adjacency
matrix
Topological order 1 0 3
Longest path to 0 0 1
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 6/7
Longest path algorithm
Compute indegree of each vertex Indegree, Longest path
Scan each column of the adjacency
matrix
Topological order 1 0 3
Longest path to 0 0 1
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 6/7
Longest path algorithm
Compute indegree of each vertex Indegree, Longest path
Scan each column of the adjacency
matrix
Topological order 1 0 3 2
Longest path to 0 0 1 1
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 6/7
Longest path algorithm
Compute indegree of each vertex Indegree, Longest path
Scan each column of the adjacency
matrix
Topological order 1 0 3 2
Longest path to 0 0 1 1
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 6/7
Longest path algorithm
Compute indegree of each vertex Indegree, Longest path
Scan each column of the adjacency
matrix
Topological order 1 0 3 2 5
Longest path to 0 0 1 1 2
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 6/7
Longest path algorithm
Compute indegree of each vertex Indegree, Longest path
Scan each column of the adjacency
matrix
Topological order 1 0 3 2 5
Longest path to 0 0 1 1 2
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 6/7
Longest path algorithm
Compute indegree of each vertex Indegree, Longest path
Scan each column of the adjacency
matrix
Topological order 1 0 3 2 5 6
Longest path to 0 0 1 1 2 3
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 6/7
Longest path algorithm
Compute indegree of each vertex Indegree, Longest path
Scan each column of the adjacency
matrix
Topological order 1 0 3 2 5 6
Longest path to 0 0 1 1 2 3
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 6/7
Longest path algorithm
Compute indegree of each vertex Indegree, Longest path
Scan each column of the adjacency
matrix
Topological order 1 0 3 2 5 6 4
Longest path to 0 0 1 1 2 3 1
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 6/7
Longest path algorithm
Compute indegree of each vertex Indegree, Longest path
Scan each column of the adjacency
matrix
Topological order 1 0 3 2 5 6 4
Longest path to 0 0 1 1 2 3 1
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 6/7
Longest path algorithm
Compute indegree of each vertex Indegree, Longest path
Scan each column of the adjacency
matrix
Topological order 1 0 3 2 5 6 4 7
Longest path to 0 0 1 1 2 3 1 4
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 6/7
Longest path algorithm
Compute indegree of each vertex
Scan each column of the adjacency
matrix 0 1
Initialize textlongest − path − to to 0
for all vertices
2
List a vertex with indegree 0 and 4
remove it from the DAG 3
Update indegrees, longest path
5 6 7
Repeat till all vertices are listed
Topological order 1 0 3 2 5 6 4 7
Longest path to 0 0 1 1 2 3 1 4
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 6/7
Summary
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 7/7
Summary
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 7/7
Summary
However, computing longest paths in arbitrary graphs is much harder than for DAGs
No better strategy known than exhaustively enumerating paths
Madhavan Mukund Longest Paths in DAGs Mathematics for Data Science 1, Week 11 7/7
Transitive Closure
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 2/8
Transitive closure of a relation
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 2/8
Transitive closure of a relation
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 2/8
Transitive closure of a relation
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 2/8
Transitive closure of a relation
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 2/8
Computing transitive closure
v8
v7
v9
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 3/8
Computing transitive closure
v8
v7
v9
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 3/8
Computing transitive closure
v9
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 3/8
Computing transitive closure
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 3/8
Using the adjacency matrix
Another strategy
v0
v1
v2
v3
v4 v6
v5
v8
v7
v9
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 4/8
Using the adjacency matrix
Another strategy
Consider the adjacency matrix A for G v0
0 1 2 3 4 5 6 7 8 9 v1
0 0 1 0 0 1 0 0 0 0 0
v2
1 0 0 1 0 0 0 0 0 0 0
2 1 0 0 0 0 0 0 0 0 0 v3
3 0 0 0 0 1 0 1 0 0 0 v4 v6
4 1 0 0 1 0 0 0 1 0 0 v5
5 0 0 0 1 0 0 0 1 0 0
6 0 0 0 0 0 1 0 0 0 0
7 0 0 0 0 1 0 0 0 1 0 v8
8 0 0 0 0 0 1 0 0 0 1 v7
9 0 0 0 0 0 0 0 0 1 0
v9
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 4/8
Using the adjacency matrix
Another strategy
Consider the adjacency matrix A for G v0
0 1 2 3 4 5 6 7 8 9 v1
0 0 1 0 0 1 0 0 0 0 0
v2
1 0 0 1 0 0 0 0 0 0 0
2 1 0 0 0 0 0 0 0 0 0 v3
3 0 0 0 0 1 0 1 0 0 0 v4 v6
4 1 0 0 1 0 0 0 1 0 0 v5
5 0 0 0 1 0 0 0 1 0 0
6 0 0 0 0 0 1 0 0 0 0
7 0 0 0 0 1 0 0 0 1 0 v8
8 0 0 0 0 0 1 0 0 0 1 v7
9 0 0 0 0 0 0 0 0 1 0
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 4/8
Using the adjacency matrix
Another strategy
Consider the adjacency matrix A for G v0
0 1 2 3 4 5 6 7 8 9 v1
0 0 1 0 0 1 0 0 0 0 0
v2
1 0 0 1 0 0 0 0 0 0 0
2 1 0 0 0 0 0 0 0 0 0 v3
3 0 0 0 0 1 0 1 0 0 0 v4 v6
4 1 0 0 1 0 0 0 1 0 0 v5
5 0 0 0 1 0 0 0 1 0 0
6 0 0 0 0 0 1 0 0 0 0
7 0 0 0 0 1 0 0 0 1 0 v8
8 0 0 0 0 0 1 0 0 0 1 v7
9 0 0 0 0 0 0 0 0 1 0
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 5/8
Paths of length 2
A A2
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
0 0 1 0 0 1 0 0 0 0 0 0 1 0 1 1 0 0 0 1 0 0
1 0 0 1 0 0 0 0 0 0 0 1
2 1 0 0 0 0 0 0 0 0 0 2
3 0 0 0 0 1 0 1 0 0 0 3
4 1 0 0 1 0 0 0 1 0 0 4
5 0 0 0 1 0 0 0 1 0 0 5
6 0 0 0 0 0 1 0 0 0 0 6
7 0 0 0 0 1 0 0 0 1 0 7
8 0 0 0 0 0 1 0 0 0 1 8
9 0 0 0 0 0 0 0 0 1 0 9
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 5/8
Paths of length 2
A A2
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
0 0 1 0 0 1 0 0 0 0 0 0 1 0 1 1 0 0 0 1 0 0
1 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0
2 1 0 0 0 0 0 0 0 0 0 2
3 0 0 0 0 1 0 1 0 0 0 3
4 1 0 0 1 0 0 0 1 0 0 4
5 0 0 0 1 0 0 0 1 0 0 5
6 0 0 0 0 0 1 0 0 0 0 6
7 0 0 0 0 1 0 0 0 1 0 7
8 0 0 0 0 0 1 0 0 0 1 8
9 0 0 0 0 0 0 0 0 1 0 9
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 5/8
Paths of length 2
A A2
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
0 0 1 0 0 1 0 0 0 0 0 0 1 0 1 1 0 0 0 1 0 0
1 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0
2 1 0 0 0 0 0 0 0 0 0 2 0 1 0 1 0 0 0 0 0 0
3 0 0 0 0 1 0 1 0 0 0 3
4 1 0 0 1 0 0 0 1 0 0 4
5 0 0 0 1 0 0 0 1 0 0 5
6 0 0 0 0 0 1 0 0 0 0 6
7 0 0 0 0 1 0 0 0 1 0 7
8 0 0 0 0 0 1 0 0 0 1 8
9 0 0 0 0 0 0 0 0 1 0 9
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 5/8
Paths of length 2
A A2
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
0 0 1 0 0 1 0 0 0 0 0 0 1 0 1 1 0 0 0 1 0 0
1 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0
2 1 0 0 0 0 0 0 0 0 0 2 0 1 0 1 0 0 0 0 0 0
3 0 0 0 0 1 0 1 0 0 0 3
4 1 0 0 1 0 0 0 1 0 0 4
5 0 0 0 1 0 0 0 1 0 0 5
6 0 0 0 0 0 1 0 0 0 0 6
7 0 0 0 0 1 0 0 0 1 0 7
8 0 0 0 0 0 1 0 0 0 1 8
9 0 0 0 0 0 0 0 0 1 0 9
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 5/8
Paths of length 2
A A2
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
0 0 1 0 0 1 0 0 0 0 0 0 1 0 1 1 0 0 0 1 0 0
1 0 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0
2 1 0 0 0 0 0 0 0 0 0 2 0 1 0 1 0 0 0 0 0 0
3 0 0 0 0 1 0 1 0 0 0 3 1 0 0 0 0 1 0 1 0 0
4 1 0 0 1 0 0 0 1 0 0 4 0 1 0 0 0 0 1 0 1 0
5 0 0 0 1 0 0 0 1 0 0 5 0 0 0 0 1 0 1 0 1 0
6 0 0 0 0 0 1 0 0 0 0 6 0 0 0 1 0 0 0 1 0 0
7 0 0 0 0 1 0 0 0 1 0 7 0 0 0 0 0 1 0 0 0 1
8 0 0 0 0 0 1 0 0 0 1 8 0 0 1 0 0 0 0 1 0 0
9 0 0 0 0 0 0 0 0 1 0 9 0 0 0 0 0 1 0 0 0 0
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 5/8
Paths of length 3 and beyond
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 6/8
Paths of length 3 and beyond
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 6/8
Paths of length 3 and beyond
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 6/8
Paths of length 3 and beyond
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 6/8
Paths of length 3 and beyond
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 6/8
Back to transitive closure
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 7/8
Back to transitive closure
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 7/8
Back to transitive closure
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 7/8
Back to transitive closure
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 7/8
Back to transitive closure
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 7/8
Back to transitive closure
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 7/8
Back to transitive closure
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 7/8
Back to transitive closure
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 7/8
Back to transitive closure
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 7/8
Summary
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 8/8
Summary
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 8/8
Summary
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 8/8
Summary
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 8/8
Summary
Madhavan Mukund Transitive Closure Mathematics for Data Science 1, Week 11 8/8
Matrix Multiplication
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 2/9
Matrices
A matrix is a two dimensional table
r × c matrix — r rows, c columns
Assume rows are numbered {0, 1, . . . , r −1}, columns are numbered {0, 1, . . . , c−1}
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 2/9
Matrices
A matrix is a two dimensional table
r × c matrix — r rows, c columns
Assume rows are numbered {0, 1, . . . , r −1}, columns are numbered {0, 1, . . . , c−1}
Graph with n nodes, n × n adjacency matrix, entries are from {0, 1}
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 2/9
Matrices
A matrix is a two dimensional table
r × c matrix — r rows, c columns
Assume rows are numbered {0, 1, . . . , r −1}, columns are numbered {0, 1, . . . , c−1}
Graph with n nodes, n × n adjacency matrix, entries are from {0, 1}
Example: Freight traffic by rail between major cities: Bangalore, Chennai, Delhi,
Hyderabad, Kolkata, Mumbai
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 2/9
Matrices
A matrix is a two dimensional table
r × c matrix — r rows, c columns
Assume rows are numbered {0, 1, . . . , r −1}, columns are numbered {0, 1, . . . , c−1}
Graph with n nodes, n × n adjacency matrix, entries are from {0, 1}
Example: Freight traffic by rail between major cities: Bangalore, Chennai, Delhi,
Hyderabad, Kolkata, Mumbai
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 2/9
Matrices
A matrix is a two dimensional table
r × c matrix — r rows, c columns
Assume rows are numbered {0, 1, . . . , r −1}, columns are numbered {0, 1, . . . , c−1}
Graph with n nodes, n × n adjacency matrix, entries are from {0, 1}
Example: Freight traffic by rail between major cities: Bangalore, Chennai, Delhi,
Hyderabad, Kolkata, Mumbai
Suppose we have freight volumes for first and second half of financial year.
April–September October–March
0 1 2 3 4 5 0 1 2 3 4 5
0 0 694 828 384 247 479 0 0 851 626 280 399 365
1 642 0 919 575 402 673 1 544 0 479 269 432 933
2 768 734 0 231 595 540 2 867 804 0 681 326 398
3 731 606 156 0 351 804 3 727 976 418 0 667 294
4 825 607 316 490 0 998 4 894 390 247 547 0 314
5 196 580 339 588 394 0 5 914 147 574 859 524 0
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 3/9
Adding matrices
Suppose we have freight volumes for first and second half of financial year.
April–September October–March
0 1 2 3 4 5 0 1 2 3 4 5
0 0 694 828 384 247 479 0 0 851 626 280 399 365
1 642 0 919 575 402 673 1 544 0 479 269 432 933
2 768 734 0 231 595 540 2 867 804 0 681 326 398
3 731 606 156 0 351 804 3 727 976 418 0 667 294
4 825 607 316 490 0 998 4 894 390 247 547 0 314
5 196 580 339 588 394 0 5 914 147 574 859 524 0
How do we compute the freight volumes for the entire financial year?
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 3/9
Adding matrices
Suppose we have freight volumes for first and second half of financial year.
April–September October–March
0 1 2 3 4 5 0 1 2 3 4 5
0 0 694 828 384 247 479 0 0 851 626 280 399 365
1 642 0 919 575 402 673 1 544 0 479 269 432 933
2 768 734 0 231 595 540 2 867 804 0 681 326 398
3 731 606 156 0 351 804 3 727 976 418 0 667 294
4 825 607 316 490 0 998 4 894 390 247 547 0 314
5 196 580 339 588 394 0 5 914 147 574 859 524 0
How do we compute the freight volumes for the entire financial year?
Add the corresponding entries in the two tables
Total freight volume from 2 (Delhi) to 4 (Kolkata) is 595 + 326 = 921
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 3/9
Adding matrices
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 4/9
Adding matrices
0 1 2 3 4 5
0 0 694 828 384 247 479
For a matrix M, M[i, j] is the entry in row 1 642 0 919 575 402 673
i, column j A 2 768 734 0 231 595 540
3 731 606 156 0 351 804
4 825 607 316 490 0 998
Let A and B represent the volumes in the 5 196 580 339 588 394 0
two half-years 0 1 2 3 4 5
0 0 851 626 280 399 365
1 544 0 479 269 432 933
B 2 867 804 0 681 326 398
3 727 976 418 0 667 294
4 894 390 247 547 0 314
5 914 147 574 859 524 0
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 4/9
Adding matrices
0 1 2 3 4 5
0 0 694 828 384 247 479
For a matrix M, M[i, j] is the entry in row 1 642 0 919 575 402 673
i, column j A 2 768 734 0 231 595 540
3 731 606 156 0 351 804
4 825 607 316 490 0 998
Let A and B represent the volumes in the 5 196 580 339 588 394 0
two half-years 0 1 2 3 4 5
0 0 851 626 280 399 365
Let C represent the annual volume 1 544 0 479 269 432 933
B 2 867 804 0 681 326 398
3 727 976 418 0 667 294
4 894 390 247 547 0 314
5 914 147 574 859 524 0
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 4/9
Adding matrices
0 1 2 3 4 5
0 0 694 828 384 247 479
For a matrix M, M[i, j] is the entry in row 1 642 0 919 575 402 673
i, column j A 2 768 734 0 231 595 540
3 731 606 156 0 351 804
4 825 607 316 490 0 998
Let A and B represent the volumes in the 5 196 580 339 588 394 0
two half-years 0 1 2 3 4 5
0 0 851 626 280 399 365
Let C represent the annual volume 1 544 0 479 269 432 933
B 2 867 804 0 681 326 398
For each i,j, C [i, j] = A[i, j] + B[i, j] 3 727 976 418 0 667 294
4 894 390 247 547 0 314
5 914 147 574 859 524 0
0 1 2 3 4 5
0 0 1545 1454 664 646 844
1 1186 0 1398 844 834 1606
C 2 1635 1538 0 912 921 938
3 1458 1582 574 0 1018 1098
4 1719 997 563 1037 0 1312
5 1110 727 913 1447 918 0
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 4/9
Adding matrices
0 1 2 3 4 5
0 0 694 828 384 247 479
For a matrix M, M[i, j] is the entry in row 1 642 0 919 575 402 673
i, column j A 2 768 734 0 231 595 540
3 731 606 156 0 351 804
4 825 607 316 490 0 998
Let A and B represent the volumes in the 5 196 580 339 588 394 0
two half-years 0 1 2 3 4 5
0 0 851 626 280 399 365
Let C represent the annual volume 1 544 0 479 269 432 933
B 2 867 804 0 681 326 398
For each i,j, C [i, j] = A[i, j] + B[i, j] 3 727 976 418 0 667 294
4 894 390 247 547 0 314
5 914 147 574 859 524 0
More concisely, C = A + B — matrix
0 1 2 3 4 5
addition 0 0 1545 1454 664 646 844
1 1186 0 1398 844 834 1606
C 2 1635 1538 0 912 921 938
3 1458 1582 574 0 1018 1098
4 1719 997 563 1037 0 1312
5 1110 727 913 1447 918 0
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 4/9
Multiplying matrices
Can we multiply matrices the same way?
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 5/9
Multiplying matrices
0 1 2 3
Can we multiply matrices the same way? 0 0 6 8 3
A 1 4 0 9 7
For each i,j, C [i, j] = A[i, j] × B[i, j]
2 6 3 0 1
3 1 0 5 0
0 1 2 3
0 0 1 2 8
B 1 5 0 4 2
2 6 0 0 1
3 2 6 4 0
0 1 2 3
0 0 6 16 24
C 1 20 0 36 14
2 36 0 0 1
3 2 0 20 0
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 5/9
Multiplying matrices
0 1 2 3
Can we multiply matrices the same way? 0 0 6 8 3
A 1 4 0 9 7
For each i,j, C [i, j] = A[i, j] × B[i, j]
2 6 3 0 1
3 1 0 5 0
This turns out to be not very useful
0 1 2 3
0 0 1 2 8
B 1 5 0 4 2
2 6 0 0 1
3 2 6 4 0
0 1 2 3
0 0 6 16 24
C 1 20 0 36 14
2 36 0 0 1
3 2 0 20 0
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 5/9
Multiplying matrices
0 1 2 3
Can we multiply matrices the same way? 0 0 6 8 3
A 1 4 0 9 7
For each i,j, C [i, j] = A[i, j] × B[i, j]
2 6 3 0 1
3 1 0 5 0
This turns out to be not very useful
Instead, we compute C [i, j] in a more 0 1 2 3
0 0 1 2 8
complicated way
B 1 5 0 4 2
2 6 0 0 1
3 2 6 4 0
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 5/9
Multiplying matrices
0 1 2 3
Can we multiply matrices the same way? 0 0 6 8 3
A 1 4 0 9 7
For each i,j, C [i, j] = A[i, j] × B[i, j]
2 6 3 0 1
3 1 0 5 0
This turns out to be not very useful
Instead, we compute C [i, j] in a more 0 1 2 3
0 0 1 2 8
complicated way
B 1 5 0 4 2
Assume r = c = n. Let 2 6 0 0 1
3 2 6 4 0
C [i, j] = A[i, 0] · B[0, j]+
A[i, 1] · B[1, j]+
···+
A[i, n − 1] · B[n − 1, j]
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 5/9
Multiplying matrices
0 1 2 3
Can we multiply matrices the same way? 0 0 6 8 3
A 1 4 0 9 7
For each i,j, C [i, j] = A[i, j] × B[i, j]
2 6 3 0 1
3 1 0 5 0
This turns out to be not very useful
Instead, we compute C [i, j] in a more 0 1 2 3
0 0 1 2 8
complicated way
B 1 5 0 4 2
Assume r = c = n. Let 2 6 0 0 1
3 2 6 4 0
C [i, j] = A[i, 0] · B[0, j]+
A[i, 1] · B[1, j]+
···+
A[i, n − 1] · B[n − 1, j]
For instance,
C [1, 3] = 4 · 8 + 0 · 2 + 9 · 1 + 7 · 0 = 41
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 5/9
Multiplying matrices
0 1 2 3
Can we multiply matrices the same way? 0 0 6 8 3
A 1 4 0 9 7
For each i,j, C [i, j] = A[i, j] × B[i, j]
2 6 3 0 1
3 1 0 5 0
This turns out to be not very useful
Instead, we compute C [i, j] in a more 0 1 2 3
0 0 1 2 8
complicated way
B 1 5 0 4 2
Assume r = c = n. Let 2 6 0 0 1
3 2 6 4 0
C [i, j] = A[i, 0] · B[0, j]+
A[i, 1] · B[1, j]+ 0 1 2 3
···+ 0 84 18 36 20
C 1 68 46 36 41
A[i, n − 1] · B[n − 1, j]
2 17 12 28 54
For instance, 3 30 1 2 13
C [1, 3] = 4 · 8 + 0 · 2 + 9 · 1 + 7 · 0 = 41
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 5/9
Multiplying matrices
0 1 2 3
Matrix product: C = A × B 0 0 6 8 3
A 1 4 0 9 7
2 6 3 0 1
3 1 0 5 0
0 1 2 3
0 0 1 2 8
B 1 5 0 4 2
2 6 0 0 1
3 2 6 4 0
0 1 2 3
0 84 18 36 20
C 1 68 46 36 41
2 17 12 28 54
3 30 1 2 13
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 6/9
Multiplying matrices
0 1 2 3
Matrix product: C = A × B 0 0 6 8 3
A 1 4 0 9 7
For r = c = n,
2 6 3 0 1
C [i, j] = A[i, 0] · B[0, j]+ 3 1 0 5 0
A[i, 1] · B[1, j]+
···+ 0 1 2 3
0 0 1 2 8
A[i, n − 1] · B[n − 1, j] B 1 5 0 4 2
2 6 0 0 1
3 2 6 4 0
0 1 2 3
0 84 18 36 20
C 1 68 46 36 41
2 17 12 28 54
3 30 1 2 13
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 6/9
Multiplying matrices
0 1 2 3
Matrix product: C = A × B 0 0 6 8 3
A 1 4 0 9 7
For r = c = n,
2 6 3 0 1
C [i, j] = A[i, 0] · B[0, j]+ 3 1 0 5 0
A[i, 1] · B[1, j]+
···+ 0 1 2 3
0 0 1 2 8
A[i, n − 1] · B[n − 1, j] B 1 5 0 4 2
n−1
2 6 0 0 1
X 3 2 6 4 0
More concisely, C [i, j] = A[i, k] · B[k, j]
k=0 0 1 2 3
0 84 18 36 20
C 1 68 46 36 41
2 17 12 28 54
3 30 1 2 13
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 6/9
Multiplying matrices
0 1 2 3
Matrix product: C = A × B 0 0 6 8 3
A 1 4 0 9 7
For r = c = n,
2 6 3 0 1
C [i, j] = A[i, 0] · B[0, j]+ 3 1 0 5 0
A[i, 1] · B[1, j]+
···+ 0 1 2 3
0 0 1 2 8
A[i, n − 1] · B[n − 1, j] B 1 5 0 4 2
n−1
2 6 0 0 1
X 3 2 6 4 0
More concisely, C [i, j] = A[i, k] · B[k, j]
k=0 0 1 2 3
Don’t require both A and B to be n × n 0 84 18 36 20
C 1 68 46 36 41
Each row entry of A must have a matching 2 17 12 28 54
column entry in B 3 30 1 2 13
If A is m × n and B is n × p, A × B is m × p
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 6/9
Multiplying matrices
Matrix product: C = A × B 0 1 2 3
0 0 6 8 3
For r = c = n, A 1 4 0 9 7
2 6 3 0 1
C [i, j] = A[i, 0] · B[0, j]+
A[i, 1] · B[1, j]+ 0 1 2
···+ 0 0 1 2
A[i, n − 1] · B[n − 1, j] B 1 5 0 4
2 6 0 0
n−1
X 3 2 6 4
More concisely, C [i, j] = A[i, k] · B[k, j]
k=0
0 1 2
0 84 18 36
Don’t require both A and B to be n × n C 1 68 46 36
2 17 12 28
Each row entry of A must have a matching
column entry in B
If A is m × n and B is n × p, A × B is m × p
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 6/9
Transitive closure
0 1 2 3 4 5
A is an adjacency matrix 0 0 1 0 0 1 0
A[i, j] = 1 if and only if there is a 1 0 0 1 0 0 0
direct edge (path of length 1) from i 2 1 0 0 0 0 0
to j 3 0 0 0 0 1 0
4 1 0 0 1 0 0
5 0 0 0 1 0 0
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 7/9
Transitive closure
0 1 2 3 4 5
A is an adjacency matrix 0 0 1 0 0 1 0
A[i, j] = 1 if and only if there is a 1 0 0 1 0 0 0
direct edge (path of length 1) from i 2 1 0 0 0 0 0
to j 3 0 0 0 0 1 0
4 1 0 0 1 0 0
A2 represents paths of length 2 5 0 0 0 1 0 0
A2 [i, j] = 1 if, for some k, A[i, k] = 1
and A[k, j] = 1
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 7/9
Transitive closure
0 1 2 3 4 5
A is an adjacency matrix 0 0 1 0 0 1 0
A[i, j] = 1 if and only if there is a 1 0 0 1 0 0 0
direct edge (path of length 1) from i 2 1 0 0 0 0 0
to j 3 0 0 0 0 1 0
4 1 0 0 1 0 0
A2 represents paths of length 2 5 0 0 0 1 0 0
A2 [i, j] = 1 if, for some k, A[i, k] = 1
and A[k, j] = 1
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 7/9
Transitive closure
0 1 2 3 4 5
A is an adjacency matrix 0 0 1 0 0 1 0
A[i, j] = 1 if and only if there is a 1 0 0 1 0 0 0
direct edge (path of length 1) from i 2 1 0 0 0 0 0
to j 3 0 0 0 0 1 0
4 1 0 0 1 0 0
A2 represents paths of length 2 5 0 0 0 1 0 0
A2 [i, j] = 1 if, for some k, A[i, k] = 1
and A[k, j] = 1
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 7/9
Transitive closure
0 1 2 3 4 5
A is an adjacency matrix 0 0 1 0 0 1 0
A[i, j] = 1 if and only if there is a 1 0 0 1 0 0 0
direct edge (path of length 1) from i 2 1 0 0 0 0 0
to j 3 0 0 0 0 1 0
4 1 0 0 1 0 0
A2 represents paths of length 2 5 0 0 0 1 0 0
A2 [i, j] = 1 if, for some k, A[i, k] = 1
and A[k, j] = 1
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 7/9
Transitive closure
0 1 2 3 4 5
A is an adjacency matrix 0 0 1 0 0 1 0
A[i, j] = 1 if and only if there is a 1 0 0 1 0 0 0
direct edge (path of length 1) from i 2 1 0 0 0 0 0
to j 3 0 0 0 0 1 0
4 1 0 0 1 0 0
A2 represents paths of length 2 5 0 0 0 1 0 0
A2 [i, j] = 1 if, for some k, A[i, k] = 1
and A[k, j] = 1
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 8/9
Transitive closure
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 8/9
Transitive closure
In other words, A2 = A × A
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 8/9
Transitive closure
A2 [i, j] = (A[i, 0] and A[0, j]) or Likewise, A3 [i, j] = 1 if, for some k,
(A[i, 1] and A[1, j]) or A2 [i, k] = 1 and A[k, j] = 1
··· Paths of length 3
(A[i, n−1] and A[n−1, j])
A2 [i, j] = A[i, 0] × A[0, j]+
A[i, 1] × A[1, j]+
···+
A[i, n−1] × A[n−1, j]
n−1
X
2
So A [i, j] = A[i, k] × A[k, j]
k=0
In other words, A2 = A × A
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 8/9
Transitive closure
A2 [i, j] = (A[i, 0] and A[0, j]) or Likewise, A3 [i, j] = 1 if, for some k,
(A[i, 1] and A[1, j]) or A2 [i, k] = 1 and A[k, j] = 1
··· Paths of length 3
(A[i, n−1] and A[n−1, j])
A3 = A2 × A
A2 [i, j] = A[i, 0] × A[0, j]+
A[i, 1] × A[1, j]+
···+
A[i, n−1] × A[n−1, j]
n−1
X
2
So A [i, j] = A[i, k] × A[k, j]
k=0
In other words, A2 = A × A
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 8/9
Transitive closure
A2 [i, j] = (A[i, 0] and A[0, j]) or Likewise, A3 [i, j] = 1 if, for some k,
(A[i, 1] and A[1, j]) or A2 [i, k] = 1 and A[k, j] = 1
··· Paths of length 3
(A[i, n−1] and A[n−1, j])
A3 = A2 × A = A × A2
A2 [i, j] = A[i, 0] × A[0, j]+
A[i, 1] × A[1, j]+
···+
A[i, n−1] × A[n−1, j]
n−1
X
2
So A [i, j] = A[i, k] × A[k, j]
k=0
In other words, A2 = A × A
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 8/9
Transitive closure
A2 [i, j] = (A[i, 0] and A[0, j]) or Likewise, A3 [i, j] = 1 if, for some k,
(A[i, 1] and A[1, j]) or A2 [i, k] = 1 and A[k, j] = 1
··· Paths of length 3
(A[i, n−1] and A[n−1, j])
A3 = A2 × A = A × A2
A2 [i, j] = A[i, 0] × A[0, j]+
A[i, 1] × A[1, j]+ In general, A`+1 = A` × A = A × A`
···+ Paths of length `+1
A[i, n−1] × A[n−1, j]
n−1
X
2
So A [i, j] = A[i, k] × A[k, j]
k=0
In other words, A2 = A × A
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 8/9
Transitive closure
A2 [i, j] = (A[i, 0] and A[0, j]) or Likewise, A3 [i, j] = 1 if, for some k,
(A[i, 1] and A[1, j]) or A2 [i, k] = 1 and A[k, j] = 1
··· Paths of length 3
(A[i, n−1] and A[n−1, j])
A3 = A2 × A = A × A2
A2 [i, j] = A[i, 0] × A[0, j]+
A[i, 1] × A[1, j]+ In general, A`+1 = A` × A = A × A`
···+ Paths of length `+1
A[i, n−1] × A[n−1, j]
Finally, A+ = A + A2 + · · · + An−1
n−1
X A+ [i, j] = 1 if i,j connected by
So A2 [i, j] = A[i, k] × A[k, j]
k=0
path of length 1 or
path of length 2 or
In other words, A2 = A × A
· · · or
path of length n−1
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 8/9
Summary
Matrix addition is defined elementwise
C [i, j] = A[i, j] + B[i, j]
A, B, C have same dimensions r × c
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 9/9
Summary
Matrix addition is defined elementwise
C [i, j] = A[i, j] + B[i, j]
A, B, C have same dimensions r × c
Madhavan Mukund Matrix Multiplication Mathematics for Data Science 1, Week 11 9/9
Summary
Matrix addition is defined elementwise
C [i, j] = A[i, j] + B[i, j]
A, B, C have same dimensions r × c
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 2/6
Shortest paths
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 2/6
Shortest paths
70
Recall that BFS explores a graph level 2 3
by level 80 6
50
BFS computes shortest path, in terms 0 1 4 5
10 20
of number of edges, to every reachable 5 10
vertex 6
May assign values to edges
Cost, time, distance, . . .
Weighted graph
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 2/6
Shortest paths
70
Recall that BFS explores a graph level 2 3
by level 80 6
50
BFS computes shortest path, in terms 0 1 4 5
10 20
of number of edges, to every reachable 5 10
vertex 6
May assign values to edges
Cost, time, distance, . . .
Weighted graph
G = (V , E ), W : E → R
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 2/6
Shortest paths
70
Recall that BFS explores a graph level 2 3
by level 80 6
50
BFS computes shortest path, in terms 0 1 4 5
10 20
of number of edges, to every reachable 5 10
vertex 6
May assign values to edges
Cost, time, distance, . . .
Weighted graph
G = (V , E ), W : E → R
Adjacency matrix: record the weight
wherever there is an edge, 0 if no edge
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 2/6
Shortest paths
70
Recall that BFS explores a graph level 2 3
by level 80 6
50
BFS computes shortest path, in terms 0 1 4 5
10 20
of number of edges, to every reachable 5 10
vertex 6
May assign values to edges
0 1 2 3 4 5 6
Cost, time, distance, . . . 0 0 10 80 0 0 0 0
Weighted graph 1 10 0 6 0 20 0 0
2 80 6 0 70 0 0 0
G = (V , E ), W : E → R 3 0 0 70 0 0 0 0
4 0 20 0 0 0 50 5
Adjacency matrix: record the weight 5 0 0 0 0 50 0 10
wherever there is an edge, 0 if no edge 6 0 0 0 0 5 10 0
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 2/6
Shortest paths in weighted graphs
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 3/6
Shortest paths in weighted graphs
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 3/6
Shortest paths in weighted graphs
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 3/6
Shortest path problems
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 4/6
Shortest path problems
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 4/6
Shortest path problems
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 4/6
Shortest path problems
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 4/6
Shortest path problems
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 4/6
Shortest path problems
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 4/6
Negative edge weights
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 5/6
Negative edge weights
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 5/6
Negative edge weights
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 5/6
Negative edge weights
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 5/6
Negative edge weights
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 5/6
Negative edge weights
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 5/6
Negative edge weights
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 5/6
Negative edge weights
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 5/6
Negative edge weights
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 6/6
Summary
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 6/6
Summary
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 6/6
Summary
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 6/6
Single Source Shortest Paths
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
Weighted graph: 70
2 3
G = (V , E ) 80 6
W :E →R 50
0 1 4 5
10 20
5 10
6
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 2/7
Single source shortest paths
Weighted graph: 70
2 3
G = (V , E ) 80 6
W :E →R 50
0 1 4 5
Single source shortest paths 10 20
5 10
Find shortest paths from a fixed vertex
to every other vertex
6
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 2/7
Single source shortest paths
Weighted graph: 70
2 3
G = (V , E ) 80 6
W :E →R 50
0 1 4 5
Single source shortest paths 10 20
5 10
Find shortest paths from a fixed vertex
to every other vertex
6
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 2/7
Single source shortest paths
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 3/7
Single source shortest paths
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 3/7
Single source shortest paths
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 3/7
Single source shortest paths
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 3/7
Single source shortest paths
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 3/7
Single source shortest paths
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 3/7
Single source shortest paths
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 4/7
Single source shortest paths
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 4/7
Single source shortest paths
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 4/7
Single source shortest paths
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 4/7
Single source shortest paths
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 4/7
Single source shortest paths
...
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 4/7
Single source shortest paths
...
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 4/7
Single source shortest paths
...
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 4/7
Single source shortest paths
Compute expected
burn time for each 70
vertex 2 3
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 5/7
Single source shortest paths
Compute expected ∞ ∞
burn time for each 70
vertex 2 3
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 5/7
Single source shortest paths
Compute expected ∞ ∞
burn time for each 70
vertex 2 3
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 5/7
Single source shortest paths
Compute expected ∞
80
burn time for each 70
vertex 2 3
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 5/7
Single source shortest paths
Compute expected ∞
80
burn time for each 70
vertex 2 3
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 5/7
Single source shortest paths
Compute expected ∞
16
burn time for each 70
vertex 2 3
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 5/7
Single source shortest paths
Compute expected ∞
16
burn time for each 70
vertex 2 3
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 5/7
Single source shortest paths
Compute expected
16 86
burn time for each 70
vertex 2 3
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 5/7
Single source shortest paths
Compute expected
16 86
burn time for each 70
vertex 2 3
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 5/7
Single source shortest paths
Compute expected
16 86
burn time for each 70
vertex 2 3
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 5/7
Single source shortest paths
Compute expected
16 86
burn time for each 70
vertex 2 3
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 5/7
Single source shortest paths
Compute expected
16 86
burn time for each 70
vertex 2 3
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 5/7
Single source shortest paths
Compute expected
16 86
burn time for each 70
vertex 2 3
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 5/7
Single source shortest paths
Compute expected
16 86
burn time for each 70
vertex 2 3
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 5/7
Dijkstra’s algorithm: Proof of correctness
Each new shortest path we discover extends an
earlier one
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 6/7
Dijkstra’s algorithm: Proof of correctness
Each new shortest path we discover extends an
earlier one
By induction, assume we have found shortest
paths to all vertices already burnt
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 6/7
Dijkstra’s algorithm: Proof of correctness
Each new shortest path we discover extends an
earlier one
Burnt vertices
By induction, assume we have found shortest
paths to all vertices already burnt
s
y
x
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 6/7
Dijkstra’s algorithm: Proof of correctness
Each new shortest path we discover extends an
earlier one
Burnt vertices
By induction, assume we have found shortest
paths to all vertices already burnt
s
y
Next vertex to burn is v, via x x
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 6/7
Dijkstra’s algorithm: Proof of correctness
Each new shortest path we discover extends an
earlier one
Burnt vertices
By induction, assume we have found shortest
paths to all vertices already burnt
s
y
Next vertex to burn is v, via x x
Cannot find a shorter path later from y to v
via w
Burn time of w ≥ burn time of v v
Edge from w to v has weight ≥ 0 w
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 6/7
Dijkstra’s algorithm: Proof of correctness
Each new shortest path we discover extends an
earlier one
Burnt vertices
By induction, assume we have found shortest
paths to all vertices already burnt
s
y
Next vertex to burn is v, via x x
Cannot find a shorter path later from y to v
via w
Burn time of w ≥ burn time of v v
Edge from w to v has weight ≥ 0 w
Madhavan Mukund Single Source Shortest Paths Mathematics for Data Science 1, Week 12 7/7
Single Source Shortest Paths with Negative Weights
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 2/1
Dijkstra’s algorithm
Recall the burning pipeline analogy
We keep track of the following
The vertices that have been burnt
The expected burn time of vertices
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 2/1
Dijkstra’s algorithm
Recall the burning pipeline analogy Initialization (assume source vertex 0)
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 2/1
Dijkstra’s algorithm
Recall the burning pipeline analogy Initialization (assume source vertex 0)
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 3/1
Correctness requires non-negative edge weights
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 3/1
Correctness requires non-negative edge weights
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 3/1
Extending to negative edge weights
v
w
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 4/1
Extending to negative edge weights
v
w
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 4/1
Extending to negative edge weights
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 4/1
Extending to negative edge weights
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 4/1
Extending to negative edge weights
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 4/1
Extending to negative edge weights
Suppose minimum weight path from 0
to k is
w1 2w 3 w w`−1 ` w
0 −→ j1 −→ j2 −→ · · · −−−→ j`−1 −→ k
Need not be minimum in terms of
number of edges
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 5/1
Extending to negative edge weights
Suppose minimum weight path from 0
to k is
w1 2 w 3 w w`−1 ` w
0 −→ j1 −→ j2 −→ · · · −−−→ j`−1 −→ k
Need not be minimum in terms of
number of edges
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 5/1
Extending to negative edge weights
Suppose minimum weight path from 0 Once we discover shortest path to j`−1 ,
to k is next update will fix shortest path to k
w1 2 w 3 w w`−1 ` w
0 −→ j1 −→ j2 −→ · · · −−−→ j`−1 −→ k
Need not be minimum in terms of
number of edges
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 5/1
Extending to negative edge weights
Suppose minimum weight path from 0 Once we discover shortest path to j`−1 ,
to k is next update will fix shortest path to k
w1 2 w 3 w w`−1 ` w
0 −→ j1 −→ j2 −→ · · · −−−→ j`−1 −→ k Repeatedly update shortest distance to
Need not be minimum in terms of each vertex based on shortest distance
number of edges to its neighbours
Every prefix of this path must itself be a Update cannot push this distance
below actual shortest distance
minimum weight path
w1
0 −→ j1
w1 2w
0 −→ j1 −→ j2
...
w1 2w 3 w w`−1
0 −→ j1 −→ j2 −→ · · · −−−→ j`−1
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 5/1
Extending to negative edge weights
Suppose minimum weight path from 0 Once we discover shortest path to j`−1 ,
to k is next update will fix shortest path to k
w1 2 w 3 w w`−1 ` w
0 −→ j1 −→ j2 −→ · · · −−−→ j`−1 −→ k Repeatedly update shortest distance to
Need not be minimum in terms of each vertex based on shortest distance
number of edges to its neighbours
Every prefix of this path must itself be a Update cannot push this distance
below actual shortest distance
minimum weight path
w1 After ` updates, all shortest paths using
0 −→ j1
w1
0 −→ 2
j1 −→
w
j2
≤ ` edges have stabilized
... Minimum weight path to any node has
w1 2w 3 w w`−1 at most n−1 edges
0 −→ j1 −→ j2 −→ · · · −−−→ j`−1
After n−1 updates, all shortest paths
have stabilized
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 5/1
Bellman-Ford Algorithm
Initialization (source vertex 0)
D(j) : minimum distance known so
far to vertex j
(
0, if j = 0
D(j) =
∞, otherwise
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 6/1
Bellman-Ford Algorithm
Initialization (source vertex 0)
D(j) : minimum distance known so
far to vertex j
(
0, if j = 0
D(j) =
∞, otherwise
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 6/1
Bellman-Ford Algorithm
Initialization (source vertex 0)
D(j) : minimum distance known so
far to vertex j
(
0, if j = 0
D(j) =
∞, otherwise
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 6/1
Bellman-Ford Algorithm
Initialization (source vertex 0)
D(j) : minimum distance known so 10
0 1
far to vertex j 8 1
(
0, if j = 0 7 2
D(j) = −4
∞, otherwise 2
1 1
Repeat n−1 times −2
6 3
For each vertex j ∈ {0, 1, . . . , n−1},
for each edge (j, k) ∈ E , −1 3
5 4
D(k) = min( D(k), −1
D(j) + W (j, k) )
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 6/1
Bellman-Ford Algorithm
v D(v ) 10
0 1
0 8 1
1
2 7 2
3 −4
1 2 1
4
5 −2
6 6 3
7 −1 3
5 4
−1
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 7/1
Bellman-Ford Algorithm
v D(v ) 10
0 1
0 0 8 1
1 ∞
2 ∞ 7 2
3 ∞ −4
1 2 1
4 ∞
5 ∞ −2
6 ∞ 6 3
7 ∞ −1 3
5 4
Initialize D(0) = 0 −1
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 7/1
Bellman-Ford Algorithm
v D(v ) 10
0 1
0 0 0 8 1
1 ∞ 10
2 ∞ ∞ 7 2
3 ∞ ∞ −4
1 2 1
4 ∞ ∞
5 ∞ ∞ −2
6 ∞ ∞ 6 3
7 ∞ 8 −1 3
5 4
Initialize D(0) = 0 −1
v D(v ) 10
0 1
0 0 0 0 8 1
1 ∞ 10 10
2 ∞ ∞ ∞ 7 2
3 ∞ ∞ ∞ −4
1 2 1
4 ∞ ∞ ∞
5 ∞ ∞ 12 −2
6 ∞ ∞ 9 6 3
7 ∞ 8 8 −1 3
5 4
Initialize D(0) = 0 −1
v D(v ) 10
0 1
0 0 0 0 0 8 1
1 ∞ 10 10 5
2 ∞ ∞ ∞ 10 7 2
3 ∞ ∞ ∞ ∞ −4
1 2 1
4 ∞ ∞ ∞ ∞
5 ∞ ∞ 12 8 −2
6 ∞ ∞ 9 9 6 3
7 ∞ 8 8 8 −1 3
5 4
Initialize D(0) = 0 −1
v D(v ) 10
0 1
0 0 0 0 0 0 8 1
1 ∞ 10 10 5 5
2 ∞ ∞ ∞ 10 6 7 2
3 ∞ ∞ ∞ ∞ 11 −4
1 2 1
4 ∞ ∞ ∞ ∞ ∞
5 ∞ ∞ 12 8 7 −2
6 ∞ ∞ 9 9 9 6 3
7 ∞ 8 8 8 8 −1 3
5 4
Initialize D(0) = 0 −1
v D(v ) 10
0 1
0 0 0 0 0 0 0 8 1
1 ∞ 10 10 5 5 5
2 ∞ ∞ ∞ 10 6 5 7 2
3 ∞ ∞ ∞ ∞ 11 7 −4
1 2 1
4 ∞ ∞ ∞ ∞ ∞ 14
5 ∞ ∞ 12 8 7 7 −2
6 ∞ ∞ 9 9 9 9 6 3
7 ∞ 8 8 8 8 8 −1 3
5 4
Initialize D(0) = 0 −1
v D(v ) 10
0 1
0 0 0 0 0 0 0 0 8 1
1 ∞ 10 10 5 5 5 5
2 ∞ ∞ ∞ 10 6 5 5 7 2
3 ∞ ∞ ∞ ∞ 11 7 6 −4
1 2 1
4 ∞ ∞ ∞ ∞ ∞ 14 10
5 ∞ ∞ 12 8 7 7 7 −2
6 ∞ ∞ 9 9 9 9 9 6 3
7 ∞ 8 8 8 8 8 8 −1 3
5 4
Initialize D(0) = 0 −1
v D(v ) 10
0 1
0 0 0 0 0 0 0 0 0 8 1
1 ∞ 10 10 5 5 5 5 5
2 ∞ ∞ ∞ 10 6 5 5 5 7 2
3 ∞ ∞ ∞ ∞ 11 7 6 6 −4
1 2 1
4 ∞ ∞ ∞ ∞ ∞ 14 10 9
5 ∞ ∞ 12 8 7 7 7 7 −2
6 ∞ ∞ 9 9 9 9 9 9 6 3
7 ∞ 8 8 8 8 8 8 8 −1 3
5 4
Initialize D(0) = 0 −1
v D(v ) 10
0 1
0 0 0 0 0 0 0 0 0 8 1
1 ∞ 10 10 5 5 5 5 5
2 ∞ ∞ ∞ 10 6 5 5 5 7 2
3 ∞ ∞ ∞ ∞ 11 7 6 6 −4
1 2 1
4 ∞ ∞ ∞ ∞ ∞ 14 10 9
5 ∞ ∞ 12 8 7 7 7 7 −2
6 ∞ ∞ 9 9 9 9 9 9 6 3
7 ∞ 8 8 8 8 8 8 8 −1 3
5 4
What if there was a negative cycle? −1
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 7/1
Bellman-Ford Algorithm
v D(v ) 10
0 1
0 0 0 0 0 0 0 0 0 8 1
1 ∞ 10 10 5 5 5 5 5
2 ∞ ∞ ∞ 10 6 5 5 5 7 2
3 ∞ ∞ ∞ ∞ 11 7 6 6 −4
1 2 1
4 ∞ ∞ ∞ ∞ ∞ 14 10 9
5 ∞ ∞ 12 8 7 7 7 7 −2
6 ∞ ∞ 9 9 9 9 9 9 6 3
7 ∞ 8 8 8 8 8 8 8 −1 3
5 4
What if there was a negative cycle? −1
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 7/1
Bellman-Ford Algorithm
v D(v ) 10
0 1
0 0 0 0 0 0 0 0 0 8 1
1 ∞ 10 10 5 5 5 5 5
2 ∞ ∞ ∞ 10 6 5 5 5 7 2
3 ∞ ∞ ∞ ∞ 11 7 6 6 −4
1 2 1
4 ∞ ∞ ∞ ∞ ∞ 14 10 9
5 ∞ ∞ 12 8 7 7 7 7 −2
6 ∞ ∞ 9 9 9 9 9 9 6 3
7 ∞ 8 8 8 8 8 8 8 −1 3
5 4
What if there was a negative cycle? −1
Madhavan Mukund Single Source Shortest Paths with Negative Weights Mathematics for Data Science 1, Week 12 8/1
All-Pairs Shortest Paths
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
Madhavan Mukund All-Pairs Shortest Paths Mathematics for Data Science 1, Week 12 2/9
Shortest paths in weighted graphs
Madhavan Mukund All-Pairs Shortest Paths Mathematics for Data Science 1, Week 12 3/9
Transitive closure
Recall transitive closure algorithm
Adjacency matrix A represents paths of
length 1
Madhavan Mukund All-Pairs Shortest Paths Mathematics for Data Science 1, Week 12 3/9
Transitive closure
Recall transitive closure algorithm
Adjacency matrix A represents paths of
length 1
Matrix multiplication, A2 = A × A
A2 [i, j] = 1 if there is a path of length
2 from i to j
For some k, A[i, k] = A[k, j] = 1
Madhavan Mukund All-Pairs Shortest Paths Mathematics for Data Science 1, Week 12 3/9
Transitive closure
Recall transitive closure algorithm
Adjacency matrix A represents paths of
length 1
Matrix multiplication, A2 = A × A
A2 [i, j] = 1 if there is a path of length
2 from i to j
For some k, A[i, k] = A[k, j] = 1
In general, A`+1 = A` × A,
A`+1 [i, j] = 1 if there is a path of
length `+1 from i to j
For some k, A` [i, k] = 1, A[k, j] = 1
Madhavan Mukund All-Pairs Shortest Paths Mathematics for Data Science 1, Week 12 3/9
Transitive closure
Recall transitive closure algorithm
Adjacency matrix A represents paths of
length 1
Matrix multiplication, A2 = A × A
A2 [i, j] = 1 if there is a path of length
2 from i to j
For some k, A[i, k] = A[k, j] = 1
In general, A`+1 = A` × A,
A`+1 [i, j] = 1 if there is a path of
length `+1 from i to j
For some k, A` [i, k] = 1, A[k, j] = 1
A+ = A + A2 + · · · + An−1
Madhavan Mukund All-Pairs Shortest Paths Mathematics for Data Science 1, Week 12 3/9
Transitive closure
Recall transitive closure algorithm An alternative approach
In general, A`+1 = A` × A,
A`+1 [i, j] = 1 if there is a path of
length `+1 from i to j
For some k, A` [i, k] = 1, A[k, j] = 1
A+ = A + A2 + · · · + An−1
Madhavan Mukund All-Pairs Shortest Paths Mathematics for Data Science 1, Week 12 3/9
Transitive closure
Recall transitive closure algorithm An alternative approach
B k [i, j] = 1 if there is path from i to j
Adjacency matrix A represents paths of
via vertices {0, 1, . . . , k−1}
length 1
Constraint applies only to intermediate
Matrix multiplication, A2 =A×A vertices i to j
2
A [i, j] = 1 if there is a path of length B 0 [i, j] = 1 if there is a direct edge
2 from i to j B0 = A
For some k, A[i, k] = A[k, j] = 1
In general, A`+1 = A` × A,
A`+1 [i, j] = 1 if there is a path of
length `+1 from i to j
For some k, A` [i, k] = 1, A[k, j] = 1
A+ = A + A2 + · · · + An−1
Madhavan Mukund All-Pairs Shortest Paths Mathematics for Data Science 1, Week 12 3/9
Transitive closure
Recall transitive closure algorithm An alternative approach
B k [i, j] = 1 if there is path from i to j
Adjacency matrix A represents paths of
via vertices {0, 1, . . . , k−1}
length 1
Constraint applies only to intermediate
Matrix multiplication, A2 =A×A vertices i to j
2
A [i, j] = 1 if there is a path of length B 0 [i, j] = 1 if there is a direct edge
2 from i to j B0 = A
For some k, A[i, k] = A[k, j] = 1
B k+1 [i, j] = 1 if
In general, A`+1 = A` × A, B k [i, j] = 1 — can already reach j
A`+1 [i, j] = 1 if there is a path of from i via {0, 1, . . . , k−1}
length `+1 from i to j B k [i, k] = 1 and B k [k, j] = 1 — use
For some k, A` [i, k] = 1, A[k, j] = 1 {0, 1, . . . , k−1} to go from i to k and
then from k to j
A+ = A + A2 + · · · + An−1
Madhavan Mukund All-Pairs Shortest Paths Mathematics for Data Science 1, Week 12 3/9
Warshall’s Algorithm
The algorithm on the right also Computing transitive closure
computes transitive closure — B k [i, j] = 1 if there is path from i to j
Warshall’s algorithm via vertices {0, 1, . . . , k−1}
B 0 [i, j] = A[i, j]
Direct edges, no intermediate vertices
B k+1 [i, j] = 1 if
B k [i, j] = 1, or
B k [i, k] = 1 and B k [k, j] = 1
Madhavan Mukund All-Pairs Shortest Paths Mathematics for Data Science 1, Week 12 4/9
Warshall’s Algorithm
The algorithm on the right also Computing transitive closure
computes transitive closure — B k [i, j] = 1 if there is path from i to j
Warshall’s algorithm via vertices {0, 1, . . . , k−1}
B n [i, j] = 1 if there is some path from i B 0 [i, j] = A[i, j]
to j with intermediate vertices in Direct edges, no intermediate vertices
{0, 1, . . . , n−1}
B k+1 [i, j] = 1 if
B k [i, j] = 1, or
B k [i, k] = 1 and B k [k, j] = 1
Madhavan Mukund All-Pairs Shortest Paths Mathematics for Data Science 1, Week 12 4/9
Warshall’s Algorithm
The algorithm on the right also Computing transitive closure
computes transitive closure — B k [i, j] = 1 if there is path from i to j
Warshall’s algorithm via vertices {0, 1, . . . , k−1}
B n [i, j] = 1 if there is some path from i B 0 [i, j] = A[i, j]
to j with intermediate vertices in Direct edges, no intermediate vertices
{0, 1, . . . , n−1}
B k+1 [i, j] = 1 if
Bn = A+
B k [i, j] = 1, or
B k [i, k] = 1 and B k [k, j] = 1
Madhavan Mukund All-Pairs Shortest Paths Mathematics for Data Science 1, Week 12 4/9
Warshall’s Algorithm
The algorithm on the right also Computing transitive closure
computes transitive closure — B k [i, j] = 1 if there is path from i to j
Warshall’s algorithm via vertices {0, 1, . . . , k−1}
B n [i, j] = 1 if there is some path from i B 0 [i, j] = A[i, j]
to j with intermediate vertices in Direct edges, no intermediate vertices
{0, 1, . . . , n−1}
B k+1 [i, j] = 1 if
Bn = A+
B k [i, j] = 1, or
We adapt Warshall’s algorithm to B k [i, k] = 1 and B k [k, j] = 1
compute all-pairs shortest paths
Madhavan Mukund All-Pairs Shortest Paths Mathematics for Data Science 1, Week 12 4/9
Floyd-Warshall Algorithm
Let SP k [i, j] be the length of the shortest path from i to j via vertices
{0, 1, . . . , k−1}
SP 0 [i, j] = W [i, j]
No intermediate vertices, shortest path is weight of direct edge
Assume W [i, j] = ∞ if (i, j) ∈
/E
Madhavan Mukund All-Pairs Shortest Paths Mathematics for Data Science 1, Week 12 5/9
Floyd-Warshall Algorithm
Let SP k [i, j] be the length of the shortest path from i to j via vertices
{0, 1, . . . , k−1}
SP 0 [i, j] = W [i, j]
No intermediate vertices, shortest path is weight of direct edge
Assume W [i, j] = ∞ if (i, j) ∈
/E
Madhavan Mukund All-Pairs Shortest Paths Mathematics for Data Science 1, Week 12 5/9
Floyd-Warshall Algorithm
Let SP k [i, j] be the length of the shortest path from i to j via vertices
{0, 1, . . . , k−1}
SP 0 [i, j] = W [i, j]
No intermediate vertices, shortest path is weight of direct edge
Assume W [i, j] = ∞ if (i, j) ∈
/E
Madhavan Mukund All-Pairs Shortest Paths Mathematics for Data Science 1, Week 12 6/9
Floyd-Warshall Algorithm
SP 0 0 1 2 3 4 5 6 7
0 ∞ 10 ∞ ∞ ∞ ∞ ∞ 8 10
1 ∞ ∞ ∞ ∞ ∞ 2 ∞ ∞ 0 1
2 ∞ 1 ∞ 1 ∞ ∞ ∞ ∞
8 1
3 ∞ ∞ ∞ ∞ 3 ∞ ∞ ∞ 7 2
4 ∞ ∞ ∞ ∞ ∞ −1 ∞ ∞
5 ∞ ∞ −2 ∞ ∞ ∞ ∞ ∞
−4
1 2 1
6 ∞ −4 ∞ ∞ ∞ −1 ∞ ∞
7 ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞
−2
SP 1 0 1 2 3 4 5 6 7 6 3
0 ∞ 10 ∞ ∞ ∞ ∞ ∞ 8
−1 3
1 ∞ ∞ ∞ ∞ ∞ 2 ∞ ∞ 5 4
2 ∞ 1 ∞ 1 ∞ ∞ ∞ ∞ −1
3 ∞ ∞ ∞ ∞ 3 ∞ ∞ ∞
4 ∞ ∞ ∞ ∞ ∞ −1 ∞ ∞
5 ∞ ∞ −2 ∞ ∞ ∞ ∞ ∞
6 ∞ −4 ∞ ∞ ∞ −1 ∞ ∞
7 ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞
Madhavan Mukund All-Pairs Shortest Paths Mathematics for Data Science 1, Week 12 6/9
Floyd-Warshall Algorithm
SP 1 0 1 2 3 4 5 6 7
0 ∞ 10 ∞ ∞ ∞ ∞ ∞ 8 10
1 ∞ ∞ ∞ ∞ ∞ 2 ∞ ∞ 0 1
2 ∞ 1 ∞ 1 ∞ ∞ ∞ ∞
8 1
3 ∞ ∞ ∞ ∞ 3 ∞ ∞ ∞ 7 2
4 ∞ ∞ ∞ ∞ ∞ −1 ∞ ∞
5 ∞ ∞ −2 ∞ ∞ ∞ ∞ ∞
−4
1 2 1
6 ∞ −4 ∞ ∞ ∞ −1 ∞ ∞
7 ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞
−2
6 3
−1 3
5 4
−1
Madhavan Mukund All-Pairs Shortest Paths Mathematics for Data Science 1, Week 12 7/9
Floyd-Warshall Algorithm
SP 1 0 1 2 3 4 5 6 7
0 ∞ 10 ∞ ∞ ∞ ∞ ∞ 8 10
1 ∞ ∞ ∞ ∞ ∞ 2 ∞ ∞ 0 1
2 ∞ 1 ∞ 1 ∞ ∞ ∞ ∞
8 1
3 ∞ ∞ ∞ ∞ 3 ∞ ∞ ∞ 7 2
4 ∞ ∞ ∞ ∞ ∞ −1 ∞ ∞
5 ∞ ∞ −2 ∞ ∞ ∞ ∞ ∞
−4
1 2 1
6 ∞ −4 ∞ ∞ ∞ −1 ∞ ∞
7 ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞
−2
SP 2 0 1 2 3 4 5 6 7 6 3
0 ∞ 10 ∞ ∞ ∞ 12 ∞ 8
−1 3
1 ∞ ∞ ∞ ∞ ∞ 2 ∞ ∞ 5 4
2 ∞ 1 ∞ 1 ∞ 3 ∞ ∞ −1
3 ∞ ∞ ∞ ∞ 3 ∞ ∞ ∞
4 ∞ ∞ ∞ ∞ ∞ −1 ∞ ∞
5 ∞ ∞ −2 ∞ ∞ ∞ ∞ ∞
6 ∞ −4 ∞ ∞ ∞ −2 ∞ ∞
7 ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞
Madhavan Mukund All-Pairs Shortest Paths Mathematics for Data Science 1, Week 12 7/9
Floyd-Warshall Algorithm
SP 2 0 1 2 3 4 5 6 7
0 ∞ 10 ∞ ∞ ∞ 12 ∞ 8 10
1 ∞ ∞ ∞ ∞ ∞ 2 ∞ ∞ 0 1
2 ∞ 1 ∞ 1 ∞ 3 ∞ ∞
8 1
3 ∞ ∞ ∞ ∞ 3 ∞ ∞ ∞ 7 2
4 ∞ ∞ ∞ ∞ ∞ −1 ∞ ∞
5 ∞ ∞ −2 ∞ ∞ ∞ ∞ ∞
−4
1 2 1
6 ∞ −4 ∞ ∞ ∞ −2 ∞ ∞
7 ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞
−2
6 3
−1 3
5 4
−1
Madhavan Mukund All-Pairs Shortest Paths Mathematics for Data Science 1, Week 12 8/9
Floyd-Warshall Algorithm
SP 2 0 1 2 3 4 5 6 7
0 ∞ 10 ∞ ∞ ∞ 12 ∞ 8 10
1 ∞ ∞ ∞ ∞ ∞ 2 ∞ ∞ 0 1
2 ∞ 1 ∞ 1 ∞ 3 ∞ ∞
8 1
3 ∞ ∞ ∞ ∞ 3 ∞ ∞ ∞ 7 2
4 ∞ ∞ ∞ ∞ ∞ −1 ∞ ∞
5 ∞ ∞ −2 ∞ ∞ ∞ ∞ ∞
−4
1 2 1
6 ∞ −4 ∞ ∞ ∞ −2 ∞ ∞
7 ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞
−2
SP 3 0 1 2 3 4 5 6 7 6 3
0 ∞ 10 ∞ ∞ ∞ 12 ∞ 8
−1 3
1 ∞ ∞ ∞ ∞ ∞ 2 ∞ ∞ 5 4
2 ∞ 1 ∞ 1 ∞ 3 ∞ ∞ −1
3 ∞ ∞ ∞ ∞ 3 ∞ ∞ ∞
4 ∞ ∞ ∞ ∞ ∞ −1 ∞ ∞
5 ∞ −1 −2 −1 ∞ 1 ∞ ∞
6 ∞ −4 ∞ ∞ ∞ −2 ∞ ∞
7 ∞ ∞ ∞ ∞ ∞ ∞ 1 ∞
Madhavan Mukund All-Pairs Shortest Paths Mathematics for Data Science 1, Week 12 8/9
Summary
Madhavan Mukund All-Pairs Shortest Paths Mathematics for Data Science 1, Week 12 9/9
Minimum Cost Spanning Trees
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
Roads
District hit by cyclone, roads are
damaged
Government sets to work to restore
roads
Priority is to ensure that all parts of the
district can be reached
What set of roads should be restored
first?
Madhavan Mukund Minimum Cost Spanning Trees Mathematics for Data Science 1, Week 12 2/7
Examples
Madhavan Mukund Minimum Cost Spanning Trees Mathematics for Data Science 1, Week 12 2/7
Spanning trees
0 1 2
Madhavan Mukund Minimum Cost Spanning Trees Mathematics for Data Science 1, Week 12 3/7
Spanning trees
Madhavan Mukund Minimum Cost Spanning Trees Mathematics for Data Science 1, Week 12 3/7
Spanning trees
Madhavan Mukund Minimum Cost Spanning Trees Mathematics for Data Science 1, Week 12 3/7
Spanning trees
Madhavan Mukund Minimum Cost Spanning Trees Mathematics for Data Science 1, Week 12 3/7
Spanning trees with costs
Madhavan Mukund Minimum Cost Spanning Trees Mathematics for Data Science 1, Week 12 4/7
Spanning trees with costs
Madhavan Mukund Minimum Cost Spanning Trees Mathematics for Data Science 1, Week 12 4/7
Spanning trees with costs
Example
Madhavan Mukund Minimum Cost Spanning Trees Mathematics for Data Science 1, Week 12 4/7
Spanning trees with costs
Example
Spanning tree, Cost is 114 — not
minimum cost spanning tree
Madhavan Mukund Minimum Cost Spanning Trees Mathematics for Data Science 1, Week 12 4/7
Spanning trees with costs
Example
Spanning tree, Cost is 114 — not
minimum cost spanning tree
Anonther spanning tree, Cost is 44 —
minimum cost spanning tree
Madhavan Mukund Minimum Cost Spanning Trees Mathematics for Data Science 1, Week 12 4/7
Some facts about trees
Madhavan Mukund Minimum Cost Spanning Trees Mathematics for Data Science 1, Week 12 5/7
Some facts about trees
Madhavan Mukund Minimum Cost Spanning Trees Mathematics for Data Science 1, Week 12 5/7
Some facts about trees
Madhavan Mukund Minimum Cost Spanning Trees Mathematics for Data Science 1, Week 12 5/7
Some facts about trees
Madhavan Mukund Minimum Cost Spanning Trees Mathematics for Data Science 1, Week 12 5/7
Some facts about trees
Fact 3
In a tree, every pair of vertices is connected
by a unique path.
Madhavan Mukund Minimum Cost Spanning Trees Mathematics for Data Science 1, Week 12 6/7
Some facts about trees
Fact 3
In a tree, every pair of vertices is connected
by a unique path.
•
•
•
• j
i •
•
Madhavan Mukund Minimum Cost Spanning Trees Mathematics for Data Science 1, Week 12 6/7
Some facts about trees
Fact 3 Observation
In a tree, every pair of vertices is connected Any two of the following facts about a graph
by a unique path. G implies the third
Madhavan Mukund Minimum Cost Spanning Trees Mathematics for Data Science 1, Week 12 6/7
Building minimum cost spanning trees
We will use these facts about trees to build minimum cost spanning trees
Madhavan Mukund Minimum Cost Spanning Trees Mathematics for Data Science 1, Week 12 7/7
Building minimum cost spanning trees
We will use these facts about trees to build minimum cost spanning trees
Two natural strategies
Madhavan Mukund Minimum Cost Spanning Trees Mathematics for Data Science 1, Week 12 7/7
Building minimum cost spanning trees
We will use these facts about trees to build minimum cost spanning trees
Two natural strategies
Start with the smallest edge and “grow” a tree
Prim’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees Mathematics for Data Science 1, Week 12 7/7
Building minimum cost spanning trees
We will use these facts about trees to build minimum cost spanning trees
Two natural strategies
Start with the smallest edge and “grow” a tree
Prim’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees Mathematics for Data Science 1, Week 12 7/7
Minimum Cost Spanning Trees:
Prim’s Algorithm
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 2/6
Minimum cost spanning tree (MCST)
Weighted undirected graph,
G = (V , E ), W : E → R
G assumed to be connected
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 2/6
Minimum cost spanning tree (MCST)
Weighted undirected graph,
G = (V , E ), W : E → R
G assumed to be connected
Strategy
Incrementally grow the minimum cost
spanning tree
Start with a smallest weight edge
overall
Extend the current tree by adding the
smallest edge from the tree to a vertex
not yet in the tree
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 2/6
Minimum cost spanning tree (MCST)
Weighted undirected graph,
G = (V , E ), W : E → R Example
G assumed to be connected 70
3 4
Find a minimum cost spanning tree 18 6 8
Tree connecting all vertices in V 10 20
0 1 2
Strategy
Incrementally grow the minimum cost
spanning tree
Start with a smallest weight edge
overall
Extend the current tree by adding the
smallest edge from the tree to a vertex
not yet in the tree
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 2/6
Minimum cost spanning tree (MCST)
Weighted undirected graph,
G = (V , E ), W : E → R Example
G assumed to be connected 70
3 4
Find a minimum cost spanning tree 18 6 8
Tree connecting all vertices in V 10 20
0 1 2
Strategy
Incrementally grow the minimum cost
Start with smallest edge, (1, 3)
spanning tree
Start with a smallest weight edge
overall
Extend the current tree by adding the
smallest edge from the tree to a vertex
not yet in the tree
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 2/6
Minimum cost spanning tree (MCST)
Weighted undirected graph,
G = (V , E ), W : E → R Example
G assumed to be connected 70
3 4
Find a minimum cost spanning tree 18 6 8
Tree connecting all vertices in V 10 20
0 1 2
Strategy
Incrementally grow the minimum cost
Start with smallest edge, (1, 3)
spanning tree
Extend the tree with (1, 0)
Start with a smallest weight edge
overall
Extend the current tree by adding the
smallest edge from the tree to a vertex
not yet in the tree
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 2/6
Minimum cost spanning tree (MCST)
Weighted undirected graph,
G = (V , E ), W : E → R Example
G assumed to be connected 70
3 4
Find a minimum cost spanning tree 18 6 8
Tree connecting all vertices in V 10 20
0 1 2
Strategy
Incrementally grow the minimum cost
Start with smallest edge, (1, 3)
spanning tree
Extend the tree with (1, 0)
Start with a smallest weight edge
overall Can’t add (0, 3), forms a cycle
Extend the current tree by adding the
smallest edge from the tree to a vertex
not yet in the tree
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 2/6
Minimum cost spanning tree (MCST)
Weighted undirected graph,
G = (V , E ), W : E → R Example
G assumed to be connected 70
3 4
Find a minimum cost spanning tree 18 6 8
Tree connecting all vertices in V 10 20
0 1 2
Strategy
Incrementally grow the minimum cost
Start with smallest edge, (1, 3)
spanning tree
Extend the tree with (1, 0)
Start with a smallest weight edge
overall Can’t add (0, 3), forms a cycle
Extend the current tree by adding the Instead, extend the tree with (1, 2)
smallest edge from the tree to a vertex
not yet in the tree
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 2/6
Minimum cost spanning tree (MCST)
Weighted undirected graph,
G = (V , E ), W : E → R Example
G assumed to be connected 70
3 4
Find a minimum cost spanning tree 18 6 8
Tree connecting all vertices in V 10 20
0 1 2
Strategy
Incrementally grow the minimum cost
Start with smallest edge, (1, 3)
spanning tree
Extend the tree with (1, 0)
Start with a smallest weight edge
overall Can’t add (0, 3), forms a cycle
Extend the current tree by adding the Instead, extend the tree with (1, 2)
smallest edge from the tree to a vertex Extend the tree with (2, 4)
not yet in the tree
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 2/6
Prim’s algorithm
G = (V , E ), W : E → R
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 3/6
Prim’s algorithm
G = (V , E ), W : E → R
Incrementally build an MCST
TV ⊆ V : tree vertices, already added to MCST
TE ⊆ E : tree edges, already added to MCST
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 3/6
Prim’s algorithm
G = (V , E ), W : E → R
Incrementally build an MCST
TV ⊆ V : tree vertices, already added to MCST
TE ⊆ E : tree edges, already added to MCST
Initially, TV = TE = ∅
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 3/6
Prim’s algorithm
G = (V , E ), W : E → R
Incrementally build an MCST
TV ⊆ V : tree vertices, already added to MCST
TE ⊆ E : tree edges, already added to MCST
Initially, TV = TE = ∅
Choose minimum weight edge e = (i, j)
Set TV = {i, j}, TE = {e} MCST
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 3/6
Prim’s algorithm
G = (V , E ), W : E → R
Incrementally build an MCST
TV ⊆ V : tree vertices, already added to MCST
TE ⊆ E : tree edges, already added to MCST
Initially, TV = TE = ∅
Choose minimum weight edge e = (i, j)
Set TV = {i, j}, TE = {e} MCST
Repeat n − 2 times
Choose minimum weight edge f = (u, v ) such that
u ∈ TV , v ∈
/ TV
Add v to TV , f to TE
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 3/6
Prim’s algorithm
G = (V , E ), W : E → R Example
G = (V , E ), W : E → R Example
G = (V , E ), W : E → R Example
G = (V , E ), W : E → R Example
G = (V , E ), W : E → R Example
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 4/6
Correctness of Prim’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 4/6
Correctness of Prim’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 4/6
Correctness of Prim’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 4/6
Correctness of Prim’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 4/6
Correctness of Prim’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 4/6
Correctness of Prim’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 4/6
Correctness of Prim’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 4/6
Correctness of Prim’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 4/6
Correctness of Prim’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 5/6
Correctness of Prim’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 5/6
Correctness of Prim’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 5/6
Correctness of Prim’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 5/6
Correctness of Prim’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 5/6
Correctness of Prim’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 5/6
Correctness of Prim’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 5/6
Correctness of Prim’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 5/6
Summary
Madhavan Mukund Minimum Cost Spanning Trees:Prim’s Algorithm Mathematics for Data Science 1, Week 12 6/6
Minimum Cost Spanning Trees:
Kruskal’s Algorithm
Madhavan Mukund
https://www.cmi.ac.in/~madhavan
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 2/5
Minimum cost spanning tree (MCST)
Weighted undirected graph,
G = (V , E ), W : E → R
G assumed to be connected
Strategy 2
Start with n components, each a
single vertex
Process edges in ascending order of
cost
Include edge if it does not create a
cycle
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 2/5
Minimum cost spanning tree (MCST)
Weighted undirected graph,
G = (V , E ), W : E → R Example
G assumed to be connected 70
3 4
Find a minimum cost spanning tree 18 6 8
Tree connecting all vertices in V 10 20
0 1 2
Strategy 2
Start with n components, each a
single vertex
Process edges in ascending order of
cost
Include edge if it does not create a
cycle
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 2/5
Minimum cost spanning tree (MCST)
Weighted undirected graph,
G = (V , E ), W : E → R Example
G assumed to be connected 70
3 4
Find a minimum cost spanning tree 18 6 8
Tree connecting all vertices in V 10 20
0 1 2
Strategy 2
Start with n components, each a
Start with smallest edge, (1, 3)
single vertex
Process edges in ascending order of
cost
Include edge if it does not create a
cycle
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 2/5
Minimum cost spanning tree (MCST)
Weighted undirected graph,
G = (V , E ), W : E → R Example
G assumed to be connected 70
3 4
Find a minimum cost spanning tree 18 6 8
Tree connecting all vertices in V 10 20
0 1 2
Strategy 2
Start with n components, each a
Start with smallest edge, (1, 3)
single vertex
Add next smallest edge, (2, 4)
Process edges in ascending order of
cost
Include edge if it does not create a
cycle
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 2/5
Minimum cost spanning tree (MCST)
Weighted undirected graph,
G = (V , E ), W : E → R Example
G assumed to be connected 70
3 4
Find a minimum cost spanning tree 18 6 8
Tree connecting all vertices in V 10 20
0 1 2
Strategy 2
Start with n components, each a
Start with smallest edge, (1, 3)
single vertex
Add next smallest edge, (2, 4)
Process edges in ascending order of
cost Add next smallest edge, (0, 1)
Include edge if it does not create a
cycle
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 2/5
Minimum cost spanning tree (MCST)
Weighted undirected graph,
G = (V , E ), W : E → R Example
G assumed to be connected 70
3 4
Find a minimum cost spanning tree 18 6 8
Tree connecting all vertices in V 10 20
0 1 2
Strategy 2
Start with n components, each a
Start with smallest edge, (1, 3)
single vertex
Add next smallest edge, (2, 4)
Process edges in ascending order of
cost Add next smallest edge, (0, 1)
Include edge if it does not create a Can’t add (0, 3), forms a cycle
cycle
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 2/5
Minimum cost spanning tree (MCST)
Weighted undirected graph,
G = (V , E ), W : E → R Example
G assumed to be connected 70
3 4
Find a minimum cost spanning tree 18 6 8
Tree connecting all vertices in V 10 20
0 1 2
Strategy 2
Start with n components, each a
Start with smallest edge, (1, 3)
single vertex
Add next smallest edge, (2, 4)
Process edges in ascending order of
cost Add next smallest edge, (0, 1)
Include edge if it does not create a Can’t add (0, 3), forms a cycle
cycle Add next smallest edge, (1, 2)
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 2/5
Kruskal’s algorithm
G = (V , E ), W : E → R
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 3/5
Kruskal’s algorithm
G = (V , E ), W : E → R
Let E = {e0 , e1 , . . . , em−1 } be
edges sorted in ascending order
by weight
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 3/5
Kruskal’s algorithm
G = (V , E ), W : E → R
Let E = {e0 , e1 , . . . , em−1 } be
edges sorted in ascending order
by weight
Let TE ⊆ E be the set of tree
edges already added to MCST
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 3/5
Kruskal’s algorithm
G = (V , E ), W : E → R
Let E = {e0 , e1 , . . . , em−1 } be
edges sorted in ascending order
by weight
Let TE ⊆ E be the set of tree
edges already added to MCST
Initially, TE = ∅
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 3/5
Kruskal’s algorithm
G = (V , E ), W : E → R
Let E = {e0 , e1 , . . . , em−1 } be
edges sorted in ascending order
by weight
Let TE ⊆ E be the set of tree
edges already added to MCST
Initially, TE = ∅
Scan E from e0 to em−1
If adding ei to TE creates a
loop, skip it
Otherwise, add ei to TE
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 3/5
Kruskal’s algorithm
G = (V , E ), W : E → R Example
70
Let E = {e0 , e1 , . . . , em−1 } be 2 3
edges sorted in ascending order 18 6
by weight 10
0 1 4 5
10 20
Let TE ⊆ E be the set of tree 10 5
edges already added to MCST 6
Initially, TE = ∅
Scan E from e0 to em−1
If adding ei to TE creates a
loop, skip it
Otherwise, add ei to TE
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 3/5
Kruskal’s algorithm
G = (V , E ), W : E → R Example
70
Let E = {e0 , e1 , . . . , em−1 } be 2 3
edges sorted in ascending order 18 6
by weight 10
0 1 4 5
10 20
Let TE ⊆ E be the set of tree 10 5
edges already added to MCST 6
Initially, TE = ∅ Sort E as
Scan E from e0 to em−1 {(5, 6), (1, 2), (0, 1), (4, 5), (4, 6), (0, 2), (1, 4), (2, 3)}
If adding ei to TE creates a
loop, skip it Set TE = ∅
Otherwise, add ei to TE
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 3/5
Kruskal’s algorithm
G = (V , E ), W : E → R Example
70
Let E = {e0 , e1 , . . . , em−1 } be 2 3
edges sorted in ascending order 18 6
by weight 10
0 1 4 5
10 20
Let TE ⊆ E be the set of tree 10 5
edges already added to MCST 6
Initially, TE = ∅ Sort E as
Scan E from e0 to em−1 {(5, 6), (1, 2), (0, 1), (4, 5), (4, 6), (0, 2), (1, 4), (2, 3)}
If adding ei to TE creates a
loop, skip it Add (5, 6)
Otherwise, add ei to TE Set TE = {(5, 6)}
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 3/5
Kruskal’s algorithm
G = (V , E ), W : E → R Example
70
Let E = {e0 , e1 , . . . , em−1 } be 2 3
edges sorted in ascending order 18 6
by weight 10
0 1 4 5
10 20
Let TE ⊆ E be the set of tree 10 5
edges already added to MCST 6
Initially, TE = ∅ Sort E as
Scan E from e0 to em−1 {(5, 6), (1, 2), (0, 1), (4, 5), (4, 6), (0, 2), (1, 4), (2, 3)}
If adding ei to TE creates a
loop, skip it Add (1, 2)
Otherwise, add ei to TE Set TE = {(5, 6), (1, 2)}
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 3/5
Kruskal’s algorithm
G = (V , E ), W : E → R Example
70
Let E = {e0 , e1 , . . . , em−1 } be 2 3
edges sorted in ascending order 18 6
by weight 10
0 1 4 5
10 20
Let TE ⊆ E be the set of tree 10 5
edges already added to MCST 6
Initially, TE = ∅ Sort E as
Scan E from e0 to em−1 {(5, 6), (1, 2), (0, 1), (4, 5), (4, 6), (0, 2), (1, 4), (2, 3)}
If adding ei to TE creates a
loop, skip it Add (0, 1)
Otherwise, add ei to TE Set TE = {(5, 6), (1, 2), (0, 1)}
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 3/5
Kruskal’s algorithm
G = (V , E ), W : E → R Example
70
Let E = {e0 , e1 , . . . , em−1 } be 2 3
edges sorted in ascending order 18 6
by weight 10
0 1 4 5
10 20
Let TE ⊆ E be the set of tree 10 5
edges already added to MCST 6
Initially, TE = ∅ Sort E as
Scan E from e0 to em−1 {(5, 6), (1, 2), (0, 1), (4, 5), (4, 6), (0, 2), (1, 4), (2, 3)}
If adding ei to TE creates a
loop, skip it Add (4, 5)
Otherwise, add ei to TE Set TE = {(5, 6), (1, 2), (0, 1), (4, 5)}
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 3/5
Kruskal’s algorithm
G = (V , E ), W : E → R Example
70
Let E = {e0 , e1 , . . . , em−1 } be 2 3
edges sorted in ascending order 18 6
by weight 10
0 1 4 5
10 20
Let TE ⊆ E be the set of tree 10 5
edges already added to MCST 6
Initially, TE = ∅ Sort E as
Scan E from e0 to em−1 {(5, 6), (1, 2), (0, 1), (4, 5), (4, 6), (0, 2), (1, 4), (2, 3)}
If adding ei to TE creates a
loop, skip it Skip (4, 6)
Otherwise, add ei to TE Set TE = {(5, 6), (1, 2), (0, 1), (4, 5)}
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 3/5
Kruskal’s algorithm
G = (V , E ), W : E → R Example
70
Let E = {e0 , e1 , . . . , em−1 } be 2 3
edges sorted in ascending order 18 6
by weight 10
0 1 4 5
10 20
Let TE ⊆ E be the set of tree 10 5
edges already added to MCST 6
Initially, TE = ∅ Sort E as
Scan E from e0 to em−1 {(5, 6), (1, 2), (0, 1), (4, 5), (4, 6), (0, 2), (1, 4), (2, 3)}
If adding ei to TE creates a
loop, skip it Skip (0, 2)
Otherwise, add ei to TE Set TE = {(5, 6), (1, 2), (0, 1), (4, 5)}
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 3/5
Kruskal’s algorithm
G = (V , E ), W : E → R Example
70
Let E = {e0 , e1 , . . . , em−1 } be 2 3
edges sorted in ascending order 18 6
by weight 10
0 1 4 5
10 20
Let TE ⊆ E be the set of tree 10 5
edges already added to MCST 6
Initially, TE = ∅ Sort E as
Scan E from e0 to em−1 {(5, 6), (1, 2), (0, 1), (4, 5), (4, 6), (0, 2), (1, 4), (2, 3)}
If adding ei to TE creates a
loop, skip it Add (1, 4)
Otherwise, add ei to TE Set TE = {(5, 6), (1, 2), (0, 1), (4, 5), (1, 4)}
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 3/5
Kruskal’s algorithm
G = (V , E ), W : E → R Example
70
Let E = {e0 , e1 , . . . , em−1 } be 2 3
edges sorted in ascending order 18 6
by weight 10
0 1 4 5
10 20
Let TE ⊆ E be the set of tree 10 5
edges already added to MCST 6
Initially, TE = ∅ Sort E as
Scan E from e0 to em−1 {(5, 6), (1, 2), (0, 1), (4, 5), (4, 6), (0, 2), (1, 4), (2, 3)}
If adding ei to TE creates a
loop, skip it Add (2, 3)
Otherwise, add ei to TE Set TE = {(5, 6), (1, 2), (0, 1), (4, 5), (1, 4), (2, 3)}
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 3/5
Correctness of Krusksal’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 4/5
Correctness of Krusksal’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 4/5
Correctness of Krusksal’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 4/5
Correctness of Krusksal’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 4/5
Correctness of Krusksal’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 4/5
Correctness of Krusksal’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 4/5
Correctness of Krusksal’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 4/5
Correctness of Krusksal’s algorithm
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 5/5
Summary
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 5/5
Summary
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 5/5
Summary
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 5/5
Summary
Madhavan Mukund Minimum Cost Spanning Trees:Kruskal’s Algorithm Mathematics for Data Science 1, Week 12 5/5