M.SC (I.t) Iiirdyear Daa Iitt63
M.SC (I.t) Iiirdyear Daa Iitt63
Unit - I
Introduction – Performance Analysis. Divide and conquer
Method: Binary Search, Finding Maximum and Minimum, Merge
Sort and Quick Sort.
Unit - II
Greedy Methods: Knapsack Problem, Minimum Cost
Spanning Trees, Optimal Storage on Tapes and Single Source
Shortest Path Problem.
Unit - III
Dynamic Programming: Multistage Graphs, 0/1 knapsack
and Traveling Salesman Problem. Basic Traversal and Search
Techniques: Techniques for Binary Tree, Techniques for Graphs:
Depth First Search and Breadth First Search - Connected
Components and Spanning Tree - Biconnected Components and
DFS.
Unit - IV
Backtracking: 8 Queens Problems, Sum of Subsets, Graph
Colouring, Hamiltonian Cycle and Knapsack Problem.
Unit - V
Branch and Bound: Least Cost Search. Bounding: FIFO
Branch and Bound and LC Branch and Bound. 0/1 Knapsack
Problem, Travelling Salesman Problem.
Text Books and References
1. E.Horowitz, S.Sahni and Sanguthevar Rajasekaran,
Fundamentals of Computer Algorithms , Second edition,
Universities Press.
2. S. K. Basu, Design Methods and Analysis of Algorithms , PHI,
2005.
3. Goodman and S. T. Hedetniem, Introduction to the Design and
Analysis of Algorithms , MGH, 1977
4. A.V. Aho, J.D. Ullman and J.E.Hospcraft, The Design and Analysis
of Computer Algorithms Pearson Education.
1
IITT 63: DESIGN AND ANALYSIS OF ALGORITHMS
Unit - I
Introduction – Performance Analysis. Divide and conquer
Method: Binary Search, Finding Maximum and Minimum, Merge
Sort and Quick Sort.
Introduction
What is an Algorithm?
Problem
algorithm
Notion(idea) of algorithm
Performance of a program:
Analytical
Experimental.
3
Time Complexity:
Space Complexity:
4
Data space: Data space is the space needed to store all
constant and variable values. Data space has two components:
Algorithm Specification
Pseudocode Convention
This is done to identify top level flow errors, and understand the
programming data flows that the final program is going to use.
5
This definitely helps save time during actual programming as
conceptual errors have been already corrected.
6
4. Assignment Statement are used to assign values to
variables.
While <condition> do
{
<statement-1>
<statement-2>
.
.
.
<statement-n>
}
repeat
<statement-1>
<statement-2>
.
.
.
<statement-n>
until <condition>
8
if <condition> then <statement 1> else <statement 2>
Here <condition> is a Boolean expression
case
{
:<condition-1>:<statement-1>
:<condition-2>:<statement-2>
.
.
.
:<condition-n>:<statement-n>
}
9. Read and Write : Input and Output are done using the
instructions read and write. No format is used to specify
the size of input or output quantities.
Recursive algorithm
9
We can view function as something that is called by another
function. It executes its code and then returns control to the
calling function.
The function which calls a function and that function calls its
called function is called indirect recursive function.
Performance analysis
Space complexity
Time complexity
10
Space complexity
Instruction space :
o It is the amount of space used to store compiled
version of program or instructions.
Environmental stack :
Data space
o It is the amount of memory used to store all the
variables and constants.
Example 1:
int sqare(int a)
{
Return a * a ;
}
Example 2:
Time Complexity
13
To calculate time complexity of an algorithm, we need to define a
model machine. Let us assume a machine with following
configuration...
Example 1:
int sum(int a, int b)
{
return a+b;
}
And it does not change based on the input values of a and b. That
means for all input values, it requires same amount of time i.e. 2
units.
If any program requires fixed amount of time for all input values
then its time complexity is said to be Constant Time
Complexity.
Example 2 :
int sum(int a[], int n)
{
int sum = 0,i; Time/Operation
1
Repeatation
1
Total
1
for (i = 0, i < n, i++) 1+1+1 1+(n+1)+n 2n+2
sum =sum + a[i]; 1+1 (1+1)2 2n
ruturn sum 1 1 1
4n+4
}
14
In above calculation Time/Operation
PERFORMANCE ANALYSIS
Asymptotic Notations
Asymptotic notation of an algorithm is a mathematical representation of its
complexity.
Generally, there are 3 types of asymptotic notations, they are
1. Big Oh (O)
2. Big Omega (Ω)
3. Big Theta (θ)
1. Big Oh(O)
15
If we want to represent f(n) as O(g(n)) then it must satisfy f(n) <= C x
g(n) for all values of C > 0 and n0>= 1
f(n) <= C g(n)
3n + 2 <= C n
Above condition is always TRUE for all values of C = 4 and n >= 2.
By using Big - Oh notation we can represent the time complexity as
follows...
3n + 2 = O(n)
2. Big Omega(Ω)
Example:
Consider the following f(n) and g(n)...
f(n) = 3n + 2 ; g(n) = n
If we want to represent f(n) as O(g(n)) then it must satisfy f(n) <= C x
g(n) for all values of C > 0 and n0>= 1
f(n) <= C g(n)
3n + 2 <= C n
Above condition is always TRUE for all values of C = 1 and n >= 1.
By using Big - Omega notation we can represent the time complexity as
follows...
3n + 2 = Ω(n)
3. Big Theta(θ)
That means Big - Theta notation always indicates the average time required
by an algorithm for all input values.
That means Big - Theta notation describes the average case of an algorithm
time complexity.
16
Big - Theta Notation can be defined as follows...
Consider function f(n) the time complexity of an algorithm and g(n) is the
most significant term. If C1*g(n) <= f(n) >= C2*g(n) for all n >= n0, C1,
C2 > 0 and n0 >= 1. Then we can represent f(n) as Θ(g(n)).
f(n) = Θ(g(n))
Example:
Consider the following f(n) and g(n)...
f(n) = 3n + 2 ; g(n) = n
if we want to represent f(n) as Θ(g(n)) then it must satisfy C1 g(n) <= f(n)
>= C2 g(n) for all values ofC1, C2 > 0 and n0>= 1
C1 g(n) <= f(n) >= C2 g(n)
C1 n <= 3n + 2 >= C2
Above condition is always TRUE for all values of C1 = 1, C2 = 4 and n >= 1.
By using Big - Theta notation we can represent the time complexity as
follows...
3n + 2 = Θ(n)
In divide and conquer approach, the problem in hand, is divided into smaller
sub-problems and then each problem is solved independently. When we
keep on dividing the sub problems into even smaller sub-problems, we may
eventually reach a stage where no more division is possible.
The solution of all sub-problems is finally merged in order to obtain the
solution of an original problem.
Divide and conquer approach is a three step approach,
1. Divide/Break
2. Conquer/Solve
17
3. Merge/Combine
1. Binary Search
2. Finding maximum and minimum
3. Merge Sort
4. Quick Sort
Linear Search:
This is the simplest method for searching.
In this technique of searching, the element to be found in searching the
elements to be found is searched sequentially in the list.
This method can be performed on a sorted or an unsorted list (usually
arrays).
In case of a sorted list searching starts from 0th element and continues until
the element is found from the list or the element whose value is greater than
(assuming the list is sorted in ascending order), the value being searched is
reached.
As against this, searching in case of unsorted list also begins from the
0thelement and continues until the element or the end of the list is reached.
18
Binary Search Algorithm compares the input element x with the value
of the middle element in array. If the values match, return the index of
middle.
For example:
Input:
List 0 1 2 3 4 5 6 7 8 9
10 12 20 32 50 55 65 80 99 110
0 1 2 3
10 12 20 32
Both are not matching. And 12 is smaller
than 50. So we search only in the left side sub list (i.e 10,12,20,32)
Step 2 :
Search Element (12) is compared with middle element (12)
Both are matching. So the resulting index is 1
5 6 7 8 9
55 65 80 99 110
Step 2 :
Search Element (99) is compared with middle element (80)
Both are not matching. And 99 is greater than 80. So we search again
the right side sub list (i.e 99,110)
8 9
99 110
Step 3 :
19
Search Element (99) is compared with middle element (99)
Both are matching. So the resulting index is 8.
1 Algorithm BinSrc(a,i , l, x)
//Given an array a[i, l] of elements in non decreasing order and 1 <= i <= l
//x is the searching element
2 {
3 if i = l
4 {
5 If a[i] = x then return i;
6 {
7 else return 0; (Searching element not in a given array)
8 }
9 Else
10 { // reduce problem in to sub problem.
11 mid :=[ (i+l)/2];
12 If a[mid] = x then return mid;
13 else
14 if x < a[mid] then BinSrc(a,I,mid-1,x);
15 else BinSrc(a,mid+1,l,x);
16 }
20
Finding Maximum and Minimum :
1 2 3 4 5 6 7 8 9
22 13 -5 -8 15 60 17 31 47
Recursively finding maximum and minimum
1,9,60,-8
1,5,22,-8 6,9,60,17
1,3,22,-5 4,5,15,-8 6,7,60,17 8,9,47,31
21
1,2,22,13
3,3,-5,-5
Tree of recursive calls of MaxMin
Merge Sort Algorithm
The algorithm divides the array in two halves; recursively sorts them
and finally merges the two sorted halves.
Merge sort keeps on dividing the array into equal halves until it can no
more be divided. By definition, if it is only one element in the array, it
is sorted. Then, merge sort combines the smaller sorted lists keeping
the new list sorted too.
For example :
110 32 12 55 50 80 65 99 90
110 32 12 55 50 80 65 99 90
12 55 50
110 32 80 65 99 90
110 32 12 55 50 80 65 99
32 110 12 55 50 65 80 90 99 90
12 32 55 110 50 65 80 90 99
12 32 50 55 110 65 80 90 99
12 32 50 55 65 80 90 99 110
Step :1 Check if it is only one element in the array it is already sorted, return
Step :2 Divide the array or list recursively into two halves until it can no more be divided
Step :3 Merge the smaller lists into new list in sorted order
22
1 Algorithm MergeSort(low, high)
2 {
3 If low < high
4 {
5 mid := [(low+high)/2]
6 MergeSort(low,mid);
7 MergeSort(mid+1,high);
8 Merge(low,mid,high);
9 }
10 }
1 Algorithm Merge(low,mid,high)
2 {
3 h:=low; I :=low; j := mid+1;
4 while a[h] < a[j] and j <= high
5 {
6 If a[h] <= a[j]
7 {
8 b[i] := a[h]; h :=h + 1
9 }
10 else
11 {
12 b[i] = a[j]; j := j+1;
13 }
14 I:=i+1;
15 }
16 If h > mid then
17 For k := j to high do
18 {
Quick Sort :
23
move to right side. Finally, the algorithm recursively sorts the sub arrays on
left and right of pivot element.
There are many different versions of quick Sort that pick pivot in different
ways.
Always pick first element as pivot.
Always pick last element as pivot (implemented below)
Pick a random element as pivot.
Pick median as pivot.
a[] 0 1 2 3 4 5 6
value 10 80 30 90 40 50 70
I = -1, j = 0, pivot = 70
24
Swap(a[i],a[j]
6 5 a[j]<pivot Do I++ 3 arr[]= {10,30,40,50,80,90,70}
Swap(a[i],a[j]
7 Swap(a[i+1),a[high]) arr[]= {10,30,40,50,70,90,80}
Now the j value is less than high-1 that j = 5 and high-1 is also 5,loop ends
Now take the last element a[6] = 80 as our new pivot do the partition again.
25
Unit - II
Greedy Methods: Knapsack Problem, Minimum Cost
Spanning Trees, Optimal Storage on Tapes and Single Source
Shortest Path Problem.
Greedy Methods
A greedy algorithm, as the name suggests, always makes the choice that
seems to be the best at that moment.
This means that it makes a locally-optimal choice in the hope that this choice
will lead to a globally-optimal solution.
The algorithm makes the optimal choice at each step as it attempts to find
the overall optimal way to solve the entire problem.
Knapsack Problem
26
value, determine the number of each item to include in a collection so that
the total weight is less than or equal to a given limit and the total value is as
large as possible.
The objective of Knapsack problem is to fill the knapsack with items to get
maximum benefit (value or profit) without crossing the weight capacity of
the knapsack.
Example :
Our objective is to fill the knapsack with items such that the benefit (value
or profit) is maximum.
Consider the following items and their associated weight and value:
Steps :
1. Calculate value per weight for each item (we can call this value density)
2. Sort the items as per the value density in descending order
3. Take as much item as possible not already taken in the knapsack
1. Density = value/weight
27
2. Sort the items as per density in descending order
Now we will pick items such that our benefit is maximum and total weight of the selected
items is at most W.
Objective is to fill the knapsack with items to get maximum benefit without
crossing the weight limit W = 16.
Is Weight(i)+ Total Weight <= w, if its yes, then take whole item
else
(w-total weight)/wi= 16-15=1/3=0.333
endif
So, total weight in the knapsack = 16 and total value inside it = 22.333336
Greedy algorithms don’t always yield optimal solutions but, when they do,
they’re usually the simplest and most efficient algorithms available.
28
Algorithm :
Algorithm Knapsock
Step : 1 For each item Ij,compute the ratio vj/wj (i.e., value per unit weight),
1 ≤ j ≤ n.
Step : 2 Sort the items in decreasing order of their ratios.
Step : 3 Set TotalWeight = 0, TotalBenefit = 0 , W=WeightLimit(16)
For (i=0 to n)
If item[i].weight+TotalWeight <= W
TotalWeight=TotalWeight+item[i].weight
TotalBenefit = TotalBenefit +item[i].benefit
Else
Wt = W-TotalWeight;
Value = wt * (item[i].benefit/item[i].weight)
TotalWeight = TotalWeight + Wt
TotalBenefit = TotalBenefit + value
Break; (exit loop)
Endif
Step : 4 Output the contents of Knapsack
29
Minimum Cost Spanning Trees- Network Design
What is graph?
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 –
A B B
A
C D E C D E
What is Tree?
30
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.
Definition:
31
The spanning tree does not have any cycle (loops).
Removing one edge from the spanning tree will make the graph
disconnected, i.e. the spanning tree is minimally connected.
Adding one edge to the spanning tree will create a circuit or loop, i.e.
the spanning tree is maximally acyclic (acyclic means without cycles).
you want to lease phone lines to connect them up with each other; and
You want a set of lines that connects all your offices with a minimum total
cost.
It should be a spanning (across) tree, since if a network isn’t a tree you can
always remove some edges and save money.
Telephone
Electrical
Hydraulic
32
TV cable
Computer
Road
Kruskal’s Algorithm:
Algorithm Kruskalsmst
Step:1 Sort all the edges in non-decreasing order of their weight.
Step :2 Pick the smallest edge. Check if it forms a cycle with the spanning
tree formed so far. If cycle is not formed, include this edge. Else,
discard it.
Step :3 Keep adding edges until we reach all vertices
.
Weight Edges
1 6-7
8 7 2 2-8
2 5-6
2 3
1
4 9 4 0-1
2
4 4 2-5
0
11 8 14 4
}
6 6-8
7 2-3
}
7
6
8 10 7 7-8
7 6 5
8 0-7
1 2
8 1-2
9 3-4
10 4-5
11 1-7
14 3-5
33
2. Pick edge 2-8
2
8
7 6
7 6 5
1 2
2
1
4
2
8
0
7 6 5
1 2
7 6 5
1 2
34
7. Pick edge 2-3: No cycle is formed, include it.
7
2 3
1
4
2 4
8
0
7 6 5
1 2
2 4
0
8
8 7 6 5
1 2
7
1 2 3 Total Weight is :
4 9 4+8+1+2+4+2+7+9=37
2 4
4
0
8
8 7 6 5
1 2
Since the number of edges included equals (V – 1), the algorithm stops
here.
1 t= θ;
2. While (t <less than n-1 edges) and E ≠ θ do
3. {
4. Choose an Edge (v,w) from E of lowest cost;
5. If (v,w) does not create cycle in t then
Add (v,w) to t;
else
discard (v,w);
6. }
35
Prim’s Algorithm
The first set contains the vertices already included in the MST, the
other set contains the vertices not yet included.
At every step, it considers all the edges that connect the two sets, and
picks the minimum weight edge from these edges.
After picking the edge, it moves the other endpoint of the edge to the
set containing MST.
Algorithm:
Algorithm Primsmst
Step 1: Create a set mstSet that keeps track of vertices already included in
MST.
Step 2: Assign a key value to all vertices in the input graph. Initialize all
key values as INFINITE. Assign key value as 0 for the first vertex
so that it is picked first.
Step 3: While mstSet doesn’t include all vertices
Include u to mstSet.
36
8 7
2 3
1
4 9
2
4
0
11 8 14 4
}
}
7
6
8 10
7 6 5
1 2
1. The set mstSet is initially empty and keys assigned to vertices are
2. Now pick the vertex with minimum key value. The vertex 0 is picked,
include it in mstSet.
mstSet = {0}
3. Adjacent vertices of 0 are 1 and 7 and its corresponding key values are
4 and 8. So the lowest key value 4 of vertex 1 is included in mstSet.
4
mstSet = {0,1} 1
mstSet = {0,1,7}
1
4
8 7
37
5. Now the open vertices are 1 and 7.
mstSet = {0,1,7,6}
1
4
8 7 6
1
mstSet = {0,1,7,6,5}
1
4
8 7 6 5
1 2
38
mstSet = {0,1,7,6,5,2}
1 2
4
4
8 7 6 5
1 2
mstSet = {0,1,7,6,5,2,8}
2
1
4 2
4
8
8 7 6 5
1 2
mstSet = {0,1,7,6,5,2,8,3}
7
2 3
1
4 2
4
8
0
8 7 6 5
1 2
39
10. Now the open vertices are 3 and 5
7
2 3
1
4 2
4
8
8 7 6 5
1 2
mstSet = {0,1,7,6,5,2,8,3,4}
7
2 3
1
2 9
4
4
4
8
0
8 7 6 5
1 2
Given ‘n’ programs to be stored on tape, the lengths of these programs are
i1, i2….in respectively. Suppose the programs are stored in the order of i1,
i2…in. We have a tape of length L i.e. the storage capacity of the tape is L.
We are also given ‘n’ programs where length of each program is i is Li.
It is now required to store these programs on the tape in such a way so that
the mean retrieval time is minimum. MRT is the average tome required to
retrieve any program stored on this tape.
40
The goal is to minimize MRT (Mean Retrieval Time).
Example 1:
2. We can store these 3 programs on the tape in any order but we want
that order which will minimize the MRT.
3. Suppose we store the programs in order (L1, L2, L3).
Algorithm MRT
Step 1: Sum = 0;
Step 2: For (i=1 ;i <= n,i++)
Step 3: {
Step 4: For (j=1;j<=I,j++)
Step 5: {
Step 6: Sum = sum + Lj;
Step 7: }
Step 8: MRT = sum/n;
Step 9: }
41
After getting the minimum MRT, the programs are stored in tape.
3 5 10
Algorithm Store(n,m)
// n is the number of programs, m is the number of tapes
Step 1: {
Step 2 : Sort the programs in ascending order using any of the sorting algorithm
Step 3 : J = 1; //Next tape to store on
Step 4 : For I = 1 to n do;
Step 5 : {
Step 6 : Write (“append program”, i ,”to permutation of tape”,j);
Step 7 : j = (j+1) mod m
Step 8 : }
Step 9 : }
Example:
Inserting 11 Inserting 12
Tape 1 11 Tape 1 11
Tape 2 Tape 2 12
Tape 3 Tape 3
Inserting 24 Inserting 34
Tape 1 11 Tape 1 11 34
Tape 2 12 Tape 2 12
Tape 3 24 Tape 3 24
42
Inserting 34 Inserting 34
Tape 1 11 34 Tape 1 11 34
Tape 2 12 34 Tape 2 12 34
Tape 3 24 Tape 3 24 34
Inserting 45 Inserting 56
Tape 1 11 34 45 Tape 1 11 34 45
Tape 2 12 34 Tape 2 12 34 56
Tape 3 24 34 Tape 3 24 34
43
Single Source Shortest Path Problem
Algorithm ShortestePath(v,cost,dist,n)
v- source vertex, dist[j] –shortest path between v to j, cost – overall
crossed paths, n – number of vertices
Step 1: {
Step 2: For I = I to n do
Step 3: S[i] = false; dist[i] := cost[v,i]
Step 4: }
Step 5: {
Step 6: S[v] = True ; S[v] = 0;
Step 7: {
Step 8: for j := 2 to n-1 do
Step 9: {
Step 10: S[j] = True;
Step 11: for (each k adjacent to j and s[k] = false) do
Step 12: {
Step 13: If dist[k]> dist[j] + cost[j,k] then
Step 14: dist[k] := dist[j] + cost[j,k]
Step 15: }
Step 16: }
45
Path Dist
1 1,4 10
50 10 2 1,4,5 25
1 2 3
3 1,4,5,2 45
10 20 35 4 1,3 45
30
15
4 5 6
15 3
44
Unit - III
Dynamic Programming: Multistage Graphs, 0/1 knapsack
and Traveling Salesman Problem. Basic Traversal and Search
Techniques: Techniques for Binary Tree, Techniques for Graphs:
Depth First Search and Breadth First Search - Connected
Components and Spanning Tree – Bi-connected Components and
DFS.
Dynamic Programming
The next time the same sub problem occurs, instead of re-
computing its solution, one simply looks up the previously
computed solution, thereby saving computation time as well as
storage space.
Multistage Graphs
45
In multistage graphs, starting vertex is called as source vertex
and the ending vertex is called as sink(go under).
For Example :
A 4 D
18
1 11 9
S B 5 E
2 T
13
16
5 2
C F
2
d(C,T) = min{2+d(F,T)}
= min{2+2}
=4
46
d(S,T) = min{1+23, 2+18, 5+4}
= min{24, 20, 9}
=9 d(S,T) = S C F T
47
Dynamic Programming approach – Backward Approach
d(S,A) = 1
d(S,B) = 2
d(S,C) = 5
48
0/1 knapsack
The objective is to fill the knapsack with items such that we have
a maximum profit without crossing the weight limit of the
knapsack.
Point to remember
In this problem we have a Knapsack that has a weight limit
W.
There are items i1, i2, ..., in each having weight w1, w2, …
wn and some benefit (value or profit) associated with it v1,
v2, ... vn
49
Example:
Assume that we have a knapsack with max weight capacity
W= 5. Our objective is to fill the knapsack with items such that
the benefit (value or profit) is maximum.
Following table contains the items along with their value and
weight.
Item i 1 2 3 4
Value 100 20 60 40
Weight 3 2 4 1
Total Items : 4
v[i,w] Wt=0 1 2 3 4 5
Item =0 0 0 0 0 0 0
50
If w[i] > w then
V[i,w]= v[i-1,w]
Else
Endif
for w = 0 to W do
v[0, w] = 0
endfor
for i = 1 to n do
v[i, 0] = 0
endfor
for w = 1 to W do
if w[i] > w then
v[i, w] = v[i-1, w]
else
if (val[i]+v[i-1,w-w[i]) > v[i-1,w]
v[i, w] = v[i] + v[i-1, w-w[i]]
else
v[i, w] = v[i-1, w]
endif
endif
endfor
Items that were put inside the knapsack are found using the
following rule
Algorithm ItemsPick
Set i =n and w= W
While i > 0 and w > 0 then i w V[i,w] V[I-1,w] Knapsack
4 5 140 120 4
If v[i,w] # v[i-1,w] then 3 4 100 100 4
Mark the ith item and Add to knapsack 2 4 100 100 4
Set i = i – 1 1 4 100 0 4,1=140
Set w = w-wt[i]
Else
Set I = I -1
Endif
Endwhile
52
So, items we are putting inside the knapsack are 4 and 1.
Given a list of cities and the distances between each pair of cities,
what is the shortest possible route that visits each city and
returns to the origin city?
20
10 15
4
25 30
2 3
35
Naive Solution:
n-1! = (4-1)! = 3 X 2 X 1 =6
1,2,3,4,1 = 10+35+30+20 = 95
1,2,4,3,1 = 10+25+30+15 = 80
1,3,2,4,1 = 15+35+25+20 = 95
1,3,4,2,1 = 15+30+25+10 = 80
1,4,2,3,1 = 20+25+35+15 = 95
1,4,3,2,1 = 20+30+35+10 = 95
53
Dynamic Programming Approach 1 2 3 4
1 0 10 15 20
Let’s starts from node 1 2 10 0 35 25
3 15 35 0 30
4 20 25 30 0
C(4,phi) = 20 ; C(3,phi) =15 ; C(2,phi) =10
C(2,{3}) = d(2,3)+C(3,phi) = 35 + 15 = 50
C(2,{4}) = d(2,4)+C(4,phi) = 25 + 20 = 45
C(3,{2}) = d(3,2)+C(2,phi) = 35 + 10 = 45
C(3,{4}) = d(3,4)+C(4,phi) = 30 + 20 = 50
C(4,{2}) = d(4,2)+C(2,phi) = 25 + 10 = 35
C(4,{3}) = d(4,3)+C(3,phi) = 30 + 15 = 45
54
Algorithm Dynamic_Programming_TSP
C ({1}, 1) = 0
for s = 2 to n do
for all subsets S Є {1, 2, 3, … , n} of size s and containing 1
C (S, 1) = ∞
endfor
for all j Є S and j ≠ 1
C (S, j) = min {C (S – {j}, i) + d(i, j) for i Є S and i ≠ j}
Endfor
endfor
Return min (C({1, 2, 3, …, n}, j) + d(j, i))
Trees
Tree is a non-linear data structure which organizes data in
hierarchical structure and this is a recursive definition.
Tree data structure is a collection of data (Node) which is
organized in hierarchical structure and this is a recursive
definition
Terminology used in trees
Root The top node in a tree.
The first node is called as Root Node.
Every tree must have root node.
Root node is the origin of tree data structure.
Parent In a tree data structure the node which is
predecessor of any node is called as PARENT
NODE.
Child Node In a tree data structure, the node which is
descendant(successor) of any node is called
55
as CHILD Node.
Leaf In a tree, elements with no children are called
leaves
Edge In a tree data structure, the connecting link
between any two nodes is called as EDGE. In a tree
with 'N' number of nodes there will be a maximum
of 'N-1' number of edges.
Binary Trees
In a normal tree, every node can have any number of children.
One is known as left child and the other is known as right child.
56
A
B C
D E F G
H I J
1. In - Order Traversal
2. Pre - Order Traversal
3. Post - Order Traversal
Algorithm Inorder(tree)
H-D-I-B-E-A-F-J-C-G
57
2. Pre-Order Traversal ( Root, Left, Right)
Algorithm Preorder(tree)
1. Visit the root.
2. Traverse the left subtree, (i.e.,call Preorder(left-subtree)
3. Traverse the right subtree, (i.e.,call Preorder(right-subtree))
A-B-D-H-I-E-C-F-J-G
Algorithm Postorder(tree)
1. Traverse the left subtree, i.e., call Postorder(left-subtree)
2. Traverse the right subtree,(i.e.,call Postorder(right-
subtree)
3. Visit the root.
H-I-D-E-B-J-F-G-A
Graph Traversal
Graph traversal is technique used for searching a vertex in a
graph.
The graph traversal is also used to decide the order of vertices to
be visit in the search process.
A graph traversal finds the edges to be used in the search
process without creating loops that means using graph traversal
we visit all vertices of graph without getting into looping path.
Techniques for Graph Tree Traversal
There are two graph traversal techniques and they are as,
1. Depth First Search (DFS)
2. Breadth First Search (BFS)
58
use Stack data structure with maximum size of total number of
vertices in the graph to implement DFS traversal of a graph.
Step 2 : Select any vertex as starting point for traversal. Visit that vertex and
push it on to the Stack.
Step 3 : Visit any one of the adjacent vertex of the vertex which is at top of
the stack which is not visited and push it on to the stack.
Step 4 : Repeat step 3 until there are no new vertex to be visit from the
vertex on top of the stack.
Step 5 : When there is no new vertex to be visit then use back tracking and
pop one vertex from the stack.
Rules to be followed
Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited.
Display it. Push it in a stack.
Example :
A B C
D E F
59
DFS Traversal
Step 2 : Visit any adjacent vertex of A Which is not visited (B), Push newly
visited vertex B on to the stack.
Step 3 : Visit any adjacent vertex of B Which is not visited (C), Push newly
visited vertex C on to the stack.
Step 4 : Visit any adjacent vertex of C Which is not visited (E), Push newly
visited vertex E on to the stack.
Step 5 : Visit any adjacent vertex of E Which is not visited (D), Push newly
visited vertex D on to the stack.
Step 7 : Visit any adjacent vertex of E Which is not visited (F), Push newly
visited vertex F on to the stack.
Step 8 : Visit any adjacent vertex of F Which is not visited (G), Push newly
visited vertex G on to the stack.
60
A B C
G
D E F
D Step -5 G Step -8 G
E Step -4 F Step -7 F
C Step -3 E Step -6 E
B Step -2 C
A Step -1 B
A
F
E E
C C C
B B B B
A A A A A
Step 2 : Select any vertex as starting point for traversal. Visit that vertex
and insert it into the Queue.
Step 3 : Visit all the adjacent vertices of the vertex which is at front of the
Queue which is not visited and insert them into the Queue.
61
Step 4 : When there is no new vertex to be visit from the vertex at front of
the Queue then delete that vertex from the Queue.
Step 6 : When queue becomes Empty, then produce final spanning tree by
removing unused edges from the graph
Rules to be followed
Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited.
Display it. Insert it in a queue.
Example :
A B C
G
BFS Traversal
D E F
Step 1 : Select the vertex A as starting vertex, Insert ‘A’ into queue
Step 2 : Visit all adjacent vertices of A which are not visited (D,E,B). Insert
newly visited vertices D,E,B into queue and delete A from the queue.
Step 3 : Visit all adjacent vertices of D Which are not visited (no vertices).
Delete D from queue.
Step 4 : Visit all adjacent vertices of E Which are not visited (C,F). Insert
newly visited vertices C & F into queue and delete E from the queue.
Step 5 : Visit all adjacent vertices of B Which are not visited (no vertices).
Delete C from the queue.
Step 6: Visit all adjacent vertices of C Which are not visited (G). Insert newly
62
visited vertex G into queue and delete C from the queue.
Step 7 : Visit all adjacent vertices of F Which are not visited (no vertices).
Delete F from the queue.
Step 8 : Visit all adjacent vertices of G Which are not visited (no vertices).
Delete G from the queue.
A B C
Step :1 D E F
A
Step :2
D E B
Step :3
E B
Step :4
B C F
Step :5
C F
Step :6
F G
63
Step :7
G
Step :7
Connected Components
Connected G1
Connected Graph G2
Graph G
64
Spanning Tree
65
Bi-connected Components and DFS
Articulation Point
6
6 1
5
1 5 4
4 2 3
7
7
3 10 9 8
8
10 9
Bi-connected graph:
66
A graph G is biconnected iff it contains no articulation point.
The above graph G is not biconnected. The following graph G1 is
biconnected.
1 2
5 4
Graph G1
For the case of graphs that are not biconnected, then this
algorithm will identify all the articulation points.
67
1. The root of the DFS tree is an articulation point if it has two
or more children.
Simple Algorithm
Algorithm Finding_DFS_Articulation
For each v in V do
{
If (pred[v] == null)
{ //v is a root
If (|Adj(v) | > 2)
v is an articulation point
} else
for each w in Adj(v) do
{
If (low(w) >= dfn[v])
v is an articulation point
}
}
68
8 1
6 1
7 2
1
1 5 4
2 9
4 6 2 3 3
7
6 2
3 3
8 10 9
10 4
5 5
4 10 9
8 7
5 9
6
7
8
Graph G Graph G (a) 10
DFS Spanning tree of Graph G
In the graph G (a), solid edges form the depth first spanning
tree. These edges are called tree edges. Broken edges(i.e all
remaining edges) are called back edges.
69
Low(u) = min {dfn(u), min{L(w) // w is a child of u},
Min {DFN{w} //u,w is a back edge. }
Vertex 1 2 3 4 5 6 7 8 9 10
DFN 1 6 3 2 7 8 9 10 5 4
Low 1 1 1 1 6 8 6 6 5 4
Child 4 5 9,10 3 6,7 0 8 0 0 0
low(w) >= root 6,6 5,3 1,1 8,7 0 6,9 0 0 0
dfn[v]) (AP) (AP) (AP) (AP)
71
Unit – IV
8 Queens Problem
Due to the first two restrictions, it's clear that each row and
column of the board will have exactly one queen.
72
The idea is to place queens one by one in different columns,
starting from the leftmost column. When we place a queen in a
column, we check for clashes with already placed queens. In the
current column, if we find a row for which there is no clash, we
mark this row and column as part of the solution. If we do not
find such a row due to clashes then we backtrack and return
false.
4) If all rows have been tried and nothing worked, return false
to trigger backtracking.
73
Procedure PLACE(k)
Global X(1:k)
Integer i, k
For I = 1 to k do
If X(i) = X(k) // two queens in the same column
Or Abs(X(i)-X(k)) = Abs(i-k) // in the same diagonal
Return false
Endif
Endfor
Return true
Algorithm NQueens(k,n)
Integer k, n, X(1:n)
X(1) = 0; k = 1 // k is the current row, X(k) is current column
While k > 0 // for all rows
X(k) = X(k) + 1 //move the next column
While X(k) <= n and not PLACE(k) do
X(k) = X(K) + 1
Repeat
If X(k) <= n // is position is found
Then if k =n // is solution is complete
Then print X
Else
K = k + 1;
X(k) = 0 // go to next row
Endif
Else
k = k -1 //backtrack
endif
repeat
end Nqueens
74
Sum of Subsets
o non-negative values.
o no duplicates are presented.
Example
S= {5,7,10,12,15,18,20}
0, 87
X1=1 X5=1
5, 82
X2=1
X2=0 15, 53
12, 75 X6=1 X6=0
5, 75
X3=1 X3=0 33, 54 15, 35
X3=1
22, 65 12, 65 X7=1
X4=1 X4=0 65 15,65
X4=0 35, 34
X4=1
34, 53 22,53
65 27,53 15,53
X5=1 X5=0
X5=0 X5=1 X5=1 X5=0 X5=1
30,38 15,38
37,38 42,38 27,38
49, 38 34, 38 X6=0
X6=1 X6=0
15,20
48,20 30,20
X7=1
35,0
75
So the subsets that add ups to sum 35 are,
{{15,20},{18,7,10}, {5,10,20}, {18,12,5}}
Example:
Solve following problem and draw portion of state space tree M = 35,
76
Subset Sum Description
Empty {} 0 Initial
{15} 15 Add 5th element
{15,18} 33 (33 < m) Add next element
{15,18,20} 53 (53 > m) Backtrack
{15,20} 35 Solution obtained m= 35
Algorithm:
Let, S is a set of elements and m is the expected sum of subsets.
Then:
77
Recursive Backtracking algorithm for sum of subsets problem
Algorithm SUMOFSUBS(s, k, r)
//initiations
Global integer M, n // M-Capacity, n – number of elements in set
Global real W(1:n) //Given Set
Global boolean X(1:n) // if x(1) is either 1(included in tree) or 0
Real r, s //s be cumulative sum,
Integer k, j
//generate left child. Note that s+W(k) <=M
X(k) = 1
If s+X(k) = M //subset found
return subset
else
if s+w(k)+w(k+1) <= M
call SUMOFSUBS(s+w(k), k+1, r-W(k))
endif
endif
Where
s= ∑𝑘−1
𝑗=1 (𝑊(𝑗)𝑥 𝑋(𝑗))
r= ∑𝑘−𝑛
𝑗=𝑘 (𝑊(𝑗))
78
Graph Coloring
Problems
79
If d is the degree of given graph G, then it can be colored with
(d+1) colors.
For Example:
Given graph G 1 2
4 3
m = 3 {R,G,B}
X1=R
X2=R
X2=B
1 RGRG
X2=G 2 RGRB
X
X4=B
4 RGBG
5 RBRG
X3=R
X3=R V
X3=G X3=B X4=G 6 RBRB
X4=R
V
X
X3=R X
X4=G X4=B X4=R
X4=G X4=B
X V
V X V X
=1+3+(3X3)+(3X3X3)+(3X3X3X3)
=Cn
80
Algorithm GraphColour(k)
For C = 1 to m
If issafe(k,c)
X[k] = c;
If k+1 < n
GraphColour(k+1)
Else
Print x[]
Endif
Endif
End for
Where,
k is the node we are going to colour in the level of
recursion.
X[k] is the array that holds the current colour at each node.
Algorithm issafe(k,c)
For j=1 to n
If G[k][j] == 1 and c ==x[j]
Return false
Endif
End for
Return true
Adjacency Matrix(G)
N 1 2 3 4
1 1 1 0 1
2 1 1 1 0
3 0 1 1 1
4 1 0 1 1
81
Graph C If G[K][J] if Return
Colour C=X[j]
k
1 1 G[1][1]=1 1≠0 Return True
1 1 G[1][2]=1 1≠0 X[1]=1 (red)
1 1 G[1][3]=0 1≠0
1 1 G[1][4]=1 1≠0
2 1 G[2][1]=1 1=1 Return False
2 2 G[2][1]=1 2≠1 Return True
2 2 G[2][2]=1 2≠0 X[2]=2 (green)
2 2 G[2][3]=1 2≠0
2 2 G[2][4]=0 2≠0
3 1 G[3][1]=0 1=1 Return True
3 1 G[3][2]=1 1≠2 X[3]=1 (red)
3 1 G[3][3]=1 1≠0
3 1 G[3][4]=1 1≠0
4 1 G[4][1]=1 1=1 Return False
4 2 G[4][1]=1 2≠1 Return True
4 2 G[4][2]=0 2=2 X[3]=2 (green)
4 2 G[4][3]=1 2≠1
4 2 G[4][4]=1 2≠0
82
Hamiltonian Cycle (William Rowan Hamilton -1859)
1 2 3 4
8 7 6 5
1,3,4,5,6,7,8,2,1 and
1,2,8,7,6,5,4,3,1.
83
Procedure:
ii. The current vertex must be distinct and should not have
been visited earlier.
6. When these two conditions are satisfied the current vertex is included
in the cycle, else the next vertex is tried.
7. When the nth vertex is visited we have to check, is there any path from
nth vertex to first vertex. if no path, the go back one step and after the
previous visited node.
84
Repeat
X [k]=(X [k]+1) mod (n+1); //next vertex
If (X [k]=0)
return;
Endif
X2
1 2
3 2 3
X= 1,2,3,4,1 4 2 4
X =1,4,3,2,1 X3
4 3
4 2 3 2
3 4
X4
85
Knapsack Problem using Backtracking:
We are given ‘n’ positive weights Wi and ’n’ positive profits Pi, and a
positive number ‘m’ that is the knapsack capacity, the is problem calls
for choosing a subset of the weights such that,
WiXi
1i n
m and PiXi is Maximized.
1i n
The Solution space is the same as that for the sum of subset’s
problem.
Bounding functions are needed to help kill some live nodes without
expanding them. A good bounding function for this problem is obtained
by using an upper bound on the value of the best feasible solution
obtainable by expanding the given live node.
The profits and weights are assigned in descending order depend upon
the ratio.
Solution :
After assigning the profit and weights ,we have to take the first object
weights and check if the first weight is less than or equal to the
capacity, if so then we include that object (i.e.) the unit is 1.(i.e.) K
1.
Then We are going to the next object, if the object weight is exceeded
that object does not fit. So unit of that object is ‘0’.(i.e.) K=0.
Then We are going to the bounding function ,this function determines
an upper bound on the best solution obtainable at level K+1.
86
Algorithm:
Algorithm Bknap(k,cp,cw)
// ‘m’ is the size of the knapsack; ‘n’ no.of weights & profits. W[]&P[] are
the //weights & weights. P[I]/W[I] P[I+1]/W[I+1].
//fwFinal weights of knapsack.
//fp final max.profit.
//x[k] = 0 if W[k] is not the knapsack,else X[k]=1.
{
// Generate left child.
If((W+W[k] m) then
{
Y[k] =1;
If(k<n) then Bnap(k+1,cp+P[k],Cw +W[k])
If((Cp + p[w] > fp) and (k=n)) then
{
fp = cp + P[k];
fw = Cw+W[k];
for j=1 to k do X[j] = Y[j];
}
}
87
Algorithm for Bounding function:
Algorithm Bound(cp,cw,k)
// cp current profit total.
//cw current weight total.
//kthe index of the last removed item.
//mthe knapsack size.
{
b=cp;
c=cw;
for I =- k+1 to n do
{
c= c+w[I];
if (c<m) then b=b+p[I];
else return b+ (1-(c-m)/W[I]) * P[I];
}
return b;
}
88
Unit - V
Branch and Bound: Least Cost Search. Bounding: FIFO
Branch and Bound and LC Branch and Bound. 0/1 Knapsack
Problem, Travelling Salesman Problem.
89
The algorithm explores branches of this tree, which represent
subsets of the solution set. Before enumerating the candidate
solutions of a branch, the branch is checked against upper and
lower estimated bounds on the optimal solution, and is discarded
if it cannot produce a better solution than the best one found so
far by the algorithm.
FIFO (first in, first out): always the oldest node in the queue is
used to extend the branch. This leads to a breadth-first search,
where all nodes at depth d are visited first, before any nodes at
depth d+1 are visited.
LIFO (last in, first out): always the youngest node in the queue is
used to extend the branch. This leads to a depth-first search,
where the branch is extended through every 1st child discovered
at a certain depth, until a leaf node is reached.
90
It realizes that it has made a bad It realizes that it already has a better
choice & undoes the last choice optimal solution that the pre-solution
by backing up. leads to so it abandons that pre-solution.
It search the state space tree It completely searches the state space
until it found a solution. tree to get optimal solution.
Upper Bound:
91
approaches exhaustive enumeration as the size (n-dimensional
volume) of the region tends to zero
We are given a set of cities and distances between every pair of cities. The problem
is to find the shortest possible route that visit every city exactly one and returns to
the starting city.
12
A 11 CA A B C D Reduce
by
5 4 7
3
6
A ∞ 4 12 7 4
10
18
B 5 ∞ ∞ 18 5
B D C 11 ∞ ∞ 6 6
D 10 2 3 ∞ 2
2
A B C D A B C D
A ∞ 0 7 3 Colum A ∞ 0 8 3
B 0 ∞ ∞ 13 B 0 ∞ ∞ 13
C 5 ∞ ∞ 0 Reduced C 5 ∞ ∞ 0
D 8 0 0 ∞ D 8 0 1 ∞
(Reduced matrix) - - 1 -
92
So cost of note-1 is
Cost (1) =4 + 5 + 6 + 2 + 1 = 18
A B C D -
A ∞ ∞ ∞ ∞ 13
-
B ∞ ∞ ∞ 13 -
C 5 ∞ ∞ 0
D 8 ∞ 0 ∞
Row reduction Now we reduce this matrix and find the cost
5 -A - B C D A B C D
Colu
A ∞ ∞ ∞ ∞ m A ∞ ∞ ∞ ∞
B ∞ ∞ ∞ 0 B ∞ ∞ ∞ 0
C 5 ∞ ∞ 0 Reduced C 0 ∞ ∞ 0
D 8 ∞ 0 ∞ D 3 ∞ 0 ∞
5 - - -
So cost (2) = Cost (1) = Reduction + M[A,B]
= 18+18+0=36
93
A B C D This So Cost (3) = Cost(1)+ Reduction +M[A,C]
A ∞ ∞ ∞ ∞ Matrix = 18+0+7=25
Is
B 0 ∞ ∞ 13 already
C ∞ ∞ ∞ 0 Reduced
D 8 0 ∞ ∞
5 -A - B C D A B C A
A ∞ ∞ ∞ ∞ - A ∞ ∞ ∞ ∞
Row
B 0 ∞ ∞ ∞ - B 0 ∞ ∞ ∞
C 5 ∞ ∞ ∞ 5 Reduced C 0 ∞ ∞ ∞
D ∞ 0 0 ∞ - D ∞ 0 0 0
94
5 -A - B C D A B C A
A ∞ ∞ ∞ ∞ - A ∞ ∞ ∞ ∞
Row
B ∞ ∞ ∞ 13 13 B ∞ ∞ ∞ 0
C ∞ ∞ ∞ ∞ - Reduced C ∞ ∞ ∞ ∞
D 8 ∞ ∞ ∞ 8 D 0 ∞ ∞ ∞
A Cost (1) = 18
B D
C
Cost (2) = 36 Cost (2) = 25 Cost (3) = 25
1
A
2 3 4
B D ACD
C
5 6
B D
B
7
96