Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Design and Analysis of Algorithms.

Similar presentations


Presentation on theme: "CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Design and Analysis of Algorithms."— Presentation transcript:

1 CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Design and Analysis of Algorithms Khawaja Mohiuddin Assistant Professor, Department of Computer Sciences Bahria University, Karachi Campus, Contact: khawaja.mohiuddin@bimcs.edu.pk Lecture # 7 – Searching

2 CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Searching 2 Topics To Cover  Linear Search  Binary Search  Interpolation Search  Traversal  Types of Traversal  In-order Traversal  pre-order Traversal  post-order Traversal  Breadth first search (BFS)  Depth first search (DFS)

3 CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Searching 3  Once we have a sorted list of items after applying the sorting algorithms, we can search for specific items relatively quickly in it.  Even though some programming libraries include searching tools, it is still important to understand the searching algorithms because sometimes you can do even better than the tools.  Following are some of the searching algorithms:  Linear Search  Also called “exhaustive search”  Simply loops through the items in the array, looking for the target item  Also works on unsorted lists  But can save time if the items are sorted. If the target value isn’t in the list, the algorithm can stop early if a greater value item is reached

4 CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Searching 4  Linear Search (contd.) // Find the target item's index in the sorted array. // If the item isn't in the array, return -1. Integer: LinearSearch(Data values[], Data target) For i = 0 To - 1 // See if this is the target. If (values[i] == target) Then Return i // See if we have passed where the target would be. If (values[i] > target) Then Return -1 Next i // If we get here, the target is not in the array. Return -1 End LinearSearch

5 CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Searching 5  Linear Search (contd.)  May need to loop through the entire array to conclude that an item isn’t there  So its worst-case behavior is O(N)  Even in the average case, the algorithm’s run time is O(N)  add up the number of steps required to search for every item in the array, you get 1 + 2 + 3 +... + N = N * (N + 1) / 2  Divide that total by N to get the average search time for all the N items, you get (N + 1) / 2, which is still O(N)  This algorithm is much slower than binary search or interpolation search  But, it has the advantage that it works on linked lists and unsorted lists

6 CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Searching 6  Binary Search  The algorithm keeps track of the largest and smallest indices  Initially those bounds (min and max) are 0 and the largest index in the array  The algorithm then calculates the index halfway between min and max (mid)  If target is less than value at mid, reset max to search the left half of the array  If target is greater than value at mid, reset min to search the right half of the array  If target equals value at mid, return the index at mid.

7 CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Searching 7  Binary Search (contd.) // Find the target item's index in the sorted array. // If the item isn't in the array, return -1. Integer: BinarySearch(Data values[], Data target) Integer: min = 0 Integer: max = - 1 While (min <= max) // Find the dividing item. Integer: mid = (min + max) / 2 // See if we need to search the left or right half. If (target < values[mid]) Then max = mid - 1 Else If (target > values[mid]) Then min = mid + 1 Else Return mid End While // If we get here, the target is not in the array. Return -1 End BinarySearch

8 CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Searching 8  Binary Search (contd.)  At each step, the algorithm divides in half the number of items that might contain the target  If the array contains N items, after O(log N) steps, the section of the array that might hold the target contains only one item  So the algorithm either finds the item or concludes that it isn’t in the array  That means the algorithm has O(log N) run time

9 CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Searching 9  Interpolation Search  Uses the value of the target item to guess where in the array it might lie  Achieves much faster search times  For example, if array contains 50 items with values between 1 and 100 and if the target value is 30, it lies about 30 percent of the way from the smallest to the largest value, so the item may be somewhere near index 15.  Depending on the distribution of the numbers in the array, this may not be exactly correct  But it should get you fairly close to the target item’s position.

10 CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Searching 10  Interpolation Search (contd.) Integer: InterpolationSearch(Data values[], Data target) Integer: min = 0 Integer: max = values.Length - 1 While (min <= max) // Find the dividing item. Integer: mid = min + (max - min) * (target - values[min]) / (values[max] - values[min]) If (values[mid] == target) Then Return mid End While Return -1 End InterpolationSearch

11 CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Searching 11  Interpolation Search (contd.)  Couple of problems:  The mid calculation can result in an overflow  A value of mid that is not between min and max  Can solve those problems by putting few conditional statements  The trickiest part of this algorithm is the statement that calculates mid  For example, if values[min] is 100, values[max] is 200, and target is 125, then: (target - values[min]) / (values[max] - values[min]) = (125 - 100) / (200 - 100) = 25 / 100 = 0.25  In the worst case, if the data is extremely unevenly distributed, and you’re looking for the worst possible target value, this algorithm has O(N) performance  If the distribution is reasonably uniform, the expected performance is O(log(log N)). (proving that is outside the scope of this course)

12 CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Traversal and Its Types 12  The search that requires visiting every node in a structure is called Traversal.  The purpose of visiting a node could be to perform any operation.  Many algorithms search a tree structure for a particular node.  Binary trees have following types of traversals: Depth-first Traversal and Breadth-first Traversal.  Depth-first Traversal (3 sub-types): 1. Pre-order Traversal 2. In-order Traversal 3. Post-order Traversal  Breadth-first Traversal

13 CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Traversal and Its Types 13  Pre-order Traversal  In a preorder traversal, the algorithm processes a node, and then its left child, and then its right child.  The full traversal order is D, B, A, C, E  i.e.: Visit D, Output D, Visit B, Output B, Visit A, Output A, Visit B, Visit C, Output C, Visit B, Visit D, Visit E, Output E, Visit D  The following pseudo-code shows a natural recursive implementation of this algorithm: TraversePreorder(BinaryNode: node) If (node.LeftChild != null) Then TraversePreorder(node.LeftChild) If (node.RightChild != null) Then TraversePreorder(node.RightChild) End TraversePreorder

14 CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Traversal and Its Types 14  In-order Traversal  In an in-order or symmetric traversal, the algorithm processes a node’s left child, the node, and then the node’s right child.  The full traversal order is A, B, C, D, E.  i.e.: Visit D, Visit B, Visit A, Output A, Visit B, Output B, Visit C, Output C, Visit B, Visit D, Output D, Visit E, Output E, Visit D TraverseInorder(BinaryNode: node) If (node.LeftChild != null) Then TraverseInorder(node.LeftChild) If (node.RightChild != null) Then TraverseInorder(node.RightChild) End TraverseInorder

15 CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Traversal and Its Types 15  Post-order Traversal  In a post-order traversal, the algorithm processes a node’s left child, and then its right child, and then the node.  The full traversal is A, C, B, E, D.  i.e.: Visit D, Visit B, Visit A, Output A, Visit B, Visit C, Output C, Visit B, Output B, Visit D, Visit E, Output E, Visit D, Output D TraversePostorder(BinaryNode: node) If (node.LeftChild != null) Then TraversePostorder(node.LeftChild) If (node.RightChild != null) Then TraversePostorder(node.RightChild) End TraversePostorder

16 CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Traversal and Its Types 16  Breadth-first Traversal  In a breadth-first traversal, the algorithm processes all the nodes at a given level of the tree in left-to-right order before processing the nodes at the next level.  The full traversal is D, B, E, A, C.  This algorithm does not naturally follow the structure of the tree as the previous traversal algorithms do.  The tree shown in Figure 10-5 has no child link from node E to node A, so it’s not obvious how the algorithm moves from node E to node A.  One solution is to add a node’s children to a queue and then process the queue later, after you’ve finished processing the parents’ level.

17 CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Traversal and Its Types 17  Breadth-first Traversal (contd.) TraverseBreadthFirst(BinaryNode: root) // Create a queue to hold children for later processing. Queue : children = New Queue () // Place the root node on the queue. children.Enqueue(root) // Process the queue until it is empty. While (children Is Not Empty) // Get the next node in the queue. BinaryNode: node = children.Dequeue() // Process the node. // Add the node's children to the queue. If (node.LeftChild != null) children.Enqueue(node.LeftChild) If (node.RightChild != null) children.Enqueue(node.RightChild) End While End TraverseDepthFirst

18 CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Traversal and Its Types 18  Traversal Run Times  The recursive algorithms for pre-order, in-order and post-order traversal travel down the tree to the leaf nodes.  Then, as the recursive calls unwind, they travel back up to the root.  After an algorithm visits a node and then returns to the node’s parent, the algorithm doesn’t visit that node again.  That means the algorithms visit each node once. So if a tree contains N nodes, they all have O(N) run time.  The breadth-first traversal algorithm processes nodes as they move through a queue.  Each node enters the queue once, so if the tree has N nodes, the algorithm takes O(N) time.  In the worst case, if the tree is a perfect binary tree, its bottom level holds roughly half the total number of nodes, so if the tree holds N nodes, the queue holds O(N ÷ 2) = O(N) nodes.


Download ppt "CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Design and Analysis of Algorithms."

Similar presentations


Ads by Google