Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 615: Design & Analysis of Algorithms Chapter 5: Searching Brassard & Bratley Chap.: Chapter 9 Page 226-228 & 291-327.

Similar presentations


Presentation on theme: "CS 615: Design & Analysis of Algorithms Chapter 5: Searching Brassard & Bratley Chap.: Chapter 9 Page 226-228 & 291-327."— Presentation transcript:

1 CS 615: Design & Analysis of Algorithms Chapter 5: Searching Brassard & Bratley Chap.: Chapter 9 Page 226-228 & 291-327

2 19 October 2015CS 615 Design & Analysis of Algorithms2 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.Searching (Brassard & Bratley, Chap. 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)

3 19 October 2015CS 615 Design & Analysis of Algorithms3 Lecture Content Binary Search Traversing Trees Depth-First Search Breadth-First Search Backtracking Branch-and-Bound The Minimax Principle

4 19 October 2015CS 615 Design & Analysis of Algorithms4 Binary Search Used to look up a word in a dictionary or a name in telephone directory Aim: Find a value in a sorted array Return the location of the value in array Algorithm: Compare the middle element of the array if it is equal to the number Return the index if the value is less than the middle element apply the same algorithm to the first part of the array if the value is greater than the middle element apply the same algorithm to the second half of the array execute the operation until the value is found return the index or there are no more elements to search return not found

5 19 October 2015CS 615 Design & Analysis of Algorithms5 PseudoCode for Binary Search function binsearch(T[1..n], n) if n=0 or x>T[n] then return n+1 else 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 i k=(i+j)/2 if x=T[k] then return k if x<T[k] then return binrec(T[i..k],x) else return binrec(T[k+1..j],x)

6 19 October 2015CS 615 Design & Analysis of Algorithms6 Example: Binary Search Search for 12: -5-208891213132631 i=1k=5j=10 i=6k=8j=10 i=6k=7j=8 12345678910

7 19 October 2015CS 615 Design & Analysis of Algorithms7 Traversing Trees Visiting each node of the tree once Explore tree from left to right: Preorder Visit the node itself first Then all nodes in the left sub-tree Finally all nodes in the right sub-tree Inorder Visit all nodes in the left sub-tree Then the node itself Finally all nodes in the right sub-tree Postorder Visit all nodes in the left sub-tree Then all nodes in the right sub-tree Finally the node itself There are three symmetric techniques to explore the tree from right to left A B D HI E JK C F LM G NO ABDHIEJKCFLMGNO Preorder Traversing HDIBJEKALFMCNGO Inorder Traversing HIDJKEBLMFNOGCA Postorder Traversing

8 19 October 2015CS 615 Design & Analysis of Algorithms8 Depth-First Search Go deep in one direction as much as possible Before looking for another possibility If the structure is A graph associate a spanning tree to the graph A tree a preorder searching Visited nodes must be marked Use a variable in the strucuture to mark if the node is visited 1 2 56 34 78 1 2 3 6 5 4 7 8 12563478

9 19 October 2015CS 615 Design & Analysis of Algorithms9 Breadth-First Search Visit all neighbours Before looking for the child nodes If the structure is A graph associate a spanning tree to the graph A tree Visit all child nodes Before searching their sub-child nodes Visited nodes must be marked Use a variable in the strucuture to mark if the node is visited 1 2 56 34 78 1 2 56 34 78 12345678

10 19 October 2015CS 615 Design & Analysis of Algorithms10 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 for Is found out that cannot exist in the branch Return back to previous step And continue the search 1 2 914 34 78 11151223 Search for 7 Backtrack

11 19 October 2015CS 615 Design & Analysis of Algorithms11 Branch-and-Bound A technique used for exploring an implicit directed graph Used to look for optimal solution of a problem At each node Calculate a bound to provide a solution If the bound shows that any such solution is worse than the solution found so far No need to go on exploring this graph Prune the branch Continue to search can be used with both depth-first or breadth-first search

12 19 October 2015CS 615 Design & Analysis of Algorithms12 The Minimax Principle Sometimes it is impossible to complete a search Due to large number of nodes Example: some games like chess The only solution is to be content with a partial solution Minimax: Is a heuristic Used to find a move possibly better than all other moves

13 19 October 2015CS 615 Design & Analysis of Algorithms13 Minimax Example 10 -310-8 -75-310-200-510-152016-814-300-8-9 5-3 10 201140-8 Player: Rule A max B A B min max min

14 19 October 2015CS 615 Design & Analysis of Algorithms14 End of Chapter 5 Searching


Download ppt "CS 615: Design & Analysis of Algorithms Chapter 5: Searching Brassard & Bratley Chap.: Chapter 9 Page 226-228 & 291-327."

Similar presentations


Ads by Google