Notes-Analysis & Design of Algorithms(BCS401)_removed
Notes-Analysis & Design of Algorithms(BCS401)_removed
88
Analysis and Design of Algorithms (BCS401)
89
Analysis and Design of Algorithms (BCS401)
Analysis
90
Analysis and Design of Algorithms (BCS401)
Hamiltonian cycles
91
Analysis and Design of Algorithms (BCS401)
92
Analysis and Design of Algorithms (BCS401)
Assignment Problem
Let us illustrate the branch-and-bound approach by applying it to the problem of assigning n
people to n jobs so that the total cost of the assignment is as small as possible.
An instance of the assignment problem is specified by an n × n cost matrix C so that we can
state the problem as follows: select one element in each row of the matrix so that no two
selected elements are in the same column and their sum is the smallest possible. We will
demonstrate how this problem can be solved using the branch-and-bound technique by
considering the small instance of the problem. Consider the data given below.
93
Analysis and Design of Algorithms (BCS401)
How can we find a lower bound on the cost of an optimal selection without actually solving
the problem?
We can do this by several methods. For example, it is clear that the cost of any solution,
including an optimal one, cannot be smaller than the sum of the smallest elements in each
of the matrix’s rows. For the instance here, this sum is 2 + 3+ 1+ 4 = 10.We can and will
apply the same thinking to partially constructed solutions. For example, for any legitimate
selection that selects 9 from the first row, the lower bound will be 9 + 3 + 1+ 4 = 17.
Rather than generating a single child of the last promising node as we did in backtracking, we
will generate all the children of the most promising node among non-terminated leaves in the
current tree. (Non terminated, i.e., still promising, leaves are also called live.) How can we
tell which of the nodes is most promising? We can do this by comparing the lower bounds of
the live nodes. It is sensible to consider a node with the best bound as most promising,
although this does not, of course, preclude the possibility that an optimal solution will
ultimately belong to a different branch of the state-space tree. This variation of the strategy is
called the best-first branch-and-bound.
We start with the root that corresponds to no elements selected from the cost matrix. The
lower-bound value for the root, denoted lb, is 10. The nodes on the first level of the tree
correspond to selections of an element in the first row of the matrix, i.e., a job for person a.
See the figure given below.
Figure: Levels 0 and 1 of the state-space tree for the instance of the assignment
problem being solved with the best-first branch-and-bound algorithm. The number
above a node shows the order in which the node was generated. A node‘s fields
indicate the job number assigned to person a and the lower bound value, lb, for this
node.
So we have four live leaves—nodes 1 through 4—that may contain an optimal solution. The
most promising of them is node 2 because it has the smallest lower bound value. Following
our best-first search strategy, we branch out from that node first by considering the three
different ways of selecting an element from the second row and not in the second column -
the three different jobs that can be assigned to person b. See the figure given below (Fig
12.7).
94
Analysis and Design of Algorithms (BCS401)
Of the six live leaves—nodes 1, 3, 4, 5, 6, and 7—that may contain an optimal solution, we
again choose the one with the smallest lower bound, node 5. First, we consider selecting the
third column‘s element from c‘s row (i.e., assigning person c to job 3); this leaves us with no
choice but to select the element from the fourth column of d‘s row (assigning person d to job
4). This yields leaf 8 (Figure 12.7), which corresponds to the feasible solution {a→2, b→1,
c→3, d →4} with the total cost of 13. Its sibling, node 9, corresponds to the feasible solution
{a→2,b→1, c→4, d →3} with the total cost of 25. Since its cost is larger than the cost of the
solution represented by leaf 8, node 9 is simply terminated. (Of course, if its cost were
smaller than 13, we would have to replace the information about the best solution seen so far
with the data provided by this node.)
Now, as we inspect each of the live leaves of the last state-space tree—nodes1, 3, 4, 6, and 7
in Figure 12.7—we discover that their lower-bound values are not smaller than 13, the value
of the best selection seen so far (leaf 8). Hence, we terminate all of them and recognize the
solution represented by leaf 8 as the optimal solution to the problem.
95
Analysis and Design of Algorithms (BCS401)
But there is a less obvious and more informative lower bound for instances with symmetric
matrix D, which does not require a lot of work to compute. We can compute a lower bound
on the length l of any tour as follows. For each city i, 1≤ i ≤ n, find the sum si of the distances
from city i to the two nearest cities; compute the sums of these n numbers, divide the result
by 2, and, if all the distances are integers, round up the result to the nearest integer:
lb = ⌈s/2⌉... (1)
For example, for the instance in Figure 2.2a, formula (1) yields
Moreover, for any subset of tours that must include particular edges of a given graph, we can
modify lower bound (formula 1) accordingly. For example, for all the Hamiltonian circuits of
the graph in Figure 2.2a that must include edge (a, d), we get the following lower bound by
summing up the lengths of the two shortest edges incident with each of the vertices, with the
required inclusion of edges (a, d)and (d, a):
We now apply the branch-and-bound algorithm, with the bounding function given by
formula-1, to find the shortest Hamiltonian circuit for the graph in Figure 2.2a.
To reduce the amount of potential work, we take advantage of two observations.
1. First, without loss of generality, we can consider only tours that start at a.
2. Second, because our graph is undirected, we can generate only tours in which b is
visited before c. (Refer Note at the end of section 2.2 for more details)
In addition, after visiting n−1= 4 cities, a tour has no choice but to visit the remaining
unvisited city and return to the starting one. The state-space tree tracing the algorithm‘s
application is given in Figure 2.2b.
Note: An inspection of graph with 4 nodes (figure given below) reveals three pairs of tours
that differ only by their direction. Hence, we could cut the number of vertex permutations by
half. We could, for example, choose any two intermediate vertices, say, b and c, and then
consider only permutations in which b precedes c. (This trick implicitly defines a tour‘s
direction.)
Figure: Solution to a small instance of the traveling salesman problem by exhaustive search.
96
Analysis and Design of Algorithms (BCS401)
Figure 2.2(a)Weighted graph. (b) State-space tree of the branch-and-bound algorithm to find
a shortest Hamiltonian circuit in this graph. The list of vertices in a node specifies a
beginning part of the Hamiltonian circuits represented by the node.
Discussion
The strengths and weaknesses of backtracking are applicable to branch-and-bound as well.
The state-space tree technique enables us to solve many large instances of difficult
combinatorial problems. As a rule, however, it is virtually impossible to predict which
instances will be solvable in a realistic amount of time and which will not.
In contrast to backtracking, solving a problem by branch-and-bound has both the challenge
and opportunity of choosing the order of node generation and finding a good bounding
function. Though the best-first rule we used above is a sensible approach, it may or may not
lead to a solution faster than other strategies. (Artificial intelligence researchers are
particularly interested in different strategies for developing state-space trees.)
Finding a good bounding function is usually not a simple task. On the one hand, we want this
function to be easy to compute. On the other hand, it cannot be too simplistic - otherwise, it
would fail in its principal task to prune as many branches of a state-space tree as soon as
possible. Striking a proper balance between these two competing requirements may require
intensive experimentation with a wide variety of instances of the problem in question.
97
Analysis and Design of Algorithms (BCS401)
1≤𝑖≤𝑛
It is convenient to order the items of a given instance in descending order by their value-to-
weight ratios.
Each node on the ith level of state space tree, 0 ≤ i ≤ n, represents all the subsets of n items
that include a particular selection made from the first i ordered items. This particular
selection is uniquely determined by the path from the root to the node: a branch going to the
left indicates the inclusion of the next item, and a branch going to the right indicates its
exclusion.
We record the total weight w and the total value v of this selection in the node, along with
some upper bound ub on the value of any subset that can be obtained by adding zero or more
items to this selection. A simple way to compute the upper bound ub is to add to v, the total
value of the items already selected, the product of the remaining capacity of the knapsack W
– w and the best per unit payoff among the remaining items, which is vi+1/wi+1:
ub = v + (W − w)(vi+1/wi+1).
Example: Consider the following problem. The items are already ordered in descending order
of their value-to-weight ratios.
Let us apply the branch-and-bound algorithm. At the root of the state-space tree (see Figure
12.8), no items have been selected as yet. Hence, both the total weight of the items already
selected w and their total value v are equal to 0. The value of the upper bound is 100.
Node 1, the left child of the root, represents the subsets that include item 1. The total weight
and value of the items already included are 4 and 40, respectively; the value of the upper
bound is 40 + (10 − 4) * 6 = 76.
98
Analysis and Design of Algorithms (BCS401)
Node 2 represents the subsets that do not include item 1. Accordingly, w = 0, v = 0, and ub =
0 + (10 − 0) * 6 = 60. Since node 1 has a larger upper bound than the upper bound of node 2,
it is more promising for this maximization problem, and we branch from node 1 first. Its
children—nodes 3 and 4—represent subsets with item 1 and with and without item 2,
respectively. Since the total weight w of every subset represented by node 3 exceeds the
knapsack‘s capacity, node 3 can be terminated immediately.
Node 4 has the same values of w and v as its parent; the upper bound ub is equal to 40 + (10
− 4) * 5 = 70. Selecting node 4 over node 2 for the next branching (Due to better ub), we get
nodes 5 and 6 by respectively including and excluding item 3. The total weights and values as
well as the upper bounds for these nodes are computed in the same way as for the preceding
nodes.
Branching from node 5 yields node 7, which represents no feasible solutions, and node 8,
which represents just a single subset {1, 3} of value 65. The remaining live nodes 2 and 6
have smaller upper-bound values than the value of the solution represented by node 8. Hence,
both can be terminated making the subset {1, 3} of node 8 the optimal solution to the
problem.
99
Analysis and Design of Algorithms (BCS401)
preceding subsection.) For the knapsack problem, however, every node of the tree represents
a subset of the items given. We can use this fact to update the information about the best
subset seen so far after generating each new node in the tree. If we had done this for the
instance investigated above, we could have terminated nodes 2 and 6 before node 8 was
generated because they both are inferior to the subset of value 65 of node 5.
100
Analysis and Design of Algorithms (BCS401)
101
Analysis and Design of Algorithms (BCS401)
102
Analysis and Design of Algorithms (BCS401)
103
Analysis and Design of Algorithms (BCS401)
104
Analysis and Design of Algorithms (BCS401)
Conclusion
105
Analysis and Design of Algorithms (BCS401)
106
Analysis and Design of Algorithms (BCS401)
print(‗0‘); failure
procedure NSORT(A,n);
//sort n positive integers//
var integer A(n), B(n), n, i, j;
begin
B := 0; //B is initialized to zero//
for i := 1 to n do
begin
j := choice(1:n);
if B(j) <> 0 then failure;
B(j) := A(j);
end;
107
Analysis and Design of Algorithms (BCS401)
But there are some problems which are known to be in NP but don‘t know if they‘re in P. The
traditional example is the decision-problem version of the Travelling Salesman Problem
(decision-TSP). It‘s not known whether decision-TSP is in P: there‘s no known poly-time
solution, but there‘s no proof such a solution doesn‘t exist.
There are problems that are known to be neither in P nor NP; a simple example is to
enumerate all the bit vectors of length n. No matter what, that takes 2n steps.
Now, one more concept: given decision problems P and Q, if an algorithm can transform a
solution for P into a solution for Q in polynomial time, it‘s said that Q is poly-time
reducible (or just reducible) to P.
The most famous unsolved problem in computer science is ―whether P=NP or P≠NP? ‖
108
Analysis and Design of Algorithms (BCS401)
NP-Complete problems have the property that it can be solved in polynomial time if all other
NP-Complete problems can be solved in polynomial time. i.e if anyone ever finds a poly-time
solution to one NP-complete problem, they‘ve automatically got one for all the NP-complete
problems; that will also mean that P=NP.
Example for NP-complete is CNF-satisfiability problem. The CNF-satisfiability problem
deals with boolean expressions. This is given by Cook in 1971. The CNF-satisfiability
problem asks whether or not one can assign values true and false to variables of a given
boolean expression in its CNF form to make the entire expression true.
Over the years many problems in NP have been proved to be in P (like Primality Testing).
Still, there are many problems in NP not proved to be in P. i.e. the question still remains
whether P=NP? NP Complete Problems helps in solving this question. They are a subset
of NP problems with the property that all other NP problems can be reduced to any of them in
polynomial time. So, they are the hardest problems in NP, in terms of running time. If it can
be showed that any NP-Complete problem is in P, then all problems in NP will be in P
(because of NP-Complete definition), and hence P=NP=NPC.
NP Hard Problems - These problems need not have any bound on their running time. If
any NP-Complete Problem is polynomial time reducible to a problem X, that problem X
belongs to NP-Hard class. Hence, all NP-Complete problems are also NP-Hard. In other
words if a NP-Hard problem is non-deterministic polynomial time solvable, it is a NP-
Complete problem. Example of a NP problem that is not NPC is Halting Problem.
If a NP-Hard problem can be solved in polynomial time then all NP-Complete can be solved
in polynomial time.
―All NP-Complete problems are NP-Hard but not all NP-Hard problems are not NP-
Complete.‖NP-Complete problems are subclass of NP-Hard
The more conventional optimization version of Traveling Salesman Problem for finding the
shortest route is NP-hard, not strictly NP-complete.
*****
109
Analysis and Design of Algorithms (BCS401)
Module-1
1. Find gcd(31415,14142) by applying Euclid’s algorithm. Estimate how many times it is faster when
compared to the algorithm based on consecutive integer checking.
2. Compare the order of growth of 1/2n (n-1) and n^2.
3. Explain the mathematical analysis of Fibonacci recursive algorithm.
4. Write Bruteforce string matching algorithm.
5. Define three asymptotic notations.
6. Design a recursive algorithm for solving tower of Hanoi problem and give the general plan of
analyzing that algorithm. Show that the time complexity of tower of Hanoi algorithm is exponential in
nature.
7. With algorithm and a suitable example, explain how the brute force string matching algorithm works.
Analyse for its complexity.
8. With the help of a flow chart, explain the various steps of algorithm design and analysis process.
9. If f1(n) ∈ O(g1(n)) and f2(n) ∈ O O(g2(n)) prove that f1(n)+f2(n) ∈ O(max {g(n),g2(n)}).
10. Write an algorithm for selection sort and show that the time complexity of this algorithm is quadratic.
11. What is an algorithm? What are the properties of an algorithm? Explain with an example.
12. Express using asymptotic notation i) n! ii) 6*2n+n2.
13. Give formal definitions of asymptotic notations.
14. Give informal definitions of asymptotic notations.
Module-2
1. 1Find the upper bound of recurrences given below by substitution method. a.
2T(n/2)+n ii) T(n/2)+1
2. Sort the following elements using merge sort. Write the recursion tree.70,20,30,40,10,50,60
3. Write the algorithm for quick sort. Derive the worst case time efficiency of the algorithm.
4. Give general divide and conquer recurrence with necessary explanation. Solve the recurrence a.
i)T (n) =2T (n/2) +1
b. ii)T (n) =T (n/2) +n
5. Explain with suitable example a sorting algorithm that uses divide and conquer technique which
divides the problem size by considering position. Give the corresponding algorithms and analyse for
time complexity.
6. Give the problem definition of a defective chess board? Explain clearly how divide and conquer
method can be applied to solve 4 * 4 defective chess board problem.
7. What is divide and conquer method. Show that the worst case efficiency of binary search algorithm
is(log).
8. What is stable algorithm? Is quick sort stable? Explain with example.
9. Give an algorithm for merge sort.
10. Trace the merge sort algorithm for the data : 60,50,25,10,35,25,75,30
11. Explain matrix multiplication with respect to divide and conquer technique.
110
Analysis and Design of Algorithms (BCS401)
Module-3
3. What is the solution generated by the function job scheduling (JS) when
n=5,[P1,P2,P3,P4,P5]=[10,15,10,5,1] and [d1,d2,d3,d4,d5]=[2,2,1,3,3]
4. Apply PRIMS algorithm for the following graph to find minimum spanning tree.
5. What is job sequencing with deadline problem?Find solution generated by job sequencing problem
with deadlines for 7 jobs given profits 3,5,20,18,1,6,30 and deadlines 1,3,4,3,2,1,2 respectively.
6. Define minimum cost spanning tree. Give high level description of Prim’s algorithm to find minimum
spanning tree and find minimum spanning tree for graph shown in following figure using Prim’s
algorithm.
7. What is a knapsack problem? Obtain solution for the knapsack problem using greedy method for n=3,
capacity m=20 values 25, 24, 15 and weights 18, 15, 10 respectively.
8. Write Krushal’s algorithm to construct a minimum spanning tree and show that the time efficiency is
O (| ∈ |log| ∈ |).
9. Apply Kruskal’s algorithm to find the min spanning tree of the graph.
111
Analysis and Design of Algorithms (BCS401)
12. Solve the following single source shortest path problem assuming vertex 5 as the source.
Module-4
16. Use Dynamic programming, compute the shortest path from vertex 1 to all other vertices
17. Solve the Knapsack instance n=3,{W1,W2,W3}={1,2,2} and {P1,P2,P3}={18,16,6} and M=4 by
dynamic programming.
18. For the given graph, obtain optimal cost tour using dynamic programming.
112
Analysis and Design of Algorithms (BCS401)
19. What is dynamic programming? Explain how you would solve all pair shortest paths problem using
dynamic programming.
20. Give the necessary recurrence relation used to solve 0/1 knapsack problem using dynamic
programming. Apply it to solve the following instance and show the results n=4, m=5 values
12,10,20,15 and weights are 2,1,3,2 respectively.
21. Solve the following TSP which is represented as a graph shown in the figure using dynamic
programming
22. Write the dynamic programming algorithm to compute binomial co-efficient and obtain its time
complexity.
23. Explain Warshall algorithm to find the transitive closure of a directed graph. Apply this algorithm to
the graph given below.
24. State Floyd’s algorithm. Solve all pairs shortest path problem for the given graph using Floyd
algorithm
25. Using Floyd’s algorithm solve the all pair shortest problem for the graph whose weight matrix is
given below:
113
Analysis and Design of Algorithms (BCS401)
31. What are the three variations of decrease and conquer technique.
32. Conduct DFS for the following graph:
33. Apply DFS based algorithm to solve topological sorting problem for the following graph:
34. Construct shift table for the pattern EARN and search for the same in text FAIL-MEANS-FIRST-
ATTEMPT- IN-LEARNING using Horspool algorithm.
35. Explain the working of depth-first search algorithm for the graph shown in following figure.
36. With pseudocode, explain how the searching for a pattern BARBER in the given text
JIM_SAW_ME_IN_BARBER_SHOP is performed using Horspool’s algorithm.
37. With suitable example, explain topological sorting.
38. Explain decrease and conquer method, with a suitable example.
39. Apply the DFS – based algorithm to solve the topological sorting problem for given graph.
114
Analysis and Design of Algorithms (BCS401)
40. Write and explain DFS and BFS algorithm with example.
41. Obtain topologies sorting for the given digraph using source removal method.
Module-5
14 Explain how the TSP problem can be solved, using branch and bound
method. 15 Solve 8 – queens problem for a feasible sequence (6, 4, 7, 1).
16 What is back tracking? Apply back tracking problem to solve the instance of the sum of
subset problem: S= {3, 5, 6, 7} and d=15.
17 Write an algorithm to place all queens on the chess board.
18 What is Nearest-neighbor algorithm to compute TSP problem?
19 Explain the various models for parallel computations.
115
Analysis and Design of Algorithms (BCS401)
20 Let the i/p to the prefix computation be 5,12,8,6,3,9,11,12,1,5,6,7,10,4,3,5 and there are four
116
Analysis and Design of Algorithms (BCS401)
processors and ⊕ stands for addition. With diagram explain how prefix computation is done
21 by
Letparallel
the i/p algorithm.
to the prefix computation be 5,12,8,6,3,9,11,12,1,5,6,7,10,4,3,5 and there are four
processors and ⊕ stands for addition. With diagram explain how prefix computation is done
by work optimal algorithm.
22 Explain how M is calculated using parallel algorithm for given graph.
117
Analysis and Design of Algorithms (BCS401)
Analysis and Design of Algorithms (BCS401)
Analysis and Design of Algorithms (BCS401)
Analysis and Design of Algorithms (BCS401)
Analysis and Design of Algorithms (BCS401)
Analysis and Design of Algorithms (BCS401)
Analysis and Design of Algorithms (BCS401)
Analysis and Design of Algorithms (BCS401)
Analysis and Design of Algorithms (BCS401)
Analysis and Design of Algorithms (BCS401)
Analysis and Design of Algorithms (BCS401)
Analysis and Design of Algorithms (BCS401)
Analysis and Design of Algorithms (BCS
Analysis and Design of Algorithms (BCS401)
Analysis and Design of Algorithms (BCS401)
Analysis and Design of Algorithms (B
Analysis and Design of Algorithms (BC
Analysis and Design of Algorithms (BCS401)
Analysis and Design of Algorithms (BCS401)
Analysis and Design of Algorithms (BCS401)
Analysis and Design of Algorithms (BCS401)
Analysis and Design of Algorithms (BCS401)
Analysis and Design of Algorithms (BCS401)
Institute Mission
To support value-based education with state of art infrastructure.
To empower women with the additional skill for professional future career
To enrich students with research blends in order to fulfil the international challenges