Algorithm Short Notes38387373md

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Algorithm Short Notes

Algorithm

1 ASYMPTOTIC
NOTATION

1.1 Introduction of Course

1.2 Algorithm Concept and Life Cycle Steps


1.2.1 Algorithm
• An Algorithm consists finite number of steps to solve any problem.
• Every step involves some operations and each operation must be definite and effective.
Algorithm

1.2.2 Life Cycle Steps

1.3 Needs of Analysis

In performance comparison comparing different algorithms for optimal solution.


1.3.1 Time Complexity
Time complexity of an algorithm quantifies the amount of time taken by an algorithm to run as a function of the input size.

1.3.2 Space Complexity


Space complexity of an algorithm quantifies the amount of space or memory taken by an algorithm to run as a function of input
size.

Note:
To find the time complexity of an algorithm, find the loops and also consider larger loops.
Space complexity is dependent on two things input size and some extra space (stack space link, space list etc).
Algorithm

1.4 Methodology of Analysis

1.5 Types of Analysis

Worst Case
The input class for which the algorithm does maximum work and hence, take maximum time.

Best Case
The input class for which the algorithm does minimum work hence, take minimum time.

Average Case
Average case can be calculated form best case to worst case.

1.6 Asymptotic Notations


Suppose, T(n) be a function of time for any algorithm.
Algorithm

1.7 Types of Asymptotic Notations

1.7.1 Big O – Notation


Two Functions f (n), g (n)
f (n) = O (g (n))
When the growth of g(n) is same or higher than f (n) like a  b
Example:
f (n) = 3n +10, g(n) = n2 + 2n + 5
f (n) = O(g (n))

1.7.2 Ω - Notation
f (n) = (g (n))
 f (n)  C  g (n) (a  b)

Example: 3n = (2n )

1.7.3  - Notation
If f(n) ≤ g(n)
And
f(n) ≥ g(n)

f(n) = g(n)
∴ f(n) = (g(n))

Example:
f(n) = 2n2, g(n) = n+10
f(n) > g(n) here
so, f(n) = Ω(g(n)) or g(n)=O(f(n))
Algorithm

1.7.4. Properties with respect to asymptotic notations

Reflexive Symmetric Transitive Transpose symmetric


Big oh (O) 🗸 × 🗸 🗸
Big omega () 🗸 × 🗸 🗸
Theta () 🗸 🗸 🗸 ×
Small oh (o) × × 🗸 🗸
Small omega () × × 🗸 🗸

Example 1. Consider the following function


n
f (n) =  p = q
p =1
Which of the following is/are true for ‘q’
(a) (n4) (b) (n5) (c) O(n5) (d) (n3)
Solution: (a, c, d)
n
f (n) =  P3
P=1
= 13 + 23 + 33 + 43 ................. + n3
  n +1
2
=  n 
  2 
= O(n4) or  (n4)
=  (n4)
Example 2. Consider the following functions:
n
f (n) =  P½ = q
P=1
Find the value of q in terms of asymptotic notation.
n
Solution: f (n) =  P½
P=1

= 1+ (2 )½ + (3)½ + .......
2  n 3 2 −1
=
3  

= 2n 2 − 2
3

3 3
1.5
= O(n )
= O(n n)
Example 3. Arrange the following functions in increasing order.
f1 = nlogn, f2 = n, f3 = 2n , f4 = 3n , f5 = n!, f6 = nn , f7 = log n, f8 =100nlogn
→ f7  f 2  fl = f8  f3  f 4  f5  f6
Algorithm

Example 4. Arrange the following functions in increasing order.


f1 =10, f 2 = n, f3 = log log n, f 4 = (log n)2 , f5 = n2
f6 = n log n, f7 = n!, f8 = 2n , f9 = nn , f10 = n2 log n
→ f1  f3  f 4  f 2  f6  f5  f10  f8  f7  f9

Example 5. Arrange the following functions in increasing order.


f1 = log log n f9 = n log log n
f 2 = log n f10 = n2 log n
f3 = (log n)2 f11 = n3
f4 = log n f12 = 2n
f5 = n1/10 f13 = en
f6 = n f14 = n!
f 7 = n2 f15 = nn
f8 = n log n f16 = n3/2
f1 < f4 < f2 < f3 < f5 < f6 < f9 < f8 < f16 < f7 < f10 < f11 < f12 < f13 < f14 < f15
alogb c clogb a
2log2 n nlog2 2 = n

Example 6. Arrange the following functions in increasing order.


f1 = n! , f 2 = nn
f1 = n  (n − 1)(n − 2)  ...  3  2  1
f 2 = n  n  n  n  ...  n  n  n
f 2  f1
 f1 = O( f 2 )
2n  3n  4n  n! nn

Question.
Which of following is TRUE?
(1) 2log2 n = O(n 2 ) TRUE
(2) n2  23log2 n = O(n5 ) TRUE
(3) 2n = O(22n ) TRUE
(4) log n = O(log log n) FALSE
(5) log log n = O(n log n) TRUE
Solution:
(1) 2log2 n = O(n 2 )
= n log 2 2
=n
Algorithm

= n = O(n2 )
(2) n2  23log2 n = O(n5 )
= n 2  n 3 log 2 2
= n 2  n3
= n5
= n5 = O(n5 )
(3) 2n = 22n
2n = 2n.2n
2n  22n
2n = O(22n ) True

(4) log n  loglog n


log n  O(log n)
False
(5) log log n  nlog n
loglog n = O(nlog n)
True

1.8. Analysis of an Algorithm

Algorithms

Without Loop Interactive Algorithm Recursive Algorithm

1.8.1 Without loop

Example: int fun (in + n)


{
return n*(n+1)/2;
}

Solution.
Here 1 multiply, 1 division, 1 addition
⸫ O (1) [no loops, no recursion]

1.8.2. Iterative Algorithm Analysis


Example 1:
for (i =1; i ≤ n; i=i*2)
printf(“Sushil”)
Algorithm

Solution.
i=1, 2, 22, 23 ..., 2k

2k ≤ n
k log 2 ≤ log n
k ≤ log n
log 2
⸫ k ≤ log 2 n
k = log2 n
So, this will execute log2 n+1 time and Complexity O ( log 2 n )

Example 2:
For (i=1; i ≤ n; i=i*3)
printf(“Aaveg”);

Solution.
So, this will execute log3 n +1 time and complexity O ( log3 n )
➢ i = 1→2→4→8→16→...→n
i = n→n/2→n/4→n/8→...1

Example 3:
for (i = 1; i ≤ n; i++)
{
for (j=1; j ≤ 10; j++)
{
printf(“Dhananjay”);
}
}

Solution.
This will execute 10⸳n times and complexity O(n)

Example 4:
for (i = 1; i <= n; i = i*3)
for (j = 1; j ≤ n; j++)
printf(“Prapti”);
Solution.
( )
Total n log3 n +1 time execute and Complexity = O ( n log3 n )
Algorithm

1.8.3. Recursive Algorithm Analysis


Example 1:
void fun (in + n) T(n)
{
if (n > 0) 1 compare; C1 time
{
if (“% d”, n); C2 time
fun (n - 1); T(n-1)
}
}
Let T(n) be the Complexity time taken by algo for n size i/p
Solution.
T(n) = C1+C2+T(n-1)
T(n) =T(n-1) + C n>0

T (0) = C Constant

T(n) = C; n=0
T(n) = T (n - 1) + C; n > 0

Example 2:
void fun (in + n) T (n)
{
if (n > 0) C1 time
{
for (i = 1; i <= n; i + 1) n time
printf(“Hello”);
fun (n - 1); T (n - 1)
}
}
Solution.
T(n) = C1 + n - 1 + T (n - 1)
= T (n - 1) + n n>0
T (0) = C n=0

Example 3:
void fun (in + n) T(n)
{
if (n > 0) C1
{
for (i = 1; i < = n; i = i*2) log2 n
Algorithm

printf(“Divyajyoti”);
fun (n - 1); T (n - 1)
}
}

Solution. T (n) = T (n - 1) + O ( log 2 n ); n > 0


or
T (n) = T (n - 1) + log2 n
T (0) = C or n = 0
T (0) = O (1)

1.9 Solving Recurrence Relation


1.9.1 Substitution Method
Example: (1)
T (n) = T (n - 1) + C
T (1) = C

n size on problem n – 1 size x1 convert them


T (n) = T (n -1) + C

[ T (n - 2) + C] + C
T (n) = T (n - 2) + 2C

= T (n - 3) +3C
T (n) = T (n - k) + kC
⸫n–k=1
T (n) = T (1) + (n - 1) C
= C + (n - 1) C
T (n) = O (n)

Example (2)

T (n) = T (n - 1) + C⸳n
T (1) = C
Solution.
⸫ T (n) = T (n - 1) + C⸳n
= [T (n - 2) + C⸳(n – 1)] + C⸳n
= [T (n - 3) + C⸳(n – 2)] + C (n - 1) + C⸳n
= T (n - 3) + (n - 2)⸳C + (n - 1)⸳C + n⸳C
= T (n - k) + C (n – k +1) + C (n – k + 2) + ... + C (n – k + k)
⸫ n – k =1
T (n) = T (1) + T (2) + C (3) + C (4) + ... + C (n - 1) + C (n)
= C + C (2) + (3) C + 4 (C) + ... + (n - 1) C + (n)⸳C
= C [1 + 2 + 3 + ... + n]
Algorithm

(n + 1)
= C⸳n
2
2
= O (n )

Example (3)

T (n) = T (n/2) + C
T (1) = 1

Solution.

T (n) = T (n/2) + C

= [T (n/22) + C] + C
= T (n/4) + 2C

= T (n/23) + 3C
T (n) = T (n/2k) + kC
= (n/2k) = 2
T (n) = T (2) + ( log 2 n - 1) C

= 1 + ( log 2 n - 1) C
= O (log n)

Example (4)

T (1) = 1

T (n)= 2T (n/2) + C
  n 
Solution. T(n) = 2 2T   + C + C
 2 
2

 n 
= 22 T  2  + 22 C + C
2 

2 n 
= 2  2T  3  + C + 2C + C
 2  

 n
= 23 T  3  + 22 C + 2C + C
2 

 n  + 2k − 1 C + 2k − 2 C + ... + 21  C + C
= 2k T  k 
2 
Algorithm

n
k
= 1  n = 2k
2
→ T (n) = nT (1) + 2k - 1⸳ C + 2k - 2⸳ C + ... + 2C + C
= 2k + 2k - 1⸳ C + 2k – 2 + ... + 2C + C
= 2k + C (2k-1 + 2k-2 + ... + 21 + 20)
(2k −1)
= 2k + C
2 −1
= 2k + C (2k - 1)
= 2k + 2k⸳ C – C
= n⸳C
= O (n)

1.9.2 Master’s Method

T (n) = aT  n  + Θ (nk (log n) p)


b
a ≥ 1, b > 1, k ≥ 0, p = real number

If a > bk or log b a  k

(
T (n) = Θ nlogb a )

Question 1. T (n) = 2T  n  + (n)0log n


 2
Solution. a = 2, b = 2, k = 0
a > bk; 2 > 20; 2 > 1
T (n) = Θ (n)

Question 2. T (n) = 2T  n  + n
2
Solution. a = 2, b = 2, k = 1, p = 0
T(n) = Θ (n ⸳log n)

Question 3. T(n)=2 T  n  + nlog n


2

Solution. a = 2, b = 2, k = 1, p = 1

⸫ T (n) = Θ( n (log n)2)

(b) If p < 0 then T (n)


T (n) = O (nk)
Algorithm

T(n) = T   + C
Question 4. n
2
Solution.
T(n) = Θ (n2log n)

1.9.3. Recursive Tree

T(n) = 2T   + C
n
(1)
2
T (1) = C

n
k
=1 → n = 2k → k = log 2 n
2
Total Work done = C + 2C + 22C + 23C + ... + 2kC
= C (1 + 2 + 22 + ... + 2k)
 2k+1 −1 
= c 
 2 −1 

= C (2k+1 - 1
= C (2⸳2k - 1)
= C (2n - 1)
= O (n)
Algorithm

T(n) = 2T   + n
n
(2)
2

n
−1;n − 2k ;k − log 2 n
2k

⸫ n + n + n + ... + n

=kn
= n nlog 2 n

= O(nlog 2 n)

T(n) = 4T   + n
n
(3)
2

n = 2k, k = log2 n

n n n


= n + 4   + 42   + ...+ 4k  
2 2  2
Algorithm

= n 1+ 2 + 22 + 23 + ...+ 2k 
 

 2k+1 −1 
= n

 2 −1 

(
= n 2 2 k−1 )
= n ( 2n ) − 1

( )
= O n2

T (n) = T   +T   + n
n 2n
(4) T(1) = 1
3  3 

n
−1; n − 3i; i − log3 n 3
3i
= n + n +...+ log3 n T (n)
= (n + n + n +...+ log3 n) ≥ n + ... + log3 n

Ω(n⸳log 3/ 2 n )
Algorithm

1.10 Recurrence Relations and their Time Complexity


T (n) = C; n = 2
O (logn)
T(n) = 2 T(√𝑛) + C; n > 2
T (n) = C; n = 2
O(n)
T(n) = T(n – 1) + C ; n > 2
T (n) = C; n = 1
O(n2)
T(n) = T(n – 1) + n + C ; n > 2
T (n) = C; n = 1
O(2n)
T(n) = 2T(n – 1) + C ; n > 1
T (n) = C; n = 1
T(n) = 2T  n  + C ; n > 1 (n)
 2
T (n) = C; n = 1
T(n) = 2T  n  + n ; n > 1 (nlogn)
 2
T (n) = C; n = 1
T(n) = T  n  + C; n > 1 (logn)
 2
T (n) = 1; n = 2
T(n) = T ( n ) + C; n > 2 (loglogn)

1.11 Space Complexities


Algorithm

Int n, A[n];
Algorithm Rsum(A, n)
{
if (n = 1) return (A(1));
else;
return (A[n] + RSum(A, (n–1));
}

• Time Complexity = O(n)


• Space Complexity
• We need stack space
• Stack is used to store activation records of function calls
• Size of activation records is trivial
• Stack size that we need = O(n)
• Space complexity = O(n)

Algorithm A(n)
{
if (n = 1) return;
else;
{
A  n  ;
 2
}
}

Recurrence relation
T(n) = C; n = 1
T(n) = T  n  + C; n > 1
 2
Time Complexity = O(log n)

Space Complexity
• Space complexity will depend on number of activation record pushed into the stack
Suppose, n = 16
A (1)
A (2)
For n = 2K we are pushing
A (4)
the ‘K’ activation record
A (8)
A (16)
Algorithm

 Space Complexity

n = 2K
log n = Klog2 2
K = log n
2

Space Complexity = O(log n)

Example 3
Algorithm A(n)
{
if (n = 2) return;
else;
return (A n );
}

Solution:
T (n) = 1; n =2
T (n) = T ( n )+C; n > 2
Time Complexity = O (loglogn)

Space Complexity
Suppose n = 16

A(1)
A(2)
A(4)
A(16)
n
 For 2 2k manner we are pushing in stack
n
2 2k  2
n
log 2 2  log 2 2
2k
n  2K
K  log n
2

Space complexity = O log n ( 2


)
❑❑❑
Algorithm

2 DIVIDE AND CONQUER


2.1 DAC Application

2.2 Finding Maximum Minimum element

Recurrence Relation:
 1 if n = 1 or n = 2
 
T (n) =   n  
  2 
2T +1; n  2 
 

Time Complexity:
T (n) = O(n)

• Time complexity is same for every case (Best case/Worst case).

Space Complexity:
Algorithm

Space Complexity = O(n) + O(logn)


= O(n)

Number of comparisons to find maximum / minimum element on an given array of n elements:


3n
Comparison = −2
2

2.3 Power of an Element

Recurrence relation:
1 if n = 1 
 
T (n) =   n  
T  2  + 1; n  1
 

Time Complexity:

T (n) = T   + C
n
2
T (n) = O(log n)

Space Complexity:

Space Complexity = 4B + O(logn)


= O(logn)
Number of multiplications to find an
Multiplication = O(logn)

2.4 Binary Search


Given a sorted array and an element x, need to return the index of element x if it is present then 1, otherwise – 1.
Recurrence relation:

1 ; n =1 
 
T (n) =   n  
T  2  + C; n  1
 
Algorithm

Time Complexity:

Space Complexity:

Space complexity = O(n) + O(1)


= O (n)

2.5 Merge Algorithm


• Merging two sorted sub arrays of input size m,n.

• Number of comparisons to merge two sorted sub arrays of size m,n.


Comparisons = m + n – 1 (worst case)

Number of moves =m + n (Outplace Algorithm)

Number of comparisons in best case of merging two sorted subarrays of size m, n.

comparisons = min (m, n)

Moves = m + n (Always)

Note:
Best Case comes in comparisons no effect on moves.
Algorithm

2.5.1 Merge Sort Algorithm:

Note:
• In GATE exam if merge sort given then always consider outplace.
• If array size very large, merge sort preferable.
• If array size very small, then prefer insertion sort.
• Merge sort is stable sorting technique.
Algorithm

2.6 Quick Sort Algorithm

th
Example 1: In Quick for sorting n elements, the  n  smallest element is selected as pivot. what is the worst-case time
 16 
Complexity?

Solution.

T (n) = T   + T 
n 15n 
 +O (n)
 16   16 
= (solve by recursive tree method)

Example 2: The median of n elements can be found in O (n) time then, what is the time complexity of quick sort algo in
which median selected as pivot?

Solution.
T (n) = O (n) + C + O (n) + T (n/2) + T (n/2)

Find median swap median Partition algo


with last
= 2T (n/2) + C ∙ n
= O(n log n)
Algorithm

2.6.1 Randomized Quick Sort


• In Randomized quick short algorithm selection of pivot element can be taken randomly.

2.7 Counting Number of Inversion


• Counting number of inversion on given an array of an element.
Time complexity T(n) = O(nlogn)

2.8 Selection Procedure


Find Kth smallest on given an array of an element and integer K.

Time Complexity:
T(n) = O(n2)

Space complexity:
Space Complexity = O(n)

2.9 Strassen’s matrix Multiplication


Algorithm

2.10 Comparison Based Sorting Algorithms

Sorting Basic logic of sorting Algo BC AC WC Stable Inplace


Algorithm sorting sorting

Quick sort Choose pivot element place in (nlogn) (nlogn) (n2) No Yes
correct position

Merge sort Divide to equal parts recursively sort (nlogn) (nlogn) = (nlogn) = n Yes No
each sub part & marge them n logn log n

Heap sort Build heap(max) delete max place (nlogn) (nlogn) (nlogn) No Yes

Bubble sort Compare exchange (n) (n2) (n2) Yes Yes

Selection sort Find position of min element (n2) (n2) (n2) No Yes
from [1 to n]

Insertion sort Insert a [i + 1] into correct position (n) (n2) (n2) Yes Yes

❑❑❑
Algorithm

3 GREEDY
TECHNIQUE
3.1 Greedy Technique
• Greedy method is an algorithm design strategy used for solving problems where solution are seen as result of making
a sequence of decisions.
• A problem may contain more than one solution.

3.2 Terminology

3.3 Applications of greedy


Algorithm

3.4 Knapsack Problem


Time complexity T(n) = O(nlogn)

3.5 Job Sequence with Deadline


• Single CPU only.
• Arrival time of each job is same.
• No pre-emption.

3.6 Optimal Merge Pattern


• This is a problem related to merging of files. Given a set of n-files in sorted order. It is required to merge
them into a single sorted file with 2-way merging.
• This problem is like merging process in merge sort. In merge sort we were interested in number of
comparisons but in optimal merge pattern we are interested in record movement (i.e moving a record from
one file to another file).
o If file F1 has 'n' records and file 'F2 ' has 'm' records then number of record movement will be 'm+n'. 1 2
The problem of optimal merge pattern involves merging of n-files (n≥2).
• At any point choose two records with least weight merge them and put them in list and continue it until all records are
merged.

• Time complexity T(n) = O(nlogn)


• Space complexity = O(n)

3.7 Huffman Coding


• Huffman coding is essentially a non-uniform encoding with convention that the character with higher
frequency (probability) of occurrence will be enclosed with less number of bits.

• It comes under data compression technique.


• Time complexity T(n) = O(nlogn)
Algorithm

3.8 Minimum Cost Spanning Tree


3.8.1 Graph
Graph (V., E)

Set of vertices set of edges


• Let G(V, E) be a simple graph then

Maximum edges =
V(V-1)
2

E
V(V-1)
2
E  C.V2 C is constant

Note:
E = O(V2)
Log E = O(logV)
3.8.2 Graph Representation
Graph Representation

Adjacency matrix Adjacency list

• For more edges (Dense Graph) Adj. matrix is better (density more).
• For less edge (sparse graph) Adj list is better.
Matrix List
(1) Finding degree of vertex Time Complexity O(V) Every Case O(1) Best Case
O(V1) Worst Case
O(V2) Every Case O(V+2E) Worst Case
(2)Finding total edges  Time Complexity O(V) Best Case
O(1) O(V-1) Worst Case
(3) Finding 2-vertices adjacent (or)not  Time Complexity O(1) Best Case

O(V2) Every Case O(V+E) Every Case


(4) G(V,E)  space

3.8.3 What is Spanning Tree


A subgraph T(V, E’) of G(V, E) where E’ is the subset of (Eʹ E) is a spanning tree iff ‘T’ is a tree.
A sub graph G(‘V, E’) of G(V, E) is said to be spanning tree.
(1) T’ should contain all vertices of G
(2) T’ should contain (V-1) edged where V is number of vertices without cycle.
Algorithm

(3) T’ should connected.


3.8.4 Minimum Cost Spanning Tree
Minimum cost spanning tree is the one in which cost of the spanning tree formed should be minimum.

3.8.5 Prims Algorithm


• Select Any vertex
Time complexity = V + VlogV + 2E + ElogV
= O(E + V)logV

Using Sorted Array & Adjacency List


V + 2E + E × V = O(EV)

Using Sorted Array & Adjacency List


V × O(1) + V2 + E × V= O(EV)

3.8.6 Kruskal algorithm


• Take first minimum edge
Time complexity = E log E + (V + E)
= O(E log E) = O(ElogV)
If edges are already sorted
TC = O(E + V)

3.9 Single Source Shortest Path


3.9.1 Dijkstra Algorithm
• Using min heap & adjacency list = O(E + V)logV
• Using adjacency Matrix & Min heap = O 2 ElogV)
(V

• Using adjacency list & Unsorted Array = O 2


(V )

• Using adjacency list & Sorted Doubly Linked List = O(EV)

3.9.2 Bellman-Ford
• Time Complexity = O(EV)
• If negative edge weight cycle then for some vertices Incorrect answer.
❑❑❑
Algorithm

4 PROGRAMMING
DYNAMIC

4.1 Dynamic Programming


In dynamic programming for optimal solution always computes distinct function calls.

4.2 Terminology

4.3 Application of Dynamic Programming

4.4 Fibonacci Series


• Time complexity T(n) = O(nlogn)
• Computes distinct function calls.
Algorithm

4.5 Job Sequence with Deadline


• Single CPU only
• Arrival time of each job is same
• No pre-emption

4.6 Longest Common Sub sequence (LCS)


• For common subsequence always consider two strings:
• P = <ABCDB> – Q = <BDCABA>
• Common subsequences for both ‘p’ are
• S = <A>
• S = <AB>
• S = <CAB>
• S = <BDAB>
• A common subsequence of longest length is known as longest common subsequence.
• For above problem longest common subsequence will be of length u.

4.6.1 Applications of LCS


1. Genomics
2. Software engineering applications
3. Plagiarism
4. Data gathering system of search engines

4.6.2 Algorithm for LCS


LCS (p,q)
{
1. For i 0 to n −1
L[i −1]=0
2. For j 0 to m −1
L[−1, j]=0
Algorithm

3. For i 0 to n −1
For j 0 to m −1
If ( p[i] =q[ j])then
L[i, j]=1+ L (i −1, j −1];
else
L[i, j]=max{Li, j −1, L[i −1, j]}

}
• Time complexity of step 1 = O(n)
• Time complexity of step 2 = O(m)
• Time complexity of step 3 = O(mn)
• Total Time complexity = O(n) + O(m) + O(mn)
= O(mn)
• Space complexity = O[(M+1).(n+1)]
= O(mn)

4.7 Matrix Chain Multiplications


Two matrices ‘A’ and ‘B’ are compatible if and only number of column of first matrix must be equal to number of rows of
second matrix.

4.7.1 Brute force method


 1 2n 
Number of parenthesizing for a given chain is given by Catalan number:  Cn 
n +1 

Time complexity = O(nn)


Space complexity = O(n)

4.7.2 Algorithm For Matrix Chain Multiplication


The time complexity of multiply the given chain of n matrices <A1, A2 A3…. An> using dynamic programming (district
function call) is O(n3)

Space complexity = O(n2)


Algorithm

4.8 O/1 Knapsack Problem


The maximum profit can be achieved by O/1 knapsack problem where capacity of problem is ‘p’ and number of objects are
‘q’.

4.9 Sum of Subset Problem


• Given n-elements and an integer 'm', it is required to determine whether there exists a subset of given n elements, whose
sum equal M.
• This is a decision problem (True/False).

4.9.1 Algorithm for Sum of Subset Problem


SoS(n, M, A)
// A [1 . . . n] is an array of elements
{
1. for i = 0 to n
for j = 0 to M
if (i > =0 and j = 0)
SoS [i, j] = T
else
if (i = 0 and j > 0)
SoS [i, j] = F;
else
if (A[i] > j)
SoS [i, j] = SoS [i – 1, j]
else
SoS [i, j] = SoS [i – 1, j] or
SoS [i – 1, j – A[i]]
}
Algorithm

4.9.2 Time Complexity of SoS


Two for loops are there thus repeating for (n * m) times. Thus, time complexity = O(n * m)
Time complexity of SoS becomes exponential if M = 2n
T.C = O(n × 2n)

You might also like