Design and Analysis of Algorithm Questions and Answers
Design and Analysis of Algorithm Questions and Answers
Design and Analysis of Algorithm Questions and Answers
input or inputs for which the algorithm runs the fastest among all possible inputs of that size.
13. State
how binomial coefficient is computed through algorithm? Or Write an algorithm to find the
number of binary digits in the binary representation of a positive decimal integer. [M-15]
ALGORITHM Binary[n]
//Input: A positive decimal integer n
//Output: The number of binary digits in n‘s binary
representation count ←1
while n >1do
count ←count
+1 n← n/2
return count
16 marks
1. Explain the various Asymptotic Notations used in algorithm design? Or Discuss the
properties of asymptotic notations. Or Explain the basic efficiency classes with notations.
[N-14][M-15][M-16]
Asymptotic notation is a notation, which is used to take meaningful statement about the
efficiency of
EXAMPLE Compute the factorial function F[n] = n! for an arbitrary nonnegative integer n
ALGORITHM F[n]
//Computes n! recursively
//Input: A nonnegative integer n
//Output: The value of n!
if n = 0 return 1
else return F[n − 1] * n
Simplify the sum using standard formulas and rules [see Appendix A]
EXAMPLE Consider the problem of finding the value of the largest element in a list of n numbers
ALGORITHM MaxElement[A[0..n−1]]
//Determines the value of the largest element in a given array
//Input: An array A[0..n−1] of real numbers
//Output: The value of the largest
element in A maxval ←A[0]
fori ←1 to
n−1do if A[i]>
maxval
maxval←A[i]
return maxval
EXAMPLE 2 Given two n×n matrices A and B, find the time efficiency of the definition-based
algorithm for computing their produc tC =AB. By definition,C is an n×n matrix whose elements
are computed as the scalar [dot] products of the rows of matrix A and the columns of
matrixB: whereC[i, j]=A[i, 0]B[0, j]+...+A[i, k]B[k, j]+...+A[i, n−1]B[n−1, j] for every pair of
indices 0 ≤i, j ≤n−1.
2. What are the fundamental steps to solve an algorithm? Explain. Or Describe in detail about
the steps in analyzing and coding an algorithm. [M-15][M-14]
An algorithm is a sequence of unambiguous instructions for solving problem, i.e., for
obtaining a required output for any legitimate input in a finite amount of time.
Algorithmic steps are
ii. If the problem is to so complex and not able to get exact solution then
it is called approximation algorithm.
Simplicity of an algorithm
Generality of an algorithm
Time efficiency of an algorithm
Space efficiency of an algorithm
f. Coding an algorithm
i. The coding / implementation of an algorithm is done by a suitable programming
language like C, C++, JAVA.
ii. The transition from an algorithm to a program can be done either incorrectly or
very inefficiently. Implementing an algorithm correctly is necessary. The Algorithm
power should not reduce by inefficient implementation.
iii. Standard tricks like computing a loop‘s invariant outside the loop, collecting
common sub expressions, replacing expensive operations by cheap ones, selection of
programming language and so on should be known to the programmer.
first match occurring in the ith position of the list is pin for every i, and the
number of comparisons made by the algorithm in such a situation is
obviously i. Cavg(n) =(n+ 1)/2
5. Orders of growth
Big oh , Big omega and Big Theta notations described above 2 Question.
2 marks
Steps
1. Divide Step: If given array A has zero or one element, return S; it is already sorted.
Otherwise, divide A into two arrays, A1 and A2, each containing about half of the elements
of A.
13. Distinguish
between BFS and DFS. [M-13]
DFS follows the procedure:
a. Select an unvisited node x, visit it, and treat as the current node
b. Find an unvisited neighbor of the current node, visit it, and make it the new current node;
c. If the current node has no unvisited neighbors, backtrack to the its parent, and make
that parent the new current node;
d. Repeat steps 3 and 4 until no more nodes can be visited.
e. If there are still unvisited nodes, repeat from
step 1. BFS follows the procedure:
f. Select an unvisited node x, visit it, have it be the root in a BFS tree being formed.
Its level is called the current level.
g. From each node z in the current level, in the order in which the level nodes were
visited, visit all the unvisited neighbors of z. The newly visited nodes from this
level form a new level that becomes the next current level.
h. Repeat step 2 until no more nodes can be visited.
i. If there are still unvisited nodes, repeat from Step 1
16 marks
1. Write the algorithm to perform Binary Search and compute its time complexity. Or
Explain binary search algorithm with an example.[N-14][N-15]
BINARY SEARCH ALGORITHM
l 0; r n-1
while l r do
m [l+r]/2
if K = A[m] return m
else if K < A[m] r
m-1 else l m+1
return -1
For Example
The following is our sorted array and let us assume that we need to search the location of
value 31 using binary search.
Now we compare the value stored at location 4, with the value being searched, i.e. 31. We
find that the value at location 4 is 27, which is not a match. As the value is greater than 27
and we have a sorted array,
to change our low to mid + 1 and find the new mid value again. low = mid + 1, mid = low +
[high - low] / 2 Our new mid is 7 now. We compare the value stored at location 7 with our
target value 31.
The value stored at location 7 is not a match, rather it is less than what we are looking for.
So, the value must be in the lower part from this location.
We compare the value stored at location 5 with our target value. We find that it is a match.
Binary search halves the searchable items and thus reduces the count of comparisons to be
made to very less numbers.
2. Write down the algorithm to construct a convex hull based on divide and conquer
strategy. Or Explain the convex hull problem and the solution involved behind it.[N-15]
CONVEX HULL OR QUICK HULL PROBLEM
Convex hull: smallest convex set that includes given points. An O[n^3] brute force time.
Assume points are sorted by x-coordinate values
Identify extreme points P1 and P2 [leftmost and rightmost]
Compute upper hull recursively:
1. find point Pmax that is farthest away from line P1P2
2. compute the upper hull of the points to the left of line P1Pmax
3. compute the upper hull of the points to the left of line PmaxP2
Compute lower hull in a similar manner
Finding point farthest away from line P1P2 can be done in linear time
Time efficiency: T[n] = T[x] + T[y] + T[z] + T[v] + O[n], where x + y +
z +v <= n. worst case: Θ[n2] T[n] = T[n-1] + O[n]
average case: Θ[n]
If points are not initially sorted by x-coordinate value, this can be accomplished in
O[n log n] time. Several O[n log n] algorithms for convex hull are known.
3. Explain the closest pair and convex hull problem in brute force technique. [N-15]
CLOSEST-PAIR AND CONVEX-HULL PROBLEMS
Find the two closest points in a set of n points [in the two-dimensional
Cartesian plane]. Brute-force algorithm
Compute the distance between every pair of distinct points
And return the indexes of the points for which the distance is the smallest.
ALGORITHM BruteForceClosestPair[P ]
//Finds distance between two closest points in the plane by brute force
//Input: A list P of n [n ≥ 2] points p1[x1, y1], . . . , pn[xn, yn]
//Output: The distance between the closest pair of
points d←∞
for i ←1 to n − 1
do for j ←i + 1 to
n do
d ←min[d, sqrt[[xi− xj ]2 + [yi− yj ]2]] //sqrt is
square root return d
CONVEX HULL THEOREM The convex hull of any set S of n>2 points not all on the same
line is a convex polygon with the vertices at some of the points of S. [If all the points do
lie on the same line, the polygon degenerates to a line segment but still with the
endpoints at two points of S.]
The convex-hull problem is the problem of constructing the convex hull for a given set S
of n points. To solve it, we need to find the points that will serve as the vertices of
the polygon in question. Mathematicians call the vertices of such a polygon
―extreme points.‖
4. Develop a pseudo code for divide and conquer algorithm for merge two sorted arrays
into a single sorted one – explain with example. or Write down the algorithm for merge
sorting. Explain how the following elements get sorted
[310,285,179,652,351,423,861,254,450,520] or Sort the following set of elements using
merge sort:12,24,8,71,4,23,6,89,56. Or State and Explain Merge sort algorithm and give
the recurrence relation and efficiency [M-14] [N-15][M-15][M-
16]
MERGE SORT
Merge sort is a perfect example of a successful application of the divide-and conquer
technique. It sorts a given array A[0..n − 1] by dividing it into two halves A[0..n/2− 1]
and A[n/2..n − 1], sorting each of them recursively, and then merging the two smaller
sorted arrays into a single sorted one.
Procedure:
Merge sort sorts a given array A[0..n-1] by dividing it into two halves a[0..[n/2]-1] and
A[n/2..n-1] sorting each of them recursively then merging the two smaller sorted arrays
into a single sorted one.
Divide Step: If given array A has zero or one element, return S; it is already sorted.
Otherwise, divide A into two arrays, A1 and A2, each containing about half of the
elements of A.
Recursion Step: Recursively sort array A1 and A2.
Conquer Step: Combine the elements back in A by merging the sorted arrays A1
and A2 into a sorted sequence
The non-recursive version of Mergesort starts from merging single elements into sorted pairs.
ALGORITHM Merge[B[0..p − 1], C[0..q − 1], A[0..p + q − 1]]
//Merges two sorted arrays into one sorted array
//Input: Arrays B[0..p − 1] and C[0..q − 1] both sorted
//Output: Sorted array A[0..p + q − 1] of the elements of
B and C i ←0; j ←0; k←0
while i <p and j <q
do if B[i]≤ C[j ]
A[k]←B[i]; i ←i + 1
else A[k]←C[j ]; j ←j + 1
k←k + 1
if i = p
8 3 2 9 7 1 5 4
8 3 2 9 71 5 4
8 3 2 9 7 1 5 4
3 8 2 9 1 7 4 5
2 3 8 9 1 4 5 7
1 2 3 4 5 7 89
QUICK SORT
Quicksort is the other important sorting algorithm that is based on the divide-and-
conquer approach. quicksort divides input elements according to their value. A
partition is an arrangement of the array‘s elements so that all the elements to the left
of some element A[s] are less than or equal to A[s], and all the elements to the right
of A[s] are greater than or equal to it:
Sort the two subarrays to the left and to the right of A[s] independently. No work
required to combine the solutions to the subproblems.
Here is pseudocode of quicksort: call Quicksort[A[0..n − 1]] where As a partition algorithm use
the HoarePartition
ALGORITHM Quicksort[A[l..r]]
//Sorts a subarray by quicksort
//Input: Subarray of array A[0..n − 1], defined by its left and right
// indices l and r
//Output: Subarray A[l..r] sorted in
nondecreasing order if l < r
Quicksort[A[l..s − 1]]
Quicksort[A[s + 1..r]]
Example: 5 3 1 9 8 2 4 7
2 314589 7
1 234578 9
1 234578 9
1 234578 9
1 234578 9
5. Explain the method used for performing Multiplication of two large integers. Explain
how divide and conquer method can be used to solve the same. [M-16]
Some applications like modern cryptography require manipulation of integers
that are over 100 decimal digits long. Since such integers are too long to fit in a single
word of a modern
computer, they require special treatment.In the conventional pen-and-pencil algorithm for
multiplying two n- digit integers, each ofthe n digits of the first number is multiplied by each
of the n digits of the second number for thetotal of n2 digit multiplications.
The divide-and-conquer method does the above multiplication in less than n2 digit
multiplications. For any pair of two-digit numbers a = a1a0 and b = b1b0, their product
c can be
6. Find all the solution to the traveling salesman problem [cities and distance shown
below] by exhaustive search. Give the optimal solutions. Or Explain exhaustive searching
techniques with example. Or Find the optimal solution to the fractional knapsack problem
with example. Or Solve the given knapsack problem un=3,m=20,[p1,p2,p3]=[25,24,15],
[w1,w2,w3]=[18,15,10][M-15][N-14][M-14][N-15] [M-16] TRAVELING SALESMAN PROBLEM
The traveling salesman problem [TSP] is one of the combinatorial problems. The problem
asks to find the shortest tour through a given set of n cities that visits each city exactly
once before
returning to the city where it started.The problem can be conveniently modeled by a
weighted graph, with the graph‘s vertices representing the cities and the edge weights
specifying the distances. Then the problem can bestated as the problem of finding the shortest
Hamiltonian circuit of the graph.
[A Hamiltoniancircuit is defined as a cycle that passes through all the vertices of the graph exactly
once].
A Hamiltonian circuit can also be defined as a sequence of n + 1 adjacent vertices
vi0, vi1, . . . , vin−1, vi0, where the first vertex of the sequence is the same as the last one
and all the other n − 1 vertices are distinct. All circuits start and end at one particular
vertex.
Figure presents a small instance of the problem and its solution by this
Tour Length
a ---> b ---> c ---> d ---> a I = 2 + 8 + 1 + 7 = 18
a ---> b ---> d ---> c ---> a I = 2 + 3 + 1 + 5 = 11 optimal
a ---> c ---> b ---> d ---> a I = 5 + 8 + 3 + 7 = 23
a ---> c ---> d ---> b ---> a I = 5 + 1 + 3 + 2 = 11 optimal
a ---> d ---> b ---> c ---> a I = 7 + 3 + 8 + 5 = 23
a ---> d ---> c ---> b ---> a I = 7 + 1 + 8 + 2 = 18
FIGURE Solution to a small instance of the traveling salesman problem by exhaustive search.
Time Complexity of TSP:O[n-1!]
KNAPSACK PROBLEM
Given n items of known weights w1, w2, . . . , wn and values v1, v2, . . . , vn and a
knapsack of capacity W, find the most valuable subset of the items that fit into the
knapsack.
Real time examples:
A Thief who wants to steal the most valuable loot that fits into his knapsack,
A transport plane that has to deliver the most valuable set of items to a remote
location without exceeding the plane‘s capacity.
The exhaustive-search approach to this problem leads to generating all the subsets of the set
of n items given, computing the total weight of each subset in order to identify feasible
subsets [i.e., the ones with the total weight not exceeding the knapsack capacity], and
finding a subset of the largest value among them.
Time Complexity of KP: O[2n]
Given n items:
• weights: w1 w2 … wn
• values: v1 v2 … vn
• a knapsack of capacity W
Find most valuable subset of the items that fit into the
knapsack Example: Knapsack capacity W=16
item weight value
1 2 $20
2 5 $30
3 10 $50
4 5 $10
ASSIGNMENT PROBLEM
There are n people who need to be assigned to n jobs, one person per job. The cost of
assigning person i to job j is C[i,j]. Find an assignment that minimizes the total cost.
For Example,
Job 0 Job 1 Job 2 Job 3
Person 0 9 2 7 8
Person 1 6 4 3 7
Person 2 5 8 1 8
Person 3 7 6 9 4
9 2 7 8
C= 6 4 3 7
5 8 1 8
76 9 4
Assignment [col.#s] Total Cost
1, 2, 3, 4 9+4+1+4=18
1, 2, 4, 3 9+4+8+9=30
1, 3, 2, 4 9+3+8+4=24
1, 3, 4, 2 9+3+8+6=26
1, 4, 2, 3 9+7+8+9=33
1, 4, 3, 2 9+7+1+6=23
etc.
[For this particular instance, the optimal assignment can be
found by exploiting the specific features of the number given. It
is: 2,1,3,4 ]
2 marks
4. Write the difference between the Greedy method and Dynamic programming.
Spanning tree of a connected graph G: a connected acyclic subgraph of G that includes all
of G‗s vertices.Minimum spanning tree of a weighted, connected graph G: a spanning
tree of G of the minimum total weight.
16 marks
1. Write and analyze the Prim‘s Algorithm. How do you construct a MST using Kruskal‘s
Algorithm? Explain. Define Spanning tree. Discuss the design steps in Kruskal‘s algorithm
tp construct MST with example.[M-14][M-15][N-15]
MINIMUM SPANNING TREE
A spanning tree of an undirected connected graph is its connected acyclic
subgraph [i.e., a tree] that contains all the vertices of the graph. If such a graph has
weights assigned to its edges, a minimum spanning tree is its spanning tree of the
smallest weight, where the weight of a tree is defined as the sum of the weights on all
its edges.
The minimum spanning tree problem is the problem of finding a minimum spanning tree
for a given weighted connected graph.
PRIM’S ALGORITHM
Prim‘s algorithm constructs a minimum spanning tree through a sequence of expanding
sub trees. The initial sub tree in such a sequence consists of a single vertex selected
arbitrarily from the set V of the graph‘s vertices. On each iteration, the algorithm
expands the current tree in the greedy manner by simply attaching to it the nearest
vertex not in that tree.
ALGORITHM Prim[G]
//Prim‘s algorithm for constructing a minimum spanning tree
//Input: A weighted connected graph G = V, E
//Output: ET , the set of edges composing a minimum spanning
tree of G VT←{v0} //the set of tree vertices can be initialized
with any vertex ET←∅
for i ←1 to |V| − 1 do
find a minimum-weight edge e∗ = [v∗, u∗] among all the
edges [v, u] such that v is in VT and u is in V − VT
VT←VT�
{u∗}
ET←ET�
{e∗} return
ET
For example,
46
KRUSKAL ALGORITHM
The algorithm begins by sorting the graph‘s edges in non-decreasing order of their
weights. Then, starting with the empty sub graph, it scans this sorted list, adding the next
edge on the list to the current sub graph if such an inclusion does not create a cycle and
simply skipping the edge otherwise.
For Example,
ALGORITHM Kruskal[G]
//Kruskal‘s algorithm for constructing a minimum spanning tree
//Input: A weighted connected graph G = V, E
//Output: ET , the set of edges composing a minimum spanning
tree of G sort E in nondecreasing order of the edge weights
w[ei1] ≤ . . . ≤ w[ei|E|]
ET←∅; ecounter ←0 //initialize the set of tree edges and
its size k←0 //initialize the number of processed edges
while ecounter < |V| −
1 do k←k + 1
if ET� {eik} is acyclic
ET<-ET<- {eik}; ecounter<-ecounter
+ 1 return ET
Efficiency of the Kruskal’s algorithm
If a graph is represented by its adjacency lists and the priority queue is
implemented as a min-heap, the running time of the algorithm is O[|E| log |V |]
in a connected graph, where
|V| − 1≤ |E|.
2. Write down and explain the algorithm to solve all pair‘s shortest paths problems? Or
Solve all pairs shortest path problem for the digraph with the weight matrix given
below. Or Describe all pairs shortest path problem and write procedure to compute
lengths of shortest paths.[M-13][M-14] [M-15]
FLOYD’S ALGORITHM
Given a weighted connected graph (undirected or directed), the all-pairs shortest paths
problem asks to find the distances—i.e., the lengths of the shortest paths— from each
vertex to all other vertices.
ALGORITHM Floyd(W[1..n, 1..n])
//Implements Floyd‘s algorithm for the all-pairs shortest-paths problem
//Input: The weight matrix W of a graph with no negative-length cycle
//Output: The distance matrix of the shortest paths‘
lengths D ←W //is not necessary if W can be
overwritten
for k←1 to n
do for i ←1 to
n do for j ←1
to n do
D[i, j ]←min{D[i, j ], D[i, k]+
D[k, j]} return D
Efficiency of Floyd‘s Algorithm: Time efficiency Θ(n 3) and Space Efficiency
is Θ(n2) For Example,
DIJKSTRA’S ALGORITHM
Consider the single-source shortest-paths problem: for a given vertex called the
source in a weighted connected graph, find shortest paths to all its other vertices. The
single-source shortest-paths problem asks for a family of paths, each leading from the source
to a different vertex in the graph, though some paths may, of course, have edges in common.
There are several well-known algorithms for finding shortest paths, including Floyd‘s algorithm
for the more general all-pairs shortest-paths problem and the best-known algorithm for the
single-source shortest-paths problem, this algorithm is applicable to undirected and
directed graphs with nonnegative weights only, First, it finds the shortest path from the
source to a vertex nearest to it, then to a second nearest, and so on. In general, before
it‘s ith iteration commences, the algorithm has already identified the shortest paths to i − 1
other vertices nearest to the source.
For Example,
ALGORITHM Dijkstra[G, s]
//Dijkstra‘s algorithm for single-source shortest paths
//Input: A weighted connected graph G = V, E with nonnegative weights
// and its vertex s
//Output: The length dv of a shortest path from s to v
// and its penultimate vertex pv for every vertex
v in V Initialize[Q] //initialize priority queue to
empty
for every vertex v in V
dv←∞; pv←null
Insert[Q, v, dv] //initialize vertex priority in the
priority queue ds←0; Decrease[Q, s, ds] //update
priority of s with ds VT←∅
for i ←0 to |V| − 1 do
u∗←DeleteMin[Q] //delete the minimum priority
element VT←VT� {u∗}
for every vertex u in V − VT that is adjacent to u∗ do if du∗ +
w[u∗, u] < du du←du∗ + w[u∗, u]; pu←u∗ Decrease[Q, u, du]
A tree constructed by the above algorithm is called a Huffman tree. It defines—in the
manner
4. Write short notes on optimal binary search tree. Or Write an algorithm to construct the
optimal binary search tree given the roots r(i,j), 0<=i<=j<=n. Also prove that this could
be performed in time O(n)
[M-15][N-14]
Let C[i,j] be minimum average number of comparisons made in T[i,j], optimal BST for keys ai < …<
aj
, where 1 ≤ i ≤ j ≤ n. Consider optimal BST among all BSTs with some ak (i ≤ k ≤ j ) as
their root; T[i,j] is the best among them.
0 1 j n
0 p1
1
goal
0
p
2
C[i,j]
i
pn
n+1
0
Running time of this algorithm: Time Efficiency Θ(n2) and Space Efficiency :
Θ(n3) For Example,
Extreme point theorem states that if S is convex and compact in a locally convex space,
then S is the closed convex hull of its extreme points: Convex set has its extreme points at
the boundary. Extreme points should be the end points of the line connecting any two points
of convex set.
16 marks
1. Summarize the Simplex method. Or Write the procedure to initialize simplex which
determine if a linear program is feasible or not . Or Use simplex to solve the farmers problem
given below.[M-16][N-15][M-15] Simplex method procedure:
Step 0 [Initialization] Present a given LP problem in standard form and set up initial tableau.
Step 1 [Optimality test] If all entries in the objective row are nonnegative then stop: the
tableau represents an optimal solution.
Step 2 [Find entering variable] Select the most negative entry in the objective row. Mark its
column to indicate the entering variable and the pivot column.
Step 3 [Find departing [leaving] variable] For each positive entry in the pivot column, calculate
the θ-ratio by dividing that row's entry in the rightmost column [solution] by its entry in the
pivot column. [If there are no positive entries in the pivot column then stop: the problem is
unbounded.] Find the row with the smallest θ- ratio, mark this row to indicate the departing
variable and the pivot row.
Step 4 [Form the next tableau] Divide all the entries in the pivot row by its entry in the pivot
column. Subtract from each of the other rows, including the objective row, the new pivot row
multiplied by the entry in the pivot column of the row in question. Replace the label of the pivot
row by the variable's name of the pivot column and go back to Step 1.
Standard form of LP problem
Must be a maximization problem
All constraints [except the nonnegativity constraints] must be in the form of linear equations
All the variables must be required to be nonnegative
Thus, the general linear programming problem in standard form with m constraints and
n unknowns [n ≥ m] is
Maximize c1 x1 + ...+ cn xn
Subject to ai 1x1+ ...+ ain xn = bi , i = 1,...,m, x1 ≥ 0, ... , xn ≥ 0
Example
maximize 3x + 5y maximize 3x + 5y + 0u
+ 0v subject to x + y ≤ 4 subject to x + y
+u=4
x + 3y ≤ 6 to x + 3y + v = 6
x≥0, y≥0 x≥0, y≥0, u≥0, v≥0
Variables u and v, transforming inequality constraints into equality constrains, are called
slack Variables
2.State and Prove Maximum Flow Min cut Theorem. Or Explain Max-Flow Problem Or How do
you compute maximum flow for the following graph using ford-Fulkerson method?[M-15][N-15]
[M-16]
Maximum Flow Problem
Problem of maximizing the flow of a material through a transportation network [e.g., pipeline
system, communications or transportation networks]
3. Write down the optimality condition and algorithmic implementation for finding M-
augumenting paths in bipartite graphs? Or Illustrate the workings of the maximum matching
algorithm on the following weighted trees. [N-15][M-15]
A matching in a graph is a subset of its edges with the property that no two edges
share a vertex. A maximum matching—more precisely, a maximum cardinality
matching—is a matching with the largest number of edges.
Case 1 (the queue‘s front vertex w is in V ) If u is a free vertex adjacent to w, it is used
as the other endpoint of an augmenting path; so the labeling stops and augmentation of
the matching commences. The augmenting path in question is obtained by moving
backward along the vertex labels (see below) to alternately add and delete its edges to
and from the current matching. If u is not free and connected to w by an edge not in M,
label u with w unless it has been already labeled.
Case 2 (the front vertex w is in U) In this case, w must be matched and we label its mate in V with
w.
Enqueue(Q, v)
return M //current matching is
maximum For Example,
2 marks
2. Define - Backtracking
Backtracking is used to solve problems with tree structures. Even problems seemingly
remote to trees such as a walking a maze are actually trees when the decision \'back-left-
straight-right\' is considered a node in a tree. The principle idea is to construct solutions one
component at a time and evaluate such partially constructed candidates
10. Define -
Hamiltonian circuit. [M – 15]
Hamiltonian is defined as a cycle that passes through all the vertices of the graph
exactly once. It is named after the Irish mathematician Sir William Rowan Hamilton
[1805-1865].It is a sequence of n+1 adjacent vertices vi0, vi1,……, vin-1, vi0 where the
first vertex of the sequence is same as the last one while all the other n-1 vertices are
distinct.
problem‗s constraints, while an optimal solution is a feasible solution with the best value of the
objective function.
15. What
are the strengths of backtracking and branch-and-bound? [M-
15] The strengths are as follows
It is typically applied to difficult combinatorial problems for which no efficient
algorithm for finding exact solution possibly exist
It holds hope for solving some instances of nontrivial sizes in an acceptable amount of time
Even if it does not eliminate any elements of a problem‗s state space and ends up
generating all its elements, it provides a specific technique for doing so, which can be of
some value
16 marks
FIGURE Complete state-space tree of the backtracking algorithm applied to the instance
The state-space tree can be constructed as a binary tree like that in the instance A =
{3, 5, 6, 7} and d = 15.
The root of the tree represents the starting point, with no decisions about the given
elements made as yet.
Its left and right children represent, respectively, inclusion and exclusion of a1 in a set
being sought. Similarly, going to the left from a node of the first level corresponds to
inclusion of a2 while going to the right corresponds to its exclusion, and so on.
Thus, a path from the root to a node on the ith level of the tree indicates which of the first
I numbers have been included in the subsets represented by that node. We record
the value of s, the sum of these numbers, in the node.
If s is equal to d, we have a solution to the problem. We can either report this result
and stop or, if all the solutions need to be found, continue by backtracking to the node‘s
parent.
If s is not equal to d, we can terminate the node as non-promising if either of the
following two inequalities holds:
� + �i+1 > � [the sum s is too large],
s + ���
=�+1 j < d [the sum s is too small].
The following well-known greedy algorithm is based on the nearest-neighbor heuristic: always
go next to the nearest unvisited city.
Step 1 Choose an arbitrary city as the start.
Step 2 Repeat the following operation until all the cities have been visited:go to the unvisited
city nearest the one visited last (ties can be broken arbitrarily).
Step 3 Return to the starting
city. For Example,
Class NP: A nondeterministic algorithm is a two-stage procedure that takes as its input
an instance I of a decision problem and does the following. Nondeterministic
(―guessing‖) stage: An arbitrary string S is generated that can be thought of as a
candidate solution to the given instance I (but may be complete gibberish as well).
State space tree for 4-queen problem. Similar to other queen problem also.