Presentation is loading. Please wait.

Presentation is loading. Please wait.

02.09.09IT 60101: Lecture #151 Foundation of Computing Systems Lecture 15 Searching Algorithms.

Similar presentations


Presentation on theme: "02.09.09IT 60101: Lecture #151 Foundation of Computing Systems Lecture 15 Searching Algorithms."— Presentation transcript:

1 02.09.09IT 60101: Lecture #151 Foundation of Computing Systems Lecture 15 Searching Algorithms

2 02.09.09IT 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

3 02.09.09IT 60101: Lecture #153 Linear Search with Array

4 02.09.09IT 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

5 02.09.09IT 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

6 02.09.09IT 60101: Lecture #156 Linear Search with Linked List

7 02.09.09IT 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

8 02.09.09IT 60101: Lecture #158 Binary Search

9 02.09.09IT 60101: Lecture #159 Complexity Analysis of Binary Search

10 02.09.09IT 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.

11 02.09.09IT 60101: Lecture #1511 Binary Search: Complexity Analysis Best case T(n) = 1 Worst case T(n) =

12 02.09.09IT 60101: Lecture #1512 Binary Search: Complexity Analysis Average Case –Successful search –Unsuccessful search

13 02.09.09IT 60101: Lecture #1513 Interpolation Search

14 02.09.09IT 60101: Lecture #1514 Interpolation Search: Complexity Analysis Interpolation search Successful1 Unsuccessful Best caseWorst caseAverage case

15 02.09.09IT 60101: Lecture #1515 Comparison: Successful Search

16 02.09.09IT 60101: Lecture #1516 Comparison: Unsuccessful Search

17 02.09.09IT 60101: Lecture #1517 Nonlinear Search Techniques

18 02.09.09IT 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

19 02.09.09IT 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

20 02.09.09IT 60101: Lecture #1520 DFS: Example

21 02.09.09IT 60101: Lecture #1521 Graph Search: BFS

22 02.09.09IT 60101: Lecture #1522 Complexity of Graph Search T(n) = n.e T(n) = n(n-1)

23 02.09.09IT 60101: Lecture #1523 Hashing Techniques Address calculation search

24 02.09.09IT 60101: Lecture #1524 Hashing Techniques Hashing Is a mappinf from key to its index (location)

25 02.09.09IT 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

26 02.09.09IT 60101: Lecture #1526 Some Hash Functions Midsquare Method k: 123423453456 k2:1522756 5499025 11943936 H(k):525492933 Limitations Computationally expensive

27 02.09.09IT 60101: Lecture #1527 Some Hash Functions Folding Method H(k) = k 1 + k 2 +... + k n Example k:1522756 549902511943936 Chopping:01 52 27 56 05 49 90 2511 94 39 36 Pure folding:01 + 52 + 27 + 56 05 + 49 + 90 + 25 11 + 94 + 39 + 36 = 136 = 169 = 180 Fold shifting: 10 + 52 + 72 + 56 50 + 49 + 09 + 25 11 + 94 + 93 + 36 = 190 = 133 = 234 Fold boundary:10 + 52 + 27 + 65 50 + 49 + 90 + 52 11 + 94 + 39 + 63 = 154 = 241 = 207

28 02.09.09IT 60101: Lecture #1528 Collision Resolution Techniques Closed hashing –also called linear probing Open hashing –also called chaining.

29 02.09.09IT 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.

30 02.09.09IT 60101: Lecture #1530 Closed Hashing Example Input: 15 11 25 16 9 8 12 8 Hash function: H(k) = k mod 7 + 1

31 02.09.09IT 60101: Lecture #1531 Closed Hashing Example Input: 15 11 25 16 9 8 12 Hash function: H(k) = k mod 7 + 1

32 02.09.09IT 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

33 02.09.09IT 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.

34 02.09.09IT 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.

35 02.09.09IT 60101: Lecture #1535 Analysis of Hashing S( ) = Average number of probes for a successful search. U( ) = Average number of probes for an unsuccessful search.

36 02.09.09IT 60101: Lecture #1536 Analysis of Hashing Closed hashing with random probing Close hashing with open probing Open hashing

37 02.09.09IT 60101: Lecture #1537 Analysis of Hashing

38 02.09.09IT 60101: Lecture #1538


Download ppt "02.09.09IT 60101: Lecture #151 Foundation of Computing Systems Lecture 15 Searching Algorithms."

Similar presentations


Ads by Google