Auto Affiliate AI Earns First Commission in 3 Hours..pdf
week1-module1-introduction-merged.pdf
1. DESIGN AND ANALYSIS
OF ALGORITHMS
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 1, Module 1
4. Topics to be covered
Asymptotic complexity
Searching and sorting in arrays
Binary search, insertion sort, selection sort, merge
sort, quick sort
Graphs and graph algorithms
Representations, reachability, connectedness
Directed acyclic graphs
Shortest paths, Spanning trees
5. Topics to be covered
Algorithmic design techniques
Divide and conquer, Greedy algorithms,
Dynamic programming
Data structures
Priority queues/heaps, Search trees,
Union of disjoint sets (union-find)
Miscellaneous topics
Intractability, …
7. Evaluation
Continuous evaluation
8 Weekly quizzes
6 programming assignments
Certification exam
Requirement for successful course completion
60% in quizzes, certification exam
Submit at least 5 of 6 assignments
At least 4 with nonzero marks
9. DESIGN AND ANALYSIS
OF ALGORITHMS
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 1, Module 2
10. Example 1: Air travel
Barbet Airlines serves several cities in India
Some cities are connected by direct flights
Want to compute all pairs of cities A,B such that A
and B are connected by a sequence of flights
15. Connected destinations
Compute paths in the graph
How do we represent the graph so that we can
manipulate it using a computer program?
Suitable data structure
How do we design an efficient algorithm for this
data representation?
16. Efficiency?
N cities, F direct flights
Computing paths depends on N and F
What is this dependency?
How large a value of N and F can we handle?
Online booking requires response in seconds
17. Variations
Flights have arrival and departure times
Only some connections are feasible
Should not have to wait overnight
… or more than 4 hours
How to compute feasible paths with constraints?
18. Other problems
Each sector has a cost
Compute cheapest route between a pair of
cities
Some aircraft grounded for maintenance
Which routes to operate to maintain
connectivity?
19. DESIGN AND ANALYSIS
OF ALGORITHMS
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 1, Module 3
20. Example 2: Xerox Shop
Campus Xerox has several photocopiers
Tomorrow is the deadline for BTech projects and
there is a rush of reports to be printed
How to schedule the pending jobs most
effectively?
21. Xerox Shop …
The number of pages for each job is known
Each customer has been promised delivery by a
deadline
Campus Xerox offers discount if deadline is not
met
How to sequentially allocate the jobs to
photocopiers to maximize revenue?
22. Xerox Shop …
Brute force
Try all possible allocations
Choose one that is optimum
Number of possibilities is exponential!
Even with 30 jobs, it would take hours to compute
an optimal schedule
23. Xerox Shop …
Decompose the problem
Choose a job to schedule first, and the machine
on which it will run, according to some strategy
Now, recursively solve the problem for N-1 jobs
24. Xerox Shop …
Greedy approach
Fix the choice of next job once and for all
Never go back and try another sequence
How to choose the next job?
Shortest processing time?
Earliest deadline?
How to show that this strategy is optimal?
25. Variations
Some photocopiers are old and slow, some are
new and fast
Time for a job depends on choice of machine
Cost of ink and paper varies across machines
Net revenue for a job depends on choice of
machine
26. Variations
Account for set up time between jobs
Need to reserve time slots to reload paper
Is there a valid greedy strategy?
27. DESIGN AND ANALYSIS
OF ALGORITHMS
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 1, Module 4
28. Example 3: Document
similarity
Given two documents, how similar are they?
Plagiarism detection
Checking changes between versions of code
Answering web search queries more effectively
29. Document similarity …
What is a good measure of similarity?
Edit distance
How many changes does one have to make to get
from one document to another?
What types of changes are allowed?
Add or remove a letter
Replace one letter by another
30. Document similarity …
Edit Distance
Minimum number of edit operations to transform
one document to another
How do we compute it?
Brute force: try all sequences and choose the best
one
Delete all of first document, add all of second
document
Impossibly inefficient!
31. Decomposing the problem
Make the first character in both documents the
same
Explore all possible edit operations that make
this possible
Recursively fix the rest of the documents
Naive recursion is inefficient
Same subproblem solved recursively many times
33. Dynamic Programming
Making recursive computations efficient
Ensure that subproblems are computed only once
How do we store and look up answers to already
solved subproblems?
34. Variations
Interested only in the meaning of the document
Focus on words
Documents are near if they overlap on many words
Order in which words occur may not matter
Useful for topic based web search
Can have dictionary of “similar” words
35. DESIGN AND ANALYSIS
OF ALGORITHMS
Arrays and lists
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 2, Module 1
36. Sequences of values
Two basic ways of storing a sequence of values
Arrays
Lists
What’s the difference?
37. Arrays
Single block of memory
Typically fixed size
Indexing is fast
Access A[i] in constant
time for any i
Inserting an element between
A[i] and A[i+1] is expensive
Contraction is expensive
38. Lists
Values scattered in memory
Each element points to the
next—“linked” list
Flexible size
Follow i links to access A[i]
Cost proportional to i
Inserting or deleting an element
is easy
“Plumbing”
39. Operations
Exchange A[i] and A[j]
Constant time in array, linear time in lists
Delete A[i] or Insert v after A[i]
Constant time in lists (if we are already at A[i])
Linear time in array
Algorithms on one data structure may not transfer to
another
Example: Binary search
40. DESIGN AND ANALYSIS
OF ALGORITHMS
Searching in an array
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 2, Module 2
41. Search problem
Is a value K present in a collection A?
Does the structure of A matter?
Array vs list
Does the organization of the information matter?
Values sorted/unsorted
42. The unsorted case
function search(A,K)
i = 0;
while i < n and A[i] != K do
i = i+1;
if i < n
return i;
else
return -1;
43. Worst case
Need to scan the entire sequence A
O(n) time for input sequence of size A
Does not matter if A is array or list
44. Search a sorted sequence
What if A is sorted?
Compare K with midpoint of A
If midpoint is K, the value is found
If K < midpoint, search left half of A
If K > midpoint, search right half of A
Binary search
45. Binary search …
bsearch(K,A,l,r) // A sorted, search for K in A[l..r-1]
if (r - l == 0) return(false)
mid = (l + r) div 2 // integer division
if (K == A[mid]) return (true)
if (K < A[mid])
return (bsearch(K,A,l,mid))
else
return (bsearch(K,A,mid+1,r))
46. Binary Search …
How long does this take?
Each step halves the interval to search
For an interval of size 0, the answer is
immediate
T(n): time to search in an array of size n
T(0) = 1
T(n) = 1 + T(n/2)
47. Binary Search …
T(n): time to search in a list of size n
T(0) = 1
T(n) = 1 + T(n/2)
Unwind the recurrence
T(n) = 1 + T(n/2) = 1 + 1 + T(n/22) = …
= 1 + 1 + … + 1 + T(n/2k)
= 1 + 1 + … + 1 + T(n/2log n) = O(log n)
48. Binary Search …
Works only for arrays
Need to be look up A[i] in constant time
By seeing only a small fraction of the sequence,
we can conclude that an element is not present!
49. DESIGN AND ANALYSIS
OF ALGORITHMS
Selection Sort
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 2, Module 3
50. Sorting
Searching for a value
Unsorted array — linear scan, O(n)
Sorted array — binary search, O(log n)
Other advantages of sorting
Finding median value: midpoint of sorted list
Checking for duplicates
Building a frequency table of values
51. How to sort?
You are a Teaching Assistant for a course
The instructor gives you a stack of exam answer
papers with marks, ordered randomly
Your task is to arrange them in descending order
52. Strategy 1
Scan the entire stack and find the paper with
minimum marks
Move this paper to a new stack
Repeat with remaining papers
Each time, add next minimum mark paper on
top of new stack
Eventually, new stack is sorted in descending
order
74. Selection Sort
SelectionSort(A,n) // Sort A of size n
for (startpos = 0; startpos < n; startpos++)
// Scan segments A[0]..A[n-1], A[1]..A[n-1], …
// Locate position of minimum element in current segment
minpos = startpos;
for (i = minpos+1; i < n; i++)
if (A[i] < A[minpos])
minpos = i;
// Move minimum element to start of current segment
swap(A,startpos,minpos)
75. Analysis of Selection Sort
Finding minimum in unsorted segment of length k
requires one scan, k steps
In each iteration, segment to be scanned reduces
by 1
t(n) = n + (n-1) + (n-2) + … + 1 = n(n+1)/2 = O(n2)
76. Recursive formulation
To sort A[i .. n-1]
Find minimum value in segment and move to A[i]
Apply Selection Sort to A[i+1..n-1]
Base case
Do nothing if i = n-1
77. Selection Sort, recursive
SelectionSort(A,start,n) // Sort A from start to n-1
if (start >= n-1)
return;
// Locate minimum element and move to start of segment
minpos = start;
for (i = start+1; i < n; i++)
if (A[i] < A[minpos])
minpos = i;
swap(A,start,minpos)
// Recursively sort the rest
SelectionSort(A,start+1,n)
78. Alternative calculation
t(n), time to run selection sort on length n
n steps to find minimum and move to position 0
t(n-1) time to run selection sort on A[1] to A[n-1]
Recurrence
t(n) = n + t(n-1)
t(1) = 1
t(n) = n + t(n-1) = n + ((n-1) + t(n-2)) = … =
n + (n-1) + (n-2) + … + 1 = n(n+1)/2 = O(n2)
79. DESIGN AND ANALYSIS
OF ALGORITHMS
Insertion Sort
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 2, Module 4
80. Sorting
Searching for a value
Unsorted array — linear scan, O(n)
Sorted array — binary search, O(log n)
Other advantages of sorting
Finding median value: midpoint of sorted list
Checking for duplicates
Building a frequency table of values
81. How to sort?
You are a Teaching Assistant for a course
The instructor gives you a stack of exam answer
papers with marks, ordered randomly
Your task is to arrange them in descending order
82. Strategy 2
First paper: put in a new stack
Second paper:
Lower marks than first? Place below first paper
Higher marks than first? Place above first paper
Third paper
Insert into the correct position with respect to first
two papers
Do this for each subsequent paper:
insert into correct position in new sorted stack
90. Strategy 2 …
Insertion Sort
Start building a sorted sequence with one element
Pick up next unsorted element and insert it into its
correct place in the already sorted sequence
91. Insertion Sort
InsertionSort(A,n) // Sort A of size n
for (pos = 1; pos < n; pos++)
// Build longer and longer sorted segments
// In each iteration A[0]..A[pos-1] is already sorted
// Move first element after sorted segment left
// till it is in the correct place
nextpos = pos
while (nextpos > 0 &&
A[nextpos] < A[nextpos-1])
swap(A,nextpos,nextpos-1)
nextpos = nextpos-1
104. Analysis of Insertion Sort
Inserting a new value in sorted segment of length
k requires upto k steps in the worst case
In each iteration, sorted segment in which to insert
increased by 1
t(n) = 1 + 2 + … + n-1 = n(n-1)/2 = O(n2)
107. Recurrence
t(n), time to run insertion sort on length n
Time t(n-1) to sort segment A[0] to A[n-2]
n-1 steps to insert A[n-1] in sorted segment
Recurrence
t(n) = n-1 + t(n-1)
t(1) = 1
t(n) = n-1 + t(n-1) = n-1 + ((n-2) + t(n-2)) = … =
(n-1) + (n-2) + … + 1 = n(n-1)/2 = O(n2)
108. O(n2) sorting algorithms
Selection sort and insertion sort are both O(n2)
So is bubble sort, which we will not discuss here
O(n2) sorting is infeasible for n over 10000
Among O(n2) sorts, insertion sort is usually better
than selection sort and both are better than
bubble sort
What happens when we apply insertion sort to
an already sorted list?
109. DESIGN AND ANALYSIS
OF ALGORITHMS
Merge Sort
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 2, Module 5
111. A different strategy?
Divide array in two equal parts
Separately sort left and right half
Combine the two sorted halves to get the full array
sorted
112. Combining sorted lists
Given two sorted lists A and B, combine into a
sorted list C
Compare first element of A and B
Move it into C
Repeat until all elements in A and B are over
Merging A and B
120. Merge Sort
Sort A[0] to A[n/2-1]
Sort A[n/2] to A[n-1]
Merge sorted halves into B[0..n-1]
How do we sort the halves?
Recursively, using the same strategy!
137. Divide and conquer
Break up problem into disjoint parts
Solve each part separately
Combine the solutions efficiently
138. Merging sorted lists
Combine two sorted lists A and B into C
If A is empty, copy B into C
If B is empty, copy A into C
Otherwise, compare first element of A and B and
move the smaller of the two into C
Repeat until all elements in A and B have been
moved
139. Merging
function Merge(A,m,B,n,C)
// Merge A[0..m-1], B[0..n-1] into C[0..m+n-1]
i = 0; j = 0; k = 0;
// Current positions in A,B,C respectively
while (k < m+n)
// Case 0: One of the two lists is empty
if (i==m) {j++; k++;}
if (j==n) {i++; k++;}
// Case 1: Move head of A into C
if (A[i] <= B[j]) { C[k] = B[j]; j++; k++;}
// Case 2: Move head of B into C
if (A[i] > B[j]) {C[k] = B[j]; j++; k++;}
140. Merge Sort
To sort A[0..n-1] into B[0..n-1]
If n is 1, nothing to be done
Otherwise
Sort A[0..n/2-1] into L (left)
Sort A[n/2..n-1] into R (right)
Merge L and R into B
141. Merge Sort
function MergeSort(A,left,right,B)
// Sort the segment A[left..right-1] into B
if (right - left == 1) // Base case
B[0] = A[left]
if (right - left > 1) // Recursive call
mid = (left+right)/2
MergeSort(A,left,mid,L)
MergeSort(A,mid,right,R)
Merge(L,mid-left,R,right-mid,B)
142. DESIGN AND ANALYSIS
OF ALGORITHMS
Merge sort: Analysis
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 2, Module 6
143. Merging sorted lists
Combine two sorted lists A and B into C
If A is empty, copy B into C
If B is empty, copy A into C
Otherwise, compare first element of A and B and
move the smaller of the two into C
Repeat until all elements in A and B have been
moved
144. Merging
function Merge(A,m,B,n,C)
// Merge A[0..m-1], B[0..n-1] into C[0..m+n-1]
i = 0; j = 0; k = 0;
// Current positions in A,B,C respectively
while (k < m+n)
// Case 1: Move head of A into C
if (j==n or A[i] <= B[j])
C[k] = A[i]; i++; k++
// Case 2: Move head of B into C
if (i==m or A[i] > B[j])
C[k] = B[j]; j++; k++
145. Analysis of Merge
How much time does Merge take?
Merge A of size m, B of size n into C
In each iteration, we add one element to C
At most 7 basic operations per iteration
Size of C is m+n
m+n ≲ 2 max(m,n)
Hence O(max(m,n)) = O(n) if m ≈ n
146. Merge Sort
To sort A[0..n-1] into B[0..n-1]
If n is 1, nothing to be done
Otherwise
Sort A[0..n/2-1] into L (left)
Sort A[n/2..n-1] into R (right)
Merge L and R into B
147. Analysis of Merge Sort …
t(n): time taken by Merge Sort on input of size n
Assume, for simplicity, that n = 2k
t(n) = 2t(n/2) + n
Two subproblems of size n/2
Merging solutions requires time O(n/2+n/2) = O(n)
Solve the recurrence by unwinding
155. Analysis of Merge Sort …
t(1) = 1
t(n) = 2t(n/2) + n
= 2 [ 2t(n/4) + n/2 ] + n = 22
t(n/22
) + 2n
= 22
[ 2t(n/23
) + n/22
] + 2n = 23
t(n/23
) + 3n
…
= 2j
t(n/2j
) + jn
When j = log n, n/2j
= 1, so t(n/2j
) = 1
log n means log2 n unless otherwise specified!
156. Analysis of Merge Sort …
t(1) = 1
t(n) = 2t(n/2) + n
= 2 [ 2t(n/4) + n/2 ] + n = 22
t(n/22
) + 2n
= 22
[ 2t(n/23
) + n/22
] + 2n = 23
t(n/23
) + 3n
…
= 2j
t(n/2j
) + jn
When j = log n, n/2j
= 1, so t(n/2j
) = 1
log n means log2 n unless otherwise specified!
t(n) = 2j
t(n/2j
) + jn = 2log n
+ (log n) n = n + n log n = O(n log n)
157. O(n log n) sorting
Recall that O(n log n) is much more efficient than
O(n2)
Assuming 108 operations per second, feasible
input size goes from 10,000 to 10,000,000
(10 million or 1 crore)
158. Variations on merge
Union of two sorted lists (discard duplicates)
If A[i] == B[j], copy A[i] to C[k] and increment i,j,k
Intersection of two sorted lists
If A[i] < B[j], increment i
If B[j] < A[i], increment j
If A[i] == B[j], copy A[i] to C[k] and increment i,j,k
Exercise: List difference: elements in A but not in B
159. Merge Sort: Shortcomings
Merging A and B creates a new array C
No obvious way to efficiently merge in place
Extra storage can be costly
Inherently recursive
Recursive call and return are expensive
160. Alternative approach
Extra space is required to merge
Merging happens because elements in left half
must move right and vice versa
Can we divide so that everything to the left is
smaller than everything to the right?
No need to merge!
161. DESIGN AND ANALYSIS
OF ALGORITHMS
Quicksort
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 2, Module 7
162. Merge Sort: Shortcomings
Merging A and B creates a new array C
No obvious way to efficiently merge in place
Extra storage can be costly
Inherently recursive
Recursive call and return are expensive
163. Alternative approach
Extra space is required to merge
Merging happens because elements in left half
must move right and vice versa
Can we divide so that everything to the left is
smaller than everything to the right?
No need to merge!
164. Divide and conquer without merging
Suppose the median value in A is m
Move all values ≤ m to left half of A
Right half has values > m
This shifting can be done in place, in time O(n)
Recursively sort left and right halves
A is now sorted! No need to merge
t(n) = 2t(n/2) + n = O(n log n)
165. Divide and conquer without merging
How do we find the median?
Sort and pick up middle element
But our aim is to sort!
Instead, pick up some value in A — pivot
Split A with respect to this pivot element
166. Quicksort
Choose a pivot element
Typically the first value in the array
Partition A into lower and upper parts with respect
to pivot
Move pivot between lower and upper partition
Recursively sort the two partitions
186. Quicksort(A,l,r) // Sort A[l..r-1]
if (r - l <= 1)) return; // Base case
// Partition with respect to pivot, a[l]
yellow = l+1;
for (green = l+1; green < r; green++)
if (A[green] <= A[l])
swap(A,yellow,green);
yellow++;
swap(A,l,yellow-1); // Move pivot into place
Quicksort(A,l,yellow); // Recursive calls
Quicksort(A,yellow+1,r);
Quicksort: Implementation
197. DESIGN AND ANALYSIS
OF ALGORITHMS
Quicksort: Analysis
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 2, Module 8
198. Quicksort
Choose a pivot element
Typically the first value in the array
Partition A into lower and upper parts with respect
to pivot
Move pivot between lower and upper partition
Recursively sort the two partitions
199. Analysis of Quicksort
Partitioning with respect to pivot takes O(n)
If pivot is median
Each partition is of size n/2
t(n) = 2t(n/2) + n = O(n log n)
Worst case?
200. Analysis of Quicksort
Worst case
Pivot is maximum or minimum
One partition is empty
Other is size n-1
t(n) = t(n-1) + n = t(n-2) + (n-1) + n
= … = 1 + 2 + … + n = O(n2
)
Already sorted array is worst case input!
201. Analysis of Quicksort
But …
Average case is O(n log n)
Sorting is a rare example where average case
can be computed
What does average case mean?
202. Quicksort: Average case
Assume input is a permutation of {1,2,…,n}
Actual values not important
Only relative order matters
Each input is equally likely (uniform probability)
Calculate running time across all inputs
Expected running time can be shown O(n log n)
203. Quicksort: randomization
Worst case arises because of fixed choice of pivot
We chose the first element
For any fixed strategy (last element, midpoint), can
work backwards to construct O(n2
) worst case
Instead, choose pivot randomly
Pick any index in [0..n-1] with uniform probability
Expected running time is again O(n log n)
204. Iterative Quicksort
Recursive calls work on disjoint segments of array
No recombination of results required
Can use an explicit stack to simulate recursion
Stack only needs to store left and right
endpoints of interval to be sorted
205. Quicksort in practice
In practice, Quicksort is very fast
Typically the default algorithm for in-built sort
functions
Spreadsheets
Built in sort function in programming
languages
206. DESIGN AND ANALYSIS
OF ALGORITHMS
Sorting: Concluding remarks
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 2, Module 9
207. Stable sorting
Sorting on multiple criteria
Assume students are listed in alphabetical order
Now sort students by marks
After sorting, are students with equal marks still
in alphabetical order?
Stability is crucial in applications like spreadsheets
Sorting column B should not disturb previous
sort on column A
208. Stable sorting …
Quicksort, as shown, is not stable
Swap operation during partitioning disturbs
original order
Merge sort is stable if we merge carefully
Do not allow elements from right to overtake
elements from left
Favour left list when breaking ties
209. Other criteria
Minimize data movement
Imagine values are heavy cartons
Want to reduce effort of moving values around
210. Which is the best?
Typically Quicksort
Be careful to avoid worst-case
Randomize choice of pivot element
Mergesort is used for “external” sorting
Database tables do not fit in memory
Need to sort on disk
211. Which is the best?
Other O(n log n) algorithms exist
Heap sort
Naive O(n2) not used except when data is small
Hybrid algorithms
Use divide and conquer for large n
Switch to insertion sort when n small (e.g. n <16)
212. DESIGN AND ANALYSIS
OF ALGORITHMS
Introduction to graphs
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 3, Module 1
213. Map
Colouring
Assign each
state or country
a colour
States that
share a border
should be
coloured
differently
How many
colours do we
need?
226. Graph
Colouring
The problem we
have solved is called
graph colouring
We used 4 colours
In fact, 4 colours are
always enough for
such maps
This is a theorem
that is surprisingly
hard to prove!
234. More graph
problems
Airline routing
Can I travel from
New Delhi to
Trivandrum
without
changing
airlines?
Again, all that is
important is the
underlying
graph
235. Graphs, formally
G = (V,E)
Set of vertices V
Set of edges E
E is a subset of pairs (v,v’): E ⊆ V × V
Undirected graph: (v,v’) and (v’,v) are the same edge
Directed graph:
(v,v’) is an edge from v to v’
Does not guarantee that (v’,v) is also an edge
240. Finding a
route
Directed graph
Find a sequence
of vertices v0, v1,
…, vk such that
v0 is New
Delhi
Each (vi,vi+1) is
an edge in E
vk is
Trivandrum
v0
v1
v2
v3
v4
v5
241. Finding a
route
Also makes sense
for undirected
graphs
Find a sequence
of vertices v0, v1,
…, vk such that
v0 is New Delhi
Each (vi,vi+1) is
an edge in E
vk is Trivandrum
v0
v1
v2
v3
v4
v5
242. DESIGN AND ANALYSIS
OF ALGORITHMS
Representing graphs
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 3, Module 2
243. Graphs, formally
G = (V,E)
Set of vertices V
Set of edges E
E is a subset of pairs (v,v’): E ⊆ V × V
Undirected graph: (v,v’) and (v’,v) are the same edge
Directed graph:
(v,v’) is an edge from v to v’
Does not guarantee that (v’,v) is also an edge
244. Finding a
route
Directed graph
Find a sequence
of vertices v0, v1,
…, vk such that
v0 is New
Delhi
Each (vi,vi+1) is
an edge in E
vk is
Trivandrum
v0
v1
v2
v3
v4
v5
245. Finding a
route
Also makes sense
for undirected
graphs
Find a sequence
of vertices v0, v1,
…, vk such that
v0 is New Delhi
Each (vi,vi+1) is
an edge in E
vk is Trivandrum
v0
v1
v2
v3
v4
v5
246. Working with graphs
We are given G = (V,E), undirected
Is there a path from source vs to target vt?
Look at the picture and see if vs and vt are
connected
How do we get an algorithm to “look at the
picture”?
247. Representing graphs
Let V have n vertices
We can assume vertices are named 1,2,…,n
Each edge is now a pair (i,j), where 1 ≤ i,j ≤ n
Let A(i,j) = 1 if (i,j) is an edge and 0 otherwise
A is an n x n matrix describing the graph
Adjacency matrix
280. Exploring graphs
Need a systematic algorithm
Mark vertices that have been visited
Keep track of vertices whose neighbours have
already been explored
Avoid going round indefinitely in circles
Two fundamental strategies: breadth first and
depth first
281. An alternative representation
1 2 3 4 5 6 7 8 9 10
1 0 1 1 1 0 0 0 0 0 0
2 1 0 1 0 0 0 0 0 0 0
3 1 1 0 0 0 0 0 0 0 0
4 1 0 0 0 1 0 0 1 0 0
5 0 0 0 1 0 1 1 0 0 0
6 0 0 0 0 1 0 1 1 1 0
7 0 0 0 0 1 1 0 0 0 0
8 0 0 0 1 0 1 0 0 1 0
9 0 0 0 0 0 1 0 1 0 1
10 0 0 0 0 0 0 0 0 1 0
Adjacency matrix has
many 0’s
Size of the matrix is n2
regardless of number
of edges
Maximum size of E is
n(n-1)/2 if we disallow
self loops
Typically E is much
smaller
282. Adjacency list
1
2
3
4
5
6 7
8 9
10
For each vertex, maintain a
list of its neighbours
1 2,3,4
2 1,3
3 1,2
4 1,5,8
5 4,6,7
6 5,7,8,9
7 5,6
8 4,6,9
9 6,8,10
10 9
283. Comparing representations
Adjacency list typically requires less space
Is j a neighbour of i?
Just check if A[i][j] is 1 in adjacency matrix
Need to scan neighbours of i in adjacency list
Which vertices are neighbours of i?
Scan all n columns in adjacency matrix
Takes time proportional to neighbours in adjacency
list
284. DESIGN AND ANALYSIS
OF ALGORITHMS
Breadth first search (BFS)
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 3, Module 3
285. Graphs, formally
G = (V,E)
Set of vertices V
Set of edges E
E is a subset of pairs (v,v’): E ⊆ V × V
Undirected graph: (v,v’) and (v’,v) are the same edge
Directed graph:
(v,v’) is an edge from v to v’
Does not guarantee that (v’,v) is also an edge
286. Finding a
route
Find a
sequence of
vertices v0, v1,
…, vk such that
v0 is source
Each (vi,vi+1)
is an edge in
E
vk is target
v0
v1
v2
v3
v4
v5
288. Adjacency list
1
2
3
4
5
6 7
8 9
10
For each vertex, maintain a
list of its neighbours
1 2,3,4
2 1,3
3 1,2
4 1,5,8
5 4,6,7
6 5,7,8,9
7 5,6
8 4,6,9
9 6,8,10
10 9
289. Finding a path
Mark vertices that have been visited
Keep track of vertices whose neighbours have
already been explored
Avoid going round indefinitely in circles
Two fundamental strategies: breadth first and
depth first
290. Breadth first search
Explore the graph level by level
First visit vertices one step away
Then two steps away
…
Remember which vertices have been visited
Also keep track of vertices visited, but whose
neighbours are yet to be explored
291. Breadth first search
Recall that V = {1,2,…,n}
Array visited[i] records whether i has been visited
When a vertex is visited for the first time, add it to
a queue
Explore vertices in the order they reach the
queue
292. Breadth first search
Exploring a vertex i:
for each edge (i,j)
if visited[j] == 0
visited[j] = 1
append j to queue
Initially, queue contains only source vertex
At each stage, explore vertex at the head of the
queue
Stop when the queue becomes empty
315. Breadth first search
function BFS(i) // BFS starting from vertex i
//Initialization
for j = 1..n {visited[j] = 0}; Q = []
//Start the exploration at i
visited[i] = 1; append(Q,i)
//Explore each vertex in Q
while Q is not empty
j = extract_head(Q)
for each (j,k) in E
if visited[k] == 0
visited[k] = 1; append(Q,k)
316. Complexity of BFS
Each vertex enters Q exactly once
If graph is connected, loop to process Q iterated n
times
For each j extracted from Q, need to examine all
neighbours of j
In adjacency matrix, scan row j: n entries
Hence, overall O(n2)
317. Complexity of BFS
Let m be the number of edges in E. What if m << n2
?
Adjacency list: scanning neighbours of j takes time
proportional to number of neighbours (degree of j)
Across the loop, each edge (i,j) is scanned twice,
once when exploring i and again when exploring j
Overall, exploring neighbours takes time O(m)
Marking n vertices visited still takes O(n)
Overall, O(n+m)
318. Complexity of BFS
For graphs, O(m+n) is considered the best
possible
Need to see each edge and vertex at least once
O(m+n) is considered to be linear in the size of the
graph
319. Enhancements to BFS
If BFS(i) sets visited[j] = 1, we know that i and j are
connected
How do we identify a path from i to j
When we mark visited[k] = 1, remember the
neighbour from which we marked it
If exploring edge (j,k) visits k, set parent[k] = j
320. Breadth first search
function BFS(i) // BFS starting from vertex i
//Initialization
for j = 1..n {visited[j] = 0; parent[j] = -1}
Q = []
//Start the exploration at i
visited[i] = 1; append(Q,i)
//Explore each vertex in Q
while Q is not empty
j = extract_head(Q)
for each (j,k) in E
if visited[k] == 0
visited[k] = 1; parent[k] = j; append(Q,k);
321. Reconstructing the path
BFS(i) sets visited[j] = 1
visited[j] = 1, so parent[j] = j’ for some j’
visited[j’] = 1, so parent[j’] = j” for some j’’
…
Eventually, trace back path to k with parent[k] = i
322. Recording distances
BFS can record how long the path is to each
vertex
Instead of binary array visited[ ], keep integer array
level[ ]
level[j] = -1 initially
level[j] = p means j is reached in p steps from i
323. Breadth first search
function BFS(i) // BFS starting from vertex i
//Initialization
for j = 1..n {level[j] = -1; parent[j] = -1}
Q = []
//Start the exploration at i, level[i] set to 0
level[i] = 0; append(Q,i)
//Explore each vertex in Q, increment level for each new vertex
while Q is not empty
j = extract_head(Q)
for each (j,k) in E
if level[k] == -1
level[k] = 1+level[j]; parent[k] = j;
append(Q,k);
346. Recording distances
BFS with level[ ] gives us the shortest path to each
node in terms of number of edges
In general, edges are labelled by a cost (money,
time, distance …)
Min cost path not same as fewest edges
Will look at shortest paths in weighted graphs later
BFS computes shortest paths if all costs are 1
347. DESIGN AND ANALYSIS
OF ALGORITHMS
Depth first search (DFS)
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 3, Module 4
348. Depth first search
Start from i, visit a neighbour j
Suspend the exploration of i and explore j instead
Continue till you reach a vertex with no unexplored
neighbours
Backtrack to nearest suspended vertex that still has an
unexplored neighbour
Suspended vertices are stored in a stack
Last in, first out: most recently suspended is checked
first
371. Depth first search
DFS is most natural to implement recursively
For each unvisited neighbour j of i, call DFS(j)
372. Depth first search
DFS is most natural to implement recursively
For each unvisited neighbour j of i, call DFS(j)
No need to explicitly maintain a stack
Stack is maintained implicitly by recursive calls
373. Depth first search
//Initialization
for j = 1..n {visited[j] = 0; parent[j] = -1}
function DFS(i) // DFS starting from vertex i
//Mark i as visited
visited[i] = 1
//Explore each neighbour of i recursively
for each (i,j) in E
if visited[j] == 0
parent[j] = i
DFS(j)
376. Complexity of DFS
Each vertex marked and explored exactly once
DFS(j) need to examine all neighbours of j
377. Complexity of DFS
Each vertex marked and explored exactly once
DFS(j) need to examine all neighbours of j
In adjacency matrix, scan row j: n entries
Overall O(n2
)
378. Complexity of DFS
Each vertex marked and explored exactly once
DFS(j) need to examine all neighbours of j
In adjacency matrix, scan row j: n entries
Overall O(n2
)
With adjacency list, scanning takes O(m) time
across all vertices
Total time is O(m+n), like BFS
381. Properties of DFS
Paths discovered by DFS are not shortest paths,
unlike BFS
Why use DFS at all?
382. Properties of DFS
Paths discovered by DFS are not shortest paths,
unlike BFS
Why use DFS at all?
Many useful features can be extracted from recording
the order in which DFS visited vertices
383. Properties of DFS
Paths discovered by DFS are not shortest paths,
unlike BFS
Why use DFS at all?
Many useful features can be extracted from recording
the order in which DFS visited vertices
DFS numbering
384. Properties of DFS
Paths discovered by DFS are not shortest paths,
unlike BFS
Why use DFS at all?
Many useful features can be extracted from recording
the order in which DFS visited vertices
DFS numbering
Maintain a counter
385. Properties of DFS
Paths discovered by DFS are not shortest paths,
unlike BFS
Why use DFS at all?
Many useful features can be extracted from recording
the order in which DFS visited vertices
DFS numbering
Maintain a counter
Increment and record counter value when entering
and leaving a vertex.
386. Depth first search
//Initialization
for j = 1..n {visited[j] = 0; parent[j] = -1}
count = 0
function DFS(i) // DFS starting from vertex i
//Mark i as visited
visited[i] = 1; pre[i] = count; count++
//Explore each neighbours of i recursively
for each (i,j) in E
if visited[j] == 0
parent[j] = i
DFS(j)
post[i] = count; count++
388. DFS numbering
pre[i] and post[i] can be used
to find
if the graph has a cycle —
i.e., a loop
cut vertex — removal
disconnects the graph
…
1
5
4
2
3
6 7
8 9
10
389. Summary
BFS and DFS are two systematic ways to explore a
graph
Both take time linear in the size of the graph with
adjacency lists
Recover paths by keeping parent information
BFS can compute shortest paths, in terms of
number of edges
DFS numbering can reveal many interesting features
390. DESIGN AND ANALYSIS
OF ALGORITHMS
Applications of BFS and DFS
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 3, Module 5
391. Graphs, formally
G = (V,E)
Set of vertices V
Set of edges E
E is a subset of pairs (v,v’): E ⊆ V × V
Undirected graph: (v,v’) and (v’,v) are the same edge
Directed graph:
(v,v’) is an edge from v to v’
Does not guarantee that (v’,v) is also an edge
392. Exploring graph structure
Breadth first search
Level by level exploration
Depth first search
Explore each vertex as soon as it is visited
DFS numbering
What can we find out about a graph using BFS/
DFS?
395. Identifying connected
components
Vertices {1,2,…,N}
Start BFS or DFS from 1
All nodes marked Visited form a connected component
Pick first unvisited node, say j, and run BFS or DFS
from j
Repeat till all nodes are visited
Update BFS/DFS to label each visited node with
component number
396. Connected components
1 2
5
9 10
3 4
7
11 12
6 8
Add a counter comp to number components
Increment counter each time a fresh BFS/DFS starts
Label each visited node j with component[j] = comp
397. Connected components
1 2
5
9 10
3 4
7
11 12
6 8
Add a counter comp to number components
Increment counter each time a fresh BFS/DFS starts
Label each visited node j with component[j] = comp
1
398. Connected components
1 2
5
9 10
3 4
7
11 12
6 8
Add a counter comp to number components
Increment counter each time a fresh BFS/DFS starts
Label each visited node j with component[j] = comp
1 2
399. Connected components
1 2
5
9 10
3 4
7
11 12
6 8
Add a counter comp to number components
Increment counter each time a fresh BFS/DFS starts
Label each visited node j with component[j] = comp
1 2
3
401. BFS tree
Edges explored by BFS form a tree
Acyclic graph = connected, with n-1 edges
1 2
3
4 5
1 2
5
9 10
3 4
7
11 12
6 8
402. BFS tree
Edges explored by BFS form a tree
Acyclic graph = connected, with n-1 edges
1 2
3
4 5
1 2
5
9 10
3 4
7
11 12
6 8
Any non-tree edge generates a cycle
458. Directed cycles
A directed graph has a cycle if and only if DFS reveals a
back edge
Can classify edges using pre and post numbers
Tree/Forward edge (u,v) :
Interval [pre(u),post(u)] contains [(pre(v),post(v)]
Backward edge (u,v):
Interval [pre(v),post(v)] contains [(pre(u),post(u)]
Cross edge (u,v):
Intervals [(pre(u),post(u)] and [(pre(v),post(v)] disjoint
459. Directed acyclic graphs
Directed graphs without cycles are useful for
modelling dependencies
Courses with prerequisites
Edge (Algebra,Calculus) indicates that Algebra
is a prerequisite for Calculus
Will look at Directed Acyclic Graphs (DAGs) soon
460. Connectivity in directed
graphs
Need to take directions into account
Nodes i and j are strongly connected if there is a
path from i to j and a path from j to i
Directed graph can be decomposed into strongly
connected components (SCCs)
All pairs of nodes in an SCC are strongly
connected
461. Computing SCCs
DFS numbering (pre and
post) can be used to
compute SCCs
[Dasgupta,
Papadimitriou,Vazirani]
2 1
5
3
7
6 4
8
462. Computing SCCs
DFS numbering (pre and
post) can be used to
compute SCCs
[Dasgupta,
Papadimitriou,Vazirani]
2 1
5
3
7
6 4
8
463. Computing SCCs
DFS numbering (pre and
post) can be used to
compute SCCs
[Dasgupta,
Papadimitriou,Vazirani]
2 1
5
3
7
6 4
8
464. Computing SCCs
DFS numbering (pre and
post) can be used to
compute SCCs
[Dasgupta,
Papadimitriou,Vazirani]
2 1
5
3
7
6 4
8
465. Computing SCCs
DFS numbering (pre and
post) can be used to
compute SCCs
[Dasgupta,
Papadimitriou,Vazirani]
2 1
5
3
7
6 4
8
466. Other properties
A number of other structural properties can be
inferred from DFS numbering
Articulation points (vertices)
Removing such a vertex disconnects the graph
Bridges (edges)
Removing such an edge disconnects the graph
467. DESIGN AND ANALYSIS
OF ALGORITHMS
Directed acyclic graphs (DAGs)
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 3, Module 6
468. Tasks with constraints
For a foreign trip you need to
Get a passport
Buy a ticket
Get a visa
Buy travel insurance
Buy foreign exchange
Buy gifts for your hosts
469. Tasks with constraints
There are constraints
Without a passport, you cannot buy a ticket or
travel insurance
You need a ticket and insurance for the visa
You need the visa for foreign exchange
You don’t want to invest in gifts unless the trip is
confirmed
470. Goal
Find a sequence in which to complete the tasks,
respecting the constraints
471. Model using graphs
Vertices are tasks
Edge from Task1 to Task2 if Task1 must come
before Task2
Getting a passport must precede buying a ticket
Getting a visa must precede buying foreign
exchange
472. Our example as a graph
Get
passport
Buy
ticket
Buy
insurance
Get
visa
Buy foreign
exchange
Buy
gifts
Order of tasks should respect dependencies
Passport, Ticket, Insurance, Visa, Gift, Forex
Passport, Insurance, Ticket, Visa, Forex, Gift
Passport, Ticket, Insurance, Visa, Forex, Gift
Passport, Insurance, Ticket, Visa, Gift, Forex
473. Our example as a graph
Get
passport
Buy
ticket
Buy
insurance
Get
visa
Buy foreign
exchange
Buy
gifts
Features of the graph
Directed
No cycles
Cyclic dependencies are unsatisfiable
474. Directed Acyclic Graphs
G = (V,E), a directed graph
No cycles
No directed path from any v in V back to itself
Such graphs are also called DAGs
475. Topological ordering
Given a DAG G = (V,E), V = {1,2,…,n}
Enumerate the vertices as {i1,i2,…,in} so that
For any edge (j,k) in E,
j appears before k in the enumeration
Also known as topological sorting
476. Topological ordering
Observation
A directed graph with cycles cannot be
topologically ordered
Path from j to k and from k to j means
j must come before k
k must come before j
Impossible!
477. Topological ordering
Claim
Every directed acyclic graph can be
topologically ordered
Strategy
First list vertices with no incoming edges
Then list vertices whose incoming neighbours
are already listed
…
479. Topological ordering
indegree(v) : number of edges into v
outdegree(v): number of edges out of v
Every dag has at least one vertex with indegree 0
Start with any v such that indegree(v) > 0
Walk backwards to a predecessor so long as
indegree > 0
If no vertex has indegree 0, within n steps we
will complete a cycle!
480. Topological ordering
Pick a vertex with indegree 0
No dependencies
Enumerate it and delete it from the graph
What remains is again a DAG!
Repeat the step above
Stop when the resulting DAG is empty
492. Topological ordering
function TopologicalOrder(G)
for i = 1 to n
indegree[i] = 0
for j = 1 to n
indegree[i] = indegree[i] + A[j][i]
for i = 1 to n
choose j with indegree[j] = 0
enumerate j
indegree[j] = -1
for k = 1 to n
if A[j][k] == 1
indegree[k] = indegree[k]-1
493. Topological ordering
Complexity is O(n2)
Initializing indegree takes time O(n2)
Loop n times to enumerate vertices
Inside loop, identifying next vertex is O(n)
Updating indegrees of neighbours is O(n)
494. Topological ordering
Using adjacency list
Scan lists once to compute indegrees — O(m)
Put all indegree 0 vertices in a queue
Enumerate head of queue and decrement
indegree of neighbours — degree(j), overall O(m)
If indegree(k) becomes 0, add to queue
Overall O(n+m)
495. Topological ordering revisited
function TopologicalOrder(G) //Edges are in adjacency list
for i = 1 to n { indegree[i] = 0 }
for i = 1 to n
for (i,j) in E //proportional to outdegree(i)
indegree[j] = indegree[j] + 1
for i = 1 to n
if indegree[i] == 0 { add i to Queue }
while Queue is not empty
j = remove_head(Queue)
for (j,k) in E //proportional to outdegree(j)
indegree[k] = indegree[k] - 1
if indegree[k] == 0 { add k to Queue }
496. DESIGN AND ANALYSIS
OF ALGORITHMS
DAGs: Longest paths
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 3, Module 7
497. Directed Acyclic Graphs
G = (V,E), a directed graph
No cycles
No directed path from any v in V back to itself
Such graphs are also called DAGs
498. Topological ordering
Given a DAG G = (V,E), V = {1,2,…,n}
Enumerate the vertices as {i1,i2,…,in} so that
For any edge (j,k) in E,
j appears before k in the enumeration
Also known as topological sorting
499. 1 2
3
4
5
6 7 8
Questions about DAGs
Imagine
these are
courses
Edges
are pre-
requisites
What is the minimum number of semesters to
complete the programme?
500. 1 2
3
4
5
6 7 8
Questions about DAGs
Imagine
these are
courses
Edges
are pre-
requisites
What is the minimum number of semesters to
complete the programme?
501. 1 2
3
4
5
6 7 8
Questions about DAGs
Imagine
these are
courses
Edges
are pre-
requisites
What is the minimum number of semesters to
complete the programme?
502. 1 2
3
4
5
6 7 8
Questions about DAGs
Imagine
these are
courses
Edges
are pre-
requisites
What is the minimum number of semesters to
complete the programme?
503. 1 2
3
4
5
6 7 8
Questions about DAGs
Imagine
these are
courses
Edges
are pre-
requisites
What is the minimum number of semesters to
complete the programme?
504. 1 2
3
4
5
6 7 8
Questions about DAGs
Imagine
these are
courses
Edges
are pre-
requisites
What is the minimum number of semesters to
complete the programme?
505. Longest path in a DAG
Equivalent to finding longest path in the DAG
If indegree(j) = 0, longest_path_to(j) = 0
If indegree(k) > 0, longest_path_to(k) is
1 + max{ longest_path_to(j) } among all
incoming neighbours j of k
506. Longest path in a DAG
To compute longest_path_to(k)
Need longest_path_to(j) for all incoming
neighbours of k
If j is an incoming neighbour, (j,k) in E
j is enumerated before k in topological order
Hence, compute longest_path_to(i) in topological
order
507. Longest path in a DAG
Let i1,i2,…,in be a topological ordering of V
All neighbours of ik appear before it in this list
From left to right, compute longest_path_to(ik) as
1 + max{ longest_path_to(ij) } among all
incoming neighbours ij of ik
Can combine this calculation with topological sort
517. Topological ordering with longest path
function TopologicalOrderWithLongestPath(G)
for i = 1 to n
indegree[i] = 0; LPT[i] = 0
for j = 1 to n
indegree[i] = indegree[i] + A[j][i]
for i = 1 to n
choose j with indegree[j] = 0
enumerate j
indegree[j] = -1
for k = 1 to n
if A[j][k] == 1
indegree[k] = indegree[k]-1
LPT[k] = max(LPT[k], 1 + LPT[j])
518. This implementation has complexity is O(n2)
As before, we can use adjacency lists to improve
the complexity to O(m+n)
Topological ordering with longest path
519. function TopologicalOrder(G) //Edges are in adjacency list
for i = 1 to n { indegree[i] = 0; LPT[i] = 0}
for i = 1 to n
for (i,j) in E //proportional to outdegree(i)
indegree[j] = indegree[j] + 1
for i = 1 to n
if indegree[i] == 0 { add i to Queue }
while Queue is not empty
j = remove_head(Queue)
for (j,k) in E //proportional to outdegree(j)
indegree[k] = indegree[k] - 1
LPT[k] = max(LPT[k], 1 + LPT[j])
if indegree[k] == 0 { add k to Queue }
Topological ordering with longest path 2
520. Summary
Dependencies are naturally modelled using DAGs
Topological ordering lists vertices without violating
dependencies
Longest path in a DAG represents minimum
number of steps to list all vertices in groups
Note: Computing the longest path with no
duplicate vertices in an arbitrary graph is not
known to have any efficient algorithm!
521. DESIGN AND ANALYSIS
OF ALGORITHMS
Shortest paths in weighted graphs
MADHAVAN MUKUND, CHENNAI MATHEMATICAL INSTITUTE
http://www.cmi.ac.in/~madhavan
NPTEL MOOC,JAN-FEB 2015
Week 4, Module 1
522. Recall that …
BFS and DFS are two systematic ways to explore a
graph
Both take time linear in the size of the graph with
adjacency lists
Recover paths by keeping parent information
BFS can compute shortest paths, in terms of
number of edges
DFS numbering can reveal many interesting features
523. Adding edge weights
Label each edge with a number—cost
Ticket price on a flight sector
Tolls on highway segment
Distance travelled between two stations
Typical time between two locations during peak
hour traffic
524. Shortest paths
Weighted graph
G=(V,E) together with
Weight function, w : E→Reals
Let e1=(v0,v1), e2 = (v1,v2), …, en = (vn-1,vn) be a path
from v0 to vn
Cost of the path is w(e1) + w(e2) + … + w(en)
Shortest path from v0 to vn : minimum cost
525. Shortest paths …
BFS finds path with fewest number of edges
In a weighted graph, need not be the shortest path
1 2
3 4
5 6
7
70
80
10
6
20
50
10 5
526. Shortest path problems
Single source
Find shortest paths from some fixed vertex, say
1, 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
527. Shortest path problems
All pairs
Find shortest paths between every pair of
vertices i and j
Railway routes, shortest way to travel between
any pair of cities
528. This lecture…
Single source shortest paths
For instance, shortest paths from 1 to 2,3,…,7
1 2
3 4
5 6
7
70
80
10
6
20
50
10 5
529. Single source shortest paths
Imagine vertices are oil depots, edges are pipelines
Set fire to oil depot at vertex 1
Fire travels at uniform speed along each pipeline
First oil depot to catch fire after 1 is nearest vertex
Next oil depot is second nearest vertex
…
534. Single source shortest paths
1 2
3 4
5 6
7
70
80
10
6
20
50
10 5
t = 0
t = 10
t = 16
t = 30
535. Single source shortest paths
1 2
3 4
5 6
7
70
80
10
6
20
50
10 5
t = 0
t = 10
t = 16
t = 30
t = 40
536. Single source shortest paths
1 2
3 4
5 6
7
70
80
10
6
20
50
10 5
t = 0
t = 10
t = 16
t = 30
t = 40
t = 45
537. Single source shortest paths
1 2
3 4
5 6
7
70
80
10
6
20
50
10 5
t = 0
t = 10
t = 16
t = 30
t = 40
t = 45
t = 86
538. Compute expected time to burn of each vertex
Update this each time a new vertex burns
1 2
3 4
5 6
7
70
80
10
6
20
50
10 5
Single source shortest paths