Click here to load reader

Greedy Backtracking

  • View

  • Download

Embed Size (px)


greedy: Introduction, Knapsack problem, Job sequence deadline problem with exampleBacktracking: Introduction, n-queen problem. Graph coloring problem

Text of Greedy Backtracking

Greedy method

Greedy methodByAbhishek MukhopadhyayConcept about Greedy methodIt is used to solve problems that have n inputs and require us to obtain a subset that satisfies some constraints.Greedy method suggests that one can divide the algorithm that works in stages.At each stage a decision is made regarding weather or particular input is in the optimum solution.For this purpose all the inputs must be arranged in a particular order by using a selection process.

Control abstraction for GREEDYAlgorithm Greedy (a,n)//a[1:n] contains the n inputs{ Solution: = 0; //initialize the solution for i: = 1 to n do { x: = select (a); if feasible (solution, x) thensolution: = Union (solution, x); } return solution;}

Feasible solution and optimal solutionFeasible solution: Any subset of the solutions that satisfies the constraints of the problem is known as a feasible solution.Optimal solution: The feasible solution that maximizes or minimizes the given objective function is an optimal solution. Every problem will have a unique optimal solution.

Knapsack problemFractional KnapsackA knapsack with capacity m is given. we are required to place certain weights such that the sum of the weights will not exceed the knapsack capacity. Associated with each weight we have associated profit which will be earned by the inclusion of the object in to the knapsack.If it is not possible to include an object entirely a fraction of the object can be included and accordingly a fraction of the profit is earned.

Mathematical representationm=capacity of knapsack.n=no. of objects.object i has a profit pi and weight wi associated with it.If a fraction xj (o x, 1) of the object i is placed in the bad. A profit pi x xi is made.The knapsack problem can be stated mathematically as follows:

Maximize Pi xi, for 1 i nS.T.C wi xi m1 u no xi 11 i n

Example:Consider 3 objects whose profits and weights are defined as(P1, P2, P3) = ( 25, 24, 15 )(W1, W2, W3) = ( 18, 15, 10 ) n=3 m=20 Consider a knapsack of capasity 20. Determine the optimum strategy for placing the objects in to the knapsack.Example(Cont..)Greedy about profit(1,2/15,D)Greedy about weight(D,2/3,1)Greedy about profit/unit weight (0,1,1/2)

Pseudocode of knapsack problemAlgorithm Greedy knapsack (m, n)//p (1>n) and w(1:n) contain the profits and weights//resp. of the n objects ordered such that p(i)/W(i)// p[i+1)/w (i+1]. M is the knapsack size and x (1:n)// is the solution vector{for i: = 1 to n do x(i) = 0.0i// initialize x u : = m; for i: = 1 to n do { if (w(i) > u) then break; x(i): = 1.0; u: = u-w(i); } if (in) then x(i): = u/w(i);}

Job Sequencing with DeadlinesWe are given a set of n jobs.Associated with each job there is a integer dead line di0 and a profit pi>0.A feasible solution for the problem will be a subset j of jobseach job in this subset can be completed by its deadline.An optimum solution is a feasible solution with maximum value.ExampleObtain the optimal sequence for the following jobs. j1 j2 j3 j4 (P1, P2, P3, P4)= (100, 10, 15, 27) (d1, d2, d3, d4)=(2, 1, 2, 1) n =4Find out feasible solutions and optimal solutionPseudo code for JSDAlgorithm Greedy Job (d,j,n)// j is a set of jobs that can be completed by// their dead lines { j: = {1}; for i: = 2 to n do { if (all jobs in j u {i} can be completed by their dead lines) then j: = ju{ i}; } }

Self-Study Prims algorithm and Kruskals algorithm( Elaborate the algorithm with suitable example)BacktrackingBacktracking is a form of recursion.The usual scenario is that you are faced with a number of options you must choose one of these;After you make your choice you will get a new set of options;This procedure is repeated over and over until you reach a final state.If you made a good sequence of choices, your final state is agoal state.

Backtracking algorithm boolean solve(Node n) { if n is a leaf node { if the leaf is a goal node, return true else return false } else { for each child c of n { if solve(c) succeeds, return true } return false } }

N-QueensThe object is to place queens on a chess board in such as way as no queen can capture another one in a single moveRecall that a queen can move horz, vert, or diagonally an infinite distanceThis implies that no two queens can be on the same row, col, or diagonalWe usually want to know how many different placements there are4-QueensLets take a look at the simple problem of placing queens 4 queens on a 4x4 boardThe brute-force solution is to place the first queen, then the second, third, and forthAfter all are placed we determine if they are placed legallyThere are 16 spots for the first queen, 15 for the second, etc.Leading to 16*15*14*13 = 43,680 different combinationsObviously this isnt a good way to solve the problem4-QueensFirst lets use the fact that no two queens can be in the same col to help usThat means we get to place a queen in each colSo we can place the first queen into the first col, the second into the second, etc.This cuts down on the amount of workNow there are 4 spots for the first queen, 4 spots for the second, etc.4*4*4*4 = 256 different combinations4-QueensHowever, we can still do better because as we place each queen we can look at the previous queens we have placed to make sure our new queen is not in the same row or diagonal as a previously place queenThen we could use a Greedy-like strategy to select the next valid position for each col4-QueensQQQQQxQxQxQxQxQQxQQxx4-QueensQQxxQxxQQxxxQxxWe are stuck!4-QueensSo now what do we do?Well, this is very much like solving a mazeAs you walk though the maze you have to make a series of choicesIf one of your choices leads to a dead end, you need to back up to the last choice you made and take a different routeThat is, you need to change one of your earlier selectionsEventually you will find your way out of the maze4-QueensQQxxxQxxQxxQxQxxQxQQxxQQxxQxxQQxxQQxxQQQxxx4-QueensQxQxQxQxxxQQxxxQxQxxxQxQxxQxxQQxxxxQxxQxxQxQx4-QueensQxQxQQQxxQQxxxQxQxxxQxQxxxQ4-QueensQxQxxxQQQxQxQxxQxQxQQxxxxQx4-QueensThis type of problem is often viewed as a state-space treeA tree of all the states that the problem can be inWe start with an empty board state at the root and try to work our way down to a leaf nodeLeaf nodes are completed boards4-Queens112342XX1234XXXX1243XX1243XXXXX1234XXX1123XXGraph ColoringGraph coloring is the problem of coloring each vertex in a graph such that no two adjacent vertices are the same colorSome direct examples:Map coloringRegister assignmentGraph ColoringThe same issues apply as in N-QueensWe dont want to simply pick all subsetsWay too manyWe want to prune the state-space tree as soon as we find something that wont workThis implies that we need a sequence of vertices to colorAs we color the next vertex we need to make sure it doesnt conflict with any of its previously colored neighborsWe may need to backtrackGraph ColoringCAFBEDAs an example:The vertices are enumerated in order A-FThe colors are given in order: R, G, BGraph ColoringCAFBEDGraph ColoringCAFBEDGraph ColoringCAFBEDGraph ColoringCAFBEDGraph ColoringCAFBEDGraph ColoringCAFBEDStuck!Graph ColoringCAFBEDGraph ColoringCAFBEDGraph ColoringCAFBEDGraph ColoringCAFBEDGraph ColoringXXABCDEFXXXXXXXXCAFBEDAlgorithm RCA (Recursive Color Assignment)

Input: Vertex i, Set of Vertices V1 For each color c Li, if c is not assigned to any neighbour of i1.1 Assign c to i1.2 If i is the last vertex in V1.2.1 If x(G) > M1.2.1.1 x(G) = M1.2.1.2 Save the current coloring1.2.1.3 If M = LB then stop1.2.1.4 Otherwise UB = M 11.3 Otherwise execute RCA for the next vertex in V

Search related