20
Data Structures and Algorithms (CS210/ESO207/ESO211) Lecture 38 Hashing (Data structure for searching) Recursion 1

Lecture-38-CS210-2012.pptx

Embed Size (px)

Citation preview

Data Structures and Algorithms(CS210/ESO207/ESO211)

Lecture 38

• Hashing (Data structure for searching)• Recursion

1

Data structures for searching

in O(1) time

Problem Description

: {} called universe, , Aim: A data structure for a given set that can facilitate efficient searching.

A search query: Does ?Note: can be any element from .

Hash function, hash value, hash table

Hash function:is a mapping from to {} with the following characteristics.• () is computable in O(1) time in word RAM.Example: () = mod

Hash value:() is called hash value of for a given hash function , and .

Hash Table: An array of pointers storing .

012...........

01..

𝒉

Hash function, hash value, hash table

Hash function:is a mapping from to {} with the following characteristics.• () is computable in O(1) time in word RAM. Example: () = mod

Hash value: () is called hash value of for a given hash function , and .

Hash Table: An array of pointers storing .

012...........

01.....

𝒉

𝑻

𝑺

Hash function, hash value, hash table

Question: How to use (,) for searching an element ?Answer: ; Search element in the list [].

Time complexity for searching: O(length of the longest list in ).

012...........

01.....

𝒉

𝑻

𝑺

Efficiency of Hashing depends upon hash function

A hash function is good if it can evenly distributes .

Aim: To search for a good hash function for a given set .

Bad news: There can not be any hash function which is good for every .

Pigeon hole principle: If there are letters to be placed in less than pigeon holes, there must be at least one hole containing more than 1 letters. (very simple and yet very powerful principle)

Hash function, hash value, hash table

For every , there exists a subset of elements from which are hashed to same value under .So we can always construct a subset for which all elements have same hash value All elements of are present in a single list of the hah table associated with .

012...........

01..

𝒉

Hashing: Practice and theory

Practice:In real world applications, hashing works very well because is usually a random subset of .

Theory:Very efficient algorithms exists for finding almost perfect hash function for a given . • The algorithms use elementary knowledge of prime numbers.• The algorithms use simple randomization.• A typical form of the hash function: () = mod (We shall discuss such an algorithm in CS345.)

Recursion

Recursion

A very powerful way to solve various problems.

• Enumerating all permutations of a sequence.• Enumerating all subsets of a given set.• Enumerating all valid expression having n left and n right parentheses.• Enumerating all 0-1 strings of length n having no consecutive 0’s.

It is so inspiring to see that for each of the above problems, there is a 10-20 lines recursive program whereas it is too difficult, if not impossible, to write an iterative program which does the same.

Visualization of recursion

Question: How to implement recursion ?

Factorial(9){ … ... … ..9*Factorial(8) … …}

Factorial(8){ … … .. ..8*Factorial(7) … …}

Factorial(1){ … … .. ..1*Factorial(0) … …}

Factorial(0){ … … .. .. … return 1;}

Implementation of recursion

Question: What exactly is required to implement recursion ?

Answer: After the called procedure finishes, the execution should resume • at appropriate place.• In the same environment where it left.

Fun(n){ … ... … Fun(n-1) … …}

Fun(n-1){ … … .. .. … …}

Instruction number of the calling procedureThe values of local variables of the calling procedure just before the recursive call

Implementation of recursion

• A procedure that has been invoked but has not finished yet is called an active procedure.• At any moment of time, many procedures may be active. • If a procedure is recursive, then multiple instances of it may be active at a time. • For each active procedure, we need to keep its activation record.

• value of its local variables .• return address (where to return after

the execution of the procedure)

Implementation of recursion

Suppose the calls are made in the following order:

Procedure is called first. calls calls calls Note: , , , could be instances of same (recursive) procedure.-------------------When finishes, control returns to When finishes, control returns to When finishes, control returns to

RAM

Activation record of

Activation record of

Activation record of

Activation record of

Having seen the way activation records are created and used during recursion, what is the appropriate data structure to maintain a collection of activation

records ?

Stack

Implementation of recursion

• Recursion is implemented using stack (called call stack).

• Implementation of recursion requires extra space.

• There is an extra overhead of time invoking recursive calls.

Due to these reasons, a recursive algorithm is usually slower and takes more space than its iterative counterpart (if exists). So one should strive to eliminate recursion whenever possible.

Elimination of recursionmethod 1

• Simulating recursion by iteration (keeping an explicit stack)

Example: 8-queens problem• Most of the implementation (you may search WWW) are recursive.• We discussed an iterative implementation using an explicit stack.

Consequence: improvement in space and time, but only by a constant factor.

Elimination of recursionmethod 2

Using an insight into the specific problem and its recursive algorithm.

A simple example: • Computing factorial()A nontrivial example:• Inorder traversal of a BST

Inorder traversal of BST

Inorder(){ If left()<> NULL Inorder(left()); print(key()); If right()<> NULL Inorder(right())}Question: What is the extra space used by the above algorithm due to recursion ?Answer: O(height()). [This could be O() when is skewed ]

Question: How to perform Inorder traversal in O() time using O(1) extra space ?Answer: (Those whose current aim is preparation for the exam, may skip this question; others may think over this problem during bed time to get a sound sleep. A few hints are provided on the following slide.)

Inorder traversal of BST using

Hints (a sequence of right questions):Question: What is required to navigate the tree according to inorder traversal ?Answer: a mechanism to decide where to go from a given node.

Question: On how many different occasions will we visit a node during inorder traversal ?Answer: three (once from its parent, once from left child, once from its right child).

Observation: Once we know the exact (one of the three) occasions of visiting a node, we know which node we have to visit next.

Keep an additional parameter called state (capturing the occasion) while visiting a node. Design an algorithm involving transition from one state to another. Exploit the fact that the number of these states will be a constant.