1 of 43

Design and Analysis of Algorithms

L T P : 3 0 3

Total Credits : 4.5

Pre-requisites : Data Structures

2 of 43

Course Outcomes

  • Understand the fundamental principles of algorithm design.
  • Analyze algorithm efficiency using asymptotic notations.
  • Develop problem-solving skills through algorithmic thinking.
  • Learn classic algorithm design paradigms and apply them to real-world problems.
  • Understand computational intractability by exploring NP-completeness, and develop the ability to classify problems based on their complexity classes (P, NP, NP-Complete, NP-Hard).

2

3 of 43

Text books/ Reference Books

Text Books:

  • Cormen, Thomas H., Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to algorithms. MIT press, 2022.
  • Kleinberg, Jon, and Eva Tardos. Algorithm design. Pearson Education India, 2006.
  • Ellis Horowitz, Sartaj Sahani, Sanguthevar Rajashekaran, “Fundamentals of Computer Algorithms”, Orient Black Swan, 2nd Edition (2008).
  • A .V. Aho, J . E . Hopcroft, J . D . Ulman “The Design & Analysis of Computer Algorithms”, Addison Wesley, 1998.

3

4 of 43

Syllabus of the Course

1. Introduction to Algorithms

  • Algorithms vs Programs, Characteristics of algorithms, Analyzing algorithms: Time and space complexity, Asymptotic notation: Big-O, Big-Ω, Big-Θ, Small o, small- omega Recurrences and solution methods -Substitution, Recursion Tree, Master Theorem.

2. Divide and Conquer Approaches

  • General paradigm and recurrence relation, Binary Search, Merge Sort, Quick Sort, Matrix Multiplication (Strassen's Algorithm)

3. Greedy Algorithms

  • Greedy choice property and optimal substructure, Minimum Spanning Tree, Prim’s and Kruskal’s Algorithms for finding minimum spanning tress, Shortest Path problem, Dijkstra’s Algorithm to compute Shortest Path, Huffman Coding for data compression, Activity selection problem, Knapsack Problem

4

5 of 43

Syllabus of the Course (con…)

4. Dynamic Programming

  • Principle of Optimality and the distinction between bottom-up and top-down approaches in dynamic programming. Matrix Chain Multiplication, Longest Common Subsequence, 0/1 Knapsack Problem and the Floyd-Warshall algorithm for all-pairs shortest paths.

5. Graph Algorithms

  • Graph representations including adjacency lists and matrices. Breadth-first and depth-first traversals, topological sorting of directed acyclic graphs, identification of strongly connected components.

6. Advanced Topics

  • The theory of computational complexity, P, NP, NP-Complete, and NP-Hard classes. Polynomial-time reductions, prove that problems are NP-Complete using examples like SAT, 3-CNF-SAT.

5

6 of 43

Evaluation Scheme (Theory + Practical)

6

Particular

Marks Weightage (%)

Mid Term

20

End Term

40

Continuous Evaluation through Class Tests/Assignments/ Online Quiz

20

Lab Work

20

7 of 43

Motivation

We use algorithms all the time:

  • School arithmetic's: Long divisions addition with carry, square roots……..
  • Arranging books alphabetically on shelf.
  • Finding quickest route to the university.

7

8 of 43

What is an algorithm?

  • An algorithm is a step by step unambiguous instructions to solve a given problem in a finite amount of time.
  • Tool for solving a well specified computational problem.
  • Describes a specific computational procedure for achieving the desired output from a given input.

8

9 of 43

Algorithm to reach SAU

9

10 of 43

Algorithm

1. Go straight.

2. Take left turn towards Gowshala Road.

3. You will reach SAU.

Take 3rd left turn

(No ambiguity)

10

Which Left???

11 of 43

Characteristics of a good algorithm

11

Unambiguous

Effective/Correct

Termination

Defined set of inputs and outputs

12 of 43

Properties of Algorithms

  • Each Step must be a basic operation
  • It should terminate in a finite amount of time
  • It should produce atleast one output
  • It should take 0 or more input
  • Corresponding to a given set of Input we must have fixed Output
  • Platform independent
  • Every Statement must be unambiguous

12

13 of 43

Steps Required to Develop an Algorithm

  • Problem Definition - Identify & define the problem properly
  • Develop Model - Make the DFD or any other model to describe the problem graphically
  • Design - Identify steps in algorithm and write them in proper sequence
  • Testing - Try to find errors in the algorithm
  • Analysis - Measure its Time and Space Complexity
  • Optimize - Try to minimize the time and space complexity of the algorithm
  • Implement - Implement in any programming language

13

14 of 43

Performance Analysis of algorithm

14

A Priori

A Posteriori

15 of 43

A Priori vs A Posteriori analysis

15

A Priori

    • Analyzing algorithms before implementation
    • A priori analysis is an absolute analysis.
    • It is independent of language of compiler and types of hardware
    • It is the reason we use asymptotic notations to determine time and space complexity as they change from computer to computer but asymptotically they are same.

A Posteriori

    • Analyzing Algorithm after implementation
    • It is dependent on Programming language, on the CPU used, on the OS used etc.
    • Posteriori analysis is a relative analysis.
    • In industries, we cannot do a posteriori analysis. As software is generally made for anonymous users who may run it on different systems

16 of 43

Analysis of algorithm

  •  Determination of the amount of time and space resources required to execute an algorithm.

16

17 of 43

Time & Space Complexity

  • Time Complexity of an algorithm quantifies the amount of time [CPU time] taken by the algorithm to run as a function of Input Size
  • Space complexity of an algorithm quantifies the amount of main memory space taken by the algorithm to run as a function of Input Size
  • Independent of the programming language selected for the implementation of the algorithm.

17

18 of 43

Importance of analysing algorithms

  • Need to recognize limitations of various algorithms for solving a problem.
  • Need to understand relationship between problem size and running time.
  • Need to learn how to analyze an algorithm's running time without coding it.
  • Need to learn techniques for writing more efficient code.

18

19 of 43

Which algorithm is better?

There can be many algorithms to solve a problem….

All the algorithms are correct….

But which is the best??

  • Measure the running time (number of operations needed).
  • Measure the amount of memory used.

The running time of the algorithms increase as the size of the input increases.

19

20 of 43

Asymptotic Notations

  • Suppose we are considering two algorithms, A and B, for solving a given problem.

Running times of algorithm A = TA(n)

Running times of algorithm B = TB(n),

where n is a measure of the problem size.

  • If we know the problem size n in advance, then

if TA(n)< TB(n) Algorithm A is better than Algorithm B.

Unfortunately, we usually don't know the problem size beforehand.

20

21 of 43

Asymptotic Notations

  • Asymptotic notations allow us to analyze an algorithm’s running time by identifying its behavior as the input size for the algorithm increases.
  • Asymptotic – refers to study of function f as n approaches infinity

(Asymptotes : Tangents to the Curve at Infinity)

  • Analysis of Algorithms is generally done for a very large Input Size
    • To sort 10-20 nos. we would not write a sorting program

  • Apriori analysis gives time needed by the algorithm as a function of Input Size.

21

22 of 43

Which is better

Two algorithms: A1 A2

Time needed by each: n 100000n

22

23 of 43

Which is better

Two algorithms: A1 A2

Time needed by each: n 100000n

Mathematically A2 needs more time but Asymptotically they are same

23

24 of 43

Which is better

Two algorithms: A1 A2

Time needed by each: n2 100000n

24

25 of 43

Which is better

Two algorithms: A1 A2

Time needed by each: n2 10000n

Mathematically A2 needs more time but Asymptotically they are same

Till 9999 I/Ps they A2 needs more time, for 10000 I/Ps both need same time, beyond 10000 I/Ps A1 needs more time

25

26 of 43

Asymptotic Analysis

  • Constants do not matter
  • Take dominating term

Reasons

  • Machine Independent
  • We work for larger values of n

Example: f(n) = n2+4n + 20

n2 is the dominant term and the term 4n + 20 becomes insignificant as n grows larger.

We need ways to represent time/space complexity of algorithms which is independent of Machine Specifications. These ways are Asymptotic Notations

26

Asymptotically Same

n 10000n 10000000n

n n/2 n/10000

Note

n2 >>> 100000n

(1/10000) n2 >>> 100000n

27 of 43

Big Oh Notation(O)

Definition:

f(n) = O(g(n))

if there are two positive constants c and n0

such that:

f(n) ≤ c g(n) for all n ≥ n0

g(n) is an asymptotic upper bound for f(n),

which means the growth rate of f(n) is no more

than the growth rate of g(n).

27

If f(n) = 2n2

g(n) is O(n3)

28 of 43

Big Oh Notation(O)

  • To Prove f(n) = O(g(n))
  • Simply prove f(n) <= g(n)
  • If f(n)<g(n) then straightway f(n)=O(g(n)
  • Can take the help of c (c>0)
    • s.t. f(n) <= cg(n)
  • And starting from which value of n? This is n0
    • For some initial values f(n) > c g(n)

  • Example: f(n)=n, g(n)=n+2

28

29 of 43

Questions on Big Oh Notation(O)

  1. f(n) = n g(n) = 3n+2
  2. f(n) = n2 g(n) = 5n2+6n+2
  3. f(n) = 1000n g(n) = 5n2

f(n) = O(g(n)) ???

g(n) = O(f(n)) ???

29

30 of 43

Questions on Big Oh Notation(O)

  1. f(n) = n g(n) = 3n+2
  2. f(n) = n2 g(n) = 5n2+6n+2
  3. f(n) = 1000n g(n) = 5n2

1. f(n) = O(g(n)) & g(n) = O(f(n))

2. f(n) = O(g(n)) & g(n) = O(f(n))

3. f(n) = O(g(n)) & g(n) =! O(f(n))

30

31 of 43

  • If f(n) is O(n)
  • Then - f(n) is O(n2) ??
  • Then - f(n) is O(n3) ??

31

32 of 43

  • If f(n) is O(n)
    • then - f(n) is O(n2) ??
    • then - f(n) is O(n3) ??

  • If f(n) <= cn obviously f(n)<=cn2, cn3,….

  • O(n) represents infinite functions

3n+2 = O(n)

3n+5 = O(n)

2n+3 = O(n) ….

  • Always take dominating term for big O

32

f(n)=3n+2

f(n)=O(n)

f(n)=O(nlogn)

f(n)=O(n2)

f(n)=O(n3)

logn = O(n)

loglogn = O(n)

……….

O(n2) represents infinite functions

5n2 + 6

8n2 + 5

n

nlogn

logn ……..

33 of 43

Q. If f(n) = n2 + 6n + 2; then f(n) is …….

Which of the following options is true

    • O(n2 ) ✓
    • O(n3 ) ✓
    • O(n4 ) ✓
    • O(n ) ✗
    • O(nlogn ) ✗
    • O(n2 logn) ✓

33

34 of 43

Most Common Big Oh Functions

  • Constant O(1)
  • Logarithmic O(log n)
  • Linear O(n)
  • Quadratic O(n2)
  • Polynomial O(nk) k is a constant
  • Exponential O(2n)
  • Exponential O(an), a>1 is a constant

34

35 of 43

 

Definition:

f(n) = Ω(g(n)

if there exist positive constants c and n0

such that:

0 < c g(n) < f(n) for all n≥ n0

g(n) is asymptotic lower bound for f(n), which

means the growth of f(n) is no lesser than the

growth rate of g(n).

35

36 of 43

 

  •  

36

37 of 43

 

  •  

37

38 of 43

Questions on Big Oh Notation(O)

  •  

38

39 of 43

  •  

39

 

 

40 of 43

  •  

40

41 of 43

  •  

41

42 of 43

Theta Notation(Ɵ)

Definition:

f(n) = Ɵ (g(n))

if there are two positive constants c and n0

such that:

0 ≤ c1 g(n) ≤ f(n) ≤ c2 g(n)

42

This is known as asymptotic tight bound. For reasonably large values of n, the function f(n) is within the range of constant multiples of g(n). f(n) is bounded below as well as above.

43 of 43

Example Theta Notation(Ɵ)

1. f(n) = 3n+2 g(n) = n

2. f(n) = 5n2+6n+2 g(n) = n2

3. f(n) = 1000n g(n) = 5n2

f(n) = Ɵ(g(n) ??

g(n) = Ɵ(f(n)) ??

43