0% found this document useful (0 votes)
29 views12 pages

UNIT 4 Algo Min Notes

Algorithms

Uploaded by

sindhuja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views12 pages

UNIT 4 Algo Min Notes

Algorithms

Uploaded by

sindhuja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

UNIT-4

STATE SPACE SEARCH ALGORITHMS


Backtracking
is a refined brute force algorithm used to solve multi-decision problems where the final
solution is visualized as a set of decisions. The execution of a decision(choice) leads to
another set of decisions. The decisions are pursued until one encounter a successful solution
or an impasse. In case of impasse, we can backtrack and try other alternatives. Eg searching
for a item in a dark room.
Backtracking defines a solution vector as a n-tuple vector(x 1, x2,… xn) for the given problem.
Backtracking start with an initial partial solution in which as many variables as possible are
left unspecified, and then to systematically assign values to the unspecified variables until
either a single point in the search space is identified or an implicit constraint makes it
impossible to process more unspecified variables. In both cases, the algorithm continues by
going back to a partial solution generated earlier and then assigning a next value to an
unspecified variable
There are two types of constraints:
Explicit constraints: They are the rules that restrict each x i to take on values from the given
set. In 8-queen problem row number should be between 1 and 8
Implicit constraints: They are the rules that determine which of the tuples in the solution
space of ‘i’ that satisfies the criterion function. They describe the way in which x is must
relate to each other. Eg in TSP The implicit constraints demand the sequence should form a
path
Criterion function( promising function or validity function) is a function used to evaluate
the feasible solutions to find the best one. Eg in 8 queen problem, no two queen should be
placed in same row, column or diagonal.
A state space tree is an arrangement of all possible solutions in a tree like fashion. State
space tree has following terminology
 Answer state: correspond to solution states where the path from root to leaf define
the solution
 Live node: A node that is yet to generate children
 E-node: node that is currently under consideration
 Dead nodes: node that cannot proceed further.
A suitcase having 3 digit password. Digits are restricted to binary numbers and at least two
1’s are there in the password. Draw the state space tree.
Problems which are solved using backtracking method include:
1. N-Queens problem
2. Hamiltonian cycle
3. Sum of subsets
4. Graph coloring
N queen problem: Objective is to place N queens on a NXN chessboard such that no two
queens are in attacking positions. Solution is not possible for N<4. For a 4 queen problem
the solution vector van be denoted as (x1,x2,x3,x4) where xi denotes the column in which
queen is place in ith row.
Implicit Constraints: No 2 xi’s can be the same. i.e All queens must be on different rows,
columns and cannot be placed diagonal to each other
Explicit Constraints: Si ={1,2,3,4}, 1 ≤ i ≤ 4
Criterion Function: Solution space consists of 4 tuples. One possible solution is (2,4,1,3). The
reverse also can be a solution (3,1,4,2). Cannot visualize state space tree for higher values of
N since it is very large. The State space tree for a 4 queen problem is given below
Hamiltonian circuit problem
Hamiltonian cycle is a cycle that traverse all the vertices of a given graph G exactly once and
ends at the starting vertex. Construct the state space tree to check for Hamiltonian cycle for
the following graph.
Subset sum problem
It’s a variant of knapsack problem. Given n positive items with weights { w 1, w2,.. wn} and a
positive integer W, the problem is about finding all combinations of items whose sum of
weights amounts to W. The weights are usually in ascending order and unique.
Let us assume w= {1,2,5,6,8} and W=9. What are the different combinations possible using a
backtracking approach?
The answer is [1,2,6], [1,8]
The state space tree is a binary tree such that every node at level ‘i’ has two branches. One
branch indicate that an item is included, while the other branch indicate that the item is
skipped. The weight(x) on each node indicates the sum of all items included upto that level.
At level i let the next item weight is w i+1 the node is considered non promising if x+w i+1>W or
x+T<W where T is the sum of all remaining weights.
Step 1: Start with an empty set
Step 2: Add to the subset, the next element from the list
Step 3: if the subset is having sum W then stop with that subset as solution
Step 4: if subset is not feasible or we have reached the end of set then backtrack through
the subset
Step 5: if subset is feasible repeat step 2
Step 6: if we have visited all the elements without finding a solution and if no backtracking is
possible then stop
Graph coloring problem
M coloring problem: M indicates the minimum number of colors required to color the
vertices of a given graph. Assign M colors to vertices such that adjacent vertices do not share
same color. Can be stated as a decision problem or as an optimization problem.
In decision problem graph and M will be given. Find out whether coloring is possible
(Yes/No)
In optimization problem, given a graph find minimum colors needed to color.
Draw state space graph
Branch and bound:
Information about a certain partial solution fk ,1 ≤ k < n, at a certain level can indicate that
any fully-specified solution fn derived from it can never be the optimal solution. Killing of
partial solution by pruning the search tree by removing the subtree having f(k) as its root is
idea behind branch and bound
A lower bound for the cost is estimated as sum of two terms: G term can give the length of
the path fixed by the specified variables and H term can give a lower bound on remaining
tour length based on the unspecified variables. Bounding functions are used to kill live nodes
without generating all their children
function that computes the lower bound of the Solutions should be easier to compute than
the mere traversal of the subtree to have some computational gain
Compared to backtracking, branch-and-bound requires two additional items:
1. A way to provide, for every node of a state-space tree, a bound on the best value of
the objective function on any solution that can be obtained by adding further
components to the partially constructed solution represented by the node
2. The value of the best solution seen so far
The leaf nodes in the state space tree are the solution states.
15-puzzle problem
Invented by Sam Lloyd. There are 15 tiles numbered from 1 to 15. The objective of the
problem is to transform the arrangement of tiles from initial arrangement to a goal
arrangement. There is an empty slot in tile arrangement denoted by ES. Legal moves are the
moves in which tiles adjacent to the ES are moved either left, right, up or down. In figure 7
can be moved right, 4 can be moved down or 8 can be moved up. Left move is not possible
in this state.
Each tile arrangement can be represented as a node in the state space tree, and two nodes
are connected with an edge if one of the board state can be changed to another by sliding a
tile to empty slot. Edges are labeled according to direction in which ES moves. State space
tree for 15 puzzle is very large because there can be 16! Different arrangements. For 8
puzzle, hardest board take 31 moves while 15 puzzle needs 80 moves. Solved using A* or
IDA* algorithms.
Fig from Lakshmi

We start with node 1 (root node) which become E-node. The child nodes are generated.
Then node 1 dies and children 2 and 3 become live nodes.
Assignment problem : There are n people to whom n jobs have to be assigned so that total
cost of assignment is least. The cost that would accrue if the i th person is assigned to the jth
job is a known quantity C[i, j] for each pair i, j = 1, 2, ... , n
We can describe feasible solutions to the assignment problem as n-tuples where xi indicates
the job number assigned to the i th person. For example, for the cost matrix above, (2, 3, 4, 1)
indicates a feasible assignment of Person A to Job 2, Person B to Job 3, Person C to Job 4,
and Person D to Job 1

In terms of this matrix, the problem calls for a selection of one element in each row of the
matrix so that all selected elements are in different columns and the total sum of the
selected elements is the smallest possible.
We cannot select the smallest element in each row because the smallest elements may
happen to be in the same column. In fact, the smallest element in the entire matrix need not
be a component of an optimal solution
Brute force method generate all permutation of job assignment and chose the best.
Complexity is high O(n!)
Can be solved using branch and bound method.
Knapsack Problem
Given ‘n’ items of known weights ‘wi’ and values ‘vi’, i = 1, 2, ... , n, and a knapsack of
capacity ‘W’, find the most valuable subset of the items that fit in the knapsack.
There are three types of knapsack problem
1) 0/1 knapsack 2) Fractional knapsack 3) unbounded Knapsack
Step 1: Arrange the items of a given instance in descending order by their value-to-weight
ratios.

Step 2: Construct state space tree. 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 are recorded. Solution is determined by the path from the root to the node: a
branch going to the left indicates the inclusion of the next item, while a branch going to the
right indicates its exclusion
Step 3: Upper Bound is computed by adding 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

Problem

Initially sort the items in decreasing order of value/weight. At the root of the state-space
tree, 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 computed
using ‘ub’ formula is $100.
Travelling Salesman Problem: Given a set of cities and the distance between each pair of
cities, find the shortest possible route that visits every city exactly once and return to the
starting city. Can be solved by finding a minimum weight Hamiltonian cycle.
a) Naïve solution:
Step1: Consider city 1 as starting and ending point.
Step 2: Generate all permutations of remaining (n-1) cities.
Step 3: Calculate cost if the route is feasible routes.
Step 4: Return permutation with minimum cost
Complexity is high O(n!)
b) Using Branch and bound
Solve Travelling salesman problem using branch and bound algorithm

Step1 : Find the lower bound using the formula below

LB=[ ∑ ∑ of cost of two least edges adjacent ¿ vertex v]/2¿


v ∈V

At root node
a=(a-b)+(a-c)=2+5=7 (among three edges of a these were the least)
LB=[(2+5)+(2+3)+(1+5)+(1+3)]/2=11
At level 1 we will consider a-b, a-c and a-d distances
Consider Node 1 Since it is a-b we must consider it whereever applicable
a=(a-b)+(a-c)=2+5=7
b=(a-b)+(b-d)=2+3=5
c=(a-c)+(c-d)=5+1=6 here (a-b) edge not considered since it is not connect to c
d=(b-d)+(c-d)=3+1=4
LB=(7+5+6+4)/2=11
Similarly for node 2 a-c hence consider edge (a-c) whenever applicable
LB=11
Node 3 is a-d hence consider a-d whenever applicable
LB=14
In level 1 smallest is 11. Consider node 1. Expand node1 into node 4 and 5
Node 4 a-b-c Hence consider edges a-b and b-c whenever applicable
LB=(7+10+9+4)/2=15
Node 5 is a-b-d hence consider edges a-b and b-d wherever applicable
LB=(7+5+6+4)/2=11
The full state space graph is shown below. Node 2 and Node 3 are not explored since the
bounds are inferior to already found best solution.

You might also like