0% found this document useful (0 votes)
133 views6 pages

Home-Work - 1: Submitted To: - Submitted By

The document discusses algorithm analysis and design homework questions. It includes solving a recurrence equation, comparing selection and insertion sort, providing examples of brute force approaches, discussing different forms of recurrence relations and issues in algorithm design such as problem description, data analysis, pseudocode, and modules. It also analyzes the steps in mathematically analyzing non-recursive algorithms such as identifying input size, basic operations, worst/average/best cases, setting up summations, and simplifying to determine computational complexity.

Uploaded by

girijesh89
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
133 views6 pages

Home-Work - 1: Submitted To: - Submitted By

The document discusses algorithm analysis and design homework questions. It includes solving a recurrence equation, comparing selection and insertion sort, providing examples of brute force approaches, discussing different forms of recurrence relations and issues in algorithm design such as problem description, data analysis, pseudocode, and modules. It also analyzes the steps in mathematically analyzing non-recursive algorithms such as identifying input size, basic operations, worst/average/best cases, setting up summations, and simplifying to determine computational complexity.

Uploaded by

girijesh89
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

HOME-WORK -1

ALGORITHM ANALYSIS & DESIGN

Submitted To: - Submitted


By:-
Miss Navdeep Kaur Salil Batra

B-Tech (H) M-
Tech (CSE)

Roll No:-
RA17M1B39

Section:-A17M1

Reg.No:-
7050070045
Part-A
Q1:- Solve the recurrence equation.
T (n) =2T (n-1) +n2^n+n2.

Ans:- T(n) + [T(n-1) + T(n-2) + ------------------- +T(2) + T(1)]

Summing all the right hand sides of the above equations:


[T(n-1) + T(n-2) + ------------------- +T(2) + T(1)] +[ (n+1) + (n) + (n-1) + ---- + 3
+ 2] + T(0)
Equating the two sides
T(n) = [ (n+1) + (n) + (n-1) + ---- + 3 + 2] + T(0)
= 1 + 2 + 3 + 4 + ----------+ n + (n+1)
= (n+1)(n+2)/2
= (n2 + 2n +2)/2
< n2/2
= O((n2)

Q2. Explore the various scenarios in which selection sort is


better than Insertion sort.
Ans:- 1) Insertion sort typically makes fewer comparisons
than selection sort, it requires more writes because the inner loop can
require shifting large sections of the sorted portion of the array. In general,
insertion sort will write to the array O(n2) times, whereas selection sort will
write only O(n) times. For this reason selection sort may be preferable in
cases where writing to memory is significantly more expensive than reading,
such as with EEPROM or flash memory.

2) Selection sort is preferable to insertion sort in terms of number of


writes (Θ(n) swaps versus Ο(n2) swaps), it almost always far exceeds (and
never beats) the number of writes thatcycle sort makes, as cycle sort is
theoretically optimal in the number of writes. This can be important if writes
are significantly more expensive than reads, such as
with EEPROM orFlash memory, where every write lessens the lifespan of the
memory.

3) It can be seen as an advantage for some real-time applications that


selection sort will perform identically regardless of the order of the array,
while insertion sort's running time can vary considerably
Q3:- Write any four examples for Brute Force Approach.
Ans:- Brute Force:-
Force:-A straightforward approach, usually based directly on
the problem’s statement and definitions of the concepts involved

Following are the four Examples:

1. Computing an (a > 0, n a nonnegative integer)

2. Computing n!

3. Multiplying two matrices

4. Searching for a key of a given value in a list

Example Explained:-

Brute force sorting algorithm:-


Selection Sort Scan the array to find its smallest element and swap it with
the first element. Then, starting with the second element, scan the
elements to the right of it to find the smallest among them and swap it
with the second elements. Generally, on pass i (0 ≤ i ≤ n-2), find the
smallest element in A[i..n-1] and swap it with A[i]:

A[0] ≤ . . . ≤ A[i-1] | A[i], . . . , A[min], . . ., A[n-1]


in their final positions
Example: 7 3 2 5
Part-B

Q4:- . Discuss the two different forms of recurrence


Ans:-1)INDUCTION with forward substitution.
2) ITERATING RECURSION

Q5:-Discuss the various issues pertaining to the algorithm


design
Ans:-Various Issues pertaining to the algorithm design:-

1) Precise description of the problem.

• In the business world this typically comes from the client.

• Often we need to help the client be precise!

• In research, the description is derived from hypotheses we hope


to verify.

• In the classroom, this comes from the instructor

not always precise enough?

2) Data analysis and creation of data descriptions.

What data is provided as part of a problem

instance? what form does it take?

● What data do we need to generate in order to

model the problem?


● What are the best data structures for this

application?

– best might mean minimum memory usage.

– best might mean leads to fastest algorithms.

3) Pseudo-code (general algorithm description)

Description of an algorithm in a mix of English

and programming type expressions.

● Generally a mathematical description of the steps

required to solve a problem.


● Some organizations develop formal pseudo-code

languages (consistent problem description

language withing the organization).

– code generators are possible.

– this is almost programming..

4) Modules

– scheme – the functions needed

– OOP – objects needed

– imperative lanuages: sequence of changes of state.

– declarative languages: collection of declarations

Q6:- . Analyze the various steps in mathematical analysis


of nonrecursive algorithms
Ans:- Steps in mathematical analysis of nonrecursive algorithms:

1) Decide on parameter n indicating input size

2) Identify algorithm’s basic operation

3)Determine worst, average, and best case for input of size n

" if the basic operation count depends not only on n


4) Set up summation for C(n) reflecting algorithm’s loop structure

" Express the number of times the algorithm’s basic operation is executed

5) Simplify summation using standard formulas and rules of sum

manipulation

" Find a closed-form formula for the count or, at the very least, establish its
order

of growth.

Example 1:-

Input size = n, the number of elements in the array


! Algorithm’s basic operation is “comparison”
" It is executed on each repetition of the loop
! Formula for the basic operation count:
" Sum is simply 1 repeated by n-1 times.
!
C(n) = 1
i=1
n"1
# = n "1$ %(n)
Algorithm MaxElement(A[0.. n - 1])
//Determines the value of the largest element in a given array
//Input: An array A[0.. n - 1] of real numbers
//Output: The value of the largest element in A
maxval ! A[0]
for i ! 1 to n - 1 do
if A[i] > maxval
maxval ! A[i]
return maxval

You might also like