Algorithms and complexity

Ace your homework & exams now with Quizwiz!

Rehashing

1. A way to avoid performance deterioration of hash function when load factor > 0.9 2. Creating a larger hash table, twice the size of the original one 3. Revisiting each key and 4. calculating their new hash address 5. Inserting them in the hash table cons: unpexted long delays pros: happen infrequently

Separate chaining

1. An item in the hash table is a linked list of keys with the same address. 2. Number of probes in -successful search = 1 + alpha/2 -unsuccessful search = alpha 3. Pros: -Deletion is easy -Good for dynamic programming environment when number of keys are hard to predict. - Sorting /ordering of chains is possible and records can be pulled up front. 4. Cons: Use extra memory

Where to perform the rotation?

1. Calculate the balance factor 2. Determine where balance factor is out of range? 3. Rebalance the lowest unbalanced subtree.

How to check if a tree is an AVL Tree?

1. Check if Binary? max 2 children per node? 2. Check if Binary Search tree? - left_subtree elements < root r - right_subtree elements > root r NOTICE: Completeness: filled from top to bottom, left to right is not a condition of AVL tree. 3. Calculate balance factor. - H on left - H on right -difference -1, 0, or 1 ?

Challenges of hashing

1. Collision handling 2. design of hash function

What are the properties of AVL tree?

1. Deletion is harder to implement than insertion. Both 0(logn) 2. In worst case, 45% more comparioson needed to be done than in a perfectly balanced BST 3. The balance of AVL tree gurantees depth of 0(logn), even for random data

Hashing Cons and pros

1. If need to traverse all item in sorted order, hash table is no good 2. Unless use SEPARATE chaining, Deletion is virtually impossible 3. Hard to predict the volume of the data. Rehashing is a stop the world operation pros: 1. Faster data retrieval for thousands and millions of keys

What is rotation in context of AVL tree?

1. Local transformation of a binary search tree to regain balance. 2. Rotation means we are changing the direction where the nodes are pointing to.

How to hash strings?

1. Say, you have chs that have distinct coding 2. Take each ch of your string and convert it's code to 5 digit binary number. 3. Place the binary number side by side. 4. Take this large binary collection and convert it to decimal number by following the formula: sum of all (ch coding * 32 **position of ch when counted from right to left) 5. Do a hash function on the resulting decimal number to get the address.

How many comparisons need to be made while Searching with Presorting

1. Searching for 1 item -Bruteforce: 0(n). So, n comparison -Presorting and then Binary search: 0(nlogn) 2. searching for multiple item m -Bruteforce = mn number of comaprisons -Sorting + Binary search = nlogn + m * logn numbe rof comparisons

What are the methods of collision handling?

1. Separate chaining 2. Open addressing methods/ closed hashing - two types: - Linear probing - Double Hashing

What are the properties of an ideally designed hash function?

1. Should be easy to calculate when the keys are strings and integers. 2. should distribute the keys evenly into the hash table 3. Hash table size m should be large enough to allow efficient operation without taking too much memory.

How to build an AVL tree ?

1. To build an AVL tree pretend to look for the element 2. The search will take you to a fringe or leaf node. 3. Insert the element were you expect to find it. 4. Correct the balance factor by rotation: if unbalanced.

How to build a 2-3 tree by insertion?

1. To insert element k, pretend to look for it. 2. The search will take you to a leaf node. If the leaf node is a 2-node, insert k right away. 3. Else If we had a 3 node, along with the resident 2 elements, the element k will form a TEMPORARY node, in sorted order. Let's say they are: k1,k2,k3 4. Split: Split the temporary node - k1 and k3 will form their own 2-nodes -k2 will be PROMOTED as a parent node of k1 and k3 5. The promotion may make the parent node to OVERFLOW. In that case, the overflowing node will be split too. 6. Height changes, only when the root OVERFLOWS>

What are the operations supported by hash table?

1. insert 2. find 3. look up 4. Initialize: search and insert if not found 5. delete 6. Rehash

How to avoid performance deterioration of a hash function?

1. track load factor alpha 2. Rehash when alpha reaches 0.9

Open addressing by linear probing

1.Store the colliding keys to the next available empty cell 2. If end of table, wrap around. 3. How to search? ht.find() to find the original location, then do a linear search. 4. Number of probes in -successful search: 1/2 + 1/2(1-alpha) -unsuccessful search: 1/2 + 1/2(1-alpha)**2 5. Pros: -space efficient -worse case performance is miserable but still better than binary(logn) or linear search. 6. Cons -deletion is almost impossible -Clustering: clusters of contiguous cells being occupied

Robin karp string matching algorithm

1.To search for a string p of size m in a larger text S, determine hash(p), 2. and check hash value of each substring of S of size m 3. If hash.pattern == hash.substring > do a brute force string matching to confirm 4. Sliding window concept: To create the sliding window - remove the high order digit -add the low order digit 5. Useful for large string

Open addressing/closed hashing methods

1.When all keys are stored in the hash table itself . 2. Load factor alpha = n/m must be <= 1.

What are the different types of rotations? How to do that?

4 types: 1. Right- rotation when: TP, left C, left GC How : zero on C > put TP to it's right > put GC to C's left 2. Left- rotation when: TP, right C, right GC How: zero on right C > put TP to it's left > put GC to C's right 3. Left- Right rotation when: TP, left C, to C's right GC How: zero on GC > put it's parent C to it's left > put TP to GC's right 4. Right - Left rotation when : TP, right C, to C's left GC How: zero on GC > put it's parent C to it's right > put TP to GC's left

Binary Search Tree

A binary tree that satisfies the BST property: BST property: All elements of left subtree is smaller than ROOT r and all elements of right subtree are larger than ROOTr Purpose: Binary search tree helps to avoid sorting bc it is already sorted. INORDER (left- ROOT- right) traversal creates already sorted list

Transform and conquer

A problem solving technique. This approach uses: 1. Instance simplification: - some sort of preprocessing like sorting -- uniqueness checking -- Mode finding -- Median finding 2. Representational change: -Representing an array as binary search tree(BST) to facilitate 0(logn) no of comparison 3. Problem reduction

What are the ways to balance BST to facilitate faster search?

A) Instance simplification - AVL trees - Red-black Tree - Splay tree B) Representational change - B trees ( 2 nodes) - 2-3 trees (3 nodes) - 2-3-4 trees (4 nodes)

Algorithm to cheque for uniqueness A) brute force B) with presorting

Brute force: 0(n**2) for i <-- 0 to n-2 do for j <-- i + 1 to n-1 do if A[i] == A[j] return False return True With presorting: 0(nlogn) sort(A[0....n-1) #0(nlogn) for i <-- 0 to n-1 do #after sorting,rest 0(n) if A[i] == A[i+1] return False return True complexity : nlogn + n So, 0(nlogn)

Collision in hashing

Collision happens when multiple keys have same address in the hash table

Hash table

Has table is an abstract data structure like stacks and queues.

Balance factor AVL Tree

IN a BST, the height difference between the left-subtree and the right-subtree is called the balance factor. AVL Tree: An AVL tree is a BST which balance factor is between -1, 0, 1 FOR EVERY SUBTREE

Open addressing by Double hashing

In case of collision, Use another hash function to calculate an offset to be used to probe for an empty cell. new address = h(k) + s(k) if occupied, new address = h(k) + 2*s(k)

How to search for an elemnt in binary search tree?

Rem BST property: subTreeleft < root r subTreeright > root r 1. If root r = k then we have found our element 2. Otherwise search left or right depending on k < r or k > r 3. If reasonably balanced: search takes 0(logn) comparisons in worst case. 4. if unbalanced: as bad as lineear search. 0(n)

How to insert an item in Binary Search Tree? Build a BST: 8, 10, 5, 3, 13, 6

Rem BST property: subTreeleft < root r subTreeright > root r 1. pretend to search for the item 2. The search will take you to the fringe of the BST 3. Insert the element where you expect to find it

ALgorithm to find mode with presorting

SORT(A[0.....n-1]) i = 0 maxfreq = 0 while i < n: runlength = 1 while i+runlength< n andA[i+runlength]=A[i] runlength = runlength + 1 if runlength > maxfreq then maxfreq = runlength mode = A[i] i = i + runlength return mode after sorting the rest take liner time practice: 5 2 4 2

Hash function

The hash function determines the address of the key

what is hashing?

a standard way of implementing the abstract data type 'dictionary'

Hash address

h(k) is the hash address

Why hashing?

implemented well, it makes data retrieval very fast.

2-3 trees 2-3-4 trees

purpose: simple way to keep search tree balanced 1. 2-3 trees/ 3 node - a node holds 2 items and max 3 children -The items are ordered: m<n - far left child : < m - far right child: > n -middle child : between m and n 2. 2-3-4 tree/4-node -a node holds 3 items and max 4 children


Related study sets

Psychology Chapter 4: Nature, Nurture, and Human Diversity

View Set

MCB 100 Exam 2 (Lecture 11 start)

View Set

BUS491 - Chapter 8 Review Questions

View Set

Ch 4-6 Accounting Practice Problems

View Set