presentation on b tress. heap trees.hashing

57
Presented to: Madam sobia kazmi Presented by:- kinza Arshad(05) Sayyida Tabinda kokab(08) Qandeel Saddique(18) Parveen Tabassum(33) Amna Aftab(34) Adin Ajaz(37)

Upload: bindiya-syed

Post on 13-Feb-2017

408 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: presentation on b tress. heap trees.hashing

Presented to: Madam sobia kazmi Presented by:- kinza Arshad(05) Sayyida Tabinda kokab(08) Qandeel Saddique(18) Parveen Tabassum(33) Amna Aftab(34) Adin Ajaz(37)

Page 2: presentation on b tress. heap trees.hashing

Presentation outlines

Merge sort B-trees Hashing Heap trees

Page 3: presentation on b tress. heap trees.hashing

Merge Sort

SORTING:-arranging array elements in ascending order

MERGE SORT:- A sorting technique to arrange array element in ascending order

sorting elements by merging elements

combo of sorting and merging

Page 4: presentation on b tress. heap trees.hashing

Merge Sort (cont,,,)

CONCEPT:

1. Divide the list in half

2. Merge sort the first half

3. Merge sort the second half

4. Merge both halves back together

Page 5: presentation on b tress. heap trees.hashing

Merge Sort (cont,,,)

Merge sort is based on the divide-and-conquer paradigm. To sort A[p .. r]:

1. Divide StepIf a given array A has zero or one element, simply return; it is already sorted. Otherwise, split A[p .. r] into two subarrays A[p .. q] and A[q + 1 .. r], each containing about half of the elements of A[p .. r]. That is, q is the halfway point of A[p .. r].2. Conquer StepConquer by sorting the two subarrays A[p .. q] and A[q + 1 .. r].

Page 6: presentation on b tress. heap trees.hashing

Merge Sort (cont,,,)

3. Combine StepCombine the elements back in A[p .. r] by merging the two sorted subarrays A[p .. q] and A[q + 1 .. r] into a sorted sequence. To accomplish this step, we will define a procedure MERGE (A, p, q, r).Note that the recursion bottoms out when the subarray has just one element, so that it is trivially sorted.

Page 7: presentation on b tress. heap trees.hashing

Merge sort ( cont,,,)

Merge sort (divide-and-conquer) Divide array into two halves.

A L G O R I T H M S

divideA L G O R I T H M S

Page 8: presentation on b tress. heap trees.hashing

Merge sort(cont,,,)

Mergesort (divide-and-conquer) Divide array into two halves. Recursively sort each half.

sort

A L G O R I T H M S

divideA L G O R I T H M S

A G L O R H I M S T

Page 9: presentation on b tress. heap trees.hashing

Merge sort(cont,,,)

Mergesort (divide-and-conquer) Divide array into two halves. Recursively sort each half. Merge two halves to make sorted whole.

merge

sort

A L G O R I T H M S

divideA L G O R I T H M S

A G L O R H I M S T

A G H I L M O R S T

Page 10: presentation on b tress. heap trees.hashing

auxiliary array

smallest smallest

A G L O R H I M S T

Merging Merge. Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done.

A

Page 11: presentation on b tress. heap trees.hashing

auxiliary array

smallest smallest

A G L O R H I M S T

A

Merging Merge. Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done.

G

Page 12: presentation on b tress. heap trees.hashing

auxiliary array

smallest smallest

A G L O R H I M S T

A G

Merging Merge. Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done.

H

Page 13: presentation on b tress. heap trees.hashing

auxiliary array

smallest smallest

A G L O R H I M S T

A G H

Merging Merge. Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done.

I

Page 14: presentation on b tress. heap trees.hashing

auxiliary array

smallest smallest

A G L O R H I M S T

A G H I

Merging Merge. Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done.

L

Page 15: presentation on b tress. heap trees.hashing

auxiliary array

smallest smallest

A G L O R H I M S T

A G H I L

Merging Merge. Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done.

M

Page 16: presentation on b tress. heap trees.hashing

auxiliary array

smallest smallest

A G L O R H I M S T

A G H I L M

Merging Merge. Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done.

O

Page 17: presentation on b tress. heap trees.hashing

auxiliary array

smallest smallest

A G L O R H I M S T

A G H I L M O

Merging Merge. Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done.

R

Page 18: presentation on b tress. heap trees.hashing

auxiliary array

first halfexhausted smallest

A G L O R H I M S T

A G H I L M O R

Merging Merge. Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done.

S

Page 19: presentation on b tress. heap trees.hashing

auxiliary array

first halfexhausted smallest

A G L O R H I M S T

A G H I L M O R S

Merging Merge. Keep track of smallest element in each sorted half. Insert smallest of two elements into auxiliary array. Repeat until done.

T

Page 20: presentation on b tress. heap trees.hashing

Algorithm Merge sortInput :- An array A[l…r] where l and r are the lower and upper

index of A.Output :- Array a[l…r] with all element arranged in ascending

order .Steps: 1. if(r<=l) then2. Return3. else

4. Mid=[(l+r)/2]5. MergeSort(A[l…mid])6. MergeSort(A[mid+1…r])7. Merge(A,L,mid,r)8. Endif9. Stop

Page 21: presentation on b tress. heap trees.hashing

Pros:

It is a stable sort, and there is no worst-case scenario. It is faster, the temp array holds the resulting array until both left and right

sides are merged into the temp array, then the temp array is appended over the input array.

It is used in tape drives to sort data - its good with parallel processing.

Cons: The memory requirement is doubled. Takes longer to merge because if the next element is in the right side then

all of the elements must be moved down.

Page 22: presentation on b tress. heap trees.hashing

b-tre

es

Page 23: presentation on b tress. heap trees.hashing

Definition of a B-treeA B-tree of order m is an m-way tree (i.e., a tree where

each node may have up to m children) in which: 1. the number of keys in each non-leaf node is one

less than the number of its children and these keys partition the keys in the children in the fashion of a search tree

2.all leaves are on the same level 3.all non-leaf nodes except the root have at least m / 2

children 5. a leaf node contains no more than m – 1 keys

The number m should always be odd

B-Trees 23

Page 24: presentation on b tress. heap trees.hashing

An example B-Tree

B-Trees 24

51 6242

6 12

26

55 607064 90

45

1 2 4 7 8 13 15 18 25

27 29 46 48 53

A B-tree of order 5 containing 26 items

Note that all the leaves are at the same levelNote that all the leaves are at the same level

Page 25: presentation on b tress. heap trees.hashing

Constructing a B-tree

Suppose we start with an empty B-tree and keys arrive in the following order:1 12 8 2 25 5 14 28 17 7 52 16 48 68 3 26 29 53 55 45

We want to construct a B-tree of order 5The first four items go into the root:

To put the fifth item in the root would violate condition 5

Therefore, when 25 arrives, pick the middle key to make a new root

B-Trees 25

1 2 8 12

Page 26: presentation on b tress. heap trees.hashing

Constructing a B-tree

B-Trees 26

Page 27: presentation on b tress. heap trees.hashing

Constructing a B-tree (cont,,,)

B-Trees27

1 2

8

12 25

6, 14, 28 get added to the leaf nodes:

1 2

8

12 146 25 28

Page 28: presentation on b tress. heap trees.hashing

Constructing a B-tree (cont,,,)

B-Trees

Adding 17 to the right leaf node would over-fill it, so we take the middle key, promote it (to the root) and split the leaf

8 17

12 14 25 281 2 6

7, 52, 16, 48 get added to the leaf nodes8 17

12 14 25 281 2 6 16 48 527

Page 29: presentation on b tress. heap trees.hashing

Constructing a B-tree (cont,,,)

B-Trees

Adding 68 causes us to split the right most leaf, promoting 48 to the root, and adding 3 causes us to split the left most leaf, promoting 3 to the root; 26, 29, 53, 55 then go into the leaves

3 8 17 48

52 53 55 6825 26 28 291 2 6 7 12 14 16

Adding 45 causes a split of 25 26 28 29

and promoting 28 to the root then causes the root to split

Page 30: presentation on b tress. heap trees.hashing

Constructing a B-tree (cont,,,)

B-Trees

17

3 8 28 48

1 2 6 7 12 14 16 52 53 55 6825 26 29 45

Page 31: presentation on b tress. heap trees.hashing

Inserting into a B-TreeAttempt to insert the new key into a leafIf this would result in that leaf becoming too big, split the

leaf into two, promoting the middle key to the leaf’s parent

If this would result in the parent becoming too big, split the parent into two, promoting the middle key

This strategy might have to be repeated all the way to the top

If necessary, the root is split in two and the middle key is promoted to a new root, making the tree one level higher

B-Trees

Page 32: presentation on b tress. heap trees.hashing

Insert Node (63)

B-Trees

Page 33: presentation on b tress. heap trees.hashing

After Insert 63

B-Trees

Page 34: presentation on b tress. heap trees.hashing

Insert Node (93)

B-Trees

Page 35: presentation on b tress. heap trees.hashing

After Insert 99

B-Trees

Page 36: presentation on b tress. heap trees.hashing

Removal from a B-tree

• During insertion, the key always goes into a leaf. There are three possible ways we can do this:

• 1 - If the key is already in a leaf node, and removing it doesn’t cause that leaf node to have too few keys, then simply remove the key to be deleted.

• 2 - If the key is not in a leaf then it is guaranteed (by the nature of a B-tree) that its predecessor or successor will be in a leaf -- in this case we can delete the key and promote the predecessor or successor key to the non-leaf deleted key’s position.

B-Trees

Page 37: presentation on b tress. heap trees.hashing

Removal from a B-tree (2)3: if one of them has more than the min.

number of keys then we can promote one of its keys to the parent and take the parent key into our lacking leaf

if neither of them has more than the min. number of keys then the lacking leaf and one of its neighbours can be combined with their shared parent (the opposite of promoting a key) and the new leaf will have the correct number of keys.

B-Trees

Page 38: presentation on b tress. heap trees.hashing

B-Tree Delete Operation

• After deletion node is at least half full. (inverse of insertion case 1)

Page 39: presentation on b tress. heap trees.hashing

B-Tree Delete Operation

Page 40: presentation on b tress. heap trees.hashing

B-Tree Delete OperationUnderflow occurs and the keys in the left & right . Merge the underflow node and a sibling

Page 41: presentation on b tress. heap trees.hashing

B-Tree Delete Operation

• Underflow occurs, height decreases after merging.

Page 42: presentation on b tress. heap trees.hashing

HASHING

Page 43: presentation on b tress. heap trees.hashing

Hashing

• Is a means used to order and access elements in a list quickly by using a function of the key value to identify its location in the list.

• The function of the key value is called a hash function.

Page 44: presentation on b tress. heap trees.hashing

Hashing

Idea: – Use a function h to compute the slot for each key

– Store the element in slot h(k)

• A hash function h transforms a key into an index in a hash table T[0…m-1]:

h : U → {0, 1, . . . , m - 1}

• We say that k hashes to slot h(k)

Page 45: presentation on b tress. heap trees.hashing

U(universe of keys)

K(actualkeys)

0

m - 1

h(k3)

h(k2) = h(k5)

h(k1)h(k4)

k1k4 k2

k5k3

h : U → {0, 1, . . . , m - 1}hash table size: m

Hashing

Page 46: presentation on b tress. heap trees.hashing

Advantages of Hashing

• Reduce the range of array indices handled:

m instead of |U| where m is the hash table size: T[0, …, m-1]

• Storage is reduced.

• simplicity

Page 47: presentation on b tress. heap trees.hashing

Properties of Good Hash Functions

• Good hash function properties(1) Easy to compute(2) Approximates a random function i.e., for every input, every output is equally likely.(3) Minimizes the chance that similar keys hash to the

same sloti.e., strings such as pt and pts should hash to different slot.

• We will discuss two methods:– Division method

– Multiplication method

Page 48: presentation on b tress. heap trees.hashing

The Division Method• Idea:

– Map a key k into one of the m slots by taking the remainder of k divided by m

h(k) = k mod m• Advantage:

– fast, requires only one operation

• Disadvantage: – Certain values of m are bad (i.e., collisions), e.g.,

• power of 2• non-prime numbers

Page 49: presentation on b tress. heap trees.hashing

The Multiplication Method

Idea:(1) Multiply key k by a constant A, where 0 < A < 1(2) Extract the fractional part of kA(3) Multiply the fractional part by m(4) Truncate the result

h(k) = = m (k A mod 1)

• Disadvantage: Slower than division method

• Advantage: Value of m is not critical

fractional part of kA = kA - kA. ., 12.3 12e g

Page 50: presentation on b tress. heap trees.hashing

Example – Multiplication Method

Suppose k=6, A=0.3, m=32

(1) k x A = 1.8

(2) fractional part:

(3) m x 0.8 = 32 x 0.8 = 25.6

(4)

1.8 1.8 0.8

25.6 25 h(6)=25

Page 51: presentation on b tress. heap trees.hashing

Collisions

U(universe of keys)

K(actualkeys)

0

m - 1

h(k3)

h(k2) = h(k5)

h(k1)h(k4)

k1k4 k2

k5k3

Collisions occur when h(ki)=h(kj), i≠j

Page 52: presentation on b tress. heap trees.hashing

• For a given set K of keys: – If |K| ≤ m, collisions may or may not happen,

depending on the hash function!

– If |K| > m, collisions will definitely happen • i.e., there must be at least two keys that have the same hash

value

• Avoiding collisions completely might not be easy.

Collisions

Page 53: presentation on b tress. heap trees.hashing

Handling Collisions

• We will discuss main methods:

(1) Chaining

Page 54: presentation on b tress. heap trees.hashing

Chaining• Idea:

– Put all elements that hash to the same slot into a linked list

– Slot j contains a pointer to the head of the list of all elements that hash to j

Page 55: presentation on b tress. heap trees.hashing

Chaining

• How to choose the size of the hash table m?– Small enough to avoid wasting space.

– Large enough to avoid many collisions and keep linked-lists short.

– Typically 1/5 or 1/10 of the total number of elements.

• Should we use sorted or unsorted linked lists?– Unsorted

• Insert is fast

• Can easily remove the most recently inserted elements

Page 56: presentation on b tress. heap trees.hashing

Double Hashing

(1) Use one hash function to determine the first slot.(2) Use a second hash function to determine the

increment for the probe sequence:h(k,i) = (h1(k) + i h2(k) ) mod m, i=0,1,...

• Initial probe: h1(k)

• Second probe is offset by h2(k) mod m, so on ...• Advantage: handles clustering better• Disadvantage: more time consuming• How many probe sequences can double hashing

generate?m2

Page 57: presentation on b tress. heap trees.hashing

Double Hashing: Example

h1(k) = k mod 13

h2(k) = 1+ (k mod 11)

h(k,i) = (h1(k) + i h2(k) ) mod 13• Insert key 14:

i=0: h(14,0) = h1(14) = 14 mod 13 = 1

i=1: h(14,1) = (h1(14) + h2(14)) mod 13

= (1 + 4) mod 13 = 5i=2: h(14,2) = (h1(14) + 2 h2(14)) mod 13

= (1 + 8) mod 13 = 9

0

9

4

23

1

5678

101112

14