Escolar Documentos
Profissional Documentos
Cultura Documentos
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
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
}
Backtracking Method
Backtracking?
Backtracking Method
What is it?
Backtracking Method
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
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 [ ]
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
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
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
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
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
( 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