Title: Data Structures with C Using STL
1Data Structures with CUsing STL
- Chapter Fifteen
- Recursive Algorithms
2When a function is called...
- A transfer of control occurs from the calling
block to the code of the function. It is
necessary that there be a return to the correct
place in the calling block after the function
code is executed. This correct place is called
the return address. - When any function is called, the run-time stack
is used. On this stack is placed an activation
record (stack frame) for the function call.
3Stack Activation Frames
- The activation record stores the return address
for this function call, and also the parameters,
local variables, and the functions return value,
if non-void. - The activation record for a particular function
call is popped off the run-time stack when the
final closing brace in the function code is
reached, or when a return statement is reached in
the function code. - At this time the functions return value, if
non-void, is brought back to the calling block
return address for use there.
4Merge Sort
- Merge sort is a basic Divide and Conquer
Algorithm. - Divide the list in half, sort each half and merge
back together. - The merge operation is an order N operation, in
that it requires one pass through the elements.
5Using Merge Sort Algorithm with N 16
-
16 - 8
8 - 4 4
4 4 - 2 2 2 2
2 2 2 2 - 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 -
6Efficiency of the divide process
- From the previous slide the process of dividing a
list of n elements, until you have n lists of one
element each is done log2n times on the list of
n. - Therefore the merge sort is a O(n log2n)
algorithm.
7- // Recursive merge sort algorithm
-
- template ltclass ItemType gt
- void MergeSort ( ItemType values , int
first , int last ) -
- // Pre first lt last
- // Post Array values first . . last sorted
into ascending order. -
- if ( first lt last ) //
general case - int middle ( first last ) / 2
- MergeSort ( values, first, middle )
- MergeSort( values, middle 1, last )
- // now merge two subarrays
8How does this work on a list of numbers?
27
18
45
5
71
83
92
36
59
64
9How does this work on a list of numbers?
27
18
45
5
71
83
92
36
59
64
10How does this work on a list of numbers?
27
18
45
5
71
83
11How does this work on a list of numbers?
5
71
59
64
83
12How does this work on a list of numbers?
13Another Recursive Algorithm
- Count the number of cells in a particular blob on
the board, where a blob consists of adjacent
(horizontal, or vertical)
0
1
2
3
4
5
6
7
B
P
Y
Y
Y
Y
G
G
0
0
B
P
P
Y
Y
G
G
B
1
1
G
P
P
P
G
G
B
B
2
2
R
G
Y
P
P
P
B
B
3
3
R
G
Y
P
P
R
R
R
4
4
G
Y
Y
Y
R
P
B
B
5
5
P
G
G
Y
R
R
B
B
6
6
P
P
G
Y
Y
R
R
R
7
7
0
1
2
3
4
5
6
7
14Recursive Function to Count Blob
- int BoardcountBlob(int row, int col, Color c)
- // cell is not on the board
- if ((row lt 0) (row gt nRows) (col lt 0)
(col gt nCols)) return 0 - // cell is of wrong color therefore not in blob
- if (boardrowcol ! c) return 0
- // count space and change color to NONE to
prevent double counting - int cnt 1
- boardrowcol NONE
- // count neighboring spaces which are part of
the blob - cnt countBlob(row-1, col, c)
- cnt countBlob(row, col-1, c)
- cnt countBlob(row, col1, c)
- cnt countBlob(row1, col, c)
- return cnt
15How it works!
result myBoard.countBlob(4,6,RED)
cnt
1
0
1
2
3
4
5
6
7
B
P
Y
Y
Y
Y
G
G
0
0
B
P
P
Y
Y
G
G
B
1
1
G
P
P
P
G
G
B
B
2
2
R
G
Y
P
P
P
B
B
3
3
R
G
Y
P
P
R
R
R
4
4
G
Y
Y
Y
R
P
B
B
5
5
P
G
G
Y
R
R
B
B
6
6
P
P
G
Y
Y
R
R
R
7
7
0
1
2
3
4
5
6
7
16How it works!
result myBoard.countBlob(4,6,RED)
cnt
1
0
1
2
3
4
5
6
7
B
P
Y
Y
Y
Y
G
G
0
0
cnt countBlob(3,6,RED) return 0
B
P
P
Y
Y
G
G
B
1
1
G
P
P
P
G
G
B
B
2
2
cnt countBlob(4,5,RED)
R
G
Y
P
P
P
B
B
3
3
cnt countBlob(4,7,RED)
R
G
Y
P
P
R
N
R
4
4
cnt countBlob(5,6,RED)
G
Y
Y
Y
R
P
B
B
5
5
P
G
G
Y
R
R
B
B
6
6
P
P
G
Y
Y
R
R
R
7
7
0
1
2
3
4
5
6
7
17How it works!
result myBoard.countBlob(4,6,RED)
cnt
2
0
1
2
3
4
5
6
7
B
P
Y
Y
Y
Y
G
G
0
0
cnt countBlob(3,6,RED)
B
P
P
Y
Y
G
G
B
1
1
G
P
P
P
G
G
B
B
2
2
cnt countBlob(4,5,RED) return 1
R
G
Y
P
P
P
B
B
3
3
cnt countBlob(4,7,RED)
R
G
Y
P
P
N
N
R
4
4
cnt countBlob(5,6,RED)
G
Y
Y
Y
R
P
B
B
5
5
P
G
G
Y
R
R
B
B
6
6
P
P
G
Y
Y
R
R
R
7
7
0
1
2
3
4
5
6
7
18How it works!
result myBoard.countBlob(4,6,RED)
cnt
3
0
1
2
3
4
5
6
7
B
P
Y
Y
Y
Y
G
G
0
0
cnt countBlob(3,6,RED)
B
P
P
Y
Y
G
G
B
1
1
G
P
P
P
G
G
B
B
2
2
cnt countBlob(4,5,RED)
R
G
Y
P
P
P
B
B
3
3
cnt countBlob(4,7,RED) return 1
R
G
Y
P
P
N
N
N
4
4
cnt countBlob(5,6,RED)
G
Y
Y
Y
R
P
B
B
5
5
P
G
G
Y
R
R
B
B
6
6
P
P
G
Y
Y
R
R
R
7
7
0
1
2
3
4
5
6
7
19How it works!
result myBoard.countBlob(4,6,RED)
cnt
3
0
1
2
3
4
5
6
7
B
P
Y
Y
Y
Y
G
G
0
0
cnt countBlob(3,6,RED)
B
P
P
Y
Y
G
G
B
1
1
G
P
P
P
G
G
B
B
2
2
cnt countBlob(4,5,RED)
R
G
Y
P
P
P
B
B
3
3
cnt countBlob(4,7,RED)
R
G
Y
P
P
N
N
N
4
4
cnt countBlob(5,6,RED) return 0
G
Y
Y
Y
R
P
B
B
5
5
P
G
G
Y
R
R
B
B
6
6
return cnt
P
P
G
Y
Y
R
R
R
7
7
0
1
2
3
4
5
6
7
20Combinatorics
- A branch of mathematics concerned with the
enumeration of various sets of objects. - Recursion is useful in this area, in that it
supplies concise solutions to these problems.
21Permutations
- List all permutations of a list of objects.
- The number of solutions is n! where n is the
number of objects. - Example Permutations of string mat
m
a
t
ma
mt
am
at
tm
ta
mat
mta
amt
atm
tma
tam
22Permutation recursive function
- void permute(Cstring word, Cstring available,
VectorltCstringgt permlist) - if (available.length() 0) // permutation
completed - permlist.push_back(word)
- else int i // declare local variables
- Cstring tempword, tempavail
- for (i0 iltavailable.length() i)
- tempwordword // add new letter to
permuation - tempwordtempwordavailablei
- tempavailavailable // delete letter from
available - tempavail.erase(i,1)
- // recursive call
- permute(tempword,tempavail,permlist)
-
-
23Backtracking
- A technique for dealing with an uncertain
decision. - Presented with more than one plausible path to
follow you arbitrarily choose one, and follow it
until you either arrive at a solution or know
that that is the wrong path because you arrive at
a contradiction. - If the current path is wrong, you then back up to
the last decision made and try a different path.
24Four Queens Problem
Place 4 Queens on a 4 x 4 chess board so that no
queen can attack another queen
25Place Queen in the first Row
Q
26Place Queen in Second Row
Q
Q
27Place Queen in Third Row
- There is no safe square in the third row given
the current configuration. - Remove Queen in second row and move to next safe
space. - Try placing a Queen in the third row with this
configuration.
Q
Q
Q
Q
28Place Queen in Third Row
- Place Queen in third row.
- Try to place queen in fourth row.
Q
Q
Q
Q
Q
29Place Queen in FourthRow
- There is no safe square in the fourth row given
the current configuration. - Remove Queen in third row and try the next
space. - Since there is no safe space on the third row,
remove Queen from the second row . - Since there is more spaces to try in the second
row , move the Queen in the first row and try
again.
Q
Q
Q
Q
Q
Q
30- Put Queen in Row 1 Put Queen in Row 2
- Put Queen in Row 3 Put Queen in Row 4
Q
Q
Q
Q
Q
Q
Q
Q
Q
Q
31Four Queens Problem Solution
- bool ChessBoardAddQueen (int Row)
- int Col
- bool GoodSquare
- if ( Row gt 3 ) return true
// Problem solved - GoodSquare false
- for ( Col 0 (Col lt 3) ( !
GoodSquare ) Col ) - if ( this-gtNoAttack ( Row, Col ))
- this-gtPlaceQueen (Row,
Col) - if (this-gtAddQueen ( Row
1 )) - GoodSquare true
// Report success - else this-gtRemoveQueen
(Row, Col) // Backtrack -
- return GoodSquare
-