SlideShare a Scribd company logo
1 of 802
Download to read offline
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
Understanding Algorithms
Correctness
Efficiency
Asymptotic complexity, O( ) notation
Modelling
Graphs, data structures, decomposing the problem
Techniques
Divide and conquer, greedy, dynamic programming
Expectations
Background in programming
Any language (C, C++, Java)
Basic data structures
Arrays, lists
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
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, …
Tentative schedule
Week 1: Motivation, asymptotic complexity
Week 2: Searching and sorting
Week 3: Graphs and basic graph algorithms
Week 4: More graph algorithms, disjoint set
Week 5: Divide and conquer, heaps
Week 6: Search trees, greedy algorithms
Week 7: Dynamic programming
Week 8: Miscellaneous topics
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
January
February
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
Textbooks
Algorithm Design
Jon Kleinberg and Eva Tardos
Algorithms
Sanjoy Dasgupta, Christos Papadimitriou and
Umesh Vazirani
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
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
Delhi
Varanasi
Ahmedabad
Mumbai
Kolkata
Hyderabad
Visakhapatnam
Bangalore
Chennai
Trivandrum
Throw away the map and record the network
This is a graph—a collection of nodes and edges
Can distort the picture without changing meaning
Can distort the picture without changing meaning
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?
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
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?
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?
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
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?
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?
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
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
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?
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
Variations
Account for set up time between jobs
Need to reserve time slots to reload paper
Is there a valid greedy strategy?
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
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
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
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!
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
Naive recursion can be
inefficient
Fibonacci numbers:
F(n) = F(n-1) + F(n-2), F(1) = 1, F(2) = 1
Sequence is 1,1,2,3,5,8,13,21,….
Computing recursively
F(7) = F(6) + F(5) = (F(5) + F(4)) + (F(4)+F(3)) =
(F(4)+F(3)+F(3)+F(2)) + (F(3)+F(2)+F(2)+F(1)) = …
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?
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
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
Sequences of values
Two basic ways of storing a sequence of values
Arrays
Lists
What’s the difference?
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
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”
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
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
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
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;
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
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
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))
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)
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)
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!
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
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
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
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
Strategy 1 …
74 32 89 55 21 64
Strategy 1 …
21
74 32 89 55 21 64
Strategy 1 …
21 32
74 32 89 55 21 64
Strategy 1 …
21 32 55
74 32 89 55 21 64
Strategy 1 …
21 32 55 64
74 32 89 55 21 64
Strategy 1 …
21 32 55 64 74
74 32 89 55 21 64
Strategy 1 …
21 32 55 64 74 89
74 32 89 55 21 64
Strategy 1 …
Selection Sort
Select the next element in sorted order
Move it into its correct place in the final sorted list
Selection Sort
Avoid using a second list
Swap minimum element with value in first
position
Swap second minimum element to second
position
…
Selection Sort
74 32 89 55 21 64
Selection Sort
74 32 89 55 21 64
Selection Sort
21 32 89 55 74 64
Selection Sort
21 32 89 55 74 64
Selection Sort
21 32 89 55 74 64
Selection Sort
21 32 89 55 74 64
Selection Sort
21 32 55 89 74 64
Selection Sort
21 32 55 89 74 64
Selection Sort
21 32 55 64 74 89
Selection Sort
21 32 55 64 74 89
Selection Sort
21 32 55 64 74 89
Selection Sort
21 32 55 64 74 89
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)
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)
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
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)
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)
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
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
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
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
Strategy 2 …
74 32 89 55 21 64
Strategy 2 …
74 32 89 55 21 64
74
Strategy 2 …
74 32 89 55 21 64
32 74
Strategy 2 …
74 32 89 55 21 64
32 74 89
Strategy 2 …
74 32 89 55 21 64
32 55 74 89
Strategy 2 …
74 32 89 55 21 64
21 32 55 74 89
Strategy 2 …
74 32 89 55 21 64
21 32 55 64 74 89
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
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
Insertion Sort
74 32 89 55 21 64
Insertion Sort
74 32 89 55 21 64
Insertion Sort
32 74 89 55 21 64
Insertion Sort
32 74 89 55 21 64
Insertion Sort
32 74 55 89 21 64
Insertion Sort
32 55 74 89 21 64
Insertion Sort
32 55 74 21 89 64
Insertion Sort
32 55 21 74 89 64
Insertion Sort
32 21 55 74 89 64
Insertion Sort
21 32 55 74 89 64
Insertion Sort
21 32 55 74 64 89
Insertion Sort
21 32 55 64 74 89
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)
Recursive formulation
To sort A[0..n-1]
Recursively sort A[0..n-2]
Insert A[n-1] into A[0..n-2]
Base case: n = 1
Insertion Sort, recursive
InsertionSort(A,k) // Sort A[0..k-1]
if (k == 1)
return;
InsertionSort(A,k-1);
Insert(A,k-1);
return;
Insert(A,j) // Insert A[j] into A[0..j-1]
pos = j;
while (pos > 0 && A[pos] < A[pos-1])
swap(A,pos,pos-1);
pos = pos-1;
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)
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?
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
O(n2) sorting algorithms
Selection sort and insertion sort are both O(n2)
O(n2) sorting is infeasible for n over 100000
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
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
Merging two sorted lists
32 74 89
21 55 64
Merging two sorted lists
32 74 89
21 55 64
21
Merging two sorted lists
32 74 89
21 55 64
21 32
Merging two sorted lists
32 74 89
21 55 64
21 32 55
Merging two sorted lists
32 74 89
21 55 64
21 32 55 64
Merging two sorted lists
32 74 89
21 55 64
21 32 55 64 74
Merging two sorted lists
32 74 89
21 55 64
21 32 55 64 74 89
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!
Merge Sort
43 32 22 78 63 57 91 13
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
22 78 63 57 91 13
43 32 22 78 63 57 91 13
32 43
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
63 57 91 13
43 32 22 78 63 57 91 13
32 43 22 78
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
91 13
43 32 22 78 63 57 91 13
32 43 22 78 57 63
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
32 43 22 78 57 63 13 91
Merge Sort
43 32 22 78 63 57 91 13
63 57 91 13
43 32 22 78 63 57 91 13
32 43 22 78 57 63 13 91
22 32 43 78
Merge Sort
43 32 22 78 63 57 91 13
43 32 22 78 63 57 91 13
32 43 22 78 57 63 13 91
22 32 43 78 13 57 63 91
Merge Sort
43 32 22 78 63 57 91 13
32 43 22 78 57 63 13 91
22 32 43 78 13 57 63 91
13 22 32 43 57 63 78 91
Divide and conquer
Break up problem into disjoint parts
Solve each part separately
Combine the solutions efficiently
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
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++;}
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
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)
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
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
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++
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
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
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
Analysis of Merge Sort …
Analysis of Merge Sort …
t(1) = 1
Analysis of Merge Sort …
t(1) = 1
t(n) = 2t(n/2) + n
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
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
…
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
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
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!
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)
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)
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
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
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!
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
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
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!
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)
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
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
Quicksort
High level view
Quicksort
High level view
43 32 22 78 63 57 91 13
Quicksort
High level view
43 32 22 78 63 57 91 13
Quicksort
High level view
43 32 22 78 63 57 91 13
Quicksort
High level view
13 32 22 43 63 57 91 78
Quicksort
High level view
13 22 32 43 57 63 78 91
Quicksort: Partitioning
Quicksort: Partitioning
43 32 22 78 63 57 91 13
Quicksort: Partitioning
43 32 22 78 63 57 91 13
Quicksort: Partitioning
43 32 22 78 63 57 91 13
Quicksort: Partitioning
43 32 22 78 63 57 91 13
Quicksort: Partitioning
43 32 22 78 63 57 91 13
Quicksort: Partitioning
43 32 22 78 63 57 91 13
Quicksort: Partitioning
43 32 22 78 63 57 91 13
Quicksort: Partitioning
43 32 22 78 63 57 91 13
Quicksort: Partitioning
43 32 22 78 63 57 91 13
Quicksort: Partitioning
43 32 22 78 63 57 91 13
Quicksort: Partitioning
43 32 22 13 63 57 91 78
Quicksort: Partitioning
13 32 22 43 63 57 91 78
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
Quicksort:
Another Partitioning Strategy
Quicksort:
Another Partitioning Strategy
43 32 22 78 63 57 91 13
Quicksort:
Another Partitioning Strategy
43 32 22 78 63 57 91 13
Quicksort:
Another Partitioning Strategy
43 32 22 78 63 57 91 13
Quicksort:
Another Partitioning Strategy
43 32 22 78 63 57 91 13
Quicksort:
Another Partitioning Strategy
43 32 22 13 63 57 91 78
Quicksort:
Another Partitioning Strategy
43 32 22 13 63 57 91 78
Quicksort:
Another Partitioning Strategy
43 32 22 13 63 57 91 78
Quicksort:
Another Partitioning Strategy
43 32 22 13 63 57 91 78
Quicksort:
Another Partitioning Strategy
13 32 22 43 63 57 91 78
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
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
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?
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!
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?
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)
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)
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
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
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
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
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
Other criteria
Minimize data movement
Imagine values are heavy cartons
Want to reduce effort of moving values around
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
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)
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
Map
Colouring
Assign each
state or country
a colour
States that
share a border
should be
coloured
differently
How many
colours do we
need?
Map
Colouring
Mark each state
Map
Colouring
Mark each state
Connect states
that share a
border
Map
Colouring
Mark each state
Connect states
that share a
border
Assign colours
to dots so that
no two
connected dots
have the same
colour
Map
Colouring
Mark each state
Connect states
that share a
border
Assign colours
to dots so that
no two
connected dots
have the same
colour
Map
Colouring
Mark each state
Connect states
that share a
border
Assign colours
to dots so that
no two
connected dots
have the same
colour
Map
Colouring
Mark each state
Connect states
that share a
border
Assign colours
to dots so that
no two
connected dots
have the same
colour
Map
Colouring
Mark each state
Connect states
that share a
border
Assign colours
to dots so that
no two
connected dots
have the same
colour
Map
Colouring
Mark each state
Connect states
that share a
border
Assign colours
to dots so that
no two
connected dots
have the same
colour
Map
Colouring
Map
Colouring
In fact, the
actual map is
irrelevant!
Map
Colouring
In fact, the
actual map is
irrelevant!
All we need is
the underlying
pattern of dots
and connections
Map
Colouring
This kind of
diagram is
called a graph
Dots are nodes
or vertices
One vertex,
many vertices
Connections are
edges
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!
Graph
Colouring
Observe that the
original map
used more than
4 colours
Graph
Colouring
Observe that the
original map
used more than
4 colours
Graph
Colouring
The graph
emphasizes the
essential features
of the problem
What is
connected to
what?
Graph
Colouring
The graph
emphasizes the
essential features
of the problem
What is
connected to
what?
We can distort
this figure and the
problem remains
the same
More graph
problems
More graph
problems
Airline routing
More graph
problems
Airline routing
Can I travel from
New Delhi to
Trivandrum
without
changing
airlines?
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
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
Graph
Colouring
Undirected
graph
Graph
Colouring
Undirected
graph
Colouring C
assigns each
vertex v a colour
C(v)
Legal colouring:
if (v,v’) is in E,
then C(v) ≠ C(v’)
Finding a
route
Finding a
route
Directed graph
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
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
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
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
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
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
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”?
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
Adjacency matrix
1
2
3
4
5
6 7
8 9
10
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
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
Neighbours of i
Any column j in row i
with entry 1
Scan row i from left
to right to identify all
neighbours
Neighbours of 4 are
{1,5,8}
Adjacency matrix
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
Neighbours of i
Any column j in row i
with entry 1
Scan row i from left
to right to identify all
neighbours
Neighbours of 4 are
{1,5,8}
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
Finding a path
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
Start with vs
New Delhi is 1
Mark each neighbour as
reachable
Explore neighbours of
marked vertices
Check if target is
marked
vt =10 = Trivandrum
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
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
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
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
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
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
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
Adjacency matrix
1
2
3
4
5
6 7
8 9
10
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 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
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
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
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
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
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Visited
Queue
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Visited
Queue
head tail
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Visited
Queue
1
1
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Visited
Queue
1
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Visited
Queue
1
1
2
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Visited
Queue
1
1
2
1
3
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Visited
Queue
1
1
2
1
3
1
4
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Visited
Queue
1
1
1
3
1
4
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Visited
Queue
1
1
1
1
4
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Visited
Queue
1
1
1
1
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Visited
Queue
1
1
1
1
1
5
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Visited
Queue
1
1
1
1
1
5
1
8
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Visited
Queue
1
1
1
1
1
1
8
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Visited
Queue
1
1
1
1
1
1
6
1
8
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Visited
Queue
1
1
1
1
1
1
6
1
8
1
7
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Visited
Queue
1
1
1
1
1
1
6
1
1
7
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Visited
Queue
1
1
1
1
1
1
6
1
1
7
1
9
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Visited
Queue
1
1
1
1
1
1
1
1
7
1
9
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Visited
Queue
1
1
1
1
1
1
1
1
1
9
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Visited
Queue
1
1
1
1
1
1
1
1
1
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Visited
Queue
1
1
1
1
1
1
1
1
1
1
10
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Visited
Queue
1
1
1
1
1
1
1
1
1
1
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)
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)
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)
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
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
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);
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
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
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);
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Queue
L : Level
P : Parent
L P
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Queue
head tail
L : Level
P : Parent
L P
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Queue
0
1
L : Level
P : Parent
L P
-
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Queue
0
L : Level
P : Parent
L P
-
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Queue
0
1
2
L : Level
P : Parent
L P
-
1
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Queue
0
1
2
1
3
L : Level
P : Parent
L P
-
1
1
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Queue
0
1
2
1
3
1
4
L : Level
P : Parent
L P
-
1
1
1
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Queue
0
1
1
3
1
4
L : Level
P : Parent
L P
-
1
1
1
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Queue
0
1
1
1
4
L : Level
P : Parent
L P
-
1
1
1
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Queue
0
1
1
1
L : Level
P : Parent
L P
-
1
1
1
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Queue
0
1
1
1
2
5
L : Level
P : Parent
L P
-
1
1
1
4
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Queue
0
1
1
1
2
5
2
8
L : Level
P : Parent
L P
-
1
1
1
4
4
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Queue
0
1
1
1
2
2
8
L : Level
P : Parent
L P
-
1
1
1
4
4
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Queue
0
1
1
1
2
3
6
2
8
L : Level
P : Parent
L P
-
1
1
1
4
4
5
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Queue
0
1
1
1
2
3
6
2
8
3
7
L : Level
P : Parent
L P
-
1
1
1
4
4
5
5
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Queue
0
1
1
1
2
3
6
2
3
7
L : Level
P : Parent
L P
-
1
1
1
4
4
5
5
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Queue
0
1
1
1
2
3
6
2
3
7
3
9
L : Level
P : Parent
L P
-
1
1
1
4
4
5
5
8
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Queue
0
1
1
1
2
3
2
3
7
3
9
L : Level
P : Parent
L P
-
1
1
1
4
4
5
5
8
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Queue
0
1
1
1
2
3
2
3
3
9
L : Level
P : Parent
L P
-
1
1
1
4
4
5
5
8
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Queue
0
1
1
1
2
3
2
3
3
L : Level
P : Parent
L P
-
1
1
1
4
4
5
5
8
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Queue
0
1
1
1
2
3
2
3
3
4
10
L : Level
P : Parent
L P
-
1
1
1
4
4
5
5
8
9
1
5
4
2
3
6 7
8 9
10
Breadth first search 1
2
3
4
5
6
7
8
9
10
Queue
0
1
1
1
2
3
2
3
3
4
L : Level
P : Parent
L P
-
1
1
1
4
4
5
5
8
9
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
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
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
1
2
3
4
5
6 7
8 9
10
Depth first search 1
2
3
4
5
6
7
8
9
10
Visited
Stack of suspended vertices
1
2
3
4
5
6 7
8 9
10
Depth first search 1
2
3
4
5
6
7
8
9
10
Visited
Stack of suspended vertices
Start at 4
1
2
3
4
5
6 7
8 9
10
Depth first search 1
2
3
4
5
6
7
8
9
10
Visited
Stack of suspended vertices
Start at 4
1
1
2
3
4
5
6 7
8 9
10
Depth first search 1
2
3
4
5
6
7
8
9
10
Visited
Stack of suspended vertices
Start at 4
1
4
1
1
2
3
4
5
6 7
8 9
10
Depth first search 1
2
3
4
5
6
7
8
9
10
Visited
Stack of suspended vertices
Start at 4
1
4
1
1
1
1
2
3
4
5
6 7
8 9
10
Depth first search 1
2
3
4
5
6
7
8
9
10
Visited
Stack of suspended vertices
Start at 4
1
4
1
1
1
2
1
1
2
3
4
5
6 7
8 9
10
Depth first search 1
2
3
4
5
6
7
8
9
10
Visited
Stack of suspended vertices
Start at 4
1
4
1
1
1
1
1
2
3
4
5
6 7
8 9
10
Depth first search 1
2
3
4
5
6
7
8
9
10
Visited
Stack of suspended vertices
Start at 4
1
4
1
1
1
1
2
3
4
5
6 7
8 9
10
Depth first search 1
2
3
4
5
6
7
8
9
10
Visited
Stack of suspended vertices
Start at 4
1
1
1
1
1
2
3
4
5
6 7
8 9
10
Depth first search 1
2
3
4
5
6
7
8
9
10
Visited
Stack of suspended vertices
Start at 4
1
1
1
1
1
4
1
2
3
4
5
6 7
8 9
10
Depth first search 1
2
3
4
5
6
7
8
9
10
Visited
Stack of suspended vertices
Start at 4
1
1
1
1
1
5
1
4
1
2
3
4
5
6 7
8 9
10
Depth first search 1
2
3
4
5
6
7
8
9
10
Visited
Stack of suspended vertices
Start at 4
1
1
1
1
1
5
1
6
1
4
1
2
3
4
5
6 7
8 9
10
Depth first search 1
2
3
4
5
6
7
8
9
10
Visited
Stack of suspended vertices
Start at 4
1
1
1
1
1
5
1
1
4
1
2
3
4
5
6 7
8 9
10
Depth first search 1
2
3
4
5
6
7
8
9
10
Visited
Stack of suspended vertices
Start at 4
1
1
1
1
1
5
1
1
1
6
4
1
2
3
4
5
6 7
8 9
10
Depth first search 1
2
3
4
5
6
7
8
9
10
Visited
Stack of suspended vertices
Start at 4
1
1
1
1
1
5
1
1
1
6
1
8
4
1
2
3
4
5
6 7
8 9
10
Depth first search 1
2
3
4
5
6
7
8
9
10
Visited
Stack of suspended vertices
Start at 4
1
1
1
1
1
5
1
1
1
6
1
8
1
9
4
1
2
3
4
5
6 7
8 9
10
Depth first search 1
2
3
4
5
6
7
8
9
10
Visited
Stack of suspended vertices
Start at 4
1
1
1
1
1
5
1
1
1
6
1
8
1
4
1
2
3
4
5
6 7
8 9
10
Depth first search 1
2
3
4
5
6
7
8
9
10
Visited
Stack of suspended vertices
Start at 4
1
1
1
1
1
5
1
1
1
6
1
1
4
1
2
3
4
5
6 7
8 9
10
Depth first search 1
2
3
4
5
6
7
8
9
10
Visited
Stack of suspended vertices
Start at 4
1
1
1
1
1
5
1
1
1
1
1
4
1
2
3
4
5
6 7
8 9
10
Depth first search 1
2
3
4
5
6
7
8
9
10
Visited
Stack of suspended vertices
Start at 4
1
1
1
1
1
1
1
1
1
1
4
1
2
3
4
5
6 7
8 9
10
Depth first search 1
2
3
4
5
6
7
8
9
10
Visited
Stack of suspended vertices
Start at 4
1
1
1
1
1
1
1
1
1
1
Depth first search
Depth first search
DFS is most natural to implement recursively
For each unvisited neighbour j of i, call DFS(j)
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
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)
Complexity of DFS
Complexity of DFS
Each vertex marked and explored exactly once
Complexity of DFS
Each vertex marked and explored exactly once
DFS(j) need to examine all neighbours of j
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
)
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
Properties of DFS
Properties of DFS
Paths discovered by DFS are not shortest paths,
unlike BFS
Properties of DFS
Paths discovered by DFS are not shortest paths,
unlike BFS
Why use DFS at all?
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
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
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
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.
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++
DFS numbering
1
5
4
2
3
6 7
8 9
10
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
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
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
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
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?
Connectivity
Connected graph
1 2
3
4 5
Disconnected graph
1 2
5
9 10
3 4
7
11 12
6 8
Connectivity
Connected graph
1 2
3
4 5
Disconnected graph
Connected components
1 2
5
9 10
3 4
7
11 12
6 8
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
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
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
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
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
Cycles
Acyclic graph
1 2
3
4 5
Graph with cycles
1 2
5
9 10
3 4
7
11 12
6 8
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
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
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
0
pre
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2
0
pre
1
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2
0
pre
1 2
post
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
0
pre
1 2
post
3
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
9
0
pre
1 2
post
3
4
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
9
10
0
pre
1 2
post
3
4
5
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
9
10
0
pre
1 2
post
3
4
5 6
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
9
10
0
pre
1 2
post
3
4
5 6
7
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
9
10
0
pre
1 2
post
3
4
5 6
7
8
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
9
10
0
pre
1 2
post
3
4
5 6
7
8
9
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
9
10
3
0
pre
1 2
post
3
4
5 6
7
8
9
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
9
10
3
0
pre
1 2
post
3
4
5 6
7
8
9 10
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
9
10
3
4
0
pre
1 2
post
3
4
5 6
7
8
9 10
11
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
9
10
3
4
8
0
pre
1 2
post
3
4
5 6
7
8
9 10
11
12
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
9
10
3
4
8
7
0
pre
1 2
post
3
4
5 6
7
8
9 10
11
12
13
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
9
10
3
4
8
7
11
0
pre
1 2
post
3
4
5 6
7
8
9 10
11
12
13
14
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
9
10
3
4
8
7
11
0
pre
1 2
post
3
4
5 6
7
8
9 10
11
12
13
14 15
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
9
10
3
4
8
7
11
0
pre
1 2
post
3
4
5 6
7
8
9 10
11
12
13
14 15
16
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
9
10
3
4
8
7
11
12
0
pre
1 2
post
3
4
5 6
7
8
9 10
11
12
13
14 15
16 17
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
9
10
3
4
8
7
11
12
0
pre
1 2
post
3
4
5 6
7
8
9 10
11
12
13
14 15
16 17 18
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
9
10
3
4
8
7
11
12
0
pre
1 2
post
3
4
5 6
7
8
9 10
11
12
13
14 15
16 17 18
19
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
9
10
3
4
8
7
11
12
0
pre
1 2
post
3
4
5 6
7
8
9 10
11
12
13
14 15
16 17 18
19
20
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
9
10
3
4
8
7
11
12
0
pre
1 2
post
3
4
5 6
7
8
9 10
11
12
13
14 15
16 17 18
19
20
21
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
9
10
3
4
8
7
11
12
6
0
pre
1 2
post
3
4
5 6
7
8
9 10
11
12
13
14 15
16 17 18
19
20
21
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
9
10
3
4
8
7
11
12
6
0
pre
1 2
post
3
4
5 6
7
8
9 10
11
12
13
14 15
16 17 18
19
20
21 22
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
9
10
3
4
8
7
11
12
6
0
pre
1 2
post
3
4
5 6
7
8
9 10
11
12
13
14 15
16 17 18
19
20
21 22 23
DFS tree
1 2
5
9 10
3 4
7
11 12
6 8
1
2 5
9
10
3
4
8
7
11
12
6
0
pre
1 2
post
3
4
5 6
7
8
9 10
11
12
13
14 15
16 17 18
19
20
21 22 23
Any non-tree edge generates a cycle
Directed cycles
2 1
5
3
7
6 4
8
Directed cycles
2 1
5
3
7
6 4
8
1
Directed cycles
2 1
5
3
7
6 4
8
1
0
Directed cycles
2 1
5
3
7
6 4
8
2
1
0
1
Directed cycles
2 1
5
3
7
6 4
8
2
1
5
0
1
2
Directed cycles
2 1
5
3
7
6 4
8
2
1
5
6
0
1
2
3
Directed cycles
2 1
5
3
7
6 4
8
2
1
5
7
6
0
1
2
3
4
Directed cycles
2 1
5
3
7
6 4
8
2
1
5
7
6
0
1
2
3
4 5
Directed cycles
2 1
5
3
7
6 4
8
2
1
5
7
6
0
1
2
3
4 5
6
Directed cycles
2 1
5
3
7
6 4
8
2
1
5
7
6 8
0
1
2
3
4 5
6 7
Directed cycles
2 1
5
3
7
6 4
8
2
1
5
7
6 8
0
1
2
3
4 5
6 7 8
Directed cycles
2 1
5
3
7
6 4
8
2
1
5
7
6 8
0
1
2
3
4 5
6 7 8
9
Directed cycles
2 1
5
3
7
6 4
8
2
1
5
7
6 8
0
1
2
3
4 5
6 7 8
9
10
Directed cycles
2 1
5
3
7
6 4
8
2
1
5
3
7
6 8
0
1
2
3
4 5
6 7 8
9
10 11
Directed cycles
2 1
5
3
7
6 4
8
2
1
5
3
7
6
4
8
0
1
2
3
4 5
6 7 8
9
10 11
12
Directed cycles
2 1
5
3
7
6 4
8
2
1
5
3
7
6
4
8
0
1
2
3
4 5
6 7 8
9
10 11
12 13
Directed cycles
2 1
5
3
7
6 4
8
2
1
5
3
7
6
4
8
0
1
2
3
4 5
6 7 8
9
10 11
12 13
14
Directed cycles
2 1
5
3
7
6 4
8
2
1
5
3
7
6
4
8
0
1
2
3
4 5
6 7 8
9
10 11
12 13
14
15
Directed cycles
2 1
5
3
7
6 4
8
2
1
5
3
7
6
4
8
0
1
2
3
4 5
6 7 8
9
10 11
12 13
14
15
Tree edge
Directed cycles
2 1
5
3
7
6 4
8
2
1
5
3
7
6
4
8
0
1
2
3
4 5
6 7 8
9
10 11
12 13
14
15
Tree edge
Directed cycles
2 1
5
3
7
6 4
8
2
1
5
3
7
6
4
8
0
1
2
3
4 5
6 7 8
9
10 11
12 13
14
15
Tree edge
Forward edge
Directed cycles
2 1
5
3
7
6 4
8
2
1
5
3
7
6
4
8
0
1
2
3
4 5
6 7 8
9
10 11
12 13
14
15
Tree edge
Forward edge
Directed cycles
2 1
5
3
7
6 4
8
2
1
5
3
7
6
4
8
0
1
2
3
4 5
6 7 8
9
10 11
12 13
14
15
Tree edge
Forward edge
Back edge
Directed cycles
2 1
5
3
7
6 4
8
2
1
5
3
7
6
4
8
0
1
2
3
4 5
6 7 8
9
10 11
12 13
14
15
Tree edge
Forward edge
Back edge
Directed cycles
2 1
5
3
7
6 4
8
2
1
5
3
7
6
4
8
0
1
2
3
4 5
6 7 8
9
10 11
12 13
14
15
Tree edge
Forward edge
Back edge
Cross edge
Directed cycles
2 1
5
3
7
6 4
8
2
1
5
3
7
6
4
8
0
1
2
3
4 5
6 7 8
9
10 11
12 13
14
15
Tree edge
Forward edge
Back edge
Cross edge
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
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
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
Computing SCCs
DFS numbering (pre and
post) can be used to
compute SCCs
[Dasgupta,
Papadimitriou,Vazirani]
2 1
5
3
7
6 4
8
Computing SCCs
DFS numbering (pre and
post) can be used to
compute SCCs
[Dasgupta,
Papadimitriou,Vazirani]
2 1
5
3
7
6 4
8
Computing SCCs
DFS numbering (pre and
post) can be used to
compute SCCs
[Dasgupta,
Papadimitriou,Vazirani]
2 1
5
3
7
6 4
8
Computing SCCs
DFS numbering (pre and
post) can be used to
compute SCCs
[Dasgupta,
Papadimitriou,Vazirani]
2 1
5
3
7
6 4
8
Computing SCCs
DFS numbering (pre and
post) can be used to
compute SCCs
[Dasgupta,
Papadimitriou,Vazirani]
2 1
5
3
7
6 4
8
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
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
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
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
Goal
Find a sequence in which to complete the tasks,
respecting the constraints
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
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
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
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
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
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!
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
…
Topological ordering
indegree(v) : number of edges into v
outdegree(v): number of edges out of v
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!
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
1 2
3
4
5
6 7 8
1 2
3
4
5
6 7 8
Indegree
0 0
2
1
1
2 1 4
1 2
3
4
5
6 7 8
Indegree
0 0
2
1
1
2 1 4
2
3
4
5
6 7 8
Indegree
0
2 1 4
1
0
0
1
2
3
5
6 7 8
Indegree
0
1
1
0
1
4
1 3
3
5
6 7 8
Indegree
1
1
0
4
1
0
2
2
3
6 7 8
Indegree
1
1 4
1
0
2
1
5
6 7 8
Indegree
1
1 4 2
1
5
0
3
7 8
Indegree
1 4 2
1
5 3 6
0
8
Indegree
1 4 2 5 3 6 7
0
Indegree
1 4 2 5 3 6 7 8
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
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)
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)
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 }
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
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
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
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?
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?
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?
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?
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?
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?
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
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
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
1 2
3
4
5
6 7 8
Indegree
0 0
2
1
1
2 1 4
Longest Path 0 0
0
0
0 0 0
0
2
3
4
5
6 7 8
Indegree
0
2 1 4
1
0
0
1
Longest Path 0
0 0 0
1
1
1
0
2
3
5
6 7 8
Indegree
0
1
1
0
1
4
1 3
Longest Path 0
0
1
1
2 2
0 1
3
5
6 7 8
Indegree
1
1
0
4
1
0
2
2
Longest Path
0
1
1
2 2
0 1 0
3
6 7 8
Indegree
1
1 4
1
0
2
1
5
Longest Path
0
1
2 2
0 1 0 1
6 7 8
Indegree
1
1 4 2
1
5
0
3
Longest Path
0 2
2
0 1 0 1 1
7 8
Indegree
1 4 2
1
5 3 6
0
Longest Path
2
3
0 1 0 1 1 2
8
Indegree
1 4 2 5 3 6 7
0
Longest Path
4
0 1 0 1 1 2 3
Indegree
1 4 2 5 3 6 7 8
Longest Path
0 1 0 1 1 2 3 4
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])
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
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
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!
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
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
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
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
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
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
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
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
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
…
Single source shortest paths
1 2
3 4
5 6
7
70
80
10
6
20
50
10 5
Single source shortest paths
1 2
3 4
5 6
7
70
80
10
6
20
50
10 5
t = 0
Single source shortest paths
1 2
3 4
5 6
7
70
80
10
6
20
50
10 5
t = 0
t = 10
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
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
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
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
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
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
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf
week1-module1-introduction-merged.pdf

More Related Content

Similar to week1-module1-introduction-merged.pdf

DATA STRUCTURES unit 1.pptx
DATA STRUCTURES unit 1.pptxDATA STRUCTURES unit 1.pptx
DATA STRUCTURES unit 1.pptxShivamKrPathak
 
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...DrkhanchanaR
 
Data Structures- Part1 overview and review
Data Structures- Part1 overview and reviewData Structures- Part1 overview and review
Data Structures- Part1 overview and reviewAbdullah Al-hazmy
 
Lecture 01-2.ppt
Lecture 01-2.pptLecture 01-2.ppt
Lecture 01-2.pptRaoHamza24
 
Intro to DS.pptx
Intro to DS.pptxIntro to DS.pptx
Intro to DS.pptxUsman Ahmed
 
CS3114_09212011.ppt
CS3114_09212011.pptCS3114_09212011.ppt
CS3114_09212011.pptArumugam90
 
Analytics Boot Camp - Slides
Analytics Boot Camp - SlidesAnalytics Boot Camp - Slides
Analytics Boot Camp - SlidesAditya Joshi
 
00 - 30 Dec - Introduction
00 - 30 Dec - Introduction00 - 30 Dec - Introduction
00 - 30 Dec - IntroductionNeeldhara Misra
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)jehan1987
 
19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexityIntro C# Book
 
Mca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureMca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureRai University
 
Bsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureBsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureRai University
 
Bca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureBca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureRai University
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm AnalysisMary Margarat
 
Study on Sorting Algorithm and Position Determining Sort
Study on Sorting Algorithm and Position Determining SortStudy on Sorting Algorithm and Position Determining Sort
Study on Sorting Algorithm and Position Determining SortIRJET Journal
 
ADSA orientation.pptx
ADSA orientation.pptxADSA orientation.pptx
ADSA orientation.pptxKiran Babar
 

Similar to week1-module1-introduction-merged.pdf (20)

DATA STRUCTURES unit 1.pptx
DATA STRUCTURES unit 1.pptxDATA STRUCTURES unit 1.pptx
DATA STRUCTURES unit 1.pptx
 
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
Unit I- Data structures Introduction, Evaluation of Algorithms, Arrays, Spars...
 
Data Structures- Part1 overview and review
Data Structures- Part1 overview and reviewData Structures- Part1 overview and review
Data Structures- Part1 overview and review
 
Lecture 01-2.ppt
Lecture 01-2.pptLecture 01-2.ppt
Lecture 01-2.ppt
 
Intro to DS.pptx
Intro to DS.pptxIntro to DS.pptx
Intro to DS.pptx
 
CS3114_09212011.ppt
CS3114_09212011.pptCS3114_09212011.ppt
CS3114_09212011.ppt
 
Analytics Boot Camp - Slides
Analytics Boot Camp - SlidesAnalytics Boot Camp - Slides
Analytics Boot Camp - Slides
 
00 - 30 Dec - Introduction
00 - 30 Dec - Introduction00 - 30 Dec - Introduction
00 - 30 Dec - Introduction
 
Chapter two
Chapter twoChapter two
Chapter two
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
Chapter one
Chapter oneChapter one
Chapter one
 
19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity19. Java data structures algorithms and complexity
19. Java data structures algorithms and complexity
 
Mca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureMca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structure
 
Data structures
Data structuresData structures
Data structures
 
Bsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureBsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structure
 
Bca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureBca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structure
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
 
Study on Sorting Algorithm and Position Determining Sort
Study on Sorting Algorithm and Position Determining SortStudy on Sorting Algorithm and Position Determining Sort
Study on Sorting Algorithm and Position Determining Sort
 
Lecture 1.pptx
Lecture 1.pptxLecture 1.pptx
Lecture 1.pptx
 
ADSA orientation.pptx
ADSA orientation.pptxADSA orientation.pptx
ADSA orientation.pptx
 

Recently uploaded

Jax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined DeckJax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined DeckMarc Lester
 
From Knowledge Graphs via Lego Bricks to scientific conversations.pptx
From Knowledge Graphs via Lego Bricks to scientific conversations.pptxFrom Knowledge Graphs via Lego Bricks to scientific conversations.pptx
From Knowledge Graphs via Lego Bricks to scientific conversations.pptxNeo4j
 
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-CloudAlluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-CloudAlluxio, Inc.
 
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit Milan
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit MilanWorkshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit Milan
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit MilanNeo4j
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Clinic
 
Novo Nordisk: When Knowledge Graphs meet LLMs
Novo Nordisk: When Knowledge Graphs meet LLMsNovo Nordisk: When Knowledge Graphs meet LLMs
Novo Nordisk: When Knowledge Graphs meet LLMsNeo4j
 
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024MulesoftMunichMeetup
 
From Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST APIFrom Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST APIInflectra
 
BusinessGPT - Security and Governance for Generative AI
BusinessGPT  - Security and Governance for Generative AIBusinessGPT  - Security and Governance for Generative AI
BusinessGPT - Security and Governance for Generative AIAGATSoftware
 
The Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test AutomationThe Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test AutomationElement34
 
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypseTomasz Kowalczewski
 
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...Neo4j
 
Transformer Neural Network Use Cases with Links
Transformer Neural Network Use Cases with LinksTransformer Neural Network Use Cases with Links
Transformer Neural Network Use Cases with LinksJinanKordab
 
Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Maxim Salnikov
 
Test Automation Design Patterns_ A Comprehensive Guide.pdf
Test Automation Design Patterns_ A Comprehensive Guide.pdfTest Automation Design Patterns_ A Comprehensive Guide.pdf
Test Automation Design Patterns_ A Comprehensive Guide.pdfkalichargn70th171
 
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...OnePlan Solutions
 
Your Ultimate Web Studio for Streaming Anywhere | Evmux
Your Ultimate Web Studio for Streaming Anywhere | EvmuxYour Ultimate Web Studio for Streaming Anywhere | Evmux
Your Ultimate Web Studio for Streaming Anywhere | Evmuxevmux96
 
Auto Affiliate AI Earns First Commission in 3 Hours..pdf
Auto Affiliate  AI Earns First Commission in 3 Hours..pdfAuto Affiliate  AI Earns First Commission in 3 Hours..pdf
Auto Affiliate AI Earns First Commission in 3 Hours..pdfSelfMade bd
 

Recently uploaded (20)

Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
 
Jax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined DeckJax, FL Admin Community Group 05.14.2024 Combined Deck
Jax, FL Admin Community Group 05.14.2024 Combined Deck
 
From Knowledge Graphs via Lego Bricks to scientific conversations.pptx
From Knowledge Graphs via Lego Bricks to scientific conversations.pptxFrom Knowledge Graphs via Lego Bricks to scientific conversations.pptx
From Knowledge Graphs via Lego Bricks to scientific conversations.pptx
 
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
Abortion Clinic Pretoria ](+27832195400*)[ Abortion Clinic Near Me ● Abortion...
 
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-CloudAlluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
Alluxio Monthly Webinar | Simplify Data Access for AI in Multi-Cloud
 
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit Milan
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit MilanWorkshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit Milan
Workshop: Enabling GenAI Breakthroughs with Knowledge Graphs - GraphSummit Milan
 
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
Abortion Pill Prices Jane Furse ](+27832195400*)[ 🏥 Women's Abortion Clinic i...
 
Novo Nordisk: When Knowledge Graphs meet LLMs
Novo Nordisk: When Knowledge Graphs meet LLMsNovo Nordisk: When Knowledge Graphs meet LLMs
Novo Nordisk: When Knowledge Graphs meet LLMs
 
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
Anypoint Code Builder - Munich MuleSoft Meetup - 16th May 2024
 
From Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST APIFrom Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST API
 
BusinessGPT - Security and Governance for Generative AI
BusinessGPT  - Security and Governance for Generative AIBusinessGPT  - Security and Governance for Generative AI
BusinessGPT - Security and Governance for Generative AI
 
The Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test AutomationThe Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test Automation
 
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
[GeeCON2024] How I learned to stop worrying and love the dark silicon apocalypse
 
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
 
Transformer Neural Network Use Cases with Links
Transformer Neural Network Use Cases with LinksTransformer Neural Network Use Cases with Links
Transformer Neural Network Use Cases with Links
 
Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?Prompt Engineering - an Art, a Science, or your next Job Title?
Prompt Engineering - an Art, a Science, or your next Job Title?
 
Test Automation Design Patterns_ A Comprehensive Guide.pdf
Test Automation Design Patterns_ A Comprehensive Guide.pdfTest Automation Design Patterns_ A Comprehensive Guide.pdf
Test Automation Design Patterns_ A Comprehensive Guide.pdf
 
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
Optimizing Operations by Aligning Resources with Strategic Objectives Using O...
 
Your Ultimate Web Studio for Streaming Anywhere | Evmux
Your Ultimate Web Studio for Streaming Anywhere | EvmuxYour Ultimate Web Studio for Streaming Anywhere | Evmux
Your Ultimate Web Studio for Streaming Anywhere | Evmux
 
Auto Affiliate AI Earns First Commission in 3 Hours..pdf
Auto Affiliate  AI Earns First Commission in 3 Hours..pdfAuto Affiliate  AI Earns First Commission in 3 Hours..pdf
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
  • 2. Understanding Algorithms Correctness Efficiency Asymptotic complexity, O( ) notation Modelling Graphs, data structures, decomposing the problem Techniques Divide and conquer, greedy, dynamic programming
  • 3. Expectations Background in programming Any language (C, C++, Java) Basic data structures Arrays, lists
  • 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, …
  • 6. Tentative schedule Week 1: Motivation, asymptotic complexity Week 2: Searching and sorting Week 3: Graphs and basic graph algorithms Week 4: More graph algorithms, disjoint set Week 5: Divide and conquer, heaps Week 6: Search trees, greedy algorithms Week 7: Dynamic programming Week 8: Miscellaneous topics 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 January February
  • 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
  • 8. Textbooks Algorithm Design Jon Kleinberg and Eva Tardos Algorithms Sanjoy Dasgupta, Christos Papadimitriou and Umesh Vazirani
  • 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
  • 12. Throw away the map and record the network This is a graph—a collection of nodes and edges
  • 13. Can distort the picture without changing meaning
  • 14. Can distort the picture without changing meaning
  • 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
  • 32. Naive recursion can be inefficient Fibonacci numbers: F(n) = F(n-1) + F(n-2), F(1) = 1, F(2) = 1 Sequence is 1,1,2,3,5,8,13,21,…. Computing recursively F(7) = F(6) + F(5) = (F(5) + F(4)) + (F(4)+F(3)) = (F(4)+F(3)+F(3)+F(2)) + (F(3)+F(2)+F(2)+F(1)) = …
  • 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
  • 53. Strategy 1 … 74 32 89 55 21 64
  • 54. Strategy 1 … 21 74 32 89 55 21 64
  • 55. Strategy 1 … 21 32 74 32 89 55 21 64
  • 56. Strategy 1 … 21 32 55 74 32 89 55 21 64
  • 57. Strategy 1 … 21 32 55 64 74 32 89 55 21 64
  • 58. Strategy 1 … 21 32 55 64 74 74 32 89 55 21 64
  • 59. Strategy 1 … 21 32 55 64 74 89 74 32 89 55 21 64
  • 60. Strategy 1 … Selection Sort Select the next element in sorted order Move it into its correct place in the final sorted list
  • 61. Selection Sort Avoid using a second list Swap minimum element with value in first position Swap second minimum element to second position …
  • 62. Selection Sort 74 32 89 55 21 64
  • 63. Selection Sort 74 32 89 55 21 64
  • 64. Selection Sort 21 32 89 55 74 64
  • 65. Selection Sort 21 32 89 55 74 64
  • 66. Selection Sort 21 32 89 55 74 64
  • 67. Selection Sort 21 32 89 55 74 64
  • 68. Selection Sort 21 32 55 89 74 64
  • 69. Selection Sort 21 32 55 89 74 64
  • 70. Selection Sort 21 32 55 64 74 89
  • 71. Selection Sort 21 32 55 64 74 89
  • 72. Selection Sort 21 32 55 64 74 89
  • 73. Selection Sort 21 32 55 64 74 89
  • 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
  • 83. Strategy 2 … 74 32 89 55 21 64
  • 84. Strategy 2 … 74 32 89 55 21 64 74
  • 85. Strategy 2 … 74 32 89 55 21 64 32 74
  • 86. Strategy 2 … 74 32 89 55 21 64 32 74 89
  • 87. Strategy 2 … 74 32 89 55 21 64 32 55 74 89
  • 88. Strategy 2 … 74 32 89 55 21 64 21 32 55 74 89
  • 89. Strategy 2 … 74 32 89 55 21 64 21 32 55 64 74 89
  • 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
  • 92. Insertion Sort 74 32 89 55 21 64
  • 93. Insertion Sort 74 32 89 55 21 64
  • 94. Insertion Sort 32 74 89 55 21 64
  • 95. Insertion Sort 32 74 89 55 21 64
  • 96. Insertion Sort 32 74 55 89 21 64
  • 97. Insertion Sort 32 55 74 89 21 64
  • 98. Insertion Sort 32 55 74 21 89 64
  • 99. Insertion Sort 32 55 21 74 89 64
  • 100. Insertion Sort 32 21 55 74 89 64
  • 101. Insertion Sort 21 32 55 74 89 64
  • 102. Insertion Sort 21 32 55 74 64 89
  • 103. Insertion Sort 21 32 55 64 74 89
  • 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)
  • 105. Recursive formulation To sort A[0..n-1] Recursively sort A[0..n-2] Insert A[n-1] into A[0..n-2] Base case: n = 1
  • 106. Insertion Sort, recursive InsertionSort(A,k) // Sort A[0..k-1] if (k == 1) return; InsertionSort(A,k-1); Insert(A,k-1); return; Insert(A,j) // Insert A[j] into A[0..j-1] pos = j; while (pos > 0 && A[pos] < A[pos-1]) swap(A,pos,pos-1); pos = pos-1;
  • 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
  • 110. O(n2) sorting algorithms Selection sort and insertion sort are both O(n2) O(n2) sorting is infeasible for n over 100000
  • 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
  • 113. Merging two sorted lists 32 74 89 21 55 64
  • 114. Merging two sorted lists 32 74 89 21 55 64 21
  • 115. Merging two sorted lists 32 74 89 21 55 64 21 32
  • 116. Merging two sorted lists 32 74 89 21 55 64 21 32 55
  • 117. Merging two sorted lists 32 74 89 21 55 64 21 32 55 64
  • 118. Merging two sorted lists 32 74 89 21 55 64 21 32 55 64 74
  • 119. Merging two sorted lists 32 74 89 21 55 64 21 32 55 64 74 89
  • 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!
  • 121. Merge Sort 43 32 22 78 63 57 91 13
  • 122. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78
  • 123. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13
  • 124. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78
  • 125. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13
  • 126. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32
  • 127. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78
  • 128. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57
  • 129. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13
  • 130. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 22 78 63 57 91 13 43 32 22 78 63 57 91 13 32 43
  • 131. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 63 57 91 13 43 32 22 78 63 57 91 13 32 43 22 78
  • 132. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 91 13 43 32 22 78 63 57 91 13 32 43 22 78 57 63
  • 133. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 32 43 22 78 57 63 13 91
  • 134. Merge Sort 43 32 22 78 63 57 91 13 63 57 91 13 43 32 22 78 63 57 91 13 32 43 22 78 57 63 13 91 22 32 43 78
  • 135. Merge Sort 43 32 22 78 63 57 91 13 43 32 22 78 63 57 91 13 32 43 22 78 57 63 13 91 22 32 43 78 13 57 63 91
  • 136. Merge Sort 43 32 22 78 63 57 91 13 32 43 22 78 57 63 13 91 22 32 43 78 13 57 63 91 13 22 32 43 57 63 78 91
  • 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
  • 148. Analysis of Merge Sort …
  • 149. Analysis of Merge Sort … t(1) = 1
  • 150. Analysis of Merge Sort … t(1) = 1 t(n) = 2t(n/2) + n
  • 151. 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
  • 152. 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 …
  • 153. 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
  • 154. 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
  • 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
  • 168. Quicksort High level view 43 32 22 78 63 57 91 13
  • 169. Quicksort High level view 43 32 22 78 63 57 91 13
  • 170. Quicksort High level view 43 32 22 78 63 57 91 13
  • 171. Quicksort High level view 13 32 22 43 63 57 91 78
  • 172. Quicksort High level view 13 22 32 43 57 63 78 91
  • 174. Quicksort: Partitioning 43 32 22 78 63 57 91 13
  • 175. Quicksort: Partitioning 43 32 22 78 63 57 91 13
  • 176. Quicksort: Partitioning 43 32 22 78 63 57 91 13
  • 177. Quicksort: Partitioning 43 32 22 78 63 57 91 13
  • 178. Quicksort: Partitioning 43 32 22 78 63 57 91 13
  • 179. Quicksort: Partitioning 43 32 22 78 63 57 91 13
  • 180. Quicksort: Partitioning 43 32 22 78 63 57 91 13
  • 181. Quicksort: Partitioning 43 32 22 78 63 57 91 13
  • 182. Quicksort: Partitioning 43 32 22 78 63 57 91 13
  • 183. Quicksort: Partitioning 43 32 22 78 63 57 91 13
  • 184. Quicksort: Partitioning 43 32 22 13 63 57 91 78
  • 185. Quicksort: Partitioning 13 32 22 43 63 57 91 78
  • 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?
  • 215. Map Colouring Mark each state Connect states that share a border
  • 216. Map Colouring Mark each state Connect states that share a border Assign colours to dots so that no two connected dots have the same colour
  • 217. Map Colouring Mark each state Connect states that share a border Assign colours to dots so that no two connected dots have the same colour
  • 218. Map Colouring Mark each state Connect states that share a border Assign colours to dots so that no two connected dots have the same colour
  • 219. Map Colouring Mark each state Connect states that share a border Assign colours to dots so that no two connected dots have the same colour
  • 220. Map Colouring Mark each state Connect states that share a border Assign colours to dots so that no two connected dots have the same colour
  • 221. Map Colouring Mark each state Connect states that share a border Assign colours to dots so that no two connected dots have the same colour
  • 223. Map Colouring In fact, the actual map is irrelevant!
  • 224. Map Colouring In fact, the actual map is irrelevant! All we need is the underlying pattern of dots and connections
  • 225. Map Colouring This kind of diagram is called a graph Dots are nodes or vertices One vertex, many vertices Connections are edges
  • 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!
  • 227. Graph Colouring Observe that the original map used more than 4 colours
  • 228. Graph Colouring Observe that the original map used more than 4 colours
  • 229. Graph Colouring The graph emphasizes the essential features of the problem What is connected to what?
  • 230. Graph Colouring The graph emphasizes the essential features of the problem What is connected to what? We can distort this figure and the problem remains the same
  • 233. More graph problems Airline routing Can I travel from New Delhi to Trivandrum without changing airlines?
  • 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
  • 237. Graph Colouring Undirected graph Colouring C assigns each vertex v a colour C(v) Legal colouring: if (v,v’) is in E, then C(v) ≠ C(v’)
  • 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
  • 248. Adjacency matrix 1 2 3 4 5 6 7 8 9 10 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
  • 249. Adjacency matrix 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 Neighbours of i Any column j in row i with entry 1 Scan row i from left to right to identify all neighbours Neighbours of 4 are {1,5,8}
  • 250. Adjacency matrix 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 Neighbours of i Any column j in row i with entry 1 Scan row i from left to right to identify all neighbours Neighbours of 4 are {1,5,8}
  • 251. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 252. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 253. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 254. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 255. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 256. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 257. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 258. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 259. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 260. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 261. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 262. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 263. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 264. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 265. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 266. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 267. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 268. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 269. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 270. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 271. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 272. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 273. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 274. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 275. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 276. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 277. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 278. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 279. Finding a path 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 Start with vs New Delhi is 1 Mark each neighbour as reachable Explore neighbours of marked vertices Check if target is marked vt =10 = Trivandrum
  • 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
  • 287. Adjacency matrix 1 2 3 4 5 6 7 8 9 10 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
  • 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
  • 293. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Visited Queue
  • 294. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Visited Queue head tail
  • 295. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Visited Queue 1 1
  • 296. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Visited Queue 1
  • 297. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Visited Queue 1 1 2
  • 298. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Visited Queue 1 1 2 1 3
  • 299. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Visited Queue 1 1 2 1 3 1 4
  • 300. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Visited Queue 1 1 1 3 1 4
  • 301. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Visited Queue 1 1 1 1 4
  • 302. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Visited Queue 1 1 1 1
  • 303. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Visited Queue 1 1 1 1 1 5
  • 304. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Visited Queue 1 1 1 1 1 5 1 8
  • 305. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Visited Queue 1 1 1 1 1 1 8
  • 306. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Visited Queue 1 1 1 1 1 1 6 1 8
  • 307. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Visited Queue 1 1 1 1 1 1 6 1 8 1 7
  • 308. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Visited Queue 1 1 1 1 1 1 6 1 1 7
  • 309. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Visited Queue 1 1 1 1 1 1 6 1 1 7 1 9
  • 310. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Visited Queue 1 1 1 1 1 1 1 1 7 1 9
  • 311. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Visited Queue 1 1 1 1 1 1 1 1 1 9
  • 312. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Visited Queue 1 1 1 1 1 1 1 1 1
  • 313. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Visited Queue 1 1 1 1 1 1 1 1 1 1 10
  • 314. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Visited Queue 1 1 1 1 1 1 1 1 1 1
  • 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);
  • 324. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Queue L : Level P : Parent L P
  • 325. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Queue head tail L : Level P : Parent L P
  • 326. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Queue 0 1 L : Level P : Parent L P -
  • 327. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Queue 0 L : Level P : Parent L P -
  • 328. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Queue 0 1 2 L : Level P : Parent L P - 1
  • 329. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Queue 0 1 2 1 3 L : Level P : Parent L P - 1 1
  • 330. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Queue 0 1 2 1 3 1 4 L : Level P : Parent L P - 1 1 1
  • 331. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Queue 0 1 1 3 1 4 L : Level P : Parent L P - 1 1 1
  • 332. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Queue 0 1 1 1 4 L : Level P : Parent L P - 1 1 1
  • 333. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Queue 0 1 1 1 L : Level P : Parent L P - 1 1 1
  • 334. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Queue 0 1 1 1 2 5 L : Level P : Parent L P - 1 1 1 4
  • 335. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Queue 0 1 1 1 2 5 2 8 L : Level P : Parent L P - 1 1 1 4 4
  • 336. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Queue 0 1 1 1 2 2 8 L : Level P : Parent L P - 1 1 1 4 4
  • 337. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Queue 0 1 1 1 2 3 6 2 8 L : Level P : Parent L P - 1 1 1 4 4 5
  • 338. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Queue 0 1 1 1 2 3 6 2 8 3 7 L : Level P : Parent L P - 1 1 1 4 4 5 5
  • 339. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Queue 0 1 1 1 2 3 6 2 3 7 L : Level P : Parent L P - 1 1 1 4 4 5 5
  • 340. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Queue 0 1 1 1 2 3 6 2 3 7 3 9 L : Level P : Parent L P - 1 1 1 4 4 5 5 8
  • 341. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Queue 0 1 1 1 2 3 2 3 7 3 9 L : Level P : Parent L P - 1 1 1 4 4 5 5 8
  • 342. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Queue 0 1 1 1 2 3 2 3 3 9 L : Level P : Parent L P - 1 1 1 4 4 5 5 8
  • 343. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Queue 0 1 1 1 2 3 2 3 3 L : Level P : Parent L P - 1 1 1 4 4 5 5 8
  • 344. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Queue 0 1 1 1 2 3 2 3 3 4 10 L : Level P : Parent L P - 1 1 1 4 4 5 5 8 9
  • 345. 1 5 4 2 3 6 7 8 9 10 Breadth first search 1 2 3 4 5 6 7 8 9 10 Queue 0 1 1 1 2 3 2 3 3 4 L : Level P : Parent L P - 1 1 1 4 4 5 5 8 9
  • 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
  • 349. 1 2 3 4 5 6 7 8 9 10 Depth first search 1 2 3 4 5 6 7 8 9 10 Visited Stack of suspended vertices
  • 350. 1 2 3 4 5 6 7 8 9 10 Depth first search 1 2 3 4 5 6 7 8 9 10 Visited Stack of suspended vertices Start at 4
  • 351. 1 2 3 4 5 6 7 8 9 10 Depth first search 1 2 3 4 5 6 7 8 9 10 Visited Stack of suspended vertices Start at 4 1
  • 352. 1 2 3 4 5 6 7 8 9 10 Depth first search 1 2 3 4 5 6 7 8 9 10 Visited Stack of suspended vertices Start at 4 1 4 1
  • 353. 1 2 3 4 5 6 7 8 9 10 Depth first search 1 2 3 4 5 6 7 8 9 10 Visited Stack of suspended vertices Start at 4 1 4 1 1 1
  • 354. 1 2 3 4 5 6 7 8 9 10 Depth first search 1 2 3 4 5 6 7 8 9 10 Visited Stack of suspended vertices Start at 4 1 4 1 1 1 2 1
  • 355. 1 2 3 4 5 6 7 8 9 10 Depth first search 1 2 3 4 5 6 7 8 9 10 Visited Stack of suspended vertices Start at 4 1 4 1 1 1 1
  • 356. 1 2 3 4 5 6 7 8 9 10 Depth first search 1 2 3 4 5 6 7 8 9 10 Visited Stack of suspended vertices Start at 4 1 4 1 1 1
  • 357. 1 2 3 4 5 6 7 8 9 10 Depth first search 1 2 3 4 5 6 7 8 9 10 Visited Stack of suspended vertices Start at 4 1 1 1 1
  • 358. 1 2 3 4 5 6 7 8 9 10 Depth first search 1 2 3 4 5 6 7 8 9 10 Visited Stack of suspended vertices Start at 4 1 1 1 1 1 4
  • 359. 1 2 3 4 5 6 7 8 9 10 Depth first search 1 2 3 4 5 6 7 8 9 10 Visited Stack of suspended vertices Start at 4 1 1 1 1 1 5 1 4
  • 360. 1 2 3 4 5 6 7 8 9 10 Depth first search 1 2 3 4 5 6 7 8 9 10 Visited Stack of suspended vertices Start at 4 1 1 1 1 1 5 1 6 1 4
  • 361. 1 2 3 4 5 6 7 8 9 10 Depth first search 1 2 3 4 5 6 7 8 9 10 Visited Stack of suspended vertices Start at 4 1 1 1 1 1 5 1 1 4
  • 362. 1 2 3 4 5 6 7 8 9 10 Depth first search 1 2 3 4 5 6 7 8 9 10 Visited Stack of suspended vertices Start at 4 1 1 1 1 1 5 1 1 1 6 4
  • 363. 1 2 3 4 5 6 7 8 9 10 Depth first search 1 2 3 4 5 6 7 8 9 10 Visited Stack of suspended vertices Start at 4 1 1 1 1 1 5 1 1 1 6 1 8 4
  • 364. 1 2 3 4 5 6 7 8 9 10 Depth first search 1 2 3 4 5 6 7 8 9 10 Visited Stack of suspended vertices Start at 4 1 1 1 1 1 5 1 1 1 6 1 8 1 9 4
  • 365. 1 2 3 4 5 6 7 8 9 10 Depth first search 1 2 3 4 5 6 7 8 9 10 Visited Stack of suspended vertices Start at 4 1 1 1 1 1 5 1 1 1 6 1 8 1 4
  • 366. 1 2 3 4 5 6 7 8 9 10 Depth first search 1 2 3 4 5 6 7 8 9 10 Visited Stack of suspended vertices Start at 4 1 1 1 1 1 5 1 1 1 6 1 1 4
  • 367. 1 2 3 4 5 6 7 8 9 10 Depth first search 1 2 3 4 5 6 7 8 9 10 Visited Stack of suspended vertices Start at 4 1 1 1 1 1 5 1 1 1 1 1 4
  • 368. 1 2 3 4 5 6 7 8 9 10 Depth first search 1 2 3 4 5 6 7 8 9 10 Visited Stack of suspended vertices Start at 4 1 1 1 1 1 1 1 1 1 1 4
  • 369. 1 2 3 4 5 6 7 8 9 10 Depth first search 1 2 3 4 5 6 7 8 9 10 Visited Stack of suspended vertices Start at 4 1 1 1 1 1 1 1 1 1 1
  • 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)
  • 375. Complexity of DFS Each vertex marked and explored exactly once
  • 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
  • 380. Properties of DFS Paths discovered by DFS are not shortest paths, unlike 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?
  • 393. Connectivity Connected graph 1 2 3 4 5 Disconnected graph 1 2 5 9 10 3 4 7 11 12 6 8
  • 394. Connectivity Connected graph 1 2 3 4 5 Disconnected graph Connected components 1 2 5 9 10 3 4 7 11 12 6 8
  • 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
  • 400. Cycles Acyclic graph 1 2 3 4 5 Graph with cycles 1 2 5 9 10 3 4 7 11 12 6 8
  • 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
  • 403. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8
  • 404. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1
  • 405. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 0 pre
  • 406. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 0 pre 1
  • 407. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 0 pre 1 2 post
  • 408. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 0 pre 1 2 post 3
  • 409. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 9 0 pre 1 2 post 3 4
  • 410. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 9 10 0 pre 1 2 post 3 4 5
  • 411. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 9 10 0 pre 1 2 post 3 4 5 6
  • 412. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 9 10 0 pre 1 2 post 3 4 5 6 7
  • 413. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 9 10 0 pre 1 2 post 3 4 5 6 7 8
  • 414. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 9 10 0 pre 1 2 post 3 4 5 6 7 8 9
  • 415. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 9 10 3 0 pre 1 2 post 3 4 5 6 7 8 9
  • 416. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 9 10 3 0 pre 1 2 post 3 4 5 6 7 8 9 10
  • 417. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 9 10 3 4 0 pre 1 2 post 3 4 5 6 7 8 9 10 11
  • 418. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 9 10 3 4 8 0 pre 1 2 post 3 4 5 6 7 8 9 10 11 12
  • 419. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 9 10 3 4 8 7 0 pre 1 2 post 3 4 5 6 7 8 9 10 11 12 13
  • 420. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 9 10 3 4 8 7 11 0 pre 1 2 post 3 4 5 6 7 8 9 10 11 12 13 14
  • 421. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 9 10 3 4 8 7 11 0 pre 1 2 post 3 4 5 6 7 8 9 10 11 12 13 14 15
  • 422. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 9 10 3 4 8 7 11 0 pre 1 2 post 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  • 423. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 9 10 3 4 8 7 11 12 0 pre 1 2 post 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
  • 424. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 9 10 3 4 8 7 11 12 0 pre 1 2 post 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
  • 425. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 9 10 3 4 8 7 11 12 0 pre 1 2 post 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
  • 426. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 9 10 3 4 8 7 11 12 0 pre 1 2 post 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
  • 427. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 9 10 3 4 8 7 11 12 0 pre 1 2 post 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
  • 428. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 9 10 3 4 8 7 11 12 6 0 pre 1 2 post 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
  • 429. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 9 10 3 4 8 7 11 12 6 0 pre 1 2 post 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  • 430. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 9 10 3 4 8 7 11 12 6 0 pre 1 2 post 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
  • 431. DFS tree 1 2 5 9 10 3 4 7 11 12 6 8 1 2 5 9 10 3 4 8 7 11 12 6 0 pre 1 2 post 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 Any non-tree edge generates a cycle
  • 436. Directed cycles 2 1 5 3 7 6 4 8 2 1 5 0 1 2
  • 437. Directed cycles 2 1 5 3 7 6 4 8 2 1 5 6 0 1 2 3
  • 438. Directed cycles 2 1 5 3 7 6 4 8 2 1 5 7 6 0 1 2 3 4
  • 439. Directed cycles 2 1 5 3 7 6 4 8 2 1 5 7 6 0 1 2 3 4 5
  • 440. Directed cycles 2 1 5 3 7 6 4 8 2 1 5 7 6 0 1 2 3 4 5 6
  • 441. Directed cycles 2 1 5 3 7 6 4 8 2 1 5 7 6 8 0 1 2 3 4 5 6 7
  • 442. Directed cycles 2 1 5 3 7 6 4 8 2 1 5 7 6 8 0 1 2 3 4 5 6 7 8
  • 443. Directed cycles 2 1 5 3 7 6 4 8 2 1 5 7 6 8 0 1 2 3 4 5 6 7 8 9
  • 444. Directed cycles 2 1 5 3 7 6 4 8 2 1 5 7 6 8 0 1 2 3 4 5 6 7 8 9 10
  • 445. Directed cycles 2 1 5 3 7 6 4 8 2 1 5 3 7 6 8 0 1 2 3 4 5 6 7 8 9 10 11
  • 446. Directed cycles 2 1 5 3 7 6 4 8 2 1 5 3 7 6 4 8 0 1 2 3 4 5 6 7 8 9 10 11 12
  • 447. Directed cycles 2 1 5 3 7 6 4 8 2 1 5 3 7 6 4 8 0 1 2 3 4 5 6 7 8 9 10 11 12 13
  • 448. Directed cycles 2 1 5 3 7 6 4 8 2 1 5 3 7 6 4 8 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
  • 449. Directed cycles 2 1 5 3 7 6 4 8 2 1 5 3 7 6 4 8 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  • 450. Directed cycles 2 1 5 3 7 6 4 8 2 1 5 3 7 6 4 8 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Tree edge
  • 451. Directed cycles 2 1 5 3 7 6 4 8 2 1 5 3 7 6 4 8 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Tree edge
  • 452. Directed cycles 2 1 5 3 7 6 4 8 2 1 5 3 7 6 4 8 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Tree edge Forward edge
  • 453. Directed cycles 2 1 5 3 7 6 4 8 2 1 5 3 7 6 4 8 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Tree edge Forward edge
  • 454. Directed cycles 2 1 5 3 7 6 4 8 2 1 5 3 7 6 4 8 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Tree edge Forward edge Back edge
  • 455. Directed cycles 2 1 5 3 7 6 4 8 2 1 5 3 7 6 4 8 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Tree edge Forward edge Back edge
  • 456. Directed cycles 2 1 5 3 7 6 4 8 2 1 5 3 7 6 4 8 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Tree edge Forward edge Back edge Cross edge
  • 457. Directed cycles 2 1 5 3 7 6 4 8 2 1 5 3 7 6 4 8 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Tree edge Forward edge Back edge Cross edge
  • 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 …
  • 478. Topological ordering indegree(v) : number of edges into v outdegree(v): number of edges out of v
  • 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
  • 482. 1 2 3 4 5 6 7 8 Indegree 0 0 2 1 1 2 1 4
  • 483. 1 2 3 4 5 6 7 8 Indegree 0 0 2 1 1 2 1 4
  • 487. 3 6 7 8 Indegree 1 1 4 1 0 2 1 5
  • 488. 6 7 8 Indegree 1 1 4 2 1 5 0 3
  • 489. 7 8 Indegree 1 4 2 1 5 3 6 0
  • 490. 8 Indegree 1 4 2 5 3 6 7 0
  • 491. Indegree 1 4 2 5 3 6 7 8
  • 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
  • 508. 1 2 3 4 5 6 7 8 Indegree 0 0 2 1 1 2 1 4 Longest Path 0 0 0 0 0 0 0 0
  • 509. 2 3 4 5 6 7 8 Indegree 0 2 1 4 1 0 0 1 Longest Path 0 0 0 0 1 1 1 0
  • 510. 2 3 5 6 7 8 Indegree 0 1 1 0 1 4 1 3 Longest Path 0 0 1 1 2 2 0 1
  • 512. 3 6 7 8 Indegree 1 1 4 1 0 2 1 5 Longest Path 0 1 2 2 0 1 0 1
  • 513. 6 7 8 Indegree 1 1 4 2 1 5 0 3 Longest Path 0 2 2 0 1 0 1 1
  • 514. 7 8 Indegree 1 4 2 1 5 3 6 0 Longest Path 2 3 0 1 0 1 1 2
  • 515. 8 Indegree 1 4 2 5 3 6 7 0 Longest Path 4 0 1 0 1 1 2 3
  • 516. Indegree 1 4 2 5 3 6 7 8 Longest Path 0 1 0 1 1 2 3 4
  • 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 …
  • 530. Single source shortest paths 1 2 3 4 5 6 7 70 80 10 6 20 50 10 5
  • 531. Single source shortest paths 1 2 3 4 5 6 7 70 80 10 6 20 50 10 5 t = 0
  • 532. Single source shortest paths 1 2 3 4 5 6 7 70 80 10 6 20 50 10 5 t = 0 t = 10
  • 533. 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
  • 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