16becs404 Design and Analysis of Algorithms
16becs404 Design and Analysis of Algorithms
E-CSE 2018-2019
COURSE OBJECTIVES:
• A n a l y z e the asymptotic performance of algorithms.
• Write rigorous correctness proofs for algorithms.
• D e m o n s t r a t e a familiarity with major algorithms and data structures.
• A p p l y important algorithmic design paradigms and methods of analysis.
• S y n t h e s i z e efficient algorithms in common engineering design situations.
COURSE OUTCOMES:
• For a given algorithms analyze worst-case running times of algorithms
based on asymptotic analysis and justify the correctness of algorithms .
• Describe the greedy paradigm and explain when an algorithmic design
situation calls for it. For a given problem develop the greedy algorithms.
• Describe the divide-and-conquer paradigm and explain when an
algorithmic design situation calls for it. Synthesize divide-and-conquer
algorithms. Derive and solve recurrence relation.
• Describe the dynamic-programming paradigm and explain when an
algorithmic
design situation calls for it. For a given problems of dynamic-programming and
develop the dynamic programming algorithms, and analyze it to determine its
computational complexity.
• For a given model engineering problem model it using graph and
write the corresponding algorithm to solve the problems.
• Explain the ways to analyze randomized algorithms (expected running
time, probability of error).
• Explain what an approximation algorithm is. Compute the approximation
factor of an approximation algorithm (PTAS and FPTAS).
UNIT 1:
Introduction: Characteristics of algorithm. Analysis of algorithm: Asymptotic
analysis of complexity bounds – best, average and worst-case behavior;
Performance measurements of
Algorithm, Time and space trade-offs, Analysis of recursive algorithms
through recurrence relations: Substitution method, Recursion tree method and Masters’
theorem.
UNIT 2:
Fundamental Algorithmic Strategies: Brute-Force, Greedy,Dynamic Programming,
Branch- and-Bound and Backtracking methodologies for the design of
algorithms; Illustrations
of these techniques for Problem-Solving , Bin Packing, Knap Sack TSP. Heuristics
–
characteristics and their application domains.
UNIT 3:
Graph and Tree Algorithms: Traversal algorithms: Depth First Search (DFS) and
Breadth
First Search (BFS); Shortest path algorithms, Transitive closure, Minimum Spanning
Tree,
Topological sorting, Network Flow Algorithm.
UNIT 4:
Tractable and Intractable Problems: Computability of Algorithms, Computability classes –
P,
NP, NP-complete and NP-hard. Cook’s theorem, Standard NP-complete problems
and
Reduction techniques.
UNIT 5:
Advanced Topics: Approximation algorithms, Randomized algorithms, Class of
problems beyond NP – P SPACE
TEXT BOOKS:
1. Introduction to Algorithms, 4TH Edition, Thomas H Cormen, Charles E
Lieserson, Ronald L Rivest and Clifford Stein, MIT Press/McGraw-Hill.
2. Fundamentals of Algorithms – E. Horowitz et al.
REFERENCES:
1. Algorithm Design, 1ST Edition, Jon Kleinberg and ÉvaTardos, Pearson.
2. Algorithm Design: Foundations, Analysis, and Internet Examples, Second
Edition, Michael T Goodrich and Roberto Tamassia, Wiley.
3. Algorithms -- A Creative Approach, 3RD Edition, UdiManber, Addison-
Wesley, Reading, MA.
KARPAGAM ACADEMY OF HIGHER EDUCATION
Faculty of Engineering
Lecture Plan
Teachi
No.of Supporting
S.No Topic Name ng
Periods Materials
Aids
UNIT- I INTRODUCTION
1 Introduction: Characteristiccs of algorithm. 1 R[1]-1 BB
Analysis of algorithm:: Asymptotic analysis of
2 1
complexity bounds R[1]-1 BB
3 Best, average and worst-caase behavior 1 R[1]-5 PPT
Performance measuremennts of
4 1
Algorithm R[1]-6 PPT
5 Time and space trade--offs 1 R[1]-6 PPT
6 Analysis of recursive algorithms 1 R[1]-95 PPT
Analysis of recursive algorithms through recurrence
7 1
relations R[1]-95 PPT
8 Substitution method 1 R[1]-68 BB
9 Recursion tree method 1 Web PPT
10 Masters’ theorem 1 R[1]-12 BB
11 Tutorial : Analysis of Algorithm 1 Web BB
Total 11
UNIT- II FU UNDAMENTAL ALGORITHMIC STRAT TEGIES
12 Brute-Force methodologies 1 R[1]-200 PPT
13 Greedy methodologies 1 web PPT
14 Dynamic Programming metthodologies 1 R[1] 201 BB
15 Branch- and-Bound methodoologies 1 R[1]214 PPT
16 Backtracking methodologiies 1 R[1]214 PPT
Illustrations
17
of these techniques for P Problem-Solving 1 R[1]218 PPT
18 Bin Packing 1 R[1]218 PPT
19 Knap Sack TSP 1 R[1]218 PPT
Heuristics –
20
characteristics and their appplication domains 1 R[1]221 BB
21 Tutorial : Algorithm Strategy 1 R[1]221 PPT
Total 10
UNIT- III GRAPH AND TREE ALGORITHMS
22 Graph and Tree Algorithms 1 web PPT
23 Traversal algorithms 1 web PPT
24 Depth First Search (DFS) 1 web PPT
25 Breadth First Search 1 T[1]-488 BB
26 Shortest path algorithms 1 T[1]-193 PPT
27 Transitive closure 1 T[1]-266 BB
28 Minimum Spanning Tree 1 T[1]-305 PPT
29 Topological sorting 1 T[1]-343 BB
30 Network Flow Algorithm 1 web PPT
31 Tutorial: Real-time application 1 web PPT
Total 10
UNIT- IV TRACTABLE AND INTRACTABLE PROBLEMS
32 Computability of Algorithms 1 R[1]-139 PPT
33 Computability classes 1 R[1]-139 PPT
34 P-Class 1 T[1]-140 PPT
35 NP-Class 1 R[1]-152 BB
36 NP-complete 1 R[1]-159 PPT
37 NP-hard 1 R[1]-162 BB
38 Cook’s theorem 1 R[1]-163 PPT
39 Standard NP-complete problems 1 R[1]-133 PPT
40 Reduction techniques 1 web PPT
41 Tutorial : List of problems in NP hard 1 R[1]-133 BB
Total 10
UNIT- V ADVANCED TOPICS
42 Approximation algorithms 1 R[1]-248 PPT
43 Approximation algorithms: Problems 1 R[1]-465 BB
44 Class of problems 1 R[1]-465 BB
45 Class of problems :Example Problems 1 R[1]-255 PPT
46 Class of problems: Example Problems 1 R[1]-248 PPT
47 NP SPACE 1 T[1]-1087 PPT
48 beyond NP SPACE 1 T[1]-1087 PPT
49 P SPACE 1 T[1]-690 BB
50 Tutorial: Approximation algorithm 1 T[1]-690 PPT
51 Revisions 1 T[1]-752 BB
52 Discussion on Previous University Question Papers
Total 10
Total Hours 52
TEXT BOOKS
Year of
publica
S.NO Title of the book tion
2013
Fundamentals of Algorithms – E. Horowitz et al.
2
REFERNCE BOOKS
Year of
publica
S.NO Title of the book tion
1 Algorithm Design, 1ST Edition, Jon Kleinberg and ÉvaTardos, Pearson 2011
1. Characteristics of an Algorithm
Not all procedures can be called an algorithm. An algorithm should have the following
characteristics −
Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or
phases), and their inputs/outputs should be clear and must lead to only one meaning.
Input − An algorithm should have 0 or more well-defined inputs.
Output − An algorithm should have 1 or more well-defined outputs, and should match
the desired output.
Finiteness − Algorithms must terminate after a finite number of steps.
Feasibility − Should be feasible with the available resources.
Independent − An algorithm should have step-by-step directions, which should be
independent of any programming code.
2. Analysis of Algorithm
3. Asymptotic Notations
Following are the commonly used asymptotic notations to calculate the running time
complexity of an algorithm.
Ο Notation
Ω Notation
θ Notation
Big Oh Notation, Ο
The notation Ο(n) is the formal way to express the upper bound of an algorithm's running time.
It measures the worst case time complexity or the longest amount of time an algorithm can
possibly take to complete.
Omega Notation, Ω
The notation Ω(n) is the formal way to express the lower bound of an algorithm's running time.
It measures the best case time complexity or the best amount of time an algorithm can possibly
take to complete.
Theta Notation, θ
The notation θ(n) is the formal way to express both the lower bound and the upper bound of an
algorithm's running time. It is represented as follows −
Common Asymptotic Notations
constant − Ο(1)
logarithmic − Ο(log n)
linear − Ο(n)
quadratic − Ο(n2)
cubic − Ο(n3)
polynomial − nΟ(1)
exponential − 2Ο(n)
• Analysing an algorithm
Ascertain the capabilities of the computational device: Once a problem is understood we need
to Know the capabilities of the computing device this can be done by Knowing the type of the
architecture,speed & memory availability.
Exact /approximate soln.: Once algorithm is devised, it is necessary to show that it computes
answer for all the possible legal inputs. The solution is stated in two forms,Exact solution or
approximate solution.examples of problems where an exact solution cannot be obtained are
i)Finding a squareroot of number.
Decide on the appropriate data structure:Some algorithms do not demand any in- genuity in
representing their inputs.Someothers are in fact are predicted on ingenious data structures.A
data type is a well-defined collection of data with a well-defined set of operations on it.A
data structure is an actual implementation of a particular abstract data type. The Elementary Data
Structures are ArraysThese let you access lots of data fast. (good) .You can have arrays
of any other da ta type. (good) .However, you cannot make arrays bigger if your program decides
it needs more space. (bad) .
RecordsThese let you organize non-homogeneous data into logical packages to keep every- thing
together. (good) .These packages do not include operations, just data fields (bad, which is why
we need objects) .Records do not help you process distinct items in loops (bad, which is why
arrays of records are used) SetsThese let you represent subsets of a set with such operations as
intersection, union, and equivalence. (good) .Built-in sets are limited to a certain small size. (bad,
but we can build our own set data type out of arrays to solve this problem if necessary)
Algorithm design techniques: Creating an algorithm is an art which may never be fully au-
tomated. By mastering these design strategies, it will become easier for you to devise new and
useful algorithms. Dynamic programming is one such technique. Some of the techniques are
especially useful in fields other then computer science such as operation research and electric- al
engineering. Some important design techniques are linear, non linear and integer programming
Methods of specifying an algorithm: There are mainly two options for specifying an algorithm:
use of natural language or pseudocode & Flowcharts.
A Pseudo code is a mixture of natural language & programming language like constructs. A
flowchart is a method of expressing an algorithm by a collection of connected geometric shapes.
In the above two simple algorithms, you saw how a single problem can have many solutions.
While the first solution required a loop which will execute for n number of times, the second
solution used a mathematical operator * to return the result in one line. So which one is the better
approach, of course the second one.
Space complexity is the amount of memory used by the algorithm (including the input values to
the algorithm) to execute and produce the result.
Sometime Auxiliary Space is confused with Space Complexity. But Auxiliary Space is the extra
space or the temporary space used by the algorithm during it's execution.
Space Complexity = Auxiliary Space + Input space
1. Instruction Space
It's the amount of memory used to save the compiled version of instructions.
2. Environmental Stack
For example, If a function A() calls function B() inside it, then all th variables of the
function A() will get stored on the system stack temporarily, while the function B() is
called and executed inside the funciton A().
3. Data Space
Type Size
In the above code, 4*n bytes of space is required for the array a[] elements.
4 bytes each for x, n, i and the return value.
Hence the total memory requirement will be (4n + 12), which is increasing linearly with the
increase in the input value n, hence it is called as Linear Space Complexity.
Similarly, we can have quadratic and other complex space complexity as well, as the complexity
of an algorithm increases.
But we should always focus on writing algorithm code in such a way that we keep the space
complexity minimum.
UNIT-II
The brute force algorithm consists in checking, at all positions in the text between 0 and n-m,
whether an occurrence of the pattern starts there or not. Then, after each attempt, it shifts the pattern by
exactly one position to the right.
The brute force algorithm requires no preprocessing phase, and a constant extra space in addition
to the pattern and the text. During the searching phase the text character comparisons can be done in
any order.
The time complexity of this searching phase is O(mn) (when searching for am-1b in an for
instance). The expected number of text character comparisons is 2n.
Main Features
no preprocessing phase
constant extra space needed
always shifts the window by exactly 1 position to the right
comparisons can be done in any order
searching phase in O(mn) time complexity
2n expected text characters comparisons
EXAMPLE
Bubble sort is one of the simple sorting algorithms and also popularly known as a Brute Force
Approach. The logic of the algorithm is very simple as it works by repeatedly iterating through a list of
elements, comparing two elements at a time and swapping them if necessary until all the elements are
swapped to an order.
For e.g. if we have a list of 10 elements, bubble sort starts by comparing the first two elements in the list.
If the second element is smaller than the first element then it exchanges them. Then it compares the
current second element with the third element in the list. This continues until the second last and the last
element is compared which completes one iteration through the list. By the time it completes the first
iteration the largest element in the list comes to the rightmost position.
The algorithm gets its name as we start from lowest point and “bubble up” the higher elements to the
highest point in the list. We can also follow other approach where we start from highest point and “bubble
down” lowest elements in the list. Since it only uses comparisons to operate on elements, it is a
comparison sort.
As we can see in above example the list sorted by third iteration and it is useless to go for 4th and 5th
iterations. So we use a flag which determines whether a swap operation is done in last iteration or not. If a
swap operation is not done in last iteration we will stop remaining iterations since the list is already in
sorted order. For the above example we will stop iterating once third iteration is done as the whole list is
sorted by then.
Pseudo-code
procedure bubbleSort( A : list of sortable items )
repeat
swapped = false
for i = 0 to length(A) - 1
if A[i] > A[i+1] then
swap( A[i], A[i+1] )
swapped = true
end if
end for
until not swapped
end procedure
2. Greedy Method
Greedy method is the most straightforward designed technique.
As the name suggest they are short sighted in their approach taking decision on the basis
of the information immediately at the hand without worrying about the effect these
decision may have in the future.
DEFINITION:
A problem with N inputs will have some constraints .any subsets that satisfy these
constraints are called a feasible solution.
A feasible solution that either maximize can minimize a given objectives function is
called an optimal solution.
* The function select an input from a[] and removes it. The select input value is assigned to X.
Feasible is a Boolean value function that determines whether X can be included into the
solution vector.
The function Union combines X with The solution and updates the objective function.
The function Greedy describes the essential way that a greedy algorithm will once a
particular problem is chosen ends the function subset, feasible & union are properly
implemented.
Example
Knapsack Problem
We are given n objects and knapsack or bag with capacity M object I has a weight Wi
where I varies from 1 to N.
The problem is we have to fill the bag with the help of N objects and the resulting profit
has to be maximum.
There are so many ways to solve this problem, which will give many feasible solution for
which we have to find the optimal solution.
But in this algorithm, it will generate only one solution which is going to be feasible as
well as optimal.
First, we find the profit & weight rates of each and every object and sort it according to
the descending order of the ratios.
Select an object with highest p/w ratio and check whether its height is lesser than the
capacity of the bag.
If so place 1 unit of the first object and decrement .the capacity of the bag by the weight
of the object you have placed.
Repeat the above steps until the capacity of the bag becomes less than the weight of the
object you have selected .in this case place a fraction of the object and come out of the
loop.
ALGORITHM:
Example:
Capacity=20
N=3 ,M=20
Wi=18,15,10
Pi=25,24,15
Pi/Wi=25/18=1.36,24/15=1.6,15/10=1.5
X1 X2 X3 WiXi PiXi
½ 1/3 ¼ 16.6 24.25
1 2/5 0 20 18.2
0 2/3 1 20 31
0 1 ½ 20 31.5
Of these feasible solution Solution 4 yield the Max profit .As we shall soon see this solution is
optimal for the given problem instance.
It is an algorithm design method that can be used when the solution to a problem can be
viewed as the result of a sequence of decisions.
The idea of dynamic programming is thus quit simple: avoid calculating the same thing
twice, usually by keeping a table of known result that fills up a sub instances are solved.
Divide and conquer is a top-down method. When a problem is solved by divide and
conquer, we immediately attack the complete instance, which we then divide into smaller
and smaller sub-instances as the algorithm progresses.
Dynamic programming on the other hand is a bottom-up technique. We usually start with
the smallest and hence the simplest sub- instances. By combining their solutions, we
obtain the answers to sub-instances of increasing size, until finally we arrive at the
solution of the original instances.
The essential difference between the greedy method and dynamic programming is that
the greedy method only one decision sequence is ever generated. In dynamic
programming, many decision sequences may be generated. However, sequences
containing sub-optimal sub-sequences cannot be optimal and so will not be generated.
Because of principle of optimality, decision sequences containing subsequences that are
suboptimal are not considered. Although the total number of different decision sequences is
exponential in the number of decisions(if there are d choices for each of the n decisions to be
made then there are dn possible decision sequences),Dynamic programming algorithms often
have a polynomial complexity.
Example
All Pair Shortest Path Algorithm
Let G(V,E) be a directed graph with n vertices , „E‟ is the set of edges & V is the set of n
vertices.
Each edge has an associated non-negative length.
We want to calculate the length of the shortest path between each pair of nodes.
i.e)Shortest path between every vertex to all other vertices.
Suppose the nodes of G are numbered from 1 to n, so n={1,2,...n}
cost (i,j) is length of edge <i,j> and it is called as cost adjacency matrix.
cost(i,i)=0 for i=1,2...n,
cost(i,j)=cost of edge <i,j> for all i&j
costi,j)=infinity, if the edge (i,j) does not exist.
The all pairs shortest path problem is to determine a matrix A such that A(i,j) is the length
of a shortest path from i to j.
The principle of optimality:
If k is the node on the shortest path from i to j then the part of the path from i to k
and the part from k to j must also be optimal, that is shorter.
15
30
5
5 50 5 15
15
ALGORITHM :
Algorithm Allpaths(cost,A,n)
//cost[1…n,1…n] is the cost adjacency matrix of graph with n vertices
//A[i,j] is the cost of shortest path from vertex i to j
//cost[i,i]=0.0 for 1<=i<=n
{
for i:=1 to n do
for j:=1 to n do
A[i,j]:=cost[i,j]; //copy cost into A
for k:=1 to n do
for i:=1 to n do
for j:=1 to n do
A[i,j]:=min{A[i,j], A[i,k]+ A[k,j];
}
0 5
50 0 15 5
cost(i,j) = A(i,j) = D0 = 30 0 15
15 5 0
for k= 1 to 4
When k=1
for i= 1 to 4
when i=1
for j= 1 to 4
when j=1
A[1,1]=min(A[1,1],a[1,1]+A[1,1])=min(0,0)=0
When j=2
A[1,2]=min(A[1,2],A[1,1]+A[1,2])=min(5,0+5)=5
When j=3
A[1,3]=min(A[1,3],A[1,1]+A[1,3])=min(,0+)=
When j=4
A[1,4]=min(A[1,4],A[1,1]+A[1,4])=min(,0+)=
When i=2
for j= 1 to 4
when j=1
A[2,1]=min(A[2,1],A[2,1]+A[1,1])=min(50,50+0)=50
When j=2
A[2,2]=min(A[2,2],A[2,1]+A[1,2])=min(0,50+5)=0
When j=3
A[2,3]=min(A[2,3],A[2,1]+A[1,3])=min(15,50+)=15
When j=4
A[2,4]=min(A[2,4],A[2,1]+A[1,4])=min(5,50+)=5
When i=3
for j= 1 to 4
when j=1
A[3,1]=min(A[3,1],A[3,1]+A[1,1])=min(30,30+0)=30
When j=2
A[3,2]=min(A[3,2],A[3,1]+A[1,2])=min(,30+5)=35
When j=3
A[3,3]=min(A[3,3],A[3,1]+A[1,3])=min(0,30+)=0
When j=4
A[3,4]=min(A[3,4],A[3,1]+A[1,4])=min(15,30+)=15
When i=4
for j= 1 to 4
when j=1
A[4,1]=min(A[4,1],A[4,1]+A[1,1])=min(15,15+0)=15
When j=2
A[4,2]=min(A[4,2],A[4,1]+A[1,2])=min(,15+5)=20
When j=3
A[4,3]=min(A[4,3],A[4,1]+A[1,3])=min(5,15+)=5
When j=4
A[4,4]=min(A[4,4],A[4,1]+A[1,4])=min(0,15+)=0
k
i j A[i,j]
1 1 1 0
2 5
3
4
2 1 50
2 0
3 15
4 5
3 1 30
2 35
3 0
4 15
4 1 15
2 20
3 5
4 0
0 5 0 0 0 0
50 0 15 5 P[3,2]= 1 0 0 0 0
D1= 30 35 0 15 P[4,2]= 1 P1= 0 1 0 0
15 20 5 0 0 1 0 0
0 5 20 10
D3= 45 0 15 5 P[2,1]=3
30 35 0 15
15 20 5 0
0 5 15 10
20 0 10 5 P[1,3]=4
D4= 30 35 0 15 P[2,3]=4
15 20 5 0
If you want the exact path then we have to refer the matrix p.The matrix will be,
0042
3040 0 direct path
P= 0 1 0 0
0100
Finally we see the trips from 1 to 2, & from 2 to 4, are also direct.
ALGORITHM :
D [ i , j ] = min (D[ i, j ], D[ i, k ] + D[ k, j ]
Return D
ANALYSIS:
4. Backtracking method
Many problems which deal with searching for a set of solutions or for a optimal solution
satisfying some constraints can be solved using the backtracking formulation.
The major advantage of this method is, once we know that a partial vector (x1,…xi) will
not lead to an optimal solution that (mi+1………..mn) possible test vectors may be ignored
entirely.
Many problems solved using backtracking require that all the solutions satisfy a complex
set of constraints.
i) Explicit constraints.
ii) Implicit constraints.
1) Explicit constraints:
Explicit constraints are rules that restrict each Xi to take values only from a given
set.
Some examples are,
Xi 0 or Si = {all non-negative real nos.}
Xi =0 or 1 or Si={0,1}.
Li Xi Ui or Si= {a: Li a Ui}
All tupules that satisfy the explicit constraint define a possible solution space for I.
2) Implicit constraints:
The implicit constraint determines which of the tuples in the solution space I can
actually satisfy the criterion functions.
Algorithm:
Algorithm IBacktracking (n)
// This schema describes the backtracking procedure .All solutions are generated in X[1:n]
//and printed as soon as they are determined.
{
k=1;
While (k 0) do
{
if (there remains all untried
X[k] T (X[1],[2],…..X[k-1]) and Bk (X[1],…..X[k])) is true ) then
{
if(X[1],……X[k] )is the path to the answer node)
Then write(X[1:k]);
k=k+1; //consider the next step.
}
else k=k-1; //consider backtracking to the previous set.
}
}
All solutions are generated in X[1:n] and printed as soon as they are determined.
1. Sum of subsets.
2. Graph coloring.
3. Hamiltonian cycle.
4. N-Queens problem.
Example
Graph coloring
Let „G‟ be a graph and „m‟ be a given positive integer. If the nodes of „G‟ can be colored in such
a way that no two adjacent nodes have the same color. Yet only „M‟ colors are used. So it‟s
called M-color ability decision problem.
The graph G can be colored using the smallest integer „m‟. This integer is referred to as
chromatic number of the graph.
A graph is said to be planar iff it can be drawn on plane in such a way that no two edges
cross each other.
Suppose we are given a map then, we have to convert it into planar. Consider each and
every region as a node. If two regions are adjacent then the corresponding nodes are
joined by an edge.
Consider a map with five regions and its graph.
4 5
2
1
1 is adjacent to 2, 3, 4.
2 is adjacent to 1, 3, 4, 5
3 is adjacent to 1, 2, 4
4 is adjacent to 1, 2, 3, 5
5 is adjacent to 2, 4
2 3
5 4
Al
Steps to color the Graph:
g
First create the oadjacency matrix graph(1:m,1:n) for a graph, if there is an edge between
i,j then C(i,j) = 1ri otherwise C(i,j) =0.
t
The Colors willhbe represented by the integers 1,2,…..m and the solutions will be stored
in the array X(1),X(2),………..,X(n) ,X(index) is the color, index is the node.
m
He formula is used m to set the color is,
C
X(k) = (X(k)+1) % (m+1)
ol
First one chromatico number is assigned ,after assigning a number for „k‟ node, we have to
check whether the ri adjacent nodes has got the same values if so then we have to assign the
next value. n
g(
k)
//
t
h
Repeat the procedure until all possible combinations of colors are found.
The function which is used to check the adjacent nodes and same color is,
If(( Graph (k,j) == 1) and X(k) = X(j))
Example:
1 2
4 3
N= 4
M= 3
Adjacency Matrix:
0 1 0 1
1 0 1 0
0 1 0 1
1 0 1 0
The state space tree will give all possible colors in that ,the numbers which are inside the
circles are nodes ,and the branch with a number is the colors of the nodes.
Algorithm mColoring(k)
// the graph is represented by its Boolean adjacency matrix G[1:n,1:n] .All assignments //of
1,2,……….,m to the vertices of the graph such that adjacent vertices are assigned //distinct
integers are printed. ‟k‟ is the index of the next vertex to color.
{
repeat
{
// generate all legal assignment for X[k].
Nextvalue(k); // Assign to X[k] a legal color.
If (X[k]=0) then return; // No new color possible.
If (k=n) then // Almost „m‟ colors have been used to color the „n‟ vertices
Write(x[1:n]);
Else mcoloring(k+1);
}until(false);
}
UNIT-III
GRAPH BASIC TERMINOLOGIES
A graph is a pictorial representation of a set of objects where some pairs of objects are
connected by links. The interconnected objects are represented by points termed as vertices,
and the links that connect the vertices are called edges.
Formally, a graph is a pair of sets (V, E), where V is the set of vertices and E is the set of edges,
connecting the pairs of vertices. Take a look at the following graph −
Tree Node
The code to write a tree node would be similar to what is given below. It has a data part and
references to its left and right child nodes.
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
In a tree, all nodes share common construct.
Insert Operation
The very first insertion creates the tree. Afterwards, whenever an element is to be inserted, first
locate its proper location. Start searching from the root node, then if the data is less than the key
value, search for the empty location in the left subtree and insert the data. Otherwise, search for
the empty location in the right subtree and insert the data.
Algorithm
If root is NULL
then create root node
return
endwhile
insert data
end If
1. Traversal Algorithm
Depth First Search
Depth First Search (DFS) algorithm traverses a graph in a depth wise motion and uses a stack to
remember to get the next vertex to start a search, when a dead end occurs in any iteration.
As C does not have any unvisited adjacent node so we keep popping the stack until we find a
node that has an unvisited adjacent node. In this case, there's none and we keep popping until
the stack is empty.
Breadth First Search (BFS)
Breadth First Search (BFS) algorithm traverses a graph in a breadth wise motion and uses a
queue to remember to get the next vertex to start a search, when a dead end occurs in any
iteration.
As in the example given above, BFS algorithm traverses from A to B to E to F first then to C
and G lastly to D. It employs the following rules.
Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it in a queue.
Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.
Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.
Step Traversal Description
The shortest path problem is about finding a path between 2 vertices in a graph such that the
total sum of the edges weights is minimum.
Dijkstra's Algorithm
Dijkstra's algorithm has many variants but the most common one is to find the shortest paths
from the source vertex to all other vertices in the graph.
Algorithm Steps:
Set all vertices distances = infinity except for the source vertex, set the source distance
= 0.
Push the source vertex in a min-priority queue in the form (distance , vertex), as the
comparison in the min-priority queue will be according to vertices distances.
Pop the vertex with the minimum distance from the priority queue (at first the popped
vertex = source).
Update the distances of the connected vertices to the popped vertex in case of "current
vertex distance + edge weight < next vertex distance", then push the vertex
with the new distance to the priority queue.
If the popped vertex is visited before, just continue without using it.
Apply the same algorithm again until the priority queue is empty.
Dijkstra’s algorithm finds a shortest path tree from a single source node, by building a set of
nodes that have minimum distance from the source.
distdist, an array of distances from the source node ss to each node in the graph,
initialized the following way: distdist(ss) = 0; and for all other nodes vv, distdist(vv)
= \infty∞. This is done at the beginning because as the algorithm proceeds,
the distdist from the source to each node vv in the graph will be recalculated and
finalized when the shortest distance to vv is found
QQ, a queue of all nodes in the graph. At the end of the algorithm's progress, QQ will
be empty.
SS, an empty set, to indicate which nodes the algorithm has visited. At the end of the
algorithm's run, SS will contain all the nodes of the graph.
The algorithm proceeds as follows:
1. While QQ is not empty, pop the node vv, that is not already in SS, from QQ with the
smallest distdist (vv). In the first run, source node ss will be chosen
because distdist(ss) was initialized to 0. In the next run, the next node with the
smallest distdist value is chosen.
2. Add node vv to SS, to indicate that vv has been visited
3. Update distdist values of adjacent nodes of the current node vv as follows: for each new
adjacent node uu,
Note: The weight of an edge (u,vu,v) is taken from the value associated with (u,vu,v) on the
graph.
Implementation
This is pseudocode for Dijkstra's algorithm, mirroring Python syntax. It can be used in order to
implement the algorithm in any language.
function Dijkstra(Graph, source):
dist[source] := 0 // Distance from source to source is set to 0
for each vertex v in Graph: // Initializations
if v ≠ source
dist[v] := infinity // Unknown distance function from source to each node set to infinity
add v to Q // All nodes initially in Q
for each neighbor u of v: // where neighbor u has not yet been removed from Q.
alt := dist[v] + length(v, u)
if alt < dist[u]: // A shorter path to u has been found
dist[u] := alt // Update distance of u
return dist[]
end function
Examples
We step through Dijkstra's algorithm on the graph used in the algorithm above:
[3]
2. Pick first node and calculate distances to adjacent nodes.
[4]
3. Pick next node with minimal distance; repeat adjacent node distance calculations.
[5]
A spanning tree is a subset of Graph G, which has all the vertices covered with minimum
possible number of edges. Hence, a spanning tree does not have cycles and it cannot be
disconnected..
By this definition, we can draw a conclusion that every connected and undirected Graph G has
at least one spanning tree. A disconnected graph does not have any spanning tree, as it cannot
be spanned to all its vertices.
We found three spanning trees off one complete graph. A complete undirected graph can have
maximum nn-2 number of spanning trees, where n is the number of nodes. In the above
addressed example, n is 3, hence 33−2 = 3 spanning trees are possible.
Kruskal's algorithm
Kruskal's algorithm to find the minimum cost spanning tree uses the greedy approach. This
algorithm treats the graph as a forest and every node it has as an individual tree. A tree connects
to another only and only if, it has the least cost among all available options and does not violate
MST properties.
To understand Kruskal's algorithm let us consider the following example −
Step 1 - Remove all loops and Parallel Edges
Remove all loops and parallel edges from the given graph.
In case of parallel edges, keep the one which has the least cost associated and remove all
others.
The least cost is 2 and edges involved are B,D and D,T. We add them. Adding them does
not violate spanning tree properties, so we continue to our next edge selection.
Next cost is 3, and associated edges are A,C and C,D. We add them again −
Next cost in the table is 4, and we observe that adding it will create a circuit in the graph.
−
We ignore it. In the process we shall ignore/avoid all edges that create a circuit.
We observe that edges with cost 5 and 6 also create circuits. We ignore them and move
on.
Now we are left with only one node to be added. Between the two least cost edges
available 7 and 8, we shall add the edge with cost 7.
By adding edge S,A we have included all the nodes of the graph and we now have
minimum cost spanning tree.
Prim’s Algorithm
Prim's algorithm to find minimum cost spanning tree (as Kruskal's algorithm) uses the greedy
approach. Prim's algorithm shares a similarity with the shortest path first algorithms.
Prim's algorithm, in contrast with Kruskal's algorithm, treats the nodes as a single tree and
keeps on adding new nodes to the spanning tree from the given graph.
To contrast with Kruskal's algorithm and to understand Prim's algorithm better, we shall use the
same example −
Step 3 - Check outgoing edges and select the one with less cost
After choosing the root node S, we see that S,A and S,C are two edges with weight 7 and 8,
respectively. We choose the edge S,A as it is lesser than the other.
Now, the tree S-7-A is treated as one node and we check for all edges going out from it. We
select the one which has the lowest cost and include it in the tree.
After this step, S-7-A-3-C tree is formed. Now we'll again treat it as a node and will check all
the edges again. However, we will choose only the least cost edge. In this case, C-3-D is the
new edge, which is less than other edges' cost 8, 6, 4, etc.
After adding node D to the spanning tree, we now have two edges going out of it having the
same cost, i.e. D-2-T and D-2-B. Thus, we can add either one. But the next step will again yield
edge 2 as the least cost. Hence, we are showing a spanning tree with both edges included.
UNIT-IV
P and NP Class
In Computer Science, many problems are solved where the objective is to maximize or
minimize some values, whereas in other problems we try to find whether there is a solution or
not. Hence, the problems can be categorized as follows −
Optimization Problem
Optimization problems are those for which the objective is to maximize or minimize some
values. For example,
Finding the minimum number of colors needed to color a given graph.
Finding the shortest path between two vertices in a graph.
Decision Problem
There are many problems for which the answer is a Yes or a No. These types of problems are
known as decision problems. For example,
Whether a given graph can be colored by only 4-colors.
Finding Hamiltonian cycle in a graph is not a decision problem, whereas checking a graph is
Hamiltonian or not is a decision problem.
What is Language?
Every decision problem can have only two answers, yes or no. Hence, a decision problem may
belong to a language if it provides an answer „yes‟ for a specific input. A language is the totality
of inputs for which the answer is Yes. Most of the algorithms discussed in the previous chapters
are polynomial time algorithms.
For input size n, if worst-case time complexity of an algorithm is O(nk), where k is a constant,
the algorithm is a polynomial time algorithm.
Algorithms such as Matrix Chain Multiplication, Single Source Shortest Path, All Pair Shortest
Path, Minimum Spanning Tree, etc. run in polynomial time. However there are many problems,
such as traveling salesperson, optimal graph coloring, Hamiltonian cycles, finding the longest
path in a graph, and satisfying a Boolean formula, for which no polynomial time algorithms is
known. These problems belong to an interesting class of problems, called the NP-
Complete problems, whose status is unknown.
In this context, we can categorize the problems as follows −
P-Class
The class P consists of those problems that are solvable in polynomial time, i.e. these problems
can be solved in time O(nk) in worst-case, where k is constant.
These problems are called tractable, while others are called intractable or superpolynomial.
Formally, an algorithm is polynomial time algorithm, if there exists a polynomial p(n) such that
the algorithm can solve any instance of size n in a time O(p(n)).
Problem requiring Ω(n50) time to solve are essentially intractable for large n. Most known
polynomial time algorithm run in time O(nk) for fairly low value of k.
The advantages in considering the class of polynomial-time algorithms is that all
reasonable deterministic single processor model of computation can be simulated on each
other with at most a polynomial slow-d
NP-Class
The class NP consists of those problems that are verifiable in polynomial time. NP is the class
of decision problems for which it is easy to check the correctness of a claimed answer, with the
aid of a little extra information. Hence, we aren‟t asking for a way to find a solution, but only to
verify that an alleged solution really is correct.
Every problem in this class can be solved in exponential time using exhaustive search.
P versus NP
Every decision problem that is solvable by a deterministic polynomial time algorithm is also
solvable by a polynomial time non-deterministic algorithm.
All problems in P can be solved with polynomial time algorithms, whereas all problems in NP -
P are intractable.
It is not known whether P = NP. However, many problems are known in NP with the property
that if they belong to P, then it can be proved that P = NP.
If P ≠ NP, there are problems in NP that are neither in P nor in NP-Complete.
The problem belongs to class P if it‟s easy to find a solution for the problem. The problem
belongs to NP, if it‟s easy to check a solution that may have been very tedious to find.
Cook‟s Theorem
Stephen Cook presented four theorems in his paper “The Complexity of Theorem Proving
Procedures”. These theorems are stated below. We do understand that many unknown terms are
being used in this chapter, but we don‟t have any scope to discuss everything in detail.
Following are the four theorems by Stephen Cook −
Theorem-1
If a set S of strings is accepted by some non-deterministic Turing machine within polynomial
time, then S is P-reducible to {DNF tautologies}.
Theorem-2
The following sets are P-reducible to each other in pairs (and hence each has the same
polynomial degree of difficulty): {tautologies}, {DNF tautologies}, D3, {sub-graph pairs}.
Theorem-3
For any TQ(k) of type Q, TQ(k)k√(logk)2TQ(k)k(logk)2 is unbounded
There is a TQ(k) of type Q such that TQ(k)⩽2k(logk)2TQ(k)⩽2k(logk)2
Theorem-4
If the set S of strings is accepted by a non-deterministic machine within time T(n) = 2n, and
if TQ(k) is an honest (i.e. real-time countable) function of type Q, then there is a constant K,
so S can be recognized by a deterministic machine within time TQ(K8n).
First, he emphasized the significance of polynomial time reducibility. It means that if we have a
polynomial time reduction from one problem to another, this ensures that any polynomial time
algorithm from the second problem can be converted into a corresponding polynomial time algorithm
for the first problem.
Second, he focused attention on the class NP of decision problems that can be solved in polynomial
time by a non-deterministic computer. Most of the intractable problems belong to this class, NP.
Third, he proved that one particular problem in NP has the property that every other problem in NP
can be polynomially reduced to it. If the satisfiability problem can be solved with a polynomial time
algorithm, then every problem in NP can also be solved in polynomial time. If any problem in NP is
intractable, then satisfiability problem must be intractable. Thus, satisfiability problem is the hardest
problem in NP.
Fourth, Cook suggested that other problems in NP might share with the satisfiability problem this
property of being the hardest member of NP.
UNIT-V
Approximate Algorithms
Introduction:
An Approximate Algorithm is a way of approach NP-COMPLETENESS for the optimization problem.
This technique does not guarantee the best solution. The goal of an approximation algorithm is to come as
close as possible to the optimum value in a reasonable amount of time which is at the most polynomial
time. Such algorithms are called approximation algorithm or heuristic algorithm.
o For the traveling salesperson problem, the optimization problem is to find the shortest cycle, and
the approximation problem is to find a short cycle.
o For the vertex cover problem, the optimization problem is to find the vertex cover with fewest
vertices, and the approximation problem is to find the vertex cover with few vertices.
Performance Ratios
Suppose we work on an optimization problem where every solution carries a cost. An Approximate
Algorithm returns a legal solution, but the cost of that legal solution may not be optimal.
For Example, suppose we are considering for a minimum size vertex-cover (VC). An approximate
algorithm returns a VC for us, but the size (cost) may not be minimized.
Another Example is we are considering for a maximum size Independent set (IS). An approximate
Algorithm returns an IS for us, but the size (cost) may not be maximum. Let C be the cost of the solution
returned by an approximate algorithm, and C* is the cost of the optimal solution.
We say the approximate algorithm has an approximate ratio P (n) for an input size n, where
Intuitively, the approximation ratio measures how bad the approximate solution is distinguished with the
optimal solution. A large (small) approximation ratio measures the solution is much worse than (more or
less the same as) an optimal solution.
Observe that P (n) is always ≥ 1, if the ratio does not depend on n, we may write P. Therefore, a 1-
approximation algorithm gives an optimal solution. Some problems have polynomial-time approximation
algorithm with small constant approximate ratios, while others have best-known polynomial time
approximation algorithms whose approximate ratios grow with n.
Vertex Cover
A Vertex Cover of a graph G is a set of vertices such that each edge in G is incident to at least one of
these vertices.
The decision vertex-cover problem was proven NPC. Now, we want to solve the optimal version of the
vertex cover problem, i.e., we want to find a minimum size vertex cover of a given graph. We call such
vertex cover an optimal vertex cover C*.
The idea is to take an edge (u, v) one by one, put both vertices to C, and remove all the edges incident to u
or v. We carry on until all edges have been removed. C is a VC. But how good is C?
VC = {b, c, d, e, f, g}
What is a Randomized Algorithm?
An algorithm that uses random numbers to decide what to do next anywhere in its
logic is called Randomized Algorithm.. For example, in Randomized Quick Sort, we
use random number to pick the next pivot (or we randomly shuffle the array). And
in Karger’s algorithm, we randomly pick an edge.
Possible Questions
Part-B
Answer ALL Questions (5*16=80)
21. a i) Explain in detail Big oh, Big Omega and Big Theta Notations with necessary illustrations.(8)
ii)Discuss fundamentals of algorithm analysis framework in detail. (8)
(OR)
21. b i)Discuss the important problem types in algorithm solving (8)
ii) Write short notes on fundamentals of algorithmic solving. (8)
22. a i) Using iterative method, find the asymptotic value for (8)
ii) With the help of example explain how a recursive algorithm can be represented by
recurrence relation. (8)
(OR)
22. b i) What is empirical analysis of algorithms? Discuss how empirical analysis will be done. (8)
ii) Write short notes on algorithm visualization. (8)
23. a Explain divide and conquer technique in detail. Name some problems which apply divide and
conquer techniques for solving it. Discuss binary search algorithm and analyze it. (16)
(OR)
23. b Explain selection sort algorithm with an example and explain the relevance of brute force method
in solving it. (16)
24 a) Explain Floyd’s Algorithm for all pair shortest path algorithm with example and
analyze its efficiency
(OR)
b) Write the Huffman’s Algorithm. Construct the Huffman’s tree for the following data and obtain its
Huffman’s Code.
25 a) Explain the Assignment problem in Branch and bound with Example. (16)
(OR)
b) Write backtracking algorithm for N queens problem and Hamiltonian problem. (16)
********
KARPAGAM UNIVERSITY
COIMBATORE-21
Faculty of Engineering
Department of Computer Science and Engineering
Part-B
Answer ALL Questions (5*16=80)
21. a i) Explain in detail Big oh, Big Omega and Big Theta Notations with necessary illustrations.(8)
ii)Discuss fundamentals of algorithm analysis framework in detail. (8)
(OR)
21. b i)Discuss the important problem types in algorithm solving (8)
ii) Write short notes on fundamentals of algorithmic solving. (8)
22. a i) Using iterative method, find the asymptotic value for (8)
ii) With the help of example explain how a recursive algorithm can be represented by
recurrence relation. (8)
(OR)
22. b i) What is empirical analysis of algorithms? Discuss how empirical analysis will be done. (8)
ii) Write short notes on algorithm visualization. (8)
23. a Explain divide and conquer technique in detail. Name some problems which apply divide and
conquer techniques for solving it. Discuss binary search algorithm and analyze it. (16)
(OR)
23. b Explain selection sort algorithm with an example and explain the relevance of brute force method
in solving it. (16)
24 a) Explain Floyd’s Algorithm for all pair shortest path algorithm with example and
analyze its efficiency
(OR)
b) Write the Huffman’s Algorithm. Construct the Huffman’s tree for the following data and obtain its
Huffman’s Code.
25 a) Explain the Assignment problem in Branch and bound with Example. (16)
(OR)
b) Write backtracking algorithm for N queens problem and Hamiltonian problem. (16)
Online Questions
The average and
worst case O(nlogn)
2
complexity of O(n ) O(nlogn O(nlogn), ,O(nlogn
merge sort is O(n2),O(n2) ,O(nlogn) ),O(n2) O(nlogn) )
The time
taken by binary
search to serach
for an element in O(nlogn
sorted array is O(n) O(logn) ) O(n2) O(logn)
Let there be an
array of length
‘N’, and the
selection sort
algorithm is used
to sort it, how N2
many times a times
swap function is N log N N-1
called to complete times N-1 times
the execution? log N times times
The Sorting
method which is Quick
used for external Bubble sort Merge Radix Merge
sort is sort sort sort sort
Which of the
following sorting Shell
procedure is the Quick Heap sort Bubble Bubble
slowest? sort sort sort sort
the time required
to search an
element in a
binary search tree
having n elements O(1) O( log n) O(n) O( log n)
is O(n log n)
the number of
comparisons
required by
binary search of
100000 elements
is 15 20 25 30 20
O(nlogn
Best case running ) O(nlogn
time of quick sort O(n) O(logn) )
is O(n2)
which of the
sorting algorithm
does not have a
worst case merge merge
running time of Selection insertion sort sort
O(n2) sort sort quick sort
A sort which
compares
adjacent elements quick
in a list and sort
switches where insertion bubble bubble
necessary is a sort heap sort sort sort
A characteristic Order of the length of the maximu mean of Order of
of the data that list list m value data values the
binary search tree in the list
but the linear list
search ignores, is
the
A sort which uses
the binary tree
concept such that
any number is
larger than all the
numbers is the quick
subtree below it is Selection insertion sort heap
called sort sort heap sort sort
Worst case
complexity of the
insertion sort O(n-1) O(n2)
algorithm is O(n2) O(n) O(n+1)
Average case
complexity of the
insertion sort O(n-1) O(n2)
algorithm is O(n2) O(n) O(n+1)
Best case
complexity of the
insertion sort O(n-1) O(n)
algorithm is O(n2) O(n) O(n+1)
Worst case
complexity of the
bubble sort O(n2) O(n2)
algorithm is O(n3) O(n4) O(n)
Best case
complexity of the
bubble sort O(n2) O(n2)
algorithm is O(n3) O(n4) O(n)
Average case
complexity of the
bubble sort O(n2) O(n2)
algorithm is O(n3) O(n4) O(n)
Worst case
complexity of the
selection sort O(n2) O(n2)
algorithm is O(n3) O(n4) O(n)
Average case
complexity of the
selection sort O(n2) O(n2)
algorithm is O(n3) O(n4) O(n)