Design and Analysis of Algorithms
Design and Analysis of Algorithms
UNIT-I
________________________________________________________________________________
INTRODUCTION:
Algorithm,pseudocode for expressing algorithms,performance analysis- Time complexity and space
complexity, asymptotic noatation- O notation Omega notation and Theta notation and little on
notation,probabilistic analysis,amortized complexity.
DIVIDE AND CONQUER:
General Method, applications-Binary search,merge sort, quick sort, strassen’s matrix multiplication.
___________________________________________________________________________________-
Worst case
UNIT-2
Graph Traversals
Prims algorithm
return max;
UNIT-3 NOTES
Greedy method
Time complexity is
Algorithm:
Minimum weight is 7
Prims algorithm
Time complexity=
n=number of nodes
Cost=14
Cost=17
Kruskal algorithm
n=number of nodes
Time complexity
Time complexity
UNIT-4
N-Queen’s Problem
Branch & Bound (B & B) is general algorithm (or Systematic method) for finding optimal
solution of various optimization problems, especially in discrete and combinatorial
optimization.
The B&B strategy is very similar to backtracking in that a state space tree is used to solve
a problem.
The differences are that the B&B method
Does not limit us to any particular way of traversing the tree.
It is used only for optimization problem
It is applicable to a wide variety of discrete combinatorial problem.
B&B is rather general optimization technique that applies where the greedy method &
dynamic programming fail.
It is much slower, indeed (truly), it often (rapidly) leads to exponential time complexities
in the worst case.
The term B&B refers to all state space search methods in which all children of the “E-
node” are generated before any other “live node” can become the “E-node”
Live node is a node that has been generated but whose children have not yet been
generated.
E-nodeis a live node whose children are currently being explored.
Dead node is a generated node that is not to be expanded or explored any further. All
children of a dead node have already been expanded.
Two graph search strategies, BFS & D-search (DFS) in which the exploration of a new
node cannot begin until the node currently being explored is fully explored.
Both BFS & D-search (DFS) generalized to B&B strategies.
BFSlike state space search will be called FIFO (First In First Out) search as the list of
live nodes is “First-in-first-out” list (or queue).
D-search (DFS) Like state space search will be called LIFO (Last In First Out) search
as the list of live nodes is a “last-in-first-out” list (or stack).
In backtracking, bounding function are used to help avoid the generation of sub-trees that
do not contain an answer node.
We will use 3-types of search strategies in branch and bound
1) FIFO (First In First Out) search
FIFO B&B:
FIFO Branch & Bound is a BFS.
In this, children of E-Node (or Live nodes) are inserted in a queue.
Implementation of list of live nodes as a queue
Least() Removes the head of the Queue
Add() Adds the node to the end of the Queue
Assume that node ‘12’ is an answer node in FIFO search, 1st we take E-node has ‘1’
LIFO B&B:
LIFO Brach & Bound is a D-search (or DFS).
In this children of E-node (live nodes) are inserted in a stack
Implementation of List of live nodes as a stack
Least() Removes the top of the stack
ADD()Adds the node to the top of the stack.
The search for an answer node can often be speeded by using an “intelligent” ranking
function. It is also called an approximate cost function “Ĉ”.
Expended node (E-node) is the live node with the best Ĉ value.
Branching: A set of solutions, which is represented by a node, can be partitioned into
mutually (jointly or commonly) exclusive (special) sets. Each subset in the partition is
represented by a child of the original node.
Lower bounding: An algorithm is available for calculating a lower bound on the cost of any
solution in a given subset.
Example:
8-puzzle
Def:- Find a tour of minimum cost starting from a node S going through other nodes
only once and returning to the starting point S.
B&B algorithms for this problem, the worest case complexity will not be any better than
O(n22n) but good bunding functions will enables these B&B algorithms to solve some
problem instances in much less time than required by the dynamic programming alogrithm.
State space tree for the travelling salesperson problem with n=4 and i0=i4=1
The above diagram shows tree organization of a complete graph with |V|=4.
Each leaf node ‘L’ is a solution node and represents the tour defined by the path from the root
to L.
𝑖𝑛𝑓 20 30 10 11
15 𝑖𝑛𝑓 16 4 2
3 5 𝑖𝑛𝑓 2 4
19 6 18 𝑖𝑛𝑓 3
[ 16 4 7 16 𝑖𝑛𝑓 ]
The TSP starts from node 1: Node 1
Reduced Matrix: To get the lower bound of the path starting at node 1
Row # 1: reduce by 10 Row #2: reduce 2 Row #3: reduce by 2
𝑖𝑛𝑓 10 20 0 𝑖𝑛𝑓
1 10 20 0 𝑖𝑛𝑓
1 10 20 0 1
13 𝑖𝑛𝑓 14 2 130 𝑖𝑛𝑓 14 2 120 𝑖𝑛𝑓 14 2 0
1 3 𝑖𝑛𝑓 0 2 1 3 𝑖𝑛𝑓 0 2 0 3 𝑖𝑛𝑓 0 2
16 3 15 𝑖𝑛𝑓 160 3 15 𝑖𝑛𝑓 150 3 15 𝑖𝑛𝑓 0
[ 16 4 7 16 𝑖𝑛𝑓
[ 12] 0 3 12 𝑖𝑛𝑓
[ 11] 0 3 12 𝑖𝑛𝑓 ]
Column 2: It is reduced. Column 3: Reduce by 3 Column 4: It is reduced.
Column 5: It is reduced.
In summary:
So the live nodes we have so far are:
2: cost(2) = 35, path: 1->2
3: cost(3) = 53, path: 1->3
4: cost(4) = 25, path: 1->4
5: cost(5) = 31, path: 1->5
Explore the node with the lowest cost: Node 4 has a cost of 25
Vertices to be explored from node 4: 2, 3, and 5
Now we are starting from the cost matrix at node 4 is:
2. Apply the Branch and Bound algorithm to solve the TSP, for the following cost matrix.
3. (a) Explain how the traveling salesperson problem is solved by using LC Branch
and Bound.
(b) Write the general algorithm for Branch and Bound.
4. What is traveling sales person problem? Solve the following sales person problem
instance using branch and bound.
5. (a) Draw the portion of the state space tree generated by FIFOBB using the variable tuple
size for the knapsack instances, n = 5 (P1, P2, P5) = (10, 15, 6, 8, 4),
(w1, w2,..., w5) = (4, 6, 3, 4, 2) and M = 12.
(b) Write the control abstraction of LC search.
6. a) What do you mean by bounding? Explain how these bound are useful in branch and
bound methods.
b) Explain the principles of
i) FIFO branch and Bound
ii) LC Branch and Bound
7. a) Explain the method of reduction to solve TSP problem using Branch and Bound.
b) Explain the principles of FIFO Branch and Bound.
2 marks questions
1. Define backtracking.
2. What is meant by optimization problem?
3. Define Hamiltonian circuit problem.
4. What is Hamiltonian cycle in an undirected graph?
5. Define 8queens problem.
6. List out the application of backtracking.
7. Give the explicit and implicit constraint for 8-queen problem.
8. How can we represent the solution for 8-queen problem?
9. Give the categories of the problem in backtracking.
10. Differentiate backtracking and over exhaustive search.
11. What is state space tree?
Nondeterministic algorithms