Week13_Lab.pptx

advertisement
CS1020
Week 13: 16th April 2015
Contents

2
Sit-in Lab #4
Sit-in Lab #4
Set A – Word Scramble
Set B – Numeric Sequence
3
Objective

Recursion
•
•
•
•
4
Understanding the principles of recursion
Understanding the basic requirements of a problem
Formulating a recursive solution
Figuring out the optimized way of solving a problem
Set A: Word Scramble
Problem Statement


Word Scramble: Find a word in a character array
Input
1.
2.

A character array
A word
Objectives
1.
Finding if the characters of the word are present in the character
array as substring
 All the letters of the word must be in consecutive positions in the array
2.
Finding if the characters of the word are present in the character
array as subsequence


Things to learn:
1.
2.
5
All the letters of the word must be in same order in the array
Using recursion
Using recursion in combination with loop
Set A: Word Scramble
Recursive Logic: Substring
Character
Array
b
r
Word to
search
e
a
d
t
a
t
a
l
k
Recursively
search
Search
for the
for position
the next
starting
till end
ofcharacters
the word first
of array/word
6
l
k
Set A: Word Scramble
Recursive Logic: Substring
Character
Array
r re er ae da td ai
Word to
search
r
e
a
d
SearchRecursively
again for the
Search for the search
starting position
of
the next
startingfor
position
the word
in the rest
characters
of the
word firsttill end
of the
array
of array/word
7
ln
g
Set A: Word Scramble
Recursive Logic: Subsequence
Character
Array
b
r
Word to
search
e
a
r
d
e
t
d
a
l
If a character
mismatches, ignore it
and continue to search
in the remaining array
Search
for the
Recursively
search for
starting
position
the next characters till
of the
first
endword
of array/word
8
k
Set A: Word Scramble
Program Flow
Substring search
Subsequence search
findWordInArray
1. Find the position of
first character of the
word in array  i
2. Call a recursive method
to find word in the
array given the 1st
position 
findWordInArrayRec(ar
ray,word,i,1)
3. If returns True  Print
i
4. Otherwise Continue
from step 1 for
remaining array
9
findWordInArrayRec
1. Match array[i+1] and
word[k]
2. If matches
Call
findWordInArrayRec(
array,word,i+1,k+1)
3. Otherwise return false
4. Base Case: End of
word has been
reached  return
True
findWordInArrayRec
1. Match array[i+1] and
word[k]
2. If matches
Call
findWordInArrayRec(
array,word,i+1,k+1)
3. Otherwise Continue from
step 1 for remaining
array
4. Base Case: End of word
has been reached 
return True
Set B: Numeric Sequence
Problem Statement


Numeric Sequence: Finding maximum value from a sequence
Input
1. An array integers

Objectives
1. Whether the array is sorted in increasing sequence or not

Determine whether an array is sorted, if yes, then print the
maximum element
2. Find the maximum value from a sequence which is at first
increasing then decreasing

10
Things to learn:
1. Using recursion
2. Using binary search appropriately
Set B: Numeric Sequence
Recursive Logic: Check if sorted

To check whether a complete array is sorted or not you need
to check whether every two consecutive elements are sorted
or not
5
10
15
25
20
30
Not Sorted!!
 Note that for this problem it is necessary to do
we need to check every element
11
linear scan –
Set B: Numeric Sequence
Recursive Logic: Find maximum element
 Property of the array:
1. The sequence is increasing at first and then decreasing
2. There are no duplicate elements
Maximum element is the peak element
 Last element of the increasing sequence/first element
of decreasing element


It is greater than the element before it
It is also greater than the element after it
 There is always a single element in the array satisfying
both the above properties
12
Set B: Numeric Sequence
Recursive Logic: Find maximum element
 Binary search:
1. If the element is peak element return the element // base case
2. If the current element belongs to increasing sequence
(arr[i-1] < arr[i] < arr[i+1])

search on the right side of array
3. If the current element belongs to decreasing sequence
(arr[i-1] > arr[i] > arr[i+1])

search on the left side of array
1
3
5
9
11
10
8
Belongs to
to
Belongs
Peak element!
increasing sequence
decreasing
–– search
search on
on right
left
13
Set B: Numeric Sequence
Program Flow
findMax
checkIfSorted
1. If array contains single
element return true
2. If value at index < value
at index+1, check the
rest of the array (from
index+1)
3. Otherwise return false
14
1. If array contains single element
return the element
2. If array contains 2 elements,
return the greater element
3. Check mid-point of array
i. If peak element, return the
value
ii. If belongs to increasing seq,
findMax in right half
iii. If belongs to decreasing seq,
findMax in left half
END OF FILE
15
Download