20 Daa
20 Daa
20 Daa
UNIT – I
UNIT – II
Disjoint Sets: Disjoint set operations, union and find algorithms
Backtracking: General method, applications, n-queen’s problem, sum
of subsets problem, graph coloring
UNIT - III
Dynamic Programming: General method, applications-
Optimal binary search trees, 0/1 knapsack problem, All
pairs shortest path problem, Traveling sales person
problem, Reliability design.
UNIT – IV
Greedy method: General method, applications-Job
sequencing with deadlines, knapsack problem, Minimum
cost spanning trees, Single source shortest path problem.
UNIT - V
Branch and Bound: General method, applications -
Travelling sales person problem, 0/1 knapsack problem - LC
Branch and Bound solution, FIFO Branch and Bound solution.
NP-Hard and NP-Complete problems: Basic concepts, non
deterministic algorithms, NP - Hard and NP-Complete classes,
Cook’s theorem.
TEXT BOOKS
1. Fundamentals of Computer Algorithms, Ellis Horowitz,
Satraj Sahni and Rajasekharan,3rd Edition University Press.
REFERENCES
Design and Analysis of Algorithms, Aho, Ullman and,
Pearson education.
Introduction to Algorithms, second edition, T.H.
Coremen, C.E Leiserson, R.L.Rivest and C. Stien, PHI
Pvt . Ltd./Pearson Education.
Algorithm Design; Foundations, Analysis and Internet
Examples, M.T. Goodrich and R. Tamassia, John Wiley
and sons.
INTRODUCTION
The word algorithm comes from the name of the person author
-Abu Jafar Mohammed Ibn Musa Al khowarizmi who wrote
A text book entitled-”Algorithmi de numero indorum” Now term” Algorithmi
”in the title of the book led to the term Algorithm.
An algorithm is an effective method for finding out the solution for a given
problem. It is a sequence of instruction
That conveys the method to address a problem
2.OUTPUT: At least one quantity is produced. For each input the algorithm
produced value from specific task.
4.FINITENESS: If we trace out the instructions of an algorithm, then for all cases,
the algorithm terminates after a finite number of steps.
3. An identifier begins with a letter. The data types of variables are not
explicitly declared.
node= record
{
data type 1 data 1;
data type n data n;
node *link;
}
4. There are two Boolean values TRUE and FALSE.
Logical Operators
AND, OR, NOT
Relational Operators
<, <=,>,>=, =, !=
5. Assignment of values to variables is done using the assignment statement.
<Variable>:= <expression>;
Here link is a pointer to the record type node. Individual data items of
a record can be accessed with and period.
Contd…
7. The following looping statements are employed.
For, while and repeat-until While Loop:
While < condition > do
{
<statement-1>
..
..
<statement-n>
}
For Loop:
For variable: = value-1 to value-2 step step do
{
<statement-1>
.
.
.
<statement-n>
}
repeat-until:
repeat
<statement-1>
.
.
.
<statement-n>
until<condition>
Case
{
: <condition-1> : <statement-1>
.
.
.
: <condition-n> : <statement-n>
: else : <statement-n+1>
}
9. Input and output are done using the instructions read & write. No
format is used to specify the size of input or output quantities
Contd…
10. There is only one type of procedure: Algorithm, the heading takes the
form,
Algorithm Name (Parameter lists)
1. algorithm Max(A,n)
2. // A is an array of size n
3. {
4. Result := A[1];
5. for i:= 2 to n do
6. if A[i] > Result then
7. Result :=A[i];
8. return Result;
9. }
Issue in the study of algorithm
1. How to create an algorithm.
2. How to validate an algorithm.
3. How to analyses an algorithm
4. How to test a program.
b) Linear(variable)space complexity
1.Constant space complexity: A fixed amount of space for
all the input values.
1. Algorithm Sum(a,n) 0 - 0
2.{ 0 - 0
3. S=0.0; 1 1 1
4. for i=1 to n do 1 n+1 n+1
5. s=s+a[I]; 1 n n
6. return s; 1 1 1
7. } 0 - 0
Total 2n+3
KINDS OF ANALYSIS
1.Worst-case: (usually)
• T(n) = maximum time of algorithm on any input of size n.
2.Average-case: (sometimes)
• T(n) = expected time of algorithm over all inputs of size n.
• Need assumption of statistical distribution of inputs.
3.Best-case:
• T(n) = minimum time of algorithm on any input of size n.
COMPLEXITY:
Complexity refers to the rate at which the storage time grows as a
function of the problem size
Analysis of an Algorithm
The goal of analysis of an algorithm is to compare
algorithm in running time and also Memory
management.
Running time of an algorithm depends on how long it
takes a computer to run the lines of code of the
algorithm.
Running time of an algorithm depends on
1.Speed of computer
2.Programming language
3.Compiler and translator
Examples: binary search, linear search
ASYMPTOTIC ANALYSIS:
Expressing the complexity in term of its relationship
to know function. This type analysis is called
asymptotic analysis.
The main idea of Asymptotic analysis is to have a
measure of efficiency of an algorithm , that doesn’t
depends on
1.Machine constants.
2.Doesn’t require algorithm to be implemented.
3.Time taken by program to be prepare.
ASYMPTOTIC NOTATION
1.Big oh (O)notation
2.Big omega (Ω) notation
3.Theta(Θ) notation
4.Little oh notation
5.Little omega(Ω) notation
1.Big oh (O)notation
1.Big oh (O)notation : Asymptotic “less than”(slower rate).This
notation mainly represent upper bound of algorithm run time.
Big oh (O)notation is useful to calculate maximum amount of time of
execution.
By using Big-oh notation we have to calculate worst case time complexity.
Formula : f(n)<=c g(n) n>=n0 , c>0 ,n0 >=1
Problem:-Find upper bond ,lower bond & tight bond range for
functions: f(n)= 2n+5
Solution:-Let us given that f(n)= 2n+5 , now g(n)= n
lower bond=2n, upper bond =3n, tight bond=2n
For Big –oh notation(O):- according to definition
f(n)<=cg(n) for Big oh we use upper bond so
f(n)=2n+5, g(n)=n and c=3 according to definition
2n+5<=3n
Put n=1 7<=3 false Put n=2 9<=6 false Put n=3 14<=9 false
Put n=4 13<=12 false Put n=5 15<=15 true
now for all value of n>=5 above condition is satisfied. C=3 n>=5
2. Big - omega notation :- f(n)>=c.g(n) we know that this
Notation is lower bond notation so c=2
Let f(n)=2n+5 & g(n)=2.n
Now 2n+5>=c.g(n);
2n+5>=2n put n=1
We get 7>=2 true for all value of n>=1,c=2 condition is satisfied.
3. Theta notation :- according to definition
c1.g(n)<=f(n)<=c2.g
ANALYSIS OF INSERTION-SORT(CONTD.)
T (n) an 2 bn c
RANDOMIZED ALGORITHMS
These sub problems must be solved, and then a method must be found
to combine sub solutions into a solution of the whole.
If the sub problems are still relatively large, then the divide-and-
conquer strategy can possibly be reapplied.
If the problem p and the size is n , sub problems are n1, n2 ….nk,
respectively, then the computing time of D And C is described by the
recurrence relation.
T(n)= { g(n) n small
T(n1)+T(n2)+……………+T(nk)+f(n);
otherwise.
g(n) is the time of compute the answer directly for small I/p s. f(n) is the
time for dividing P & combining the solution to sub problems.
DIVIDE AND CONQUER :GENERAL METHOD
Consider the case in which a=2 and b=2. Let T(1)=2 & f(n)=n. We have,
T(n) = 2T(n/2)+n
2[2T(n/2/2)+n/2]+n
[4T(n/4)+n]+n
4T(n/4)+2n
4[2T(n/4/2)+n/4]+2n
4[2T(n/8)+n/4]+2n
8T(n/8)+n+2n
8T(n/8)+3n
23T(n/23)+3n
By using substitution method
Let n=2k
K=logn
2
K=3
2kT(n/n)+3n
nT(1)+3N
2n+kn
2n+nlogn
Time complexity is O(nlogn)
APPLICATIONS
3.Merge Sort is also a sorting algorithm. The algorithm divides the array in
two halves, recursively sorts them and finally merges the two sorted halves.
BINARY SEARCH
1. Algorithm Bin search(a,n,x)
2. // Given an array a[1:n] of elements in non-decreasing
3. //order, n>=0,determine whether x is present and
4. // if so, return j such that x=a[j]; else return 0.
5. {
6. low:=1; high:=n;
7. while (low<=high) do
8. {
9. mid:=[(low+high)/2];
10. if (x<a[mid]) then high;
11. else if(x>a[mid]) then
12. low=mid+1;
13. else return mid;
14. }
15. return 0; }
EXAMPLE
Only the variables low, high & mid need to be traced as we simulate the
algorithm.
Each set is individually sorted, and the resulting sorted sequences are
merged to produce a single sorted sequence of n elements.
Algorithm MergeSort(low,high)
//a[low:high] is a global array to be sorted
//Small(P) is true if there is only one element
//to sort. In this case the list is already sorted.
{
if (low<high) then //if there are more than one element
{
//Divide P into subproblems
//find where to split the set
mid = [(low+high)/2];
//solve the subproblems.
mergesort (low,mid);
mergesort(mid+1,high); //combine the solutions .
merge(low,mid,high);
}
}
Algorithm: Merging 2 sorted subarrays using auxiliary storage.
1. Algorithm merge(low,mid,high)
2. /*a[low:high] is a global array containing two sorted subsets in a[low:mid] and in a[mid+1:high].The
goal is to merge these 2 sets into a single set residing in a[low:high].b[] is an auxiliary global array.
*/
3. {
4. h=low; I=low; j=mid+1;
5. while ((h<=mid) and (j<=high)) do {
6. if (a[h]<=a[j]) then {
7. b[I]=a[h];
8. h = h+1; }
9. else {
10.b[I]= a[j];
11.j=j+1; }
12.I=I+1; }
13.if (h>mid) then
14.for k=j to high do {
15.b[I]=a[k];
16.I=I+1;
17.}
18.else
19.for k=h to mid do
20.{
21.b[I]=a[k];
22.I=I+1; }
23.for k=low to high do a[k] = b[k]; }
EXAMPLE
Consider the array of 10 elements a[1:10] =(310, 285, 179, 652, 351, 423,
861, 254, 450, 520)
Algorithm Mergesort begins by splitting a[] into 2 sub arrays each of size
five (a[1:5] and a[6:10]).
The elements in a[1:5] are then split into 2 sub arrays of size 3 (a[1:3] ) and
2(a[4:5])
Then the items in a [1:3] are split into sub arrays of size 2 a[1:2] &
one(a[3:3])
The 2 values in a[1:2] are split to find time into one-element sub arrays and
now the merging begins.
Recursion tree
Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.
cn cn
cn/2 cn/2 cn
h = lg n cn/4 cn/4 cn
cn/4 cn/4
…
(1) #leaves = n (n)
Total (n lg n)
QUICK SORT
In Quick sort, the division into 2 sub arrays is made so that the sorted sub
arrays do not need to be merged later.
1. www.mit.edu
2. www.soe.stanford.edu
3. www.grad.gatech.edu
4. www.gsas.harward.edu
5. www.eng.ufl.edu
6. www.iitk.ac.in
7. www.iitd.ernet.in
8. www.ieee.org
9. www.ntpel.com
10. WWW.JNTUWORLD.COM
11. www.firstrankers.com
12. www. studentgalaxi.blogspot.com
SUGGESTED BOOKS
TEXT BOOKS