20 01 2024
20 01 2024
=================
----------
| DAA |
=> Single source shortest path( Dijkstra’s algo, Bellman ford algo, Breadth First Traversal)
6). Hashing
7) Graph Traversals
Problem
Solution
(Algorithm)
ATN()
Read a,b
c:=a+b
print(c)
Properties of an algorithm:
Analysis of algorithms
2) . Design algorithm ( Divide and Conquer, Greedy, Dynamic Programming, Branch and Bound )
4). Test
5). Implementation
FOR LOOP
Statement(s)
Expression : any statement that evaluates to some value or a statement that has some value
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:
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 = 2k
i <=n
2k <= n
k <= log2 n
k = ⌊ log2 n ⌋ (floor)
n=100
Iterations: 1 2 3 4 5 6 7
i = 1 2 4 8 16 32 64
i<n
64 < 100
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
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 = 3k
i <=n
3k <= n
k <= log3 n
k = ⌊ log3 n ⌋ (floor)
i=1 ,2 ,3 i=3, 2, 1
⌊ log2 n ⌋+ 1
i=1
⌊ log2 n ⌋+ 1
n/2k=1
Date 21-01-2024
===============
Q 3:
#Independent Loops
printf ( “ Hai ” ) ;
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
print(“Hello!”)
}
(n/2 + 1) * (n/2) * ( ⌊ log2 n ⌋+ 1 )
Q 6:
#Dependent loop
for ( i = 1 ; i < = n ; i = i ++ )
print(“Hello!”)
#Dependent loop
for ( i = 1 ; i < = n ; i = i *2 )
print(“Hai ”)
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 ++ )
print(“Hai ”)
1 2 3 n
1+2+3+…+n=n(n+1)/2
n=4
#Dependent loop
print(“Hello! ”)
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(k=1;k<=n;k++ )
print(“Hello! ”)
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
print(“Hello! ”)
=(k+1)(n+1) – (3k+1 -1 )
_______
2
Knowledge Explore:
Q 1:
print(“Hello! ”)
}
Q 2:
print(“Hello! ”)
}
Q 3:
print(“Hello! ”)