csc212 dtst tdata structurewolberg/cs212/pdf/csc212-19-searching.pdfwhen does the best-case time...
TRANSCRIPT
CSC212 D t St tCSC212 D t St tData Structure Data Structure
Lecture 18Lecture 18SearchingSearching
Instructor: George Instructor: George WolbergWolbergDepartment of Computer Science Department of Computer Science
City College of New YorkCity College of New York
@ @ George Wolberg, 2016George Wolberg, 2016 11
TopicsTopicsTopicsTopics
ApplicationsApplicationsMost Common MethodsMost Common MethodsSerial Search Serial Search Binary SearchBinary SearchyySearch by Hashing (next lecture)Search by Hashing (next lecture)
RunRun--Time AnalysisTime AnalysisyyAverageAverage--time analysistime analysisTime analysis of recursive algorithmsTime analysis of recursive algorithms
@ @ George Wolberg, 2016George Wolberg, 2016 22
y gy g
ApplicationsApplicationsApplicationsApplications
Searching a list of values is a common Searching a list of values is a common computational taskcomputational taskpp
ExamplesExamplesdatabase: student record bank account recorddatabase: student record bank account recorddatabase: student record, bank account record, database: student record, bank account record,
credit record...credit record... InternetInternet –– information retrieval: Yahoo Googleinformation retrieval: Yahoo Google Internet Internet information retrieval: Yahoo, Googleinformation retrieval: Yahoo, GoogleBiometrics Biometrics ––face/ fingerprint/ iris IDsface/ fingerprint/ iris IDs
@ @ George Wolberg, 2016George Wolberg, 2016 33
Most Common MethodsMost Common MethodsMost Common MethodsMost Common Methods
Serial Search Serial Search simplest, O(n)simplest, O(n) simplest, O(n)simplest, O(n)
Binary SearchBinary Search averageaverage case O(log n)case O(log n) averageaverage--case O(log n)case O(log n)
Search by Hashing (the next lecture)Search by Hashing (the next lecture)bb ffbetter averagebetter average--case performancecase performance
@ @ George Wolberg, 2016George Wolberg, 2016 44
Serial SearchSerial SearchSerial SearchSerial SearchPseudocode for Serial SearchPseudocode for Serial Search// search for a desired item in an array a of size n// search for a desired item in an array a of size n
A serial search A serial search algorithm steps algorithm steps
set i to 0 and set found to false;set i to 0 and set found to false;
while (i<n && ! found)while (i<n && ! found)through (part through (part of ) an array of ) an array one item aone item a
while (i<n && ! found)while (i<n && ! found){{
if (a[i] is the desired item) if (a[i] is the desired item) found = true;found = true;one item a one item a
time, looking time, looking for a “desiredfor a “desired
elseelse++i;++i;
}}for a desired for a desired item”item” if (found) if (found)
return i; // indicating the location of the desired itemreturn i; // indicating the location of the desired itemelseelse
@ @ George Wolberg, 2016George Wolberg, 2016 55
elseelsereturn return ––1; // indicating “not found”1; // indicating “not found”
Serial Search -AnalysisSerial Search -AnalysisSerial Search -AnalysisSerial Search -Analysis
Size of array: nSize of array: nBestBest--Case: O(1)Case: O(1)
33 22 44 66 55 11 88 77
( )( ) item in [0]item in [0]
WorstWorst--Case: O(n)Case: O(n)3 6 7 9
Wo sWo s C se: O( )C se: O( ) item in [nitem in [n--1] or not found1] or not found
AverageAverage--CaseCaseAverageAverage CaseCaseusually requires fewer than n array accessesusually requires fewer than n array accessesBut, what are the average accesses?But, what are the average accesses?
@ @ George Wolberg, 2016George Wolberg, 2016 66
, g, g
Average-Case Time for Serial SearchAverage-Case Time for Serial SearchAverage-Case Time for Serial SearchAverage-Case Time for Serial Search
A more accurate computation:A more accurate computation:Assume the target to be searched is in the arrayAssume the target to be searched is in the arrayAssume the target to be searched is in the arrayAssume the target to be searched is in the array and the probability of the item being in any and the probability of the item being in any
array location is the samearray location is the sameyyThe average accessesThe average accesses
2)1(2/)1(...321
n
nnn
nn
@ @ George Wolberg, 2016George Wolberg, 2016 77
When does the best-case time When does the best-case time make more sense?make more sense?
For an array of For an array of nn elements, the bestelements, the best--case case time for serial search is just one array time for serial search is just one array j yj yaccess.access.
The bestThe best--case time is more useful if thecase time is more useful if theThe bestThe best case time is more useful if the case time is more useful if the probability of the target being in the [0] probability of the target being in the [0] location is the highestlocation is the highestlocation is the highest. location is the highest. or loosely if the target is most likely in the front or loosely if the target is most likely in the front
part of the arraypart of the array
@ @ George Wolberg, 2016George Wolberg, 2016 88
part of the arraypart of the array
Binary SearchBinary SearchBinary SearchBinary Search
If If nn is huge, and the item to be searched can be in is huge, and the item to be searched can be in any locations, serial search is slow on averageany locations, serial search is slow on average
But if the items in an array are sorted, we can But if the items in an array are sorted, we can somehow know a target’s location earliersomehow know a target’s location earlier Array of integers from smallest to largestArray of integers from smallest to largest Array of strings sorted alphabetically (e.g. dictionary)Array of strings sorted alphabetically (e.g. dictionary)
A f t d t d t d b ID bA f t d t d t d b ID b Array of students records sorted by ID numbersArray of students records sorted by ID numbers
@ @ George Wolberg, 2016George Wolberg, 2016 99
Binary Search in an Integer ArrayBinary Search in an Integer ArrayBinary Search in an Integer ArrayBinary Search in an Integer Arrayif target is in the arrayif target is in the array
Items are sortedItems are sorted target = 16target = 16 n = 8n = 8
22 33 66 77 1010 1212 1616 1818
[0] [1] [2] [3] [4] [5] [6] [7][0] [1] [2] [3] [4] [5] [6] [7] n 8n 8
Go to the middle Go to the middle location i = n/2location i = n/2
[0] [1] [2] [3] [4] [5] [6] [7][0] [1] [2] [3] [4] [5] [6] [7]
location i = n/2location i = n/2 if (a[i] is target)if (a[i] is target)
done!done! else if (target <a[i]) else if (target <a[i])
go to the first halfgo to the first half else if (target >a[i])else if (target >a[i])
@ @ George Wolberg, 2016George Wolberg, 2016 1010
else if (target a[i])else if (target a[i]) go to the second halfgo to the second half
Binary Search in an Integer ArrayBinary Search in an Integer ArrayBinary Search in an Integer ArrayBinary Search in an Integer Arrayif target is in the arrayif target is in the array
Items are sortedItems are sorted target = 16target = 16 n = 8n = 8
22 33 66 77 1010 1212 1616 1818
[0] [1] [2] [3] [4] [5] [6] [7][0] [1] [2] [3] [4] [5] [6] [7] n 8n 8
Go to the middle Go to the middle location i = n/2location i = n/2
[0] [1] [2] [3] [4] [5] [6] [7][0] [1] [2] [3] [4] [5] [6] [7]
[0] [1] [2] [3] [0] [1] [2][0] [1] [2] [3] [0] [1] [2]location i = n/2location i = n/2 if (a[i] is target)if (a[i] is target)
done!done!
[0] [1] [2] [3] [0] [1] [2][0] [1] [2] [3] [0] [1] [2]
else if (target <a[i]) else if (target <a[i]) go to the first halfgo to the first half
else if (target >a[i])else if (target >a[i])
@ @ George Wolberg, 2016George Wolberg, 2016 1111
else if (target a[i])else if (target a[i]) go to the second halfgo to the second half
Binary Search in an Integer ArrayBinary Search in an Integer ArrayBinary Search in an Integer ArrayBinary Search in an Integer Arrayif target is in the arrayif target is in the array
Items are sortedItems are sorted target = 16target = 16 n = 8n = 8
22 33 66 77 1010 1212 1616 1818
[0] [1] [2] [3] [4] [5] [6] [7][0] [1] [2] [3] [4] [5] [6] [7] n 8n 8
Go to the middle Go to the middle location i = n/2location i = n/2
[0] [1] [2] [3] [4] [5] [6] [7][0] [1] [2] [3] [4] [5] [6] [7]
[0] [1] [2] [3] [0] [1] [2][0] [1] [2] [3] [0] [1] [2]location i = n/2location i = n/2 if (a[i] is target)if (a[i] is target)
done!done!
[0] [1] [2] [3] [0] [1] [2][0] [1] [2] [3] [0] [1] [2]
DONEDONE else if (target <a[i]) else if (target <a[i]) go to the first halfgo to the first half
else if (target >a[i])else if (target >a[i])
DONEDONE
@ @ George Wolberg, 2016George Wolberg, 2016 1212
else if (target a[i])else if (target a[i]) go to the second halfgo to the second half
Binary Search in an Integer ArrayBinary Search in an Integer ArrayBinary Search in an Integer ArrayBinary Search in an Integer Arrayif target is in the arrayif target is in the array
Items are sortedItems are sorted target = 16target = 16 n = 8n = 8
22 33 66 77 1010 1212 1616 1818
[0] [1] [2] [3] [4] [5] [6] [7][0] [1] [2] [3] [4] [5] [6] [7] n 8n 8
Go to the middle Go to the middle location i = n/2location i = n/2
[0] [1] [2] [3] [4] [5] [6] [7][0] [1] [2] [3] [4] [5] [6] [7]
[0] [1] [2] [3] [0] [1] [2][0] [1] [2] [3] [0] [1] [2]location i = n/2location i = n/2 if (a[i] is target)if (a[i] is target)
done!done!
[0] [1] [2] [3] [0] [1] [2][0] [1] [2] [3] [0] [1] [2]
DONEDONE else if (target <a[i]) else if (target <a[i]) go to the first halfgo to the first half
else if (target >a[i])else if (target >a[i])
DONEDONE
recursive calls: what recursive calls: what are the parameters?are the parameters?
@ @ George Wolberg, 2016George Wolberg, 2016 1313
else if (target a[i])else if (target a[i]) go to the second halfgo to the second half
are the parameters?are the parameters?
Binary Search in an Integer ArrayBinary Search in an Integer ArrayBinary Search in an Integer ArrayBinary Search in an Integer Arrayif target is in the arrayif target is in the array
Items are sortedItems are sorted target = 16target = 16 n = 8n = 8
22 33 66 77 1010 1212 1616 1818
[0] [1] [2] [3] [4] [5] [6] [7][0] [1] [2] [3] [4] [5] [6] [7] n 8n 8
Go to the middle Go to the middle location i = n/2location i = n/2
[0] [1] [2] [3] [4] [5] [6] [7][0] [1] [2] [3] [4] [5] [6] [7]
[0] [1] [2] [3] [0] [1] [2][0] [1] [2] [3] [0] [1] [2]location i = n/2location i = n/2 if (a[i] is target)if (a[i] is target)
done!done!
[0] [1] [2] [3] [0] [1] [2][0] [1] [2] [3] [0] [1] [2]
DONEDONE else if (target <a[i]) else if (target <a[i]) go to the first halfgo to the first half
else if (target >a[i])else if (target >a[i])
DONEDONE
recursive calls with parameters:recursive calls with parameters:
@ @ George Wolberg, 2016George Wolberg, 2016 1414
else if (target a[i])else if (target a[i]) go to the second halfgo to the second half array, start, size, targetarray, start, size, target
found, location // referencefound, location // reference
Binary Search in an Integer ArrayBinary Search in an Integer ArrayBinary Search in an Integer ArrayBinary Search in an Integer Arrayif target is not in the arrayif target is not in the array
Items are sortedItems are sorted target = target = 1717 n = 8n = 8
22 33 66 77 1010 1212 1616 1818
[0] [1] [2] [3] [4] [5] [6] [7][0] [1] [2] [3] [4] [5] [6] [7] n 8n 8
Go to the middle Go to the middle location i = n/2location i = n/2
[0] [1] [2] [3] [4] [5] [6] [7][0] [1] [2] [3] [4] [5] [6] [7]
[0] [1] [2] [3] [0] [1] [2][0] [1] [2] [3] [0] [1] [2]location i = n/2location i = n/2 if (a[i] is target)if (a[i] is target)
done!done!
[0] [1] [2] [3] [0] [1] [2][0] [1] [2] [3] [0] [1] [2]
else if (target <a[i]) else if (target <a[i]) go to the first halfgo to the first half
else if (target >a[i])else if (target >a[i])
@ @ George Wolberg, 2016George Wolberg, 2016 1515
else if (target a[i])else if (target a[i]) go to the second halfgo to the second half
Binary Search in an Integer ArrayBinary Search in an Integer ArrayBinary Search in an Integer ArrayBinary Search in an Integer Arrayif target is not in the arrayif target is not in the array
Items are sortedItems are sorted target = target = 1717 n = 8n = 8
22 33 66 77 1010 1212 1616 1818
[0] [1] [2] [3] [4] [5] [6] [7][0] [1] [2] [3] [4] [5] [6] [7] n 8n 8
Go to the middle Go to the middle location i = n/2location i = n/2
[0] [1] [2] [3] [4] [5] [6] [7][0] [1] [2] [3] [4] [5] [6] [7]
[0] [1] [2] [3] [0] [1] [2][0] [1] [2] [3] [0] [1] [2]location i = n/2location i = n/2 if (a[i] is target)if (a[i] is target)
done!done!
[0] [1] [2] [3] [0] [1] [2][0] [1] [2] [3] [0] [1] [2]
[0] [0][0] [0] else if (target <a[i]) else if (target <a[i])
go to the first halfgo to the first half else if (target >a[i])else if (target >a[i])
[ ] [ ][ ] [ ]
@ @ George Wolberg, 2016George Wolberg, 2016 1616
else if (target a[i])else if (target a[i]) go to the second halfgo to the second half
Binary Search in an Integer ArrayBinary Search in an Integer ArrayBinary Search in an Integer ArrayBinary Search in an Integer Arrayif target is not in the arrayif target is not in the array
Items are sortedItems are sorted target = target = 1717 n = 8n = 8
22 33 66 77 1010 1212 1616 1818
[0] [1] [2] [3] [4] [5] [6] [7][0] [1] [2] [3] [4] [5] [6] [7] n 8n 8
Go to the middle Go to the middle location i = n/2location i = n/2
[0] [1] [2] [3] [4] [5] [6] [7][0] [1] [2] [3] [4] [5] [6] [7]
[0] [1] [2] [3] [0] [1] [2][0] [1] [2] [3] [0] [1] [2]location i = n/2location i = n/2 if (a[i] is target)if (a[i] is target)
done!done!
[0] [1] [2] [3] [0] [1] [2][0] [1] [2] [3] [0] [1] [2]
[0] [0][0] [0] else if (target <a[i]) else if (target <a[i])
go to the first halfgo to the first half else if (target >a[i])else if (target >a[i])
[ ] [ ][ ] [ ]
@ @ George Wolberg, 2016George Wolberg, 2016 1717
else if (target a[i])else if (target a[i]) go to the second halfgo to the second half
Binary Search in an Integer ArrayBinary Search in an Integer ArrayBinary Search in an Integer ArrayBinary Search in an Integer Arrayif target is not in the arrayif target is not in the array
Items are sortedItems are sorted target = target = 1717 n = 8n = 8
22 33 66 77 1010 1212 1616 1818
[0] [1] [2] [3] [4] [5] [6] [7][0] [1] [2] [3] [4] [5] [6] [7] n 8n 8
Go to the middle Go to the middle location i = n/2location i = n/2
[0] [1] [2] [3] [4] [5] [6] [7][0] [1] [2] [3] [4] [5] [6] [7]
[0] [1] [2] [3] [0] [1] [2][0] [1] [2] [3] [0] [1] [2]location i = n/2location i = n/2 if (a[i] is target)if (a[i] is target)
done!done!
[0] [1] [2] [3] [0] [1] [2][0] [1] [2] [3] [0] [1] [2]
[0] [0][0] [0] else if (target <a[i]) else if (target <a[i])
go to the first halfgo to the first half else if (target >a[i])else if (target >a[i])
[ ] [ ][ ] [ ]
the size of the first half is 0!the size of the first half is 0!
@ @ George Wolberg, 2016George Wolberg, 2016 1818
else if (target a[i])else if (target a[i]) go to the second halfgo to the second half
the size of the first half is 0!the size of the first half is 0!
Binary Search in an Integer ArrayBinary Search in an Integer ArrayBinary Search in an Integer ArrayBinary Search in an Integer Arrayif target is not in the arrayif target is not in the array
target = target = 1717
If (n == 0 )If (n == 0 )
22 33 66 77 1010 1212 1616 1818
[0] [1] [2] [3] [4] [5] [6] [7][0] [1] [2] [3] [4] [5] [6] [7] If (n 0 )If (n 0 ) not found!not found!
Go to the middle Go to the middle location i = n/2location i = n/2
[0] [1] [2] [3] [4] [5] [6] [7][0] [1] [2] [3] [4] [5] [6] [7]
[0] [1] [2] [3] [0] [1] [2][0] [1] [2] [3] [0] [1] [2]location i = n/2location i = n/2 if (a[i] is target)if (a[i] is target)
done!done!
[0] [1] [2] [3] [0] [1] [2][0] [1] [2] [3] [0] [1] [2]
[0] [0][0] [0] else if (target <a[i]) else if (target <a[i])
go to the first halfgo to the first half else if (target >a[i])else if (target >a[i])
[ ] [ ][ ] [ ]
the size of the first half is 0!the size of the first half is 0!
@ @ George Wolberg, 2016George Wolberg, 2016 1919
else if (target >a[i])else if (target >a[i]) go to the second halfgo to the second half
the size of the first half is 0!the size of the first half is 0!
Binary Search CodeBinary Search CodeBinary Search CodeBinary Search Codevoid search (const int a[ ], size_t first, size_t size,
int target,bool& found, size_t& location)
6 parameters6 parameters 2 stopping 2 stopping
{size_t middle;
if (size == 0) // stopping case if not foundcasescases
2 recursive 2 recursive call casescall cases
found = false;else{
middle = first + size/2;call casescall cases ;if (target == a[middle]) // stopping case if found{
location = middle;found = true;found = true;
}else if (target < a[middle]) // search the first half
search(a, first, size/2, target, found, location);
@ @ George Wolberg, 2016George Wolberg, 2016 2020
else //search the second halfsearch(a, middle+1, (size-1)/2, target, found, location);
} }
Binary Search - AnalysisBinary Search - AnalysisBinary Search - AnalysisBinary Search - Analysisvoid search (const int a[ ], size_t first, size_t size,
int target,bool& found, size_t& location)
Analysis of Analysis of recursive recursive algorithmsalgorithms
{size_t middle;
if (size == 0) // stopping case if not foundalgorithmsalgorithms Analyze the Analyze the
worstworst--casecase A i thA i th
found = false;else{
middle = first + size/2; Assuming the Assuming the
target is in the target is in the array array
;if (target == a[middle]) // stopping case if found{
location = middle;found = true;
and we and we always go to always go to the second the second
found = true;}else if (target < a[middle]) // search the first half
search(a, first, size/2, target, found, location);
@ @ George Wolberg, 2016George Wolberg, 2016 2121
halfhalf else //search the second halfsearch(a, middle+1, (size-1)/2, target, found, location);
} }
Binary Search - AnalysisBinary Search - AnalysisBinary Search - AnalysisBinary Search - Analysisvoid search (const int a[ ], size_t first, size_t size,
int target,bool& found, size_t& location)
Analysis of Analysis of recursive recursive algorithmsalgorithms
{size_t middle;
if (size == 0) // 1 operation
Define T(n) is Define T(n) is the total the total operationsoperations
found = false;else{
middle = first + size/2; // 1 operationoperations operations when size=nwhen size=n
T( ) 6+T( /2)T( ) 6+T( /2)
; pif (target == a[middle]) // 2 operations{
location = middle; // 1 operationfound = true; // 1 operationT(n) = 6+T(n/2)T(n) = 6+T(n/2)
T(1) = 6 T(1) = 6
found = true; // 1 operation}else if (target < a[middle]) // 2 operations
search(a, first, size/2, target, found, location);
@ @ George Wolberg, 2016George Wolberg, 2016 2222
( )( )else // T(n/2) operations for the recursive call
search(a, middle+1, (size-1)/2, target, found, location);} // ignore the operations in parameter passing
}
Binary Search - AnalysisBinary Search - AnalysisBinary Search - AnalysisBinary Search - Analysis
How many recursive calls for the longest chain?How many recursive calls for the longest chain?)(nT original calloriginal call
)2/(66
)2/(6
)(
2
1
nT
nT
nT original calloriginal call1st recursion, 1 six1st recursion, 1 six2nd recursion, 2 six2nd recursion, 2 six
)2/(6...66
...)2/(66
nT
nT
m
2nd recursion, 2 six2nd recursion, 2 six
mmth recursion, m sixth recursion, m six
)1(666...66
)2/(6...66
m
nT mmth recursion, m sixth recursion, m six
and n/2and n/2mm = 1 = 1 –– target foundtarget found
d th f th i lld th f th i ll
@ @ George Wolberg, 2016George Wolberg, 2016 2323
6log6 2 n depth of the recursive call depth of the recursive call m = logm = log22nn
Worst-Case Time for Binary SearchWorst-Case Time for Binary SearchWorst-Case Time for Binary SearchWorst-Case Time for Binary Search
For an array of n elements, the worstFor an array of n elements, the worst--case time for case time for binary search is logarithmicbinary search is logarithmic We have given a rigorous proofWe have given a rigorous proof The binary search algorithm is very efficientThe binary search algorithm is very efficient
What is the average running time?What is the average running time?Th i ti f t ll fi di bTh i ti f t ll fi di b The average running time for actually finding a number The average running time for actually finding a number is O(log n) is O(log n)
Can we do a rigorous analysis????Can we do a rigorous analysis????
@ @ George Wolberg, 2016George Wolberg, 2016 2424
Can we do a rigorous analysis????Can we do a rigorous analysis????
SummarySummarySummarySummary
Most Common Search MethodsMost Common Search MethodsSerial Search Serial Search –– O(n)O(n)Binary Search Binary Search –– O (log n ) O (log n ) Search by Hashing (*) Search by Hashing (*) –– better averagebetter average--case case
performance ( next lecture)performance ( next lecture)RunRun--Time AnalysisTime AnalysisAverageAverage--time analysistime analysisTime analysis of recursive algorithmsTime analysis of recursive algorithms
@ @ George Wolberg, 2016George Wolberg, 2016 2525
HomeworkHomeworkHomeworkHomework
Review Chapters 10 & 11 (Trees), and Review Chapters 10 & 11 (Trees), and do the do the self_testself_test exercises exercises
Read Chapters 12 & 13, and Read Chapters 12 & 13, and do the do the self testself test exercises exercises __
Homework/Quiz (on Searching):Homework/Quiz (on Searching):SelfSelf--Test 12.7, p 590 (binary search reTest 12.7, p 590 (binary search re--coding)coding), p ( y, p ( y g)g)
@ @ George Wolberg, 2016George Wolberg, 2016 2626