120
Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP’S BSD COE ,DHULE 1 ADA Unit -3 I.S Borse

Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Embed Size (px)

Citation preview

Page 1: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Algorithm Design & AnalysisChapter -03

(Backtracking & Branch and Bound )T.E(Computer)

By

I.S Borse

SSVP’S BSD COE ,DHULE

1ADA Unit -3 I.S Borse

Page 2: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Outline – Chapter 31. Backtrackingi) Eight Queens Problemii) Graph Coloringiii) Hamilton Cyclesiv) Knapsack Problem2. Branch and Boundi) Traveling salesman’s problemii) lower bound theory-comparison trees for

sorting /searchingiii) lower bound on parallel computation.

1. Backtrackingi) Eight Queens Problemii) Graph Coloringiii) Hamilton Cyclesiv) Knapsack Problem2. Branch and Boundi) Traveling salesman’s problemii) lower bound theory-comparison trees for

sorting /searchingiii) lower bound on parallel computation.

2ADA Unit -3 I.S Borse

Page 3: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

A short list of categories

• Algorithm types we will consider include:– Simple recursive algorithms– Backtracking algorithms– Divide and conquer algorithms– Dynamic programming algorithms– Greedy algorithms– Branch and bound algorithms– Brute force algorithms– Randomized algorithms

3

• Algorithm types we will consider include:– Simple recursive algorithms– Backtracking algorithms– Divide and conquer algorithms– Dynamic programming algorithms– Greedy algorithms– Branch and bound algorithms– Brute force algorithms– Randomized algorithms

ADA Unit -3 I.S Borse

Page 4: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Backtracking

• Suppose you have to make a series ofdecisions, among various choices, where– You don’t have enough information to know

what to choose– Each decision leads to a new set of choices– Some sequence of choices (possibly more than

one) may be a solution to your problem

• Backtracking is a methodical way of tryingout various sequences of decisions, untilyou find one that “works”

4

• Suppose you have to make a series ofdecisions, among various choices, where– You don’t have enough information to know

what to choose– Each decision leads to a new set of choices– Some sequence of choices (possibly more than

one) may be a solution to your problem

• Backtracking is a methodical way of tryingout various sequences of decisions, untilyou find one that “works”

ADA Unit -3 I.S Borse

Page 5: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Backtracking

• Problem:

Find out all 3-bit binary numbers for which thesum of the 1's is greater than or equal to 2.

• The only way to solve this problem is to check allthe possibilities: (000, 001, 010, ....,111)

• The 8 possibilities are called the search space ofthe problem. They can be organized into a tree.

• Problem:

Find out all 3-bit binary numbers for which thesum of the 1's is greater than or equal to 2.

• The only way to solve this problem is to check allthe possibilities: (000, 001, 010, ....,111)

• The 8 possibilities are called the search space ofthe problem. They can be organized into a tree.

5ADA Unit -3 I.S Borse

Page 6: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Backtracking: Illustration

1 _ _0 _ _

0 0 _

0 0 0 0 0 1

0 1 _

0 1 0 0 1 1

1 _ _0 _ _

1 0 _

1 0 0 1 0 1

1 1 _

1 1 0 1 1 16ADA Unit -3 I.S Borse

Page 7: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Backtracking• For some problems, the only way to solve is to

check all possibilities.

• Backtracking is a systematic way to go throughall the possible configurations of a search space.

• We assume our solution is a vector (a(1),a(2),a(3), ..a(n)) where each element a(i) is selectedfrom a finite ordered set S.

• For some problems, the only way to solve is tocheck all possibilities.

• Backtracking is a systematic way to go throughall the possible configurations of a search space.

• We assume our solution is a vector (a(1),a(2),a(3), ..a(n)) where each element a(i) is selectedfrom a finite ordered set S.

7ADA Unit -3 I.S Borse

Page 8: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Backtracking and Recursion

• Backtracking is easily implemented withrecursion because:

• The run-time stack takes care of keeping trackof the choices that got us to a given point.

• Upon failure we can get to the previous choicesimply by returning a failure code from therecursive call.

• Backtracking is easily implemented withrecursion because:

• The run-time stack takes care of keeping trackof the choices that got us to a given point.

• Upon failure we can get to the previous choicesimply by returning a failure code from therecursive call.

8ADA Unit -3 I.S Borse

Page 9: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Improving Backtracking: Search Pruning• It help us to reduce the search space and

hence get a solution faster.• The idea is to a void those paths that may not

lead to a solutions as early as possible byfinding contradictions so that we can backtrackimmediately without the need to build ahopeless solution vector.

• It help us to reduce the search space andhence get a solution faster.

• The idea is to a void those paths that may notlead to a solutions as early as possible byfinding contradictions so that we can backtrackimmediately without the need to build ahopeless solution vector.

9ADA Unit -3 I.S Borse

Page 10: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Solving a maze• Given a maze, find a path from start to finish• At each intersection, you have to decide between three

or fewer choices:– Go straight– Go left– Go right

• You don’t have enough information to choose correctly• Each choice leads to another set of choices• One or more sequences of choices may (or may not)

lead to a solution• Many types of maze problem can be solved with

backtracking

10

• Given a maze, find a path from start to finish• At each intersection, you have to decide between three

or fewer choices:– Go straight– Go left– Go right

• You don’t have enough information to choose correctly• Each choice leads to another set of choices• One or more sequences of choices may (or may not)

lead to a solution• Many types of maze problem can be solved with

backtracking

ADA Unit -3 I.S Borse

Page 11: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Backtracking (animation)

?

dead end

dead end

dead end

11

start ? ??

dead end

?

success!

dead end

ADA Unit -3 I.S Borse

Page 12: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Terminology IA tree is composed of nodes

12

There are three kinds of nodes:

The (one) root node

Internal nodes

Leaf nodes

Backtracking can be thought of assearching a tree for a particular “goal”leaf node

ADA Unit -3 I.S Borse

Page 13: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Terminology II• Live node: A node which has been generated and

all of whose children have not yet generated

• E-node: The live node whose children arecurrently being generated

• Dead node : which is not expanded further or allof whose children have been generated

• DFS: Depth first node generation with boundingfunction is called backtracking.

13

• Live node: A node which has been generated andall of whose children have not yet generated

• E-node: The live node whose children arecurrently being generated

• Dead node : which is not expanded further or allof whose children have been generated

• DFS: Depth first node generation with boundingfunction is called backtracking.

ADA Unit -3 I.S Borse

Page 14: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

The backtracking algorithm

• Backtracking is really quite simple--we“explore” each node, as follows:

• To “explore” node N:1. If N is a goal node, return “success”2. If N is a leaf node, return “failure”3. For each child C of N,

3.1. Explore C3.1.1. If C was successful, return “success”

4. Return “failure”

14

• Backtracking is really quite simple--we“explore” each node, as follows:

• To “explore” node N:1. If N is a goal node, return “success”2. If N is a leaf node, return “failure”3. For each child C of N,

3.1. Explore C3.1.1. If C was successful, return “success”

4. Return “failure”

ADA Unit -3 I.S Borse

Page 15: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

n-Queens Problem

• Given: n-queens and an nxn chess board

• Find: A way to place all n queens on theboard s.t. no queens are attacking anotherqueen.

• Given: n-queens and an nxn chess board

• Find: A way to place all n queens on theboard s.t. no queens are attacking anotherqueen.

• How could you formulate this problem?• How would you represent a solution?

15ADA Unit -3 I.S Borse

Page 16: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

How Queens Work

16ADA Unit -3 I.S Borse

Page 17: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

First Solution Idea

• Consider every placement of each queen 1 ata time.– How many placements are there?

17ADA Unit -3 I.S Borse

Page 18: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

First Solution Idea

• Consider every possible placement– How many placements are there?

368,165,426,48

642

n

n368,165,426,4

8

642

n

n

18ADA Unit -3 I.S Borse

Page 19: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Second Solution Idea

• Don’t place 2 queens in the same row.– Now how many positions must be checked?

19ADA Unit -3 I.S Borse

Page 20: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

The Eight Queens Problem

1

2

3

44

5

6

7

81 2 3 4 5 6 7 8

20ADA Unit -3 I.S Borse

Page 21: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

The Eight Queens Problem

1

2

3

44

5

6

7

81 2 3 4 5 6 7 8

21ADA Unit -3 I.S Borse

Page 22: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

The Eight Queens Problem

1

2

3

44

5

6

7

81 2 3 4 5 6 7 8

22ADA Unit -3 I.S Borse

Page 23: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

The Eight Queens Problem

1

2

3

44

5

6

7

81 2 3 4 5 6 7 8

23ADA Unit -3 I.S Borse

Page 24: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Second Solution Idea

• Don’t place 2 queens in the same row.– Now how many positions must be checked?

Represent a positioning as a vector [x1, …, x8]Where each element is an integer 1, …, 8.

88 16,777, 216nn

Represent a positioning as a vector [x1, …, x8]Where each element is an integer 1, …, 8.

24ADA Unit -3 I.S Borse

Page 25: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Third Solution Idea

• Don’t place 2 queens in the same row or inthe same column.

• Generate all permutations of (1,2…8)– Now how many positions must be checked?

• Don’t place 2 queens in the same row or inthe same column.

• Generate all permutations of (1,2…8)– Now how many positions must be checked?

25ADA Unit -3 I.S Borse

Page 26: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

1

2

3

4

5

6

7

81 2 3 4 5 6 7 8

(1,2,3,4,5,6,7,8)

26ADA Unit -3 I.S Borse

Page 27: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

1

2

3

4

5

6

7

81 2 3 4 5 6 7 8

(1,2,3,4,5,6,8,7)

27ADA Unit -3 I.S Borse

Page 28: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

1

2

3

4

5

6

7

81 2 3 4 5 6 7 8

(1,2,3,4,5,8,6,7)

28ADA Unit -3 I.S Borse

Page 29: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

1

2

3

4

5

6

7

81 2 3 4 5 6 7 8

(1,2,3,4,5,8,7,6)

29ADA Unit -3 I.S Borse

Page 30: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Third Solution Idea

• Don’t place 2 queens in the same row or in the samecolumn.

• Generate all permutations of (1,2…8)– Now how many positions must be checked?

• We went from C(n2, n) to nn to n!– And we’re happy about it!

• We applied explicit constraints to shrink our search space.

! 8! 40,320n

• Don’t place 2 queens in the same row or in the samecolumn.

• Generate all permutations of (1,2…8)– Now how many positions must be checked?

• We went from C(n2, n) to nn to n!– And we’re happy about it!

• We applied explicit constraints to shrink our search space.

! 8! 40,320n

30ADA Unit -3 I.S Borse

Page 31: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Four Queens Problem

1 2 3 4

31ADA Unit -3 I.S Borse

Page 32: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Four Queens Problem

x1 = 1

x2 = 2x3 = 3

1 2 3 4

Each branch of the tree represents a decision to place a queen.The criterion function can only be applied to leaf nodes.

x4 = 4

32ADA Unit -3 I.S Borse

Page 33: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Four Queens Problem

x1 = 1

x2 = 2

x3 = 3

1 2 3 4

Is this leaf node a solution?

x4 = 4

33ADA Unit -3 I.S Borse

Page 34: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Four Queens Problem

x1 = 1

x2 = 2

x3 = 3 x3 = 4

1 2 3 4

Is this leaf node a solution?

x4 = 4 x4 = 3

34ADA Unit -3 I.S Borse

Page 35: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Four Queens Problem

Using the criterion function, this is the search tree.

35ADA Unit -3 I.S Borse

Page 36: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Four Queens Problem

x1 = 1

x2 = 2

x3 = 3 x3 = 4

1 2 3 4

A better question:Is any child of this node ever going to be a solution?

x4 = 4 x4 = 3

36ADA Unit -3 I.S Borse

Page 37: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Four Queens Problem

x1 = 1

x2 = 2

x3 = 3 x3 = 4

1 2 3 4

A better question:Is any child of this node ever going to be a solution?

No. 1 is attacking 2. Adding queens won’t fix the problem.

x4 = 4 x4 = 3

37ADA Unit -3 I.S Borse

Page 38: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Four Queens Problem

x1 = 1

x2 = 2

1 2 3 4

The partial criterion or feasibility function checks thata node may eventually have a solution.

Will this node have a solution? no.

38ADA Unit -3 I.S Borse

Page 39: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Four Queens Problem

x1 = 1

x2 = 2 x2 = 3

1 2 3 4

Will this node have a solution? Maybe.

39ADA Unit -3 I.S Borse

Page 40: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Four Queens Problem

x1 = 1

x2 = 2

x3 = 2

x2 = 3

1 2 3 4

Will this node have a solution? No.Etc.

x3 = 2

40ADA Unit -3 I.S Borse

Page 41: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Four Queens Problem

x1 = 1

x2 = 2

x3 = 2 x3 = 4

x2 = 3

1 2 3 4

Will this node have a solution? No.Etc.

41ADA Unit -3 I.S Borse

Page 42: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Four Queens Problem

Using the feasibility function, this is the search tree.

42ADA Unit -3 I.S Borse

Page 43: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Two queens are placed at positions (i ,j) and (k ,l).

They are on the same diagonal only if

i - j = k - l or i + j =k + l

The first equation implies

J – l = i – k

The second implies

J –i= k – i

Two queens lies on the same diagonal iff

| j – l| = |i - k|

Eight Queens problemTwo queens are placed at positions (i ,j) and (k ,l).

They are on the same diagonal only if

i - j = k - l or i + j =k + l

The first equation implies

J – l = i – k

The second implies

J –i= k – i

Two queens lies on the same diagonal iff

| j – l| = |i - k|ADA Unit -3 I.S Borse 43

Page 44: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Continue…………..

For instance P1=(8,1) and P2=(1,8)

So i =8, j=1 and k=1, l=8

i + j = k + l

8+1 = 1+8 =9

J – l =k- i

1- 8 = 1 - 8

Hence P1 and P2 are on the same diagonal

For instance P1=(8,1) and P2=(1,8)

So i =8, j=1 and k=1, l=8

i + j = k + l

8+1 = 1+8 =9

J – l =k- i

1- 8 = 1 - 8

Hence P1 and P2 are on the same diagonal

ADA Unit -3 I.S Borse 44

Page 45: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Eight queens problem – PlaceReturn true if a queen can be placed in Kth row and ith columnotherwise false x[] is a global array whose first (k-1) valuehave been set. Abs(x) returns absolute value of r

1. Algorithm Place(K , i)

2. {

3. For j= 1 to k-1 do

4. If ((x[ j ] = i ) // two in the same column

5. Or ( Abs ( x [ j ]- i) = Abs ( j- k))) // same diagonal

6. then return false

7. Return true

8. }

Computing time O(k-1)

1. Algorithm Place(K , i)

2. {

3. For j= 1 to k-1 do

4. If ((x[ j ] = i ) // two in the same column

5. Or ( Abs ( x [ j ]- i) = Abs ( j- k))) // same diagonal

6. then return false

7. Return true

8. }

Computing time O(k-1)ADA Unit -3 I.S Borse 45

Page 46: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Solution to N-Queens ProblemUsing backtracking it prints all possible placements of n

Queens on a n*n chessboard so that they are not attacking1. Algorithm NQueens( K, n)

2. {

3. For i= 1 to n do

4. { if place(K ,i) then

5. { x[K] := i

6. If ( k = n ) then //obtained feasible sequence of length n

7. write ( x[1:n]) //print the sequence

8. Else Nqueens(K+1 ,n) //sequence is less than the length so backtrack

9. }

10. }

11. }

1. Algorithm NQueens( K, n)

2. {

3. For i= 1 to n do

4. { if place(K ,i) then

5. { x[K] := i

6. If ( k = n ) then //obtained feasible sequence of length n

7. write ( x[1:n]) //print the sequence

8. Else Nqueens(K+1 ,n) //sequence is less than the length so backtrack

9. }

10. }

11. }

ADA Unit -3 I.S Borse 46

Page 47: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Four color theorem.

• How many colors do you need for a planarmap?– Four.

• Haken and Appel using a computer programand 1,200 hours of run time in 1976. Checked1,476 graphs.

• First proposed in 1852.

• How many colors do you need for a planarmap?– Four.

• Haken and Appel using a computer programand 1,200 hours of run time in 1976. Checked1,476 graphs.

• First proposed in 1852.

47ADA Unit -3 I.S Borse

Page 48: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Full example: Map coloring• The Four Color Theorem states that any map

on a plane can be colored with no more thanfour colors, so that no two countries with acommon border are the same color

• For most maps, finding a legal coloring is easy

• For some maps, it can be fairly difficult to finda legal coloring

48

• The Four Color Theorem states that any mapon a plane can be colored with no more thanfour colors, so that no two countries with acommon border are the same color

• For most maps, finding a legal coloring is easy

• For some maps, it can be fairly difficult to finda legal coloring

ADA Unit -3 I.S Borse

Page 49: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Coloring a map• You wish to color a map with

not more than four colors

– red, yellow, green, blue

• Adjacent countries must be indifferent colors

• You don’t have enough information to choose colors

• Each choice leads to another set of choices

• One or more sequences of choices may (or may not)lead to a solution

• Many coloring problems can be solved withbacktracking

49

• You wish to color a map withnot more than four colors

– red, yellow, green, blue

• Adjacent countries must be indifferent colors

• You don’t have enough information to choose colors

• Each choice leads to another set of choices

• One or more sequences of choices may (or may not)lead to a solution

• Many coloring problems can be solved withbacktracking

ADA Unit -3 I.S Borse

Page 50: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Map coloring as Graph Coloring

IdahoWyoming

UtahNevada Colorado

New MexicoArizona

50ADA Unit -3 I.S Borse

Page 51: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Graph Coloring Problem

• Assign colors to the vertices of a graph so thatno adjacent vertices share the same color– Vertices i, j are adjacent if there is an edge from

vertex i to vertex j.

• Find all m-colorings of a graph– Find all ways to color a graph with at most m

colors.

– m is called chromatic number

• Assign colors to the vertices of a graph so thatno adjacent vertices share the same color– Vertices i, j are adjacent if there is an edge from

vertex i to vertex j.

• Find all m-colorings of a graph– Find all ways to color a graph with at most m

colors.

– m is called chromatic number

51ADA Unit -3 I.S Borse

Page 52: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Graph coloringThe m-Coloring problem

Finding all ways to color an undirected graph usingat most m different colors, so that no two adjacentvertices are the same color.

Usually the m-Coloring problem consider as aunique problem for each value of m.

ADA Unit -3 I.S Borse 52

Graph coloringThe m-Coloring problem

Finding all ways to color an undirected graph usingat most m different colors, so that no two adjacentvertices are the same color.

Usually the m-Coloring problem consider as aunique problem for each value of m.

Page 53: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Graph Coloring

Planar graphIt can be drawn in a plane in such a way that no two edges cross

each other.

ADA Unit -3 I.S Borse 53

Page 54: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Graph Coloring

corresponded planar graph

ADA Unit -3 I.S Borse 54

Page 55: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Graph Coloring

• The top level call to m_coloring

• m_coloring(0)

• `The number of nodes in the state space treefor this algorithm

• The top level call to m_coloring

• m_coloring(0)

• `The number of nodes in the state space treefor this algorithm

ADA Unit -3 I.S Borse 55

Page 56: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Nextvalue(k)X[1]…x[k-1] have been assigned integer value in the range [1,m] such that adjacentvertices have distinct integer. A value for x[k] is determined in the range [0,m]. X[k]is assigned the next highest numbered color while maintaining distinctness fromthe adj. vertices of vertex k. if no such color exists, then x[k] is 01. Algorithm Nextvalue(k)

2. { repeat

3. { X [k]:= (x[k] +1) mod (m+1) ; //next higher color

4. If ( x[k] = 0) then return; // all colors have been used

5. for j := 1 to n do

6. { // check if this color is distinct from adjacent colors

7. If ((G[k, j] != 0) and (x[k] = x[ j ] ))

8. //if (k , j)is and edge if adj. vertices have the same color.

9. then break;

10. }

11. If (j = n+1) then return; //new color found

12. } until (false); //otherwise try to find another color

13. }

1. Algorithm Nextvalue(k)

2. { repeat

3. { X [k]:= (x[k] +1) mod (m+1) ; //next higher color

4. If ( x[k] = 0) then return; // all colors have been used

5. for j := 1 to n do

6. { // check if this color is distinct from adjacent colors

7. If ((G[k, j] != 0) and (x[k] = x[ j ] ))

8. //if (k , j)is and edge if adj. vertices have the same color.

9. then break;

10. }

11. If (j = n+1) then return; //new color found

12. } until (false); //otherwise try to find another color

13. } ADA Unit -3 I.S Borse 56

Page 57: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Graph ColoringThis algorithm was formed using recursive backtracking schema. The graph isrepresented by its boolean adjacency matrix G[1:n,1:n]. All assignment of 1,2..,m tothe vertices of the graph such that adjacent vertices are assigned distinct integer areprinted. K is the index of the next vertex to color

1. Algorithm mcoloring(k)

2. { repeat

3. { // generate all legal assignment for x[k]

4. nextvalue(k); //assign to x[k] a legal value

5. If (x[k] = 0 ) then return ; //no new color possible

7. If ( k = n) then // at most m color have been used to color the n vertices

8. Write (x[1 : n];

9. Else mcoloring(k+1)

10. } until (false)

11. }

1. Algorithm mcoloring(k)

2. { repeat

3. { // generate all legal assignment for x[k]

4. nextvalue(k); //assign to x[k] a legal value

5. If (x[k] = 0 ) then return ; //no new color possible

7. If ( k = n) then // at most m color have been used to color the n vertices

8. Write (x[1 : n];

9. Else mcoloring(k+1)

10. } until (false)

11. }

ADA Unit -3 I.S Borse 57

Page 58: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Graph coloringmcoloring problem

• Number of internal nodes in the state spacetree is

nΣi=1 mi

At each node, 0(m n) time is spent by nextvalueto determine the children corresponding tolegal colorings.

Total time = n-1Σi=0 mi+1 n = nΣi=1 mi n

= 0(n mn)

• Number of internal nodes in the state spacetree is

nΣi=1 mi

At each node, 0(m n) time is spent by nextvalueto determine the children corresponding tolegal colorings.

Total time = n-1Σi=0 mi+1 n = nΣi=1 mi n

= 0(n mn)ADA Unit -3 I.S Borse 58

Page 59: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Graph Coloring

Example2-coloring problem

No solution!

3-coloring problem

Vertex Color

v1 color1

v2 color2

v3 color3

v4 color2

Example2-coloring problem

No solution!

3-coloring problem

Vertex Color

v1 color1

v2 color2

v3 color3

v4 color2

ADA Unit -3 I.S Borse 59

Page 60: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Small Example

Enumerate all possible states; identify solutions at the leaves.Naively, non-solutions will be generated. More on that later.Note, most non-solutions not shown in this cartoon. 60ADA Unit -3 I.S Borse

Page 61: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Hamiltonian cycle (HC)Definitions

• Hamiltonian cycle (HC): is a cycle which passesonce and exactly once through every vertex of Gand returns to starting position

• Hamiltonian path: is a path which passes onceand exactly once through every vertex of G (G canbe digraph).

• A graph is Hamiltonian iff a Hamiltonian cycle(HC) exists.

• Hamiltonian cycle (HC): is a cycle which passesonce and exactly once through every vertex of Gand returns to starting position

• Hamiltonian path: is a path which passes onceand exactly once through every vertex of G (G canbe digraph).

• A graph is Hamiltonian iff a Hamiltonian cycle(HC) exists.

61ADA Unit -3 I.S Borse

Page 62: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

The Hamiltonian Circuits Problem

• Hamiltonian Circuit

• [v1, v2, v8, v7, v6, v5, v4, v3, v2]

ADA Unit -3 I.S Borse 62

Page 63: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

The Hamiltonian Circuits Problem

• No Hamiltonian Circuit!

ADA Unit -3 I.S Borse 63

Page 64: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Backtrack Algorithm

• Search all the potential solutions

• Employ pruning of some kind to restrict theamount of researching

• Advantage:

Find all solution, can decide HC exists or not

• DisadvantageWorst case, needs exponential time. Normally, take along time

• Search all the potential solutions

• Employ pruning of some kind to restrict theamount of researching

• Advantage:

Find all solution, can decide HC exists or not

• DisadvantageWorst case, needs exponential time. Normally, take along time

64ADA Unit -3 I.S Borse

Page 65: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Application

• Hamiltonian cycles in fault random geometricnetwork

• In a network, if Hamiltonian cycles exist, thefault tolerance is better.

• Hamiltonian cycles in fault random geometricnetwork

• In a network, if Hamiltonian cycles exist, thefault tolerance is better.

65ADA Unit -3 I.S Borse

Page 66: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Heuristic Algorithm

Initialize path P

While {

Find new unvisited node.

If found { Extend path P and pruning on the graph. If thischoice does not permit HC, remove the extended node.

} else

Transform Path. Try all possible endpoints of this path

Form cycle. Try to find HC

}

Initialize path P

While {

Find new unvisited node.

If found { Extend path P and pruning on the graph. If thischoice does not permit HC, remove the extended node.

} else

Transform Path. Try all possible endpoints of this path

Form cycle. Try to find HC

}

66ADA Unit -3 I.S Borse

Page 67: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

X[1: k-1] is a path o k-1 distinct vertices if x[k]=0, then no vertex has as yet beenassigned to x[k]. After execution x[k] is assigned to the next highest number vertexwhich does not already appear in x[1, k-1] & is connected by an edge to x[k-1].otherwise x[k] =0, if k=n then in addition x[k] is connected to x[1].

Algorithm Nextvalue(k)1. {

2. repeat {

3. x[k] := (x [k] +1 mod (n +1) //next vertex

4. If ( x[k] = 0) then return

5. If G[x[k-1], x[k] ≠ 0 ) then

6. { //Is there an edge?

7. for j =1 to k-1 do if (s[j] = x[k]) then break;

//check distinctness

8. If ( j= k) then //if true then vertex is distinct

9. If (( k< n ) or (( k =n) and G [x[n] , x[1] ≠ 0))

10. then return

11. }

12. } until ( false) ;

13. }

1. {

2. repeat {

3. x[k] := (x [k] +1 mod (n +1) //next vertex

4. If ( x[k] = 0) then return

5. If G[x[k-1], x[k] ≠ 0 ) then

6. { //Is there an edge?

7. for j =1 to k-1 do if (s[j] = x[k]) then break;

//check distinctness

8. If ( j= k) then //if true then vertex is distinct

9. If (( k< n ) or (( k =n) and G [x[n] , x[1] ≠ 0))

10. then return

11. }

12. } until ( false) ;

13. } ADA Unit -3 I.S Borse 67

Page 68: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

This algorithm uses the recursive formulation of backtracking tofind all the hamiltonion cycles of a graph. The graph is stored asan adjacency matrix G[1:n, 1:n]. All cycles begins at node 1.

1. Algorithm Hamiltonian(k)

2. {

3. repeat

4. { //generate values for x[k]

5. Nextvalue(k); //assign a legal next value to x[k]

6. if ( x[k] = 0 ) then return

7. if (k = n) then write ( x[1:n]);

8. else Hamiltonian(k + 1);

9. } until(false);

10. }

1. Algorithm Hamiltonian(k)

2. {

3. repeat

4. { //generate values for x[k]

5. Nextvalue(k); //assign a legal next value to x[k]

6. if ( x[k] = 0 ) then return

7. if (k = n) then write ( x[1:n]);

8. else Hamiltonian(k + 1);

9. } until(false);

10. }

ADA Unit -3 I.S Borse 68

Page 69: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Knapsack backtrackingWe are given n objects and a knapsack or bag. The objective is to obtain a filling ofthe knapsack that maximizes the total profit earned.

Given

n = number of weights

w = weights

P =profits m= knapsack capacity

Using greedy approach Choosing a subset of the weights such that

Max Σ1≤i ≤n pixi subject to Σ1≤i ≤ n wixi ≤ m

0 ≤ Xi ≤ 1 1 ≤ i ≤ n

We are given n objects and a knapsack or bag. The objective is to obtain a filling ofthe knapsack that maximizes the total profit earned.

Given

n = number of weights

w = weights

P =profits m= knapsack capacity

Using greedy approach Choosing a subset of the weights such that

Max Σ1≤i ≤n pixi subject to Σ1≤i ≤ n wixi ≤ m

0 ≤ Xi ≤ 1 1 ≤ i ≤ n

ADA Unit -3 I.S Borse 69

Page 70: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

The backtracking method

• A given problem has a set of constraints andpossibly an objective function

• The solution must be feasible and it may optimizean objective function

• We can represent the solution space for theproblem using a state space tree– The root of the tree represents 0 choice,– Nodes at depth 1 represent first choice– Nodes at depth 2 represent the second choice, etc.– In this tree a path from a root to a leaf represents a

candidate solution

• A given problem has a set of constraints andpossibly an objective function

• The solution must be feasible and it may optimizean objective function

• We can represent the solution space for theproblem using a state space tree– The root of the tree represents 0 choice,– Nodes at depth 1 represent first choice– Nodes at depth 2 represent the second choice, etc.– In this tree a path from a root to a leaf represents a

candidate solution

70ADA Unit -3 I.S Borse

Page 71: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Backtracking

• Definition: We call a node nonpromising if itcannot lead to a feasible (or optimal) solution,otherwise it is promising

• Main idea: Backtracking consists of doing a DFSof the state space tree, checking whether eachnode is promising and if the node is nonpromisingbacktracking to the node’s parent

• Definition: We call a node nonpromising if itcannot lead to a feasible (or optimal) solution,otherwise it is promising

• Main idea: Backtracking consists of doing a DFSof the state space tree, checking whether eachnode is promising and if the node is nonpromisingbacktracking to the node’s parent

71ADA Unit -3 I.S Borse

Page 72: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Template for backtracking in the case ofoptimization problems

Procedure checknode (node v )

{

node u ;

if ( value(v) is better than best )best = value(v);

if (promising (v) )for (each child u of v)

checknode (u );

}

• best is the best valueso far and is initializedto a value that is equalto or worse than anypossible solution.

• value(v) is the value ofthe solution at thenode.

Procedure checknode (node v )

{

node u ;

if ( value(v) is better than best )best = value(v);

if (promising (v) )for (each child u of v)

checknode (u );

}

• best is the best valueso far and is initializedto a value that is equalto or worse than anypossible solution.

• value(v) is the value ofthe solution at thenode.

72ADA Unit -3 I.S Borse

Page 73: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Backtracking solution to the 0/1 knapsack problemm is the size of knapsack; n is the number of wt. and profits. W[] and p[] are the wt.& profits. p[i]/w[i] >= p[i+1]/w[i+1]. fw is the final wt. of knapsack, fp is the finalmax. profit . x[ k ] = 0 if w [k] is not in the knapsack, else x[ k]=11. Algorithm Bknap( k, cp, cw)

2. { If ( cw + w[k] ≤ m) then // generate left child then

3. { y[k] := 1

4. If ( k< n) then Bknap(k +1, cp + p[k], cw + w[k]);

5. If (( cp + p[k] > fp ) and (k=n) then

6. { fp := cp + p[k]; fw := cw + w[k];

7. for j:= 1 to k do x[ j ] := y[ j ];

8. } }

9. If bound (cp, cw ,k) ≥ fp ) then //generate right child

10. {

11. y[k] := 0; if ( k < n) then Bknap( k + 1, cp, cw);

12. If (( cp > fp ) and ( k=n )) then

13. { fp:= cp; fw := cw;

14. For j :=1 to k do x[ j ] := y[ j ]

15. } } }

1. Algorithm Bknap( k, cp, cw)

2. { If ( cw + w[k] ≤ m) then // generate left child then

3. { y[k] := 1

4. If ( k< n) then Bknap(k +1, cp + p[k], cw + w[k]);

5. If (( cp + p[k] > fp ) and (k=n) then

6. { fp := cp + p[k]; fw := cw + w[k];

7. for j:= 1 to k do x[ j ] := y[ j ];

8. } }

9. If bound (cp, cw ,k) ≥ fp ) then //generate right child

10. {

11. y[k] := 0; if ( k < n) then Bknap( k + 1, cp, cw);

12. If (( cp > fp ) and ( k=n )) then

13. { fp:= cp; fw := cw;

14. For j :=1 to k do x[ j ] := y[ j ]

15. } } } ADA Unit -3 I.S Borse 73

Page 74: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Knapsack problemIt determines an upper bound on the best solution obtainable byexpanding any node Z at level K+1 of the state space tree. The objectweights and profits are w[i] and p[i]. It is assumed that p[i]/w[i] >=p[i+1]/w[i+1]1. Algorithm bound( cp, cw, k)

2. //cp is the current profit, cw is the current wt total, k is the index of lastremoved item and m is the knapsack size

3. {

4. b := cp; c := cw;

5. for i:= k+1 to n do

6. {

7. c:= c + w[ i ];

8. if ( c < m) then b:= b + p[ i ] ;

9. else return b+( 1- (c-m)/ w[ i ]*p[ i ];

10. }

11. return b;

12. }

1. Algorithm bound( cp, cw, k)

2. //cp is the current profit, cw is the current wt total, k is the index of lastremoved item and m is the knapsack size

3. {

4. b := cp; c := cw;

5. for i:= k+1 to n do

6. {

7. c:= c + w[ i ];

8. if ( c < m) then b:= b + p[ i ] ;

9. else return b+( 1- (c-m)/ w[ i ]*p[ i ];

10. }

11. return b;

12. }ADA Unit -3 I.S Borse 74

Page 75: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Example• Suppose n = 4, W = 16, and we have the following:

i pi wi pi / wi

1 $40 2 $20

2 $30 5 $6

3 $50 10 $5

4 $10 5 $2

• Note the the items are in the correct order needed byKWF

• Suppose n = 4, W = 16, and we have the following:

i pi wi pi / wi

1 $40 2 $20

2 $30 5 $6

3 $50 10 $5

4 $10 5 $2

• Note the the items are in the correct order needed byKWF

75ADA Unit -3 I.S Borse

Page 76: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

1

13

$00

$402

$00

Item 1 [$40, 2]

BItem 2 [$30, 5]

$707

2

3 $4028

maxprofit = 90

profitweight

ExampleF - not feasibleN - not optimalB- cannot lead tobest solution

maxprofit =0

maxprofit =40

maxprofit =70

$12017

4

F17>16

Item 3 [$50, 10]

Item 4 [$10, 5]

$7075

$90129

$40212

B$80126

$7077

N N

$10017

10 $901211

maxprofit = 90

Optimal

maxprofit =80F17>16

76ADA Unit -3 I.S Borse

Page 77: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Worst-case time complexity

Check number of nodes:

Time complexity:

When will it happen?

122...2221 132 nn

)2( n

Check number of nodes:

Time complexity:

When will it happen?

For a given n, W=nPi = 1, wi=1 (for 1 <=i<=n-1)

Pn=n wn=n

)2( n

77ADA Unit -3 I.S Borse

Page 78: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

• In backtracking, we used depth-first search with pruning totraverse the (virtual) state space. We can achieve betterperformance for many problems using a breadth-first searchwith pruning. This approach is known as branch-and-bound

• Branch and Bound is a general search method.• Starting by considering the root problem (the original

problem with the complete feasible region), the lower-bounding and upper-bounding procedures are applied to theroot problem.

• If the bounds match, then an optimal solution has beenfound and the procedure terminates.

Branch and BoundIntroduction

• In backtracking, we used depth-first search with pruning totraverse the (virtual) state space. We can achieve betterperformance for many problems using a breadth-first searchwith pruning. This approach is known as branch-and-bound

• Branch and Bound is a general search method.• Starting by considering the root problem (the original

problem with the complete feasible region), the lower-bounding and upper-bounding procedures are applied to theroot problem.

• If the bounds match, then an optimal solution has beenfound and the procedure terminates.

78ADA Unit -3 I.S Borse

Page 79: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

• Otherwise, the feasible region is divided into two ormore regions, these subproblems partition thefeasible region.

• The algorithm is applied recursively to thesubproblems. If an optimal solution is found to asub problem, it is a feasible solution to the fullproblem, but not necessarily globally optimal.

Branch and BoundIntroduction

• Otherwise, the feasible region is divided into two ormore regions, these subproblems partition thefeasible region.

• The algorithm is applied recursively to thesubproblems. If an optimal solution is found to asub problem, it is a feasible solution to the fullproblem, but not necessarily globally optimal.

79ADA Unit -3 I.S Borse

Page 80: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

• If the lower bound for a node exceeds the best knownfeasible solution, no globally optimal solution can exist inthe subspace of the feasible region represented by thenode. Therefore, the node can be removed fromconsideration.

• The search proceeds until all nodes have been solved orpruned, or until some specified threshold is met betweenthe best solution found and the lower bounds on allunsolved subproblems.

Branch and BoundIntroduction

• If the lower bound for a node exceeds the best knownfeasible solution, no globally optimal solution can exist inthe subspace of the feasible region represented by thenode. Therefore, the node can be removed fromconsideration.

• The search proceeds until all nodes have been solved orpruned, or until some specified threshold is met betweenthe best solution found and the lower bounds on allunsolved subproblems.

80ADA Unit -3 I.S Borse

Page 81: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Traveling salesman problem2

5 8 7 3

1

a b

c d

{ a, b, c, d }represents 4 cities

The weightsrepresent distancesbetween cities

Branch and Bound

1c d

Problem: find the shortest path from a city(say a ), visit all other cities exactly once, andreturn to the city where it started (city a ).

{ a, b, c, d }represents 4 cities

The weightsrepresent distancesbetween cities

81ADA Unit -3 I.S Borse

Page 82: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Bound on TSP Tour

1

2 5

82

19

2

3 4

5

6

75

4

310

12

Every tour must leave every vertex andand arrive at every vertex.

82ADA Unit -3 I.S Borse

Page 83: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Bound on TSP Tour

1

2 5

82

19

2

3 4

5

6

75

4

310

12

What’s the cheapest way to leave each vertex?

83ADA Unit -3 I.S Borse

Page 84: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Bound on TSP Tour

1

2 5

82

19bound=8+6+3+2+1

2

3 4

5

6

75

4

310

12

Save the sum of those costs in the bound (as a rough draft).Can we find a tighter lower bound?

84ADA Unit -3 I.S Borse

Page 85: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Bound on TSP Tour

1

2 5

8-8=02

19-8=1bound=20

2

3 4

5

6

74

4

310

12

For a given vertex, subtract the least cost departure from eachedge leaving that vertex.

85ADA Unit -3 I.S Borse

Page 86: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Bound on TSP Tour

1

2 5

00

01bound=20

2

3 4

5

0

12

1

09

6

Repeat for the other vertices.86ADA Unit -3 I.S Borse

Page 87: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Bound on TSP Tour

1

2 5

00

01bound=20

2

3 4

5

0

12

1

09

6

Does that set of edges now having 0 residual cost arrive atevery vertex?

In this case, the edges never arrive at vertex 3.87ADA Unit -3 I.S Borse

Page 88: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Bound on TSP Tour

1

2 5

00

01bound=21

2

3 4

5

0

01

1

09

6

We have to take an edge to vertex 3 from somewhere.Assume we take the cheapest. Subtract its cost from other

edges entering vertex 3 and add the cost to the bound.We have just tightened the bound. 88ADA Unit -3 I.S Borse

Page 89: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

The Bound

• It will cost at least this much to visit all thevertices in the graph.– there’s no cheaper way to get in and out of each

vertex.

– the edges are now labeled with the extra cost ofchoosing another edge.

• It will cost at least this much to visit all thevertices in the graph.– there’s no cheaper way to get in and out of each

vertex.

– the edges are now labeled with the extra cost ofchoosing another edge.

89ADA Unit -3 I.S Borse

Page 90: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Bound on TSP Tour

1

2 58

2

19

10

999 9 999 8 999999 999 4 999 2999 3 999 4 999999 6 7 999 12

1 999 999 10 999

3 4

567

4

4

310

12

Algorithms do this using a cost matrix.

999 9 999 8 999999 999 4 999 2999 3 999 4 999999 6 7 999 12

1 999 999 10 999

90ADA Unit -3 I.S Borse

Page 91: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Bound on TSP Tour

999 1 999 0 999999 999 2 999 0999 0 999 1 999999 0 1 999 6

0 999 999 9 999

1

2 50

0

01999 1 999 0 999999 999 2 999 0999 0 999 1 999999 0 1 999 6

0 999 999 9 9992

3 4

501

2

1

0

0

96

Reduce all rows.

91ADA Unit -3 I.S Borse

Page 92: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Bound on TSP Tour

999 1 999 0 999999 999 1 999 0999 0 999 1 999999 0 0 999 6

0 999 999 9 999

1

2 50

0

01999 1 999 0 999999 999 1 999 0999 0 999 1 999999 0 0 999 6

0 999 999 9 9992

3 4

501

2

1

0

0

96

Then reduce column #3. Now we have a tight bound.

92ADA Unit -3 I.S Borse

Page 93: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Using this bound for TSP in B&B

999 1 999 0 999999 999 1 999 0999 0 999 1 999999 0 0 999 6

0 999 999 9 999

bound = 21

1to2 1to3 1to4 1to5

999 1 999 0 999999 999 1 999 0999 0 999 1 999999 0 0 999 6

0 999 999 9 999

1to2 1to3 1to4 1to5

bound = 21+1

999 1 999 0 999999 999 1 999 0999 0 999 1 999999 0 0 999 6

0 999 999 9 999

999 1 999 0 999999 999 1 999 0999 0 999 1 999999 0 0 999 6

0 999 999 9 999

999 1 999 0 999999 999 1 999 0999 0 999 1 999999 0 0 999 6

0 999 999 9 999

93ADA Unit -3 I.S Borse

Page 94: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Using this bound for TSP in B&B

999 1 999 0 999999 999 1 999 0999 0 999 1 999999 0 0 999 6

0 999 999 9 999

bound = 21

1to2 1to3 1to4 1to5

999 1 999 0 999999 999 1 999 0999 0 999 1 999999 0 0 999 6

0 999 999 9 999

1to2 1to3 1to4 1to5

bound = 21+1

999 1 999 0 999999 999 1 999 0999 0 999 1 999999 0 0 999 6

0 999 999 9 999

999 1 999 0 999999 999 1 999 0999 0 999 1 999999 0 0 999 6

0 999 999 9 999

999 1 999 0 999999 999 1 999 0999 0 999 1 999999 0 0 999 6

0 999 999 9 999

94ADA Unit -3 I.S Borse

Page 95: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Focus: going from 1 to 2

999 1 999 0 999999 999 1 999 0999 0 999 1 999999 0 0 999 6

0 999 999 9 999

bound = 21

1to21 1

999 999 999 999 999999 999 1 999 0999 999 999 1 999999 999 0 999 6

0 999 999 9 999

1to2

bound = 21+1

1

2

3 4

50

00

1

1

0

001

96

1

2

3 4

5

01

1

001

96

Add extra cost from 1 to 2, exclude edges from 1 or into 2.95ADA Unit -3 I.S Borse

Page 96: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

999 1 999 0 999999 999 1 999 0999 0 999 1 999999 0 0 999 6

0 999 999 9 999

bound = 21

1to21 1

Focus: going from 1 to 2

999 999 999 999 999999 999 1 999 0999 999 999 1 999999 999 0 999 6

0 999 999 9 999

1to2

bound = 21+1+1

1

2

3 4

50

00

1

1

0

001

96

1

2

3 4

5

01

1

001

96

No edges into vertex 4 w/ 0 reduced cost.96ADA Unit -3 I.S Borse

Page 97: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

999 1 999 0 999999 999 1 999 0999 0 999 1 999999 0 0 999 6

0 999 999 9 999

bound = 21

1to21

Focus: going from 1 to 2

999 999 999 999 999999 999 1 999 0999 999 999 0 999999 999 0 999 6

0 999 999 8 999

1to2

bound = 21+1+1

1

2

3 4

5

01

0

001

86

Add cost of reducing edge into vertex 4.97ADA Unit -3 I.S Borse

Page 98: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Bounds for other choices.

999 1 999 0 999999 999 1 999 0999 0 999 1 999999 0 0 999 6

0 999 999 9 999

bound = 21

1to2 1to3 1to4 1to5

1to2(23),1to4(21)

999 999 999 999 999999 999 1 999 0999 999 999 0 999999 999 0 999 6

0 999 999 8 999

1to2 1to3 1to4 1to5

bound = 23

999 1 999 0 999999 999 1 999 0999 0 999 1 999999 0 0 999 6

0 999 999 9 999

999 999 999 999 999999 999 1 999 0999 0 999 999 999999 0 0 999 6

0 999 999 999 999

999 999 999 0 999999 999 1 999 0999 0 999 1 999999 0 0 999 6

0 999 999 9 999

bound = 999 bound = 21 bound = 999

98ADA Unit -3 I.S Borse

Page 99: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Leaves us with Two Possibilities onPriority Queue

1

2 001

1

200

0

2

3 4

5

01

1

09

6

2

3 4

50

00

1 0

0

6

bound = 23 bound = 21

99ADA Unit -3 I.S Borse

Page 100: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Leaving Vertex 4

999 999 999 999 999999 999 1 999 0999 0 999 999 999999 0 0 999 6

0 999 999 999 999

bound = 21

1

2 50

00

1 0

00

6

4to2(22), 4to3(21)4to5(28),1to2(23),

3 40

1 0 6

999 999 999 999 999999 999 0 999 0999 999 999 999 999999 999 999 999 999

0 999 999 999 999

999 999 999 999 999999 999 999 999 0999 0 999 999 999999 999 999 999 999

0 999 999 999 999

999 999 999 999 999999 999 0 999 999999 0 999 999 999999 999 999 999 999

0 999 999 999 999

4to2 4to3 4to5

bound = 22 bound = 21 bound = 28

100ADA Unit -3 I.S Borse

Page 101: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Leaving Vertex 3

999 999 999 999 999999 999 999 999 0999 0 999 999 999999 999 999 999 999

0 999 999 999 999

bound = 21

1

2 50

00

00

63to2 3to5

4to2(22), 3to2(21)1to2(23),

3 40

0 6999 999 999 999 999999 999 999 999 0999 999 999 999 999999 999 999 999 999

0 999 999 999 999

999 999 999 999 999999 999 999 999 999999 999 999 999 999999 999 999 999 999

0 999 999 999 999

3to2 3to5

bound = 21 bound = 999

101ADA Unit -3 I.S Borse

Page 102: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Search Tree for This Problem

b=21

b=23 b=999 b=21 b=999

b=22 b=21 b=28

b=21 b=999

102ADA Unit -3 I.S Borse

Page 103: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Conclusion

Although a number of algorithms have been proposedfor the integer linear programming problem, thebranch-and-bound technique has proven to bereasonably efficient on practical problems, and it hasthe added advantage that it solves continuous linearprograms as sub problems.

The technique is also used in a lot of software inglobal optimization.

Although a number of algorithms have been proposedfor the integer linear programming problem, thebranch-and-bound technique has proven to bereasonably efficient on practical problems, and it hasthe added advantage that it solves continuous linearprograms as sub problems.

The technique is also used in a lot of software inglobal optimization.

103ADA Unit -3 I.S Borse

Page 104: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Lower and Upper Bound Theory

• How fast can we sort?

• Most of the sorting algorithms are comparison sorts: only usecomparisons to determine the relative order of elements.

• Examples: insertion sort, merge sort, quicksort, heapsort.

• The best worst-case running time that we’ve seen for comparisonsorting is O(n lg n) .

• Is O(n lg n) the best we can do?

• Lower-Bound Theory can help us answer this question

• How fast can we sort?

• Most of the sorting algorithms are comparison sorts: only usecomparisons to determine the relative order of elements.

• Examples: insertion sort, merge sort, quicksort, heapsort.

• The best worst-case running time that we’ve seen for comparisonsorting is O(n lg n) .

• Is O(n lg n) the best we can do?

• Lower-Bound Theory can help us answer this question

ADA Unit -3 I.S Borse 104

Page 105: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Lower and Upper Bound Theory

• Lower Bound, L(n), is a property of the specific problem, i.e.sorting problem, MST, matrix multiplication, not of any particularalgorithm solving that problem.

• Lower bound theory says that no algorithm can do the job infewer than L(n) time units for arbitrary inputs, i.e., that everycomparison-based sorting algorithm must take at least L(n) timein the worst case.

• L(n) is the minimum over all possible algorithms, of themaximum complexity.

• Lower Bound, L(n), is a property of the specific problem, i.e.sorting problem, MST, matrix multiplication, not of any particularalgorithm solving that problem.

• Lower bound theory says that no algorithm can do the job infewer than L(n) time units for arbitrary inputs, i.e., that everycomparison-based sorting algorithm must take at least L(n) timein the worst case.

• L(n) is the minimum over all possible algorithms, of themaximum complexity.

ADA Unit -3 I.S Borse 105

Page 106: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Lower and Upper Bound Theory

• Upper bound theory says that for any arbitrary inputs, we canalways sort in time at most U(n). How long it would take to solve aproblem using one of the known Algorithms with worst-caseinput gives us a upper bound.

• Improving an upper bound means finding an algorithm withbetter worst-case performance.

• U(n) is the minimum over all known algorithms, of the maximumcomplexity.

• Both upper and lower bounds are minima over the maximumcomplexity of inputs of size n.

• The ultimate goal is to make these two functions coincide. Whenthis is done, the optimal algorithm will have L(n) = U(n).

• Upper bound theory says that for any arbitrary inputs, we canalways sort in time at most U(n). How long it would take to solve aproblem using one of the known Algorithms with worst-caseinput gives us a upper bound.

• Improving an upper bound means finding an algorithm withbetter worst-case performance.

• U(n) is the minimum over all known algorithms, of the maximumcomplexity.

• Both upper and lower bounds are minima over the maximumcomplexity of inputs of size n.

• The ultimate goal is to make these two functions coincide. Whenthis is done, the optimal algorithm will have L(n) = U(n).

ADA Unit -3 I.S Borse 106

Page 107: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Lower and Upper Bound TheoryThere are few techniques for finding lower bounds

1) Trivial Lower Bounds: For many problems it ispossible to easily observe that a lower boundidentical to n exists, where n is the number ofinputs (or possibly outputs) to the problem.

• The method consists of simply counting thenumber of inputs that must be examined and thenumber of outputs that must be produced, andnote that any algorithm must, at least, read itsinputs and write its outputs.

1) Trivial Lower Bounds: For many problems it ispossible to easily observe that a lower boundidentical to n exists, where n is the number ofinputs (or possibly outputs) to the problem.

• The method consists of simply counting thenumber of inputs that must be examined and thenumber of outputs that must be produced, andnote that any algorithm must, at least, read itsinputs and write its outputs.

ADA Unit -3 I.S Borse 107

Page 108: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Lower and Upper Bound TheoryExample-2:Finding maximum of unordered arrayrequires examining each input so it is (n).

A simple counting arguments shows that anycomparison-based algorithm for finding themaximum value of an element in a list of size nmust perform at least n-1 comparisons for anyinput

Example-2:Finding maximum of unordered arrayrequires examining each input so it is (n).

A simple counting arguments shows that anycomparison-based algorithm for finding themaximum value of an element in a list of size nmust perform at least n-1 comparisons for anyinput

ADA Unit -3 I.S Borse 108

Page 109: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Lower and Upper Bound Theory2) Information Theory: The information theory method

establishing lower bounds by computing the limitationson information gained by a basic operation and thenshowing how much information is required before agiven problem is solved.

• This is used to show that any possible algorithm forsolving a problem must do some minimal amount ofwork.

• The most useful principle of this kind is that theoutcome of a comparison between two items containsone bit of information.

2) Information Theory: The information theory methodestablishing lower bounds by computing the limitationson information gained by a basic operation and thenshowing how much information is required before agiven problem is solved.

• This is used to show that any possible algorithm forsolving a problem must do some minimal amount ofwork.

• The most useful principle of this kind is that theoutcome of a comparison between two items containsone bit of information.

ADA Unit -3 I.S Borse 109

Page 110: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Lower and Upper Bound TheoryExample-1:For the problem of searching an ordered list with

n Elements for the position of a particular item,

Proof:There are n possible outcomes, input strings

– In this case lgn comparisons are necessary,

– So, unique identification of an index in the list requires lgnbits.

– Therefore, lgn bits are necessary to specify one of the mpossibilities.

Example-1:For the problem of searching an ordered list withn Elements for the position of a particular item,

Proof:There are n possible outcomes, input strings

– In this case lgn comparisons are necessary,

– So, unique identification of an index in the list requires lgnbits.

– Therefore, lgn bits are necessary to specify one of the mpossibilities.

ADA Unit -3 I.S Borse 110

Page 111: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Lower and Upper Bound Theory

>

> >

• 2 bits of information is necessary.

a,b,c,d

a, ba c , d

>

> >

• 2 bits of information is necessary.

ADA Unit -3 I.S Borse 111

Aaaaaa

bCcc d

Page 112: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Lower and Upper Bound TheoryExample-2: By using information theory, the lower bound for the

problem of comparison-based sorting problem is (nlgn)

Proof:– If we only know that the input is orderable, then thereare n! possible outcomes

– each of the n! permutations of n things.

– Within the comparison-swap model, we can only usecomparisons to derive information

– Based on the information theory, lgn! bits of information isnecessary by the number of comparisons needed to be done inthe worst-case to sort n things.

Example-2: By using information theory, the lower bound for theproblem of comparison-based sorting problem is (nlgn)

Proof:– If we only know that the input is orderable, then thereare n! possible outcomes

– each of the n! permutations of n things.

– Within the comparison-swap model, we can only usecomparisons to derive information

– Based on the information theory, lgn! bits of information isnecessary by the number of comparisons needed to be done inthe worst-case to sort n things.

ADA Unit -3 I.S Borse 112

Page 113: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Lower and Upper Bound Theory• The result of a given comparison between two list of elements

yields a single bit of information (0=False, 1 = True).

• Each of the n! permutations of {1, 2, …, n} has to be distinguishedby the correct algorithm.

• Thus, a comparison-based algorithm must perform enoughcomparisons to produce n! cumulative pieces of information.

• Since each comparison only yieldsone bit of information, thequestion is what the minimum number of bits of informationneeded to allow n! different outcomes is, which is lgn! bits.

• The result of a given comparison between two list of elementsyields a single bit of information (0=False, 1 = True).

• Each of the n! permutations of {1, 2, …, n} has to be distinguishedby the correct algorithm.

• Thus, a comparison-based algorithm must perform enoughcomparisons to produce n! cumulative pieces of information.

• Since each comparison only yieldsone bit of information, thequestion is what the minimum number of bits of informationneeded to allow n! different outcomes is, which is lgn! bits.

ADA Unit -3 I.S Borse 113

Page 114: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Lower and Upper Bound Theory• How fast lgn! grow? We can bind n! from above

by overestimating every term of the product, andbind it below by underestimating the first n/2terms.

• n/2 x n/2 x …x n/2 x…x 2 x 1

n! = n x (n-1) x …x 2 x1

n x n x …x n

(n/2)n/2 n! nn

½(nlgn-n) lgn! nlgn

This follows that lgn! (nlgn)

• How fast lgn! grow? We can bind n! from aboveby overestimating every term of the product, andbind it below by underestimating the first n/2terms.

• n/2 x n/2 x …x n/2 x…x 2 x 1

n! = n x (n-1) x …x 2 x1

n x n x …x n

(n/2)n/2 n! nn

½(nlgn-n) lgn! nlgn

This follows that lgn! (nlgn)ADA Unit -3 I.S Borse 114

Page 115: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Decision-tree model• This method can model the execution of any comparison

based problem.

One tree for each input size n.

• View the algorithm as splitting whenever it comparestwo elements.

• The tree contains the comparisons along all possibleinstruction traces.

• The running time of the algorithm = the length of thepath taken.

• Worst-case running time = the height of tree.

• This method can model the execution of any comparisonbased problem.

One tree for each input size n.

• View the algorithm as splitting whenever it comparestwo elements.

• The tree contains the comparisons along all possibleinstruction traces.

• The running time of the algorithm = the length of thepath taken.

• Worst-case running time = the height of tree.

ADA Unit -3 I.S Borse 115

Page 116: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Decision-tree Example• Sort < a1, a2, ….an> e.g =< 9, 4, 6>

1:2

1 ≤ 2 yes 1 ≤ 2 no

2:3 1:3

2 ≤ 3 yes 2 ≤ 3 no 1 ≤ 3 yes 1 ≤ 3 no

123 1:3 2:3

yes no 213 yes no

132 312 231 321Each internal node is labeled i:j for i, j є { 1,2…n}

The left subtree shows subsequent comparisons if ai ≤ aj

The right subtree shows subsequent comparisons if ai ≥ aj shows

• Sort < a1, a2, ….an> e.g =< 9, 4, 6>

1:2

1 ≤ 2 yes 1 ≤ 2 no

2:3 1:3

2 ≤ 3 yes 2 ≤ 3 no 1 ≤ 3 yes 1 ≤ 3 no

123 1:3 2:3

yes no 213 yes no

132 312 231 321Each internal node is labeled i:j for i, j є { 1,2…n}

The left subtree shows subsequent comparisons if ai ≤ aj

The right subtree shows subsequent comparisons if ai ≥ aj showsADA Unit -3 I.S Borse 116

Page 117: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Decision-tree modeling for searchingX =A[i] algo terminates , X< A[i] left branch or X> A[i] right branch

x: A(1)

failure x: A(1)

failure x: A(n)

failure failureA comparison tree for a linear search algorithm

X =A[i] algo terminates , X< A[i] left branch or X> A[i] right branch

x: A(1)

failure x: A(1)

failure x: A(n)

failure failureA comparison tree for a linear search algorithm

ADA Unit -3 I.S Borse 117

Page 118: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Decision-tree modelExample: By using the Decision Tree method, the lower

bound for comparison-based searching on ordered inputis (lgn)

• Proof: Let us consider all possible comparison treeswhich model algorithms to solve the searching problem.

– FIND(n) is bounded below by the distance of the longestpath from the root to a leaf in such a tree.

Example: By using the Decision Tree method, the lowerbound for comparison-based searching on ordered inputis (lgn)

• Proof: Let us consider all possible comparison treeswhich model algorithms to solve the searching problem.

– FIND(n) is bounded below by the distance of the longestpath from the root to a leaf in such a tree.

ADA Unit -3 I.S Borse 118

Page 119: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

Decision-tree model• There must be n internal nodes in all of these trees

corresponding to the n possible successful occurrences of x in A.

If all internal nodes of binary tree are at levels less than or equalto k (every height k-rooted binary tree has at most 2k+1 – 1nodes), then there are at most 2k – 1 internal nodes.

Thus, n 2k –1 and FIND(n) = k log (n+1) .

Because every leaf in a valid decision tree must be reachable,the worst-case number of comparisons done by such a tree isthe number of nodes in the longest path from the root to a leaf inthe binary tree consisting of the comparison nodes.

• There must be n internal nodes in all of these treescorresponding to the n possible successful occurrences of x in A.

If all internal nodes of binary tree are at levels less than or equalto k (every height k-rooted binary tree has at most 2k+1 – 1nodes), then there are at most 2k – 1 internal nodes.

Thus, n 2k –1 and FIND(n) = k log (n+1) .

Because every leaf in a valid decision tree must be reachable,the worst-case number of comparisons done by such a tree isthe number of nodes in the longest path from the root to a leaf inthe binary tree consisting of the comparison nodes.

ADA Unit -3 I.S Borse 119

Page 120: Algorithm Design & Analysis Chapter -03 (Backtracking ... · Algorithm Design & Analysis Chapter -03 (Backtracking & Branch and Bound ) T.E(Computer) By I.S Borse SSVP ˇS BSD COE

120ADA Unit -3 I.S Borse