1. Design and Analysis of Algorithms
Lecture 1
β’ Overview of the course
β’ Closest Pair problem
1
https://moodle.cse.iitk.ac.in
CS345A
Algorithms-II
2. Aim of the course
To empower each student with the skills to design algorithms
β’ With provable guarantee on correctness.
β’ With provable guarantee on their efficiency.
2
3. Algorithm Paradigm
Motivation:
β’ Many problems whose algorithms are based on a common approach.
β A need of a systematic study of the characteristics of such approaches.
Algorithm Paradigms:
β’ Divide and Conquer
β’ Greedy Strategy
β’ Dynamic Programming
3
(advanced)
(advanced)
4. Maximum Flow
Given a network for transporting certain commodity (water/bits)
from a designated source vertex π and sink vertex π.
Each edge has a certain capacity (max rate per unit time at which commodity
can be pumped along that edge),
Compute the maximum rate at which we can pump flow from π to π.
Constraints: capacity constraint and conservation constraint. 4
π
π
π
π
π
π
2
17
5
6
8
17
4
15
16
7
14
5. Miscellaneous
β’ Matching in Graphs
Maximum matching, Stable matching
β’ Amortized Analysis
A powerful technique to analyse time complexity of algorithms
β’ String Matching
β’ Linear Programming
5
6. Last topic on Algorithms
β’ NP Complete problems
β’ Approximation/randomized Algorithms
6
8. Data structures
β’ Augmented Binary Search Trees
β’ Range Minima Data structure (optimal size)
β’ Fibonacci Heap
8
: Additional information
9. Orthogonal Range searching
Problem: Preprocess a set of π points so that given any query rectangle,
the number of points lying inside it can be reported efficiently.
Data structure:
size = O(π log π), Query = O( log2 π),
size = O(π), Query = O( π), 9
Rectangle
A novel application of augmented BST
Try to solve itβ¦
You can surely do itβ¦βΊ
Rectangle
11. An Overview
A problem in this paradigm is solved in the following way.
1. Divide the problem instance into two or more instances of the same problem.
2. Solve each smaller instance recursively (base case suitably defined).
3. Combine the solutions of the smaller instances
to get the solution of the original instance.
11
This is usually the main nontrivial step
in the design of an algorithm using
divide and conquer strategy
12. Example Problems
1. Merge Sort
2. Multiplication of two π-bit integers.
3. Counting the number of inversions in an array.
4. Median finding in linear time.
12
15. Closest Pair of Points
Problem Definition:
Given a set π· of π > π points in plane,
compute the pair of points with minimum Euclidean distance.
Deterministic algorithms:
β’ O(ππ) : Trivial algorithm
β’ O(π π₯π¨π π) : Divide and Conquer based algorithm
15
16. Hint/Tool No. 1
Exercise:
What is the maximum number of points that can be placed in a unit square
such that the minimum distance is at least 1 ?
Answer: 4.
16
1
1
2
A discrete math exercise
If there are more than
4 points, at least one
of the four small
squares will have
more than 1 points.
17. Hint/Tool No. 2
Question:
For which algorithmic problems do we need a suitable data structure ?
Answer:
If the problem involves βmanyβ operations of same type on a given data.
For example, it is worth sorting an array only if there are going to be many
search queries on it.
Let us see if you can use this principle in todayβs class itself βΊ
17
When do we use a data structure ?
24. The combine step
24
πΉπ³
πΉπΉ
πΉπ³
πΉπ³
πΉπ³
πΉπ³
Only the points lying in these
2 red squares are relevant as
far as π is concerned.
π
How many points can
there be in these 2 red
squares each of lengthπΉπ³?
Surely not more than 8
(using Hint 1)
How to find the points in
these red square for point π ?
It will take O(π) time for a given π.
It is time to use Hint/Tool no. 2.
Think for a while before going to
the next slide.
25. The combine step
25
πΉπ³
πΉπΉ
πΉπ³
πΉπ³
πΉπ³
πΉπ³
We need to find points in the
2 red square for every point
in the left strip.
So build a suitable data structure
for points in the right strip so that
we can answer such query efficiently
for each point in the left strip.
What will be
the data structure ?
An array storing the points of
the right strip in increasing
order of y-coordinates.
πΉπ³
πΉπ³
πΉπ³
πΉπ³
26. Divide and Conquer based algorithm
CP-Distance(π)
{ If (| π |=1 ) return infinity;
{ Compute π₯-median of π;
(ππΏ, ππ )οSplit-by-π₯-median(π);
πΉπ³ο CP-Distance(ππΏ) ;
πΉπΉο CP-Distance(ππ ) ;
πΉο min(πΉπ³, πΉπΉ);
ππΏ ο strip of ππΏ;
ππ ο strip of ππ ;
π΄ ο Sorted array of ππ ;
For each π β ππΏ,
π ο y-coordinate of π;
Search π΄ for points with y-coordinate within π Β± πΉ;
Compute distance from π to each of these points;
Update πΉ accordingly;
return πΉ;
}
26
Divide step
Combine/conquer step
πΆ( π log π) time
πΆ( π ) + 2 T(|π|/2) time
27. Running time of the algorithm
What is the recurrence for running time?
T(π) = c π log π + 2 T(π/2)
β
T(π) = O( π log2π)
Theorem:
There exists an O( π log2π) time algorithm to compute closest pair of π points
in plane.
27
28. Conclusion
Homework:
1. Try to improve the running time to O( π log π).
Hint: βthe code will look similar to that of MergeSortβ.
2. Ponder over the data structure for orthogonal range searching.
28
29. How does one design an algorithm ?
If you wish to find the answer on your own,
try to solve the first assignment problem on your own.
29
Without any help from the web
Without any help from the your friends
30. Assignment 1
Smallest Enclosing circle
Problem definition: Given π points in a plane,
compute the smallest radius circle that encloses all π point.
30