0% found this document useful (0 votes)
2 views

Lecture 11.4

In weighted graphs, each edge has a cost represented in an adjacency matrix, and the length of a path is the sum of these weights. The shortest path may not correspond to the minimum number of edges, and there are different shortest path problems such as single source and all-pairs. Negative edge weights can exist, but graphs should not have negative cycles for shortest paths to be well-defined.

Uploaded by

Sanskar pandey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 11.4

In weighted graphs, each edge has a cost represented in an adjacency matrix, and the length of a path is the sum of these weights. The shortest path may not correspond to the minimum number of edges, and there are different shortest path problems such as single source and all-pairs. Negative edge weights can exist, but graphs should not have negative cycles for shortest paths to be well-defined.

Uploaded by

Sanskar pandey
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Shortest Paths in Weighted Graphs

Madhavan Mukund
https://www.cmi.ac.in/~madhavan

Mathematics for Data Science 1


Week 12
Shortest paths

Recall that BFS explores a graph level 2 3


by level
0 1 4 5

Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 2/6
Shortest paths

Recall that BFS explores a graph level 2 3


by level
BFS computes shortest path, in terms 0 1 4 5
of number of edges, to every reachable
vertex 6

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

BFS computes shortest path, in terms 70


2 3
of number of edges, to every reachable
80 6
vertex
50
0 1 4 5
10 20
5 10
6

Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 3/6
Shortest paths in weighted graphs

BFS computes shortest path, in terms 70


2 3
of number of edges, to every reachable
80 6
vertex
50
In a weighted graph, add up the weights 0 1 4 5
10 20
along a path 5 10
6

Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 3/6
Shortest paths in weighted graphs

BFS computes shortest path, in terms 70


2 3
of number of edges, to every reachable
80 6
vertex
50
In a weighted graph, add up the weights 0 1 4 5
10 20
along a path 5 10
Weighted shortest path need not have 6
minimum number of edges
Shortest path from 0 to 2 is via 1

Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 3/6
Shortest path problems

Single source shortest paths


Find shortest paths from a fixed vertex
to every other vertex

Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 4/6
Shortest path problems

Single source shortest paths


Find shortest paths from a fixed vertex
to every other vertex
Transport finished product from factory
(single source) to all retail outlets

Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 4/6
Shortest path problems

Single source shortest paths


Find shortest paths from a fixed vertex
to every other vertex
Transport finished product from factory
(single source) to all retail outlets
Courier company delivers items from
distribution centre (single source) to
addressees

Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 4/6
Shortest path problems

Single source shortest paths All pairs shortest paths


Find shortest paths from a fixed vertex
to every other vertex
Transport finished product from factory
(single source) to all retail outlets
Courier company delivers items from
distribution centre (single source) to
addressees

Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 4/6
Shortest path problems

Single source shortest paths All pairs shortest paths


Find shortest paths from a fixed vertex Find shortest paths between every pair
to every other vertex of vertices i and j
Transport finished product from factory
(single source) to all retail outlets
Courier company delivers items from
distribution centre (single source) to
addressees

Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 4/6
Shortest path problems

Single source shortest paths All pairs shortest paths


Find shortest paths from a fixed vertex Find shortest paths between every pair
to every other vertex of vertices i and j
Transport finished product from factory Optimal airline, railway, road routes
(single source) to all retail outlets between cities
Courier company delivers items from
distribution centre (single source) to
addressees

Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 4/6
Negative edge weights

Negative edge weights

Can negative edge weights be


meaningful?

Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 5/6
Negative edge weights

Negative edge weights

Can negative edge weights be


meaningful?
Taxi driver trying to head home at the
end of the day

Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 5/6
Negative edge weights

Negative edge weights

Can negative edge weights be


meaningful?
Taxi driver trying to head home at the
end of the day
Roads with few customers, drive
empty (positive weight)

Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 5/6
Negative edge weights

Negative edge weights

Can negative edge weights be


meaningful?
Taxi driver trying to head home at the
end of the day
Roads with few customers, drive
empty (positive weight)
Roads with many customers, make
profit (negative weight)

Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 5/6
Negative edge weights

Negative edge weights

Can negative edge weights be


meaningful?
Taxi driver trying to head home at the
end of the day
Roads with few customers, drive
empty (positive weight)
Roads with many customers, make
profit (negative weight)
Find a route toward home that
minimizes the cost

Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 5/6
Negative edge weights

Negative edge weights Negative cycles

Can negative edge weights be A negative cycle is one whose weight is


meaningful? negative
Sum of the weights of edges that
Taxi driver trying to head home at the make up the cycle
end of the day
Roads with few customers, drive
empty (positive weight)
Roads with many customers, make
profit (negative weight)
Find a route toward home that
minimizes the cost

Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 5/6
Negative edge weights

Negative edge weights Negative cycles

Can negative edge weights be A negative cycle is one whose weight is


meaningful? negative
Sum of the weights of edges that
Taxi driver trying to head home at the make up the cycle
end of the day
Roads with few customers, drive By repeatedly traversing a negative
empty (positive weight) cycle, total cost keeps decreasing
Roads with many customers, make
profit (negative weight)
Find a route toward home that
minimizes the cost

Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 5/6
Negative edge weights

Negative edge weights Negative cycles

Can negative edge weights be A negative cycle is one whose weight is


meaningful? negative
Sum of the weights of edges that
Taxi driver trying to head home at the make up the cycle
end of the day
Roads with few customers, drive By repeatedly traversing a negative
empty (positive weight) cycle, total cost keeps decreasing
Roads with many customers, make If a graph has a negative cycle, shortest
profit (negative weight) paths are not defined
Find a route toward home that
minimizes the cost

Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 5/6
Negative edge weights

Negative edge weights Negative cycles

Can negative edge weights be A negative cycle is one whose weight is


meaningful? negative
Sum of the weights of edges that
Taxi driver trying to head home at the make up the cycle
end of the day
Roads with few customers, drive By repeatedly traversing a negative
empty (positive weight) cycle, total cost keeps decreasing
Roads with many customers, make If a graph has a negative cycle, shortest
profit (negative weight) paths are not defined
Find a route toward home that
minimizes the cost Without negative cycles, we can
compute shortest paths even if some
weights are negative
Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 5/6
Summary

In a weighted graph, each edge has a cost


Entries in adjacency matrix capture edge weights

Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 6/6
Summary

In a weighted graph, each edge has a cost


Entries in adjacency matrix capture edge weights

Length of a path is the sum of the weights


Shortest path in a weighted graph need not be minimum in terms of number of edges

Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 6/6
Summary

In a weighted graph, each edge has a cost


Entries in adjacency matrix capture edge weights

Length of a path is the sum of the weights


Shortest path in a weighted graph need not be minimum in terms of number of edges

Different shortest path problems


Single source — from one designated vertex to all others
All-pairs — between every pair of vertices

Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 6/6
Summary

In a weighted graph, each edge has a cost


Entries in adjacency matrix capture edge weights

Length of a path is the sum of the weights


Shortest path in a weighted graph need not be minimum in terms of number of edges

Different shortest path problems


Single source — from one designated vertex to all others
All-pairs — between every pair of vertices

Negative edge weights


Should not have negative cycles
Without negative cycles, shortest paths still well defined

Madhavan Mukund Shortest Paths in Weighted Graphs Mathematics for Data Science 1, Week 12 6/6

You might also like