IT 60101: Lecture #151 Foundation of Computing Systems Lecture 15 Searching Algorithms
IT 60101: Lecture #152 Searching Techniques Several searching techniques Linear search –Sequential search with array –Sequential search with linked list –Binary search –Interpolation –Hashing Nonlinear search –Tree search »Binary tree search: AVL, Red-black, Splay trees search –Graph search »DFS, BFS
IT 60101: Lecture #153 Linear Search with Array
IT 60101: Lecture #154 Complexity Analysis: Linear Search Case 1: The key matches with the first element T(n) = 1 Case 2: Key does not exist T(n) = n Case 3: The key is present at any location in the array
IT 60101: Lecture #155 Complexity Analysis: Summary CaseNumber of key comparisonsAsymptotic complexityRemark Case 1T(n) = 1T(n) = O(1)Best case Case 2T(n) = nT(n) = O(n)Worst case Case 3T(n) = O(n)Average case
IT 60101: Lecture #156 Linear Search with Linked List
IT 60101: Lecture #157 Complexity Analysis: Linear Search with Linked List CaseNumber of key comparisonsAsymptotic complexityRemark Case 1T(n) = 1T(n) = O(1)Best case Case 2T(n) = O(n)Average case Case 3T(n) = O(n)Worst case
IT 60101: Lecture #158 Binary Search
IT 60101: Lecture #159 Complexity Analysis of Binary Search
IT 60101: Lecture #1510 Complexity Analysis of Binary Search Let n be the total number of elements in the list under search and there exist an integer k such that For successful search: –If, then the binary search algorithm requires at least one comparison and at most k comparisons. For unsuccessful search: –If, then the binary search algorithm requires k comparisons. –If, then the binary search algorithm requires either k-1 or k number of comparisons.
IT 60101: Lecture #1511 Binary Search: Complexity Analysis Best case T(n) = 1 Worst case T(n) =
IT 60101: Lecture #1512 Binary Search: Complexity Analysis Average Case –Successful search –Unsuccessful search
IT 60101: Lecture #1513 Interpolation Search
IT 60101: Lecture #1514 Interpolation Search: Complexity Analysis Interpolation search Successful1 Unsuccessful Best caseWorst caseAverage case
IT 60101: Lecture #1515 Comparison: Successful Search
IT 60101: Lecture #1516 Comparison: Unsuccessful Search
IT 60101: Lecture #1517 Nonlinear Search Techniques
IT 60101: Lecture #1518 Nonlinear Search Techniques: Binary Search Tree Search Successful search Unsuccessful search Remark Minimum number of comparisons Maximum number of comparisons Minimum number of comparisons Maximum number of comparisons Case 11Best Case 2 12n-132n+1 Worst Case 3 1 Average
IT 60101: Lecture #1519 Graph Search : DFS 1.Push the starting vertex into the stack OPEN 2.While OPEN is not empty do 3.POP a vertex V 4.If V is not in VISIT 5.Visit the vertex V 6.Store V in VISIT 7.Push all the adjacent vertex of V onto OPEN 8.EndIf 9.EndWhile 10.Stop
IT 60101: Lecture #1520 DFS: Example
IT 60101: Lecture #1521 Graph Search: BFS
IT 60101: Lecture #1522 Complexity of Graph Search T(n) = n.e T(n) = n(n-1)
IT 60101: Lecture #1523 Hashing Techniques Address calculation search
IT 60101: Lecture #1524 Hashing Techniques Hashing Is a mappinf from key to its index (location)
IT 60101: Lecture #1525 Some Hash Functions Division Method H(k) = k (mod h)if indices starts from 0 H(k) = k (mod h) + 1if indices start from 1 Examples H(31) = 31(mod 13) = 5 H(31) = 31 (mod 13) + 1 = 6
IT 60101: Lecture #1526 Some Hash Functions Midsquare Method k: k2: H(k): Limitations Computationally expensive
IT 60101: Lecture #1527 Some Hash Functions Folding Method H(k) = k 1 + k k n Example k: Chopping: Pure folding: = 136 = 169 = 180 Fold shifting: = 190 = 133 = 234 Fold boundary: = 154 = 241 = 207
IT 60101: Lecture #1528 Collision Resolution Techniques Closed hashing –also called linear probing Open hashing –also called chaining.
IT 60101: Lecture #1529 Closed Hashing The search will continue until any one of the following cases occurs: The key value is found. Unoccupied (or empty) location is encountered. It reaches to the location where the search was started.
IT 60101: Lecture #1530 Closed Hashing Example Input: Hash function: H(k) = k mod 7 + 1
IT 60101: Lecture #1531 Closed Hashing Example Input: Hash function: H(k) = k mod 7 + 1
IT 60101: Lecture #1532 Drawback of Closed Hashing Clustering –key values are clustered in large groups and as a result sequential search becomes slower and slower. Following are some solutions known to avoid this situation: Random probing Double hashing or rehashing Quadratic probing
IT 60101: Lecture #1533 Open Hashing Also called separate chaining –This method uses a hash table as an array of pointers –Each pointer points a linked list hash table is an array of list headers.
IT 60101: Lecture #1534 Advantages of Open Hashing 1.Overflow situation never arises. Hash table maintains lists which can contain any number of key values. 2.Collision resolution can be achieved very efficiently if the lists maintain an ordering of keys, so that keys can be searched quickly. 3.Insertion and deletion become quick and easy task in open hashing. Deletion proceeds in exactly the same way as deletion of a node in single linked list. 4.Finally, open hashing is best suitable in applications where number of key values varies drastically as it uses dynamic storage management policy.
IT 60101: Lecture #1535 Analysis of Hashing S( ) = Average number of probes for a successful search. U( ) = Average number of probes for an unsuccessful search.
IT 60101: Lecture #1536 Analysis of Hashing Closed hashing with random probing Close hashing with open probing Open hashing
IT 60101: Lecture #1537 Analysis of Hashing
IT 60101: Lecture #1538