Você está na página 1de 91

String Permutation (Backtrack)

Rod Cutting Problem

Submitted to Submitted by
Md. Mahfuz Reza Md. Wakil Khan
Assistant Professor CE-15054
Dept. of CSE
MBSTU Mahmudul Hassan
CE-16054
String Permutation of Given String

Naive & Backtracking Method


Naive Method

ex.
for( initialize ; condition ; inc/dec)
We can generate permutation of a {
given string using nested loops . for( initialize ; condition ; inc/dec)
To generate permutation of a string {
of length n , n numbers of nested for( initialize ; condition ; inc/dec)
loops needed. {
// conditions and statement
}
}
}
Naive Method
ara [ ] A B

for( x = 0 ; x < 2 ; x++)


To generate permutation of a {
given array using naive method, print( ara [ x ] );
following code can be helpful
for( y = 0 ; y < 2 ; y++)
{
-> If the array length be a bigger if(x==y)continue;
number , such as 1000 , we have
print( ara [ y ] );
to write a code similarly using
1000 Nested for Loops. }

}
Backtracking Method

Backtracking?
Backtracking Method

What is it?
Backtracking Method

Backtracking is an algorithmic technique. When finding a solution by trying one


of several choices, If the choice proves incorrect, computation backtracks or
restarts at the point of choice and tries another choice. It is often convenient to
maintain choice points and alternate choices using recursion.

It demonstrates that sometimes you need to return to a previous state and re-
evaluate a previous decision in order to solve a problem.
Backtracking Method
Backtracking means tracking (or moving) back in order to find other path to get
the required solution.
Suppose, we want to go from Source to End from the following graph

Source B

C End
Backtracking Method

Source B
Backtracking

C End

First, we may select the way to B from Source. But we don’t find any forward edge
from B to reach End. So, we Backtrack to A and choose the way to C to reach End.
Backtracking Method For String Permutation

To construct all n! permutations of a string of length n… …

 set up an array/vector of n cells to keep tracking of visited array index

 set up another array/vector of n cells to store used characters

 free up visited array index to non-visited when necessary ( Backtrack)

 print stored permuted characters when found the Base Case


Task

Construct all permutations for “ A B C”


Sample Code For String Permutation ( Backtracking )

int visited[27]; char str[27]; int main()


void permute(char *ara, int level, int n) {
{ int x,y,z,t,n,cou=0;
if(level==n){ printf("Ara Length >>\n");
for(int y=0;y<n;y++)printf("%c",str[y]); scanf("%d",&n);
printf("\n"); char ara[n];
return;
} printf("Enter String >>\n");
cin>>ara;
for(int x=0; x<n; x++) memset( visited,0,sizeof(visited) );
{
if(!visited[x]) system("cls");
{ printf("\nPermutation of %s are ...\n\n",ara);
visited[x]=1;
str[level] = ara[x]; permute(ara,0,n);
permute(ara,level+1,n);
visited[x]=0; //Backtrack return 0;
} }
}
}
How the CODE will work ?
ABC
× × × visited [ ]
000
A B C
depth

0
1
2

str [ ]
ABC
 × × visited [ ]
100
A B C
depth

A 0
1
2

str [ ]
ABC
 × × visited [ ]
100
A B C
ABC depth
100
A 0
1
2

str [ ]
ABC
  × visited [ ]
100
A B C
ABC depth
110
A 0
B 1
2

str [ ]
ABC
  × visited [ ]
100
A B C
ABC depth
110
A 0

ABC B 1
110 2

str [ ]
ABC
   visited [ ]
100
A B C
ABC depth
110
A 0

ABC B 1
111 C 2

str [ ]
ABC
   visited [ ]
100
A B C
ABC depth
110
A 0

ABC B 1
111 C 2

str [ ]

Base Case
print A B C
ABC
   visited [ ]
100
A B C
ABC depth
110
A 0
void permute(char *ara, int level, int n)
ABC { B 1
………………………
111 for( int x=0 ; x<n; x++) C 2
{
if( !visited[x] ){ str [ ]
visited[x]=1;
str [level] = ara [x];
Base Case permute(ara,level+1,n);
visited[x]=0; //Backtrack
print A B C }
}
}
ABC
level = 0    visited [ ]
100
A B C
ABC depth
level = 1
110
A 0

ABC B 1
level = 2
111 C 2

str [ ]

Base Case level = 3


print A B C
ABC
   visited [ ]
100
A B C
ABC depth
110
A 0

ABC B 1
111 C 2
if(level==n){
str [ ]
for( int y=0;y<n ; y++)printf("%c",str[y]);
printf("\n");
Base Case return;
print A B C }
... … … … … … …
... … … … … … …
permute(ara,level+1,n);
ABC
   visited [ ]
100
A B C
ABC depth
110
A 0

ABC B 1
111 C 2

str [ ]
return
ABC
  × visited [ ]
100
A B C
ABC depth
110
A 0

ABC B 1
110 C 2

str [ ]
return
ABC
 ×  visited [ ]
100
A B C
ABC depth
101
A 0

ABC C 1
110 C 2

void permute(char *ara, int level, int n) str [ ]


{
return ………………………
for( int x=0 ; x<n; x++)
{
………………
}
}
ABC
 ×  visited [ ]
100
A B C
ABC depth
101
A 0

ABC C 1
110 C 2

str [ ]
return
ABC
 ×  visited [ ]
100
A B C
ABC depth
101
A 0

ABC ABC C 1
110 101 C 2

str [ ]
return
ABC
   visited [ ]
100
A B C
ABC depth
101
A 0

ABC ABC C 1
110 111 B 2

str [ ]
return Base Case
print ACB
ABC
   visited [ ]
100
A B C
ABC depth
101
A 0

ABC ABC C 1
110 111 B 2

str [ ]
return return
ABC
 ×  visited [ ]
100
A B C
ABC depth
101
A 0

ABC ABC C 1
110 101 B 2

str [ ]
return return
ABC
 × × visited [ ]
100
A B C
ABC depth
100
A 0

ABC ABC C 1
110 101 B 2

str [ ]
return return
ABC
× × × visited [ ]
000
A B C
ABC depth
100
A 0

ABC ABC C 1
110 101 B 2

str [ ]
return return
ABC
×  × visited [ ]
010
A B C
ABC depth
100
B 0

ABC ABC C 1
110 101 B 2

str [ ]
return return
ABC
  × visited [ ]
010
A B C
ABC ABC depth
100 110 B 0

ABC ABC A 1
110 101 B 2

str [ ]
return return
ABC
   visited [ ]
010
A B C
ABC ABC depth
100 110 B 0

ABC ABC A 1
ABC
110 101 C 2
111
str [ ]
return return
ABC
   visited [ ]
010
A B C
ABC ABC depth
100 110 B 0

ABC ABC A 1
ABC
110 101 C 2
111
str [ ]
return return Base Case
print BAC
ABC
   visited [ ]
010
A B C
ABC ABC depth
100 110 B 0

ABC ABC A 1
ABC
110 101 C 2
111
str [ ]
return return return
ABC
  × visited [ ]
010
A B C
ABC ABC depth
100 110 B 0

ABC ABC A 1
ABC
110 101 C 2
110
str [ ]
return return return
ABC
×  × visited [ ]
010
A B C
ABC ABC depth
100 010 B 0

ABC ABC A 1
ABC
110 101 C 2
110
str [ ]
return return return
ABC
×   visited [ ]
010
A B C
ABC ABC depth
100 011 B 0

ABC ABC C 1
ABC
110 101 C 2
110
str [ ]
return return return
ABC
   visited [ ]
010
A B C
ABC ABC depth
100 011 B 0

ABC ABC C 1
ABC ABC
110 101 A 2
110 111
str [ ]
return return return
ABC
   visited [ ]
010
A B C
ABC ABC depth
100 011 B 0

ABC ABC C 1
ABC ABC
110 101 A 2
110 111
str [ ]
return return return Base Case
print BCA
ABC
   visited [ ]
010
A B C
ABC ABC depth
100 011 B 0

ABC ABC C 1
ABC ABC
110 101 A 2
110 111
str [ ]
return return return return
ABC
×   visited [ ]
010
A B C
ABC ABC depth
100 011 B 0

ABC ABC C 1
ABC ABC
110 101 A 2
110 011
str [ ]
return return return return
ABC
×  × visited [ ]
010
A B C
ABC ABC depth
100 010 B 0

ABC ABC C 1
ABC ABC
110 101 A 2
110 011
str [ ]
return return return return
ABC
× × × visited [ ]
000
A B C
ABC ABC depth
100 010 B 0

ABC ABC C 1
ABC ABC
110 101 A 2
110 011
str [ ]
return return return return
ABC
× ×  visited [ ]
001
A B C
ABC ABC depth
100 010 C 0

ABC ABC C 1
ABC ABC
110 101 A 2
110 011
str [ ]
return return return return
ABC
 ×  visited [ ]
001
A B C
ABC ABC ABC depth
100 010 101
C 0

ABC ABC A 1
ABC ABC
110 101 A 2
110 011
str [ ]
return return return return
ABC
   visited [ ]
001
A B C
ABC ABC ABC depth
100 010 101
C 0

ABC ABC ABC A 1


ABC ABC
110 101 111 B 2
110 011
str [ ]
return return return return
ABC
   visited [ ]
001
A B C
ABC ABC ABC depth
100 010 101
C 0

ABC ABC ABC A 1


ABC ABC
110 101 111 B 2
110 011
str [ ]
return return return return Base Case
print CAB
ABC
   visited [ ]
001
A B C
ABC ABC ABC depth
100 010 101
C 0

ABC ABC ABC A 1


ABC ABC
110 101 111 B 2
110 011
str [ ]
return return return return return
ABC
 ×  visited [ ]
001
A B C
ABC ABC ABC depth
100 010 101
C 0

ABC ABC ABC A 1


ABC ABC
110 101 101 B 2
110 011
str [ ]
return return return return return
ABC
× ×  visited [ ]
001
A B C
ABC ABC ABC depth
100 010 001
C 0

ABC ABC ABC A 1


ABC ABC
110 101 101 B 2
110 011
str [ ]
return return return return return
ABC
×   visited [ ]
001
A B C
ABC ABC ABC depth
100 010 011
C 0

ABC ABC ABC B 1


ABC ABC
110 101 101 B 2
110 011
str [ ]
return return return return return
ABC
   visited [ ]
001
A B C
ABC ABC ABC depth
100 010 011
C 0

ABC ABC ABC ABC B 1


ABC ABC
110 101 101 111 A 2
110 011
str [ ]
return return return return return
ABC
   visited [ ]
001
A B C
ABC ABC ABC depth
100 010 011
C 0

ABC ABC ABC ABC B 1


ABC ABC
110 101 101 111 A 2
110 011
str [ ]
return return return return return Base Case
print CBA
ABC
   visited [ ]
001
A B C
ABC ABC ABC depth
100 010 011
C 0

ABC ABC ABC ABC B 1


ABC ABC
110 101 101 111 A 2
110 011
str [ ]
return return return return return return
ABC
×   visited [ ]
001
A B C
ABC ABC ABC depth
100 010 011
C 0

ABC ABC ABC ABC B 1


ABC ABC
110 101 101 011 A 2
110 011
str [ ]
return return return return return return
ABC
× ×  visited [ ]
001
A B C
ABC ABC ABC depth
100 010 001
C 0

ABC ABC ABC ABC B 1


ABC ABC
110 101 101 011 A 2
110 011
str [ ]
return return return return return return
ABC
× × × visited [ ]
000
A B C
ABC ABC ABC depth
100 010 001
C 0

ABC ABC ABC ABC B 1


ABC ABC
110 101 101 011 A 2
110 011
str [ ]
return return return return return return
Permutation of “ ABC “ ……..

ABC
ACB
BAC
BCA
CAB
CBA
Rod Cutting Problem

Dynamic Programming
Problem Description

Sterling Enterprises buys long steel rods of length n and cuts them into
shorter rods in a way so that selling the pieces maximizes the total amount
of money , the management of Sterling Enterprises wants to know
maximized total amount that they can sell.
Given Data

Suppose, A piece of length i is worth 𝑝𝑖 dollars. The length of the rod


can be cut in inches = 1,2,3,4
Full, Rod length is 5 .

Length i 1 2 3 4

Price 𝑝𝑖 2 5 7 11
Dynamic Solution
Rod Lengths

Cost of
Lengths 0 1 2 3 4 5

(2) 1

(5) 2

(7) 3

( 11 ) 4
Rod Lengths

Cost of
Lengths 0 1 2 3 4 5

(2)  1 2

(5) 2

(7) 3

( 11 ) 4
Rod Lengths

Cost of
Lengths 0 1 2 3 4 5

(2)  1 2 4

(5) 2

(7) 3

( 11 ) 4
Rod Lengths

Cost of
Lengths 0 1 2 3 4 5

(2)  1 2 4 6

(5) 2

(7) 3

( 11 ) 4
Rod Lengths

Cost of
Lengths 0 1 2 3 4 5

(2)  1 2 4 6 8

(5) 2

(7) 3

( 11 ) 4
Rod Lengths

Cost of
Lengths 0 1 2 3 4 5

(2)  1 2 4 6 8 10

(5) 2

(7) 3

( 11 ) 4
Rod Lengths

Cost of
Lengths 0 1 2 3 4 5

(2) 1 2 4 6 8 10

(5)  2 2

(7) 3

( 11 ) 4
Rod Lengths

Cost of
Lengths 0 1 2 3 4 5

(2) 1 2 4 6 8 10

(5)  2 2 ?
(7) 3

( 11 ) 4

? = max ( new_calculated_value , upper index value )


Rod Lengths

Cost of
Lengths 0 1 2 3 4 5

(2) 1 2 4 6 8 10

(5)  2 2 5

(7) 3

( 11 ) 4
Rod Lengths

Cost of To make rod of total length 3 , If we pick


Lengths up a rod cut of length 2 , then selling cost
0 1 2 3 4 5
is 5 and maximum selling cost for
length 1 is 2 .
(2) 1 2 4 6 8 10
So, total selling cost
= ( cost of rod length of 2 + maximum
(5)  2 2 5 7 cost of rod length of 1)
= ( 5 + 2)
=7
(7) 3
Index value = max ( 7 , upper index value )

( 11 ) 4
Rod Lengths

Cost of
Lengths 0 1 2 3 4 5

(2) 1 2 4 6 8 10

(5)  2 2 5 7

(7) 3

( 11 ) 4
Rod Lengths

Cost of
Lengths 0 1 2 3 4 5

(2) 1 2 4 6 8 10

(5)  2 2 5 7 10

(7) 3

( 11 ) 4
Rod Lengths

Cost of
Lengths 0 1 2 3 4 5

(2) 1 2 4 6 8 10

(5)  2 2 5 7 10 12

(7) 3

( 11 ) 4
Rod Lengths

Cost of
Lengths 0 1 2 3 4 5

(2) 1 2 4 6 8 10

(5) 2 2 5 7 10 12

(7)  3 2

( 11 ) 4
Rod Lengths

Cost of
Lengths 0 1 2 3 4 5

(2) 1 2 4 6 8 10

(5) 2 2 5 7 10 12

(7)  3 2 5

( 11 ) 4
Rod Lengths

Cost of
Lengths 0 1 2 3 4 5

(2) 1 2 4 6 8 10

(5) 2 2 5 7 10 12

(7)  3 2 5 7

( 11 ) 4
Rod Lengths

Cost of
Lengths 0 1 2 3 4 5

(2) 1 2 4 6 8 10

(5) 2 2 5 7 10 12

(7)  3 2 5 7 10

( 11 ) 4
Rod Lengths

Cost of
Lengths 0 1 2 3 4 5

(2) 1 2 4 6 8 10

(5) 2 2 5 7 10 12

(7)  3 2 5 7 10 12

( 11 ) 4
Rod Lengths

Cost of
Lengths 0 1 2 3 4 5

(2) 1 2 4 6 8 10

(5) 2 2 5 7 10 12

(7) 3 2 5 7 10 12

( 11 )  4 2
Rod Lengths

Cost of
Lengths 0 1 2 3 4 5

(2) 1 2 4 6 8 10

(5) 2 2 5 7 10 12

(7) 3 2 5 7 10 12

( 11 )  4 2 5
Rod Lengths

Cost of
Lengths 0 1 2 3 4 5

(2) 1 2 4 6 8 10

(5) 2 2 5 7 10 12

(7) 3 2 5 7 10 12

( 11 )  4 2 5 7
Rod Lengths

Cost of
Lengths 0 1 2 3 4 5

(2) 1 2 4 6 8 10

(5) 2 2 5 7 10 12

(7) 3 2 5 7 10 12

( 11 )  4 2 5 7 11
Rod Lengths

Cost of
Lengths 0 1 2 3 4 5

(2) 1 2 4 6 8 10

(5) 2 2 5 7 10 12

(7) 3 2 5 7 10 12

( 11 )  4 2 5 7 11 13
Solution of the Problem

Maximum selling
cost amount of the
rod is 13 .
Thank
You

Você também pode gostar