cs 615: design & analysis of algorithms chapter 5: searching brassard & bratley chap.:...
TRANSCRIPT
CS 615: Design & Analysis of
Algorithms
Chapter 5: SearchingBrassard & Bratley Chap.:
Chapter 9Page 226-228 & 291-327
April 20, 2023 CS 615 Design & Analysis of Algorithms 2
Course Content
1. Introduction, Algorithmic Notation and Flowcharts (Brassard & Bratleyü, Chap. 3)
2. Efficiency of Algorithms (Brassard & Bratley, Chap. 2)
3. Basic Data Structures (Brassard & Bratley, Chap. 5)4. Sorting (Weiss, Chap. 7)
5.5. SearchingSearching ( (Brassard & BratleyBrassard & Bratley,, Chap Chap. 9). 9)6. Graph Algorithms (Weiss, Chap. 9)7. Randomized Algorithms (Weiss, Chap.: 10)8. String Searching (Sedgewick, Chap. 19)9. NP Completeness (Sedgewick, Chap. 40)
April 20, 2023 CS 615 Design & Analysis of Algorithms 3
Lecture Content
Binary SearchTraversing TreesDepth-First SearchBreadth-First SearchBacktrackingBranch-and-BoundThe Minimax Principle
April 20, 2023 CS 615 Design & Analysis of Algorithms 4
Binary SearchUsed to look up
a word in a dictionaryor a name in telephone directory
Aim: Find a value in a sorted arrayReturn the location of the value in array
Algorithm:Compare the middle element of the arrayif it is equal to the number
Return the index
if the value is less than the middle elementapply the same algorithm to the first part of the array
if the value is greater than the middle elementapply the same algorithm to the second half of the array
execute the operation untilthe value is found
return the index
or there are no more elements to searchreturn not found
April 20, 2023 CS 615 Design & Analysis of Algorithms 5
PseudoCode for Binary Searchfunction binsearch(T[1..n], n)
if n=0 or x>T[n] then return n+1else return binrec(T[1..n],x)
{Binary search for x in subarray T[i..j] with promise that T[i-1]<x<=T[j]}function binrec(T[i..j],x)
if i=j then return ik=(i+j)/2if x=T[k] then return kif x<T[k] then return binrec(T[i..k],x)
else return binrec(T[k+1..j],x)
April 20, 2023 CS 615 Design & Analysis of Algorithms 6
Example: Binary SearchSearch for 12:
-5 -2 0 8 8 9 12 13 26 31
i=1 k=5 j=10
i=6 k=8 j=10
i=6 k=7 j=8
1 2 3 4 5 6 7 8 9 10
April 20, 2023 CS 615 Design & Analysis of Algorithms 7
Traversing TreesVisiting each node of the tree onceExplore tree from left to right:
PreorderVisit the node itself firstThen all nodes in the left sub-treeFinally all nodes in the right sub-tree
InorderVisit all nodes in the left sub-treeThen the node itselfFinally all nodes in the right sub-tree
PostorderVisit all nodes in the left sub-treeThen all nodes in the right sub-treeFinally the node itself
There are three symmetric techniques
to explore the tree from right to left
A
B
D
H I
E
J K
C
F
L M
G
N O
A B D H I E J K C F L M G N O
Preorder Traversing
H D I B J E K A L F M C N G O
Inorder Traversing
H I D J K E B L M F N O G C A
Postorder Traversing
April 20, 2023 CS 615 Design & Analysis of Algorithms 8
Depth-First Search
Go deep in one direction as much as possible
Before looking for another possibility
If the structure isA graph
associate a spanning tree to the graphA tree
a preorder searching
Visited nodes must be markedUse a variable in the strucuture to mark if the node is visited
1
2
5 6
3 4
7 8
1
2
3
6
5
4
7
8
1 2 5 6 3 4 7 8
April 20, 2023 CS 615 Design & Analysis of Algorithms 9
Breadth-First Search
Visit all neighboursBefore looking for the child nodes
If the structure isA graph
associate a spanning tree to the graph
A treeVisit all child nodesBefore searching their sub-child nodes
Visited nodes must be markedUse a variable in the strucuture to mark if the node is visited
1
2
5 6
3 4
7 8
1
2
5 6
3 4
7 8
1 2 3 4 5 6 7 8
April 20, 2023 CS 615 Design & Analysis of Algorithms 10
Backtracking
When the graph is too large or infinitive
Depth and breadth-first techniques are infeasible It is better to return to a node
That may provide a chance to find the searched node
If the node searched forIs found out that cannot exist in the branchReturn back to previous stepAnd continue the search
1
2
9 14
3 4
7 8
11 15 12 23
Search for 7
Backtrack
Backtrack
Backtrack
Backtrack
April 20, 2023 CS 615 Design & Analysis of Algorithms 11
Branch-and-BoundA technique used for
exploring an implicit directed graph
Used to look for optimal solution of a problem
At each nodeCalculate a bound to provide a solutionIf the bound shows that
any such solution is worse than the solution found so far
No need to go on exploring this graphPrune the branchContinue to search
can be used with bothdepth-first or breadth-first search
April 20, 2023 CS 615 Design & Analysis of Algorithms 12
The Minimax PrincipleSometimes it is impossible to complete a search
Due to large number of nodesExample: some games like chessThe only solution is
to be content with a partial solution
Minimax:Is a heuristicUsed to find a move
possibly better than all other moves
April 20, 2023 CS 615 Design & Analysis of Algorithms 13
Minimax Example
10
-3 10 -8
-7 5 -3 10 -20 0 -5 10 -15 20 1 6 -8 14 -30 0 -8 -9
5 -3 10 10 20 1 14 0 -8
Player: Rule
A max
B
A
B
min
max
min