0% found this document useful (0 votes)
45 views16 pages

Algorithm Design & Analysis Course

Uploaded by

vtjdjango
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views16 pages

Algorithm Design & Analysis Course

Uploaded by

vtjdjango
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

COL 351:

Analysis and Design of Algorithms


Lecture 1
Grading Policy
1. Quizzes - 15%
(surprised / announced)

2. Exams - 35% + 40%

3. Class participation - 5% (Lec) + 5% (Tut)

Academic Honesty
Cheating or allowing anyone to copy in quizzes, exams would lead to penalty of one grade

Course webpage: https://web.iitd.ac.in/~keerti/Courses/COL351-2025.htm


COL 106 This Course

Arrays, Link lists, Stacks • Designing algorithms

• Different algorithm paradigms


Trees, Binary trees, AVL trees, KD trees
1. Greedy algorithms

2. Dynamic programming
How to use these data structures
3. Divide & Conquer
in computational problems?

• Queues are used in BFS • Hard Problems:


Problems which are unlikely to
• Stacks are used in DFS
have an ef cient solution.
• Priority queues are used in
Dijkstra’s algorithm
• How to prove that a problem is hard?
fi
Algorithm Paradigms

1. Divide and Conquer (Eg. Merge sort, quick


— Divide the problem into smaller problems sort, binary search)
— Solve the smaller problems
— Combine

2. Dynamic Programming
(Eg. insertion sort)
Reduce the problem on an input of size n into
problems of size n − 1, n − 2, n − 3,.. etc.

3. Greedy Strategy
(Eg. Selection sort)
Build solution greedily.
Divide and Conquer
Example: Merge Sort

4 2 10 5 16 3 9

L1 4 2 10 5 16 3 9 L2

Sorted L1 2 4 5 10 3 9 16 Sorted L2
Example: Merge Sort
MergeSort(L)
n = length(L); n
T(n) = 2 T( ) + O(n)
If n = 1 then Return; 2
A = new list of size n;
Set L1 = L[0,
2]
L2 = L[ 2 + 1, n − 1];
n n
and
MergeSort(L1);
MergeSort(L2);
Set x, y, pos = 0;

While (x < length(L1) or y < length(L2))


If (L1[x] ⩽ L2[y] and x < length(L1)) then
Set A[pos] = L1[x], and increment pos and x by 1;
Else
Set A[pos] = L2[x], and increment pos and y by 1;
Example: Merge Sort
n
Let T(n) = number of steps taken by the algorithm. Then, T(n) ⩽ 2 T( ) + cn
2

cn cn

T(n)
T(n/2) T(n/2) cn/2 cn/2

Sum in each level = cn


T(n/4) T(n/4) T(n/4) T(n/4)

Height = log2 n

So, T(n) = O(n log n)


Merge Sort - Unequal balance
Let T(n) = number of steps taken by the algorithm.

What if | L1 | = n/3, and | L2 | = 2n/3?

cn cn

T(n)
T(n/3) T(2n/3) cn/3 2cn/3

T(n/9) T(2n/9) T(2n/9) T(4n/9)


Sum in each level ⩽ cn

Max height = log1.5 n

So, T(n) = O(n log n)


Merge Sort - Unequal balance

Let T(n) = number of steps taken by the algorithm.

What if | L1 | = n , and | L2 | = n − n?
Dynamic Programming
Nth Fibonacci Number

De ned by recurrence relation F0 = 0, F1 = 1, and Fn = Fn−2 + Fn−1 for n > 1.

1. If n = 0,1 then return a binary list of containing (n)2;


2. A = FibNaive(n − 1);
3. B = FibNaive(n − 2);
4. Return BinarySum(A, B);

FibNaive(n)

T(n) = T(n − 2) + T(n − 1) + Time-to-add-two-large-numbers

n
Remark: T(n) is exponential here because T(n) ⩾ Fn ⩾ 2
fi
Nth Fibonacci Number - Improved algorithm

De ned by recurrence relation F0 = 0, F1 = 1, and Fn = Fn−2 + Fn−1 for n > 1.

1. Allocate an array A of size n to store references/pointers.


2. For i = 0,1 : Let A[i] store pointer to binary list of containing (i)2;
3. For i= 2 to n:
A[i] = BinarySum(A[i − 1], A[i − 2]);
4. Return A[n];

Fibonacci-Improved( n )

T(n) = n × Time-to-add-two-large-numbers = O(n 2) (Note : Fn ⩽ 4n)


fi
Greedy Algorithms
Selection Sort

Given: An array A storing n distinct integers.

1. Allocate an array B of size n to store solution.


2. Let B[0] store minimum element of array A;
3. For i= 1 to n − 1:
B[i] = Smallest element in A greater than equal to B[i − 1];
4. Return B;

Sort( A, n)

T(n) = O(n 2)
Homework: Course-prerequisite ordering

Given : A DAG (directed acyclic graph) corresponding to course-prerequisites for courses


you are keen in taking.

Find : A linear ordering of vertices of DAG (course-prerequisite graph) such that for every
directed edge (u, v), the vertex u comes before v in the ordering.

1 4

6 1 4 2 3 5 6

2 3

You might also like