0% found this document useful (0 votes)
18 views19 pages

20 01 2024

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

20 01 2024

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

Date 20--01-2024:

=================

----------

| DAA |

1). Analysing algorithms

=> Asymptotic notation [ O , Ω , Θ , o , ω ]

=> Recurrence relation [ substitution method , Tree method, Master’s theorem]

2). Divide and Conquer

=> Finding maxmin

=> Binary Search

=> Merge Sort

=> Quick Sort

=> Strassen’s matrix multiplication

=> Selection Procedure

=> Finding Inversions

=> Continuous maximum sub array sum

3). Greedy Technique

=> Job Sequencing with dead lines

=> Knapsack problem

=> Huffman coding

=> Optimal merge patterns

=> Minimum Cost Spanning Tree (Prims, Kruskal)

=> Single source shortest path( Dijkstra’s algo, Bellman ford algo, Breadth First Traversal)

4). Dynamic Programming

=> 0/1 Knapsack problem

=> Longest common subsequence (LCS)

=> Fibonacci series


=> Matrix chain multiplication

=> Sum of subsets problem

=> All pairs shortest path

=> Optimal cost BST

5). Sorting Techniques

=> Heap Sort

=> Bubble Sort

=> Selection Sort

=> Insertion Sort

=> Counting Sort

=> Radix Sort

6). Hashing

7) Graph Traversals

8). Tree Traversals


INTRODUCTION

Algorithm: It is a combination of sequence of finite steps to solve a problem

Problem

Solution

(Algorithm)

ATN()

Read a,b

c:=a+b

print(c)

Properties of an algorithm:

1) It should terminate after finite time


2) It should produce at least one output
3) It should take zero or more inputs
4) It should be deterministic

Analysis of algorithms

1). Time Complexity [ CPU – Time ]

2). Space Complexity [Main Memory]


Time Complexity

Apostiary analysis Apriori analysis


T(A) = C(A) + R(A) Primitive operations
| |
compiler cpu
s/w H/W ( Type of hardware)

Exact answer( relative answer) Approximate analysis ( absolute analysis)


Dependent on s/w and h/w It is independent of s/w and h/w

Steps required to design algorithm:

1). Problem definition

2) . Design algorithm ( Divide and Conquer, Greedy, Dynamic Programming, Branch and Bound )

3). Draw the flow chart

4). Test

5). Implementation
FOR LOOP

for ( exp1 ; expr2 ;exp3 )

Statement(s)

Expression : any statement that evaluates to some value or a statement that has some value

1 => value of the expression is 1

10.46 => value of the expression is 10.46

i =1 => The value that is assigned to the variable is the value of the expression

printf (“Hello!”) => returns number of characters printed on the screen successfully

Knowledge Explore:

for ( i=1 ; printf(“hello!”) ; printf(“welcome”) )

printf(“Hai”);

Infinite loop
Question : How many times printf statement gets executed assume that n > = 1 . n is very large

Q 1:
for ( i = 1 ; i < = n ; i = i * 2 )

printf ( “ Hello ! ” ) ;

Ans:

For what values of i the loop is executed then we can count the iterations

i=1 = 20 = > 1st iteration

i=1 * 2 =2 = 21 => 2nd iteration

i = 2 * 2=4 = 22 => 3rd iteration

i = 4 * 2 =8 = 23 => 4th iteration

i = 8 * 2 =16 = 24 => 5th iteration

what is the last value of i for which the loop runs

i = 2k

i = 2k => (K+1)th iteration

loop executes (k+1) times

i <=n

2k <= n

log2 2k <= log2 n

k <= log2 n

k = ⌊ log2 n ⌋ (floor)

Loop executes ⌊ log2 n ⌋+ 1 times


Example 1:

n=100

Iterations: 1 2 3 4 5 6 7

i = 1 2 4 8 16 32 64

i<n

64 < 100

⌊ log2 100 ⌋+ 1 = ⌊ 6.643856 ⌋ + 1 = 6 + 1=7

Example 2:

n=128

Iterations: 1 2 3 4 5 6 7 8

i = 1 2 4 8 16 32 64 128

i=n

128=128

⌊ log2 128 ⌋+ 1 = ⌊ 7 ⌋ + 1 = 7 + 1=8


Q 2:

for ( i = 1 ; i < = n ; i = i * 3 )

printf ( “ Hello ! ” ) ;

Ans:

For what values of i the loop is executed then we can count the iterations

i=1 = 30 = > 1st iteration

i=1 * 3 =3 = 31 => 2nd iteration

i = 3 * 3 =9 = 32 => 3rd iteration

i = 9 * 3 =27 = 33 => 4th iteration

i = 27 * 3 =81 = 34 => 5th iteration

what is the last value of i for which the loop runs

i = 3k

i = 3k => (K+1)th iteration

loop executes (k+1) times

i <=n

3k <= n

log3 3k <= log3 n

k <= log3 n

k = ⌊ log3 n ⌋ (floor)

Loop executes ⌊ log3 n ⌋+ 1 times


for ( i=1 ; i <= 3 ; i++) for ( i=3 ; i >=1 ; i--)
{ {
code code
} }

i=1 ,2 ,3 i=3, 2, 1

3 times loop runs 3 times loop runs

for ( i=1 ; i <= n ; i = i*2 ) for ( i=n; i >=1 ; i = i /2)


{ {
code code
} }

i =20 21 22 23…..2k i=n/1 n/2 n/4 n/8 …… n/2k

⌊ log2 n ⌋+ 1
i=1

⌊ log2 n ⌋+ 1
n/2k=1

Date 21-01-2024

===============

Q 3:

#Independent Loops

for ( i = 1 ; i < = n ; i = i ++)

for ( j = 1 ; j <=n ; j= j*2)

printf ( “ Hai ” ) ;

i=1 i=2 …………………………………..i=n

⌊ log2 n ⌋+ 1 ⌊ log2 n ⌋+ 1 ⌊ log2 n ⌋+ 1

n * (⌊ log2 n ⌋+ 1 )
Q 4:

for ( i = 1 ; i < = n ; i = i + 2 )
for
{ (i=1;i<=n;i=i*3)
{ for ( j=1 ; j<=n ; j++ )

{printf ( “ Hello ! ” ) ;
} printf ( “ Hello ! ” ) ;

⌈ n/2 ⌉ * n

If n is odd, ,

n=9

i=1 3 5 7 9 # 5 iterations

n=12

i=1 3 5 7 9 11 # 6 iterations

Q 5:

//Assume n is even

for ( i = n/2 ; i < = n ; i = i ++ ) ------------------------n/2 + 1

for ( j=1; j<=n/2; j++ ) -------------------- n/2

for ( k=1 ; k<=n; k=k*2) ----------------⌊ log2 n ⌋+ 1

print(“Hello!”)

}
(n/2 + 1) * (n/2) * ( ⌊ log2 n ⌋+ 1 )

Q 6:

#Dependent loop

for ( i = 1 ; i < = n ; i = i ++ )

for ( j=i; j<=3*i ; j++ )

print(“Hello!”)

i=1 i=2 i=3…………………………………………i=n

j=1,2,3 j=2 to 6 j=3 to 9 j=n to 3n

3 6-2+1=5 9-3+1=7 3n-n+1=2n+1

3+5+7+………... +(2n+1) in A.P

n/2 * (3+2n+1)= n(n+2)


Q 7:

#Dependent loop

for ( i = 1 ; i < = n ; i = i *2 )

for ( j=1; j<=i ; j++ )

print(“Hai ”)

i=1 i=2 i=4 i=8 ………………………………………………….i=2k

j=1 j=1,2 j=1,2,3,4 j=1,2,3…8 j=1,2,3…..,2k

1 2 4 8 2k

1+2+22+22+23+……..+2k

2k+1 - 1

=2⌊ log2 n ⌋+ 1 - 1
Q 8:

#Dependent loop

for ( i = 1 ; i < = n ; i = i ++ )

for ( j=1; j<=i ; j++ )

print(“Hai ”)

i=1 i=2 i=3 ……………………………… i=n

j=1 j=1,2 j=1,2,3 j=1,2,3…n

1 2 3 n

1+2+3+…+n=n(n+1)/2

n=4

i=1 (j=1) 1 time

i=2 (j=1,2) 2 time

i=3 (j=1,2,3) 3 times

i=4 (j=1,2,3,4) 4 times


Q 9:

#Dependent loop

for ( i = 1 ; i < = n ; i = i*2 )

for ( j=1; j<=i ; j=j*2 )

print(“Hello! ”)

i=1 i=2 i=4 i=8 i=16……………………………………….i=2k

j=1 j=1 , 2 j=1,2,4 j=1,2,4,8 j=1,2,4,8,16 j=1,2,4,8,…. ,2k

1 2 3 4 5 (k+1)

1+2+3+……………….+(k+1)

k∗(k + 1)
= +1
2
( ⌊ log 2 n)(⌊ log 2 n ⌋ +1)
= +1
2
Q 10:

#Dependent loop

for ( i = 1 ; i < = n ; i = i++)

for ( j=1; j<=i*i; j++ )

for(k=1;k<=n;k++ )

print(“Hello! ”)

i=1 i=2 i=3 i=4 …………………………i=n

j=1 j=1,2,3,4 j=1,2,3….9 j=1,2,,3……16 j=1,2,3…..n2

k=1,2…n 4n 9n 16n n2 .n

n+4n+9n+16n+……+ n2 .n

n(1+4+9+16+….+ n2)

n.n(n+1)(2n+1)

_____________

6
Q 11

#Dependent loop

for ( i = 1 ; i < = n ; i = i*3)

for ( j= i; j<=n; j++ )

print(“Hello! ”)

i=1 i=3 i=9 …………………………….i=3k

j=1,2…,n j=3ton j=9ton j= 3k to n

n+1-1 n-3+1 n-9+1 n-3k+1

n+1-30 + n+1-31 + n+1-32+ n+1-33 +…………… + n+1-3k

(k+1)(n+1) – (30 + 31 + 32+ 33 +…………… + 3k)

=(k+1)(n+1) – (3k+1 -1 )

_______

2
Knowledge Explore:

Q 1:

for ( i = 0 ; i < = n-1 ; i ++)

for ( j= 2; j<=i+1; j++ )

for(k=j+1; k<=i+j; k++)

print(“Hello! ”)

}
Q 2:

for ( i = 1 ; i < = n ; i ++)

for ( j= 1; j<=i; j++ )

for(k=j ; k<=i+j; k++)

print(“Hello! ”)

}
Q 3:

for ( i = 1 ; i < = n ; i ++)

for ( j= i+1; j<=n; j++ )

for(k=j ; k<=j ; k++)

print(“Hello! ”)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy