Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 53

CSA06

Design and Analysis of Algorithms

Unit 1 – Tutorial 1

Saveetha School of Engineering


SIMATS, Chennai
Date : 22.04.2021
Day 4
Topics
S.No Topic Name
Day 1
1 Algorithm Basics
2
NAME OF PRESENTATION
Fundamentals of Algorithmic Problem Solving
Important Problem types
3
Day 2
4 Fundamentals of the Analysis of Company Name
Algorithm Efficiency
5 Analysis Frame work
6 Asymptotic Notations and Basic Efficiency Class
Day 3
7 Recurrence Equation
8 Substitution Method
9 Master’s Method
1. Algorithm Basics
•Algorithm
An algorithm is a sequence of unambiguous instructions for
solving a problem, i.e., for obtaining a required output for any
legitimate input in a finite amount of time.
Try This
1. What are the characteristics of the algorithm?

a. Input
b. Definiteness
c. Finiteness
d. All the above
Try This
2. ← operator used in algorithm for ____.

a. Assignment
b. Addition
c. Division
d. Multiplication
Try This
3. Programming constructs and assignment operators
are included in ____ section of an algorithm.

a. Comment
b. Head
c. Body
d. Input
2. Fundamentals of Algorithmic Problem Solving
Try This
4. Name the ways in specifying an algorithm?

a. Natural Language
b. C language
c. Flowchart
d. None of the above
Try This
5. ______is the first step in solving the problem
A. Understanding the Problem
B. Identify the Problem
C. Evaluate the Solution
D. None of these

6. ______is the last step in solving the problem


A. Understanding the Problem
B. Identify the Problem
C. Coding the algorithm
D. None of these
Try This
7. Following are called logical operators
A. +, -, *, /
B. <, >, <=, >=
C. AND, OR, NOT
D. \, MOD

8. The main measure for efficiency of an algorithm are-


A. Processor and Memory
B. Complexity and Capacity
C. Data and Space
D. Time and space
Try This
9. Examples of O(1) algorithms are______________.
A. Multiplying two numbers.
B. assigning some value to a variable
C. displaying some integer on console
D. All of the above

10. The worst case time complexity of linear search


is________.
A. O(1)
B. O(log n)
C. O(n)
D. O(n logn)
Try This
11. The below symbol represents ____ in a flowchart.

a. Decision
b. Transition
c. Input and Output
d. Stop State
Try This
12. Factors for analyzing the algorithm are _____.

a. Time efficiency
b. Space efficiency
c. Declaration in an algorithm
d. None of the above
3. Important Problem types

(i). Sorting
(ii). Searching
(iii). Graph problems
Try This
13. The complexity of the sorting algorithm measures the …… as a
function of the number n of items to be sorter.
A. average time
B. running time
C. average-case complexity
D. case-complexity

14. Finding an element, whether that is present in a list or not. It is called__


1. Sorting
2. Searching
3. Storing
4. none
Try This
15. Arranging data in a particular format is referred to as
_____.

a. Sorting
b. Searching
c. Shortest Path Algorithm
d. Graph Traversal
Try This
16. The worst-case occur in linear search algorithm
when …….

A. Item is somewhere in the middle of the array


B. Item is not in the array at all
C. Item is the last element in the array
D. Item is the last element in the array or item is not
there at all
4. Fundamentals of the Analysis of Algorithm
Efficiency
 Time efficiency (time complexity): indicates how fast an algorithm runs
 Space efficiency (space complexity): refers to the amount of memory
units required by the algorithm in addition to the space needed for its input and output
 Measuring an Input size - The time efficiency of an algorithm is typically as a
function of the input size
 Units for Measuring Running time
 Order of Growth
 Best-case, Average-case, Worst-case
Example 1
•Algorithm Sum(A,n)
•{
•S = 0; 1
•For i = 1 to n do n+1
•{
•S= s + A[i]; n
}
Return s; 1

}
Order of Growth
Best-case, Average-case, Worst-case

Sequential Search
Best-case, Average-case, Worst-case

Sequential Search
Best Case
k = 7, n = 5 [7,8,9,1,2]
Comparison = 1 time
Cbest(n) = 1
Worst Case
k=2, n=5 [7,8,9,1,2]
Comparison = 5 times – n times
Cworst(n) = n
Best-case, Average-case, Worst-case

Sequential Search
Average Case
k = 7, n = 5 [1,2,3,7,8]
P (0<=P<=1)
Cavg(n) = [1.p/n+2.p/n+….+i.p/n+….+n.p/n] + n.(1-p)
=p/n [1+2+….+n]+n(1-p)
= p/n . N(n+1)/2 + n(1-p)
Success : p=1  n+1/2
Unsuccess : p=0 : n
Asymptotic Analysis
3. Asymptotic Notation

Asymptotic order is concerned with how the running


time of an
algorithm increases with the size of the input, if input
increases
from small value to large values
1. Big-Oh notation (O)
2. Big-Omega notation (Ω)
3. Theta notation (θ)
4. Little-oh notation (o)
5. Little-omega notation (ω)
Try This
17. The data structure required for Breadth First Traversal on a graph is
___
1. Queue
2. Array
3. Stack
4. Tree

18. The time factor when determining the efficiency of algorithm is measured
by__
5. Counting the number of key operations
6. Counting the microseconds
7. Counting the number of statements
8. Counting the kilobytes of algorithm
Try This
19. In ___type of search the list is divided in to two parts.
1. Linear search
2. Binary search
3. random search
4. None

20. Asymptotic complexity of linear search with array in worst case is


5. O(1)
6. O(n)
7. O(n/2)
8. O(logn)
Big-Oh Notation (O)
Definition: A function t(n) is said to be in O(g(n)), denoted
as t(n) ϵ O(g(n)), if t(n) is bounded above by some
constant multiple of g(n) for all large n. i.e., if there exist
some positive constant c and some non-negative integer
n0 such that
O(g(n)) = {f(n); 0<=f(n)<=C(g(n)) for all n>=n0}, where C
and n0 are positive constants.
 f(n) is Big ‘O’ of g(n)  f(n)= O(g(n))
 it denotes the worst case complexity.
 Big-oh - Asymptotic upper bound on a
 function.
Big-Omega notation (Ω)
Definition: A function t(n) is said to be in Ω(g(n)),denoted as
t(n) ϵ Ω(g(n)), if t(n) is bounded below by some positive
constant multiple of g(n) for all large n. i.e., there exist
some positive constant c and some non-negative integer
n0. Such that
f(n)>=C(g(n), n>n0 , C>0, n0>=1
 it denotes the best case complexity.
 It represents the lower bound
of the resources required to
solve a problem.
Theta notation (θ)
Definition: A function t(n) is said to be in θ (g(n)), denoted
t(n) ϵ θ(g(n)), if t(n) is bounded both above and below by
some positive constant multiples of g(n) for all large n. i.e., if
there exist some positive constant c1 and c2 and some non-
negative integer n0 such that
C1g(n)<=f(n)<=C2g(n), where C1,C2>0 for n>=n0 and n0>=1
 f(n)=θ(g(n))
 it denotes the average case complexity
 f(n) is bounded by g(n) both
in upper as well as
in lower bounds
Example 1
Example 1
Try This
21. Longest running time of an algorithm can be represented
by ____ notation.
1. Big-oh
2. Big-omega
3. Big Theta
4. None of the above
Practice Problems
1. Find the Big oh notation for the following function:
F(n) = 2n+2 and g(n) = n2

2. Find the Big omega notation for the following function:


F(n) = 2n2+5 and g(n)=7n
7. Recurrence Equations

•When an algorithm contains a recursive call to itself , its


running time can be described by recurrence equation.
•For Example, the Worst Case Running Time T(n) of the
MERGE SORT Procedures is described by the recurrence.
Example
T(n)= T(n-1)+T(n-2)+2
Fib(n) = T(n-1)+T(n-2)
If (n<=1) 1
return 1 Recurrence Equation
Else
return fib(n-1)+fib(n-2)
T (n) = 1 if n<=1
T(n-1)+
T(n-2) + T(n-1)+T(n-2) if n>0
1
This 1 is for adding both
Recurrence Equations

METHODS FOR SOLVING RECURRENCE:

•Substitution Method – Backward & Forward


•Recursion Tree Method
•Master Method
Try This
22. In linear search with array, how many comparisons are needed in
average case?
1. 0
2. 1
3. n
4. n+1/2

23. In linear search with array, how many comparisons are needed in worst
case?
5. 0
6. 1
7. n
8. n/2
8. Substitution Method – Backward (Iteration) Method
Substitution Method – Backward (Iteration) Method
Example 2
Try This
24. If a key is found in a list that is called ___ type of search
1. Unsuccessful
2. successful
3. partial success
4. partial unsuccessful

25. In linear search with array, how many comparisons are needed in best
case?
5. 0
6. 1
7. n
8. n/2
Recursion Tree Method
•1. Recursion Tree Method is a pictorial representation of an
iteration method which is in the form of a tree where at each
level nodes are expanded.
•2. In general, we consider the second term in recurrence as
root.
•3. It is useful when the divide & Conquer algorithm is used.
•4. Expand the children.
•5. Add up each level.
•6. Sum up the levels.
Example 1
Example 1
Try This
26. Searching techniques are classified in to__ types
1. 2
2. 3
3. 4
4. none

27. The element that is going to be searched in a list is called ___


5. Key
6. item
7. table
8. file
9. Master’s Method
The problem is divided into number of sub-problems each of
size n/b and need time f(n) to combine the solution
The running time T(n) can be ,
Practice Problems
Solve the following recurrence equations using Masters
Method:
1. T(n) = 4T(n/2)+n
2. T(n) = 2T(n/2)+n2
3. T(n) = 4T(n/2)+n3
4. T(n) = 3T(2n/6)+n
5. T(n) = 8T(n/4)+n
6. T(n) = 16T(n/8)+n2
Try This
28. DFS is a ____ type of search
1. Just tree search
2. multi-way tree search
3. graph search
4. None

29. BFS is a ____ type of search


5. Just tree search
6. multi-way tree search
7. graph search
8. None
Try This
30. In the following type of searching key-comparisons are needed

1. Linear search
2. Non linear search
3. Address calculation search
4. A and B
Thank You

You might also like