Universal Hashing When attempting to foil an malicious adversary, randomize the algorithm Universal hashing: pick a hash function randomly when the algorithm.

Slides:



Advertisements
Similar presentations
David Luebke 1 6/1/2014 CS 332: Algorithms Medians and Order Statistics Structures for Dynamic Sets.
Advertisements

Chapter 12 Binary Search Trees
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Jan Binary Search Trees What is a search binary tree? Inorder search of a binary search tree Find Min & Max Predecessor and successor BST insertion.
Analysis of Algorithms CS 477/677 Binary Search Trees Instructor: George Bebis (Appendix B5.2, Chapter 12)
Binary Search Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
The complexity and correctness of algorithms (with binary trees as an example)
David Luebke 1 5/4/2015 Binary Search Trees. David Luebke 2 5/4/2015 Dynamic Sets ● Want a data structure for dynamic sets ■ Elements have a key and satellite.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Binary Search Trees Comp 550.
Binary Trees, Binary Search Trees CMPS 2133 Spring 2008.
Binary Trees, Binary Search Trees COMP171 Fall 2006.
Binary Search Trees CIS 606 Spring Search trees Data structures that support many dynamic-set operations. – Can be used as both a dictionary and.
BST Data Structure A BST node contains: A BST contains
Lecture 11: Binary Search Trees Shang-Hua Teng. Data Format KeysEntryKeysSatellite data.
Lec 15 April 9 Topics: l binary Trees l expression trees Binary Search Trees (Chapter 5 of text)
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Lecture 10: Search Structures and Hashing
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
CS 2133: Data Structures Binary Search Trees.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
Lecture 10 Trees –Definiton of trees –Uses of trees –Operations on a tree.
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Tree (new ADT) Terminology:  A tree is a collection of elements (nodes)  Each node may have 0 or more successors (called children)  How many does a.
Binary Trees, Binary Search Trees RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 4: Data Structures.
David Luebke 1 10/25/2015 CS 332: Algorithms Skip Lists Hash Tables.
1 Binary Trees Informal defn: each node has 0, 1, or 2 children Informal defn: each node has 0, 1, or 2 children Formal defn: a binary tree is a structure.
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
Outline Binary Trees Binary Search Tree Treaps. Binary Trees The empty set (null) is a binary tree A single node is a binary tree A node has a left child.
Binary Search Tree Qamar Abbas.
Preview  Graph  Tree Binary Tree Binary Search Tree Binary Search Tree Property Binary Search Tree functions  In-order walk  Pre-order walk  Post-order.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
Binary Search Trees (BST)
Lecture 19. Binary Search Tree 1. Recap Tree is a non linear data structure to present data in hierarchical form. It is also called acyclic data structure.
Mudasser Naseer 1 1/25/2016 CS 332: Algorithms Lecture # 10 Medians and Order Statistics Structures for Dynamic Sets.
CS6045: Advanced Algorithms Data Structures. Hashing Tables Motivation: symbol tables –A compiler uses a symbol table to relate symbols to associated.
CSE 2331/5331 Topic 8: Binary Search Tree Data structure Operations.
David Luebke 1 3/19/2016 CS 332: Algorithms Augmenting Data Structures.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
Many slides here are based on E. Demaine , D. Luebke slides
Binary Search Trees What is a binary search tree?
CS 3013 Binary Search Trees.
Binary Search Trees.
CS 332: Algorithms Hash Tables David Luebke /19/2018.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Dynamic Order Statistics
CS200: Algorithms Analysis
CS 3013 Binary Search Trees.
Lecture 7 Algorithm Analysis
Binary Trees, Binary Search Trees
Lecture 7 Algorithm Analysis
Chapter 12: Binary Search Trees
CS6045: Advanced Algorithms
Lecture 7 Algorithm Analysis
Binary Trees, Binary Search Trees
Binary SearchTrees [CLRS] – Chap 12.
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Binary Trees, Binary Search Trees
Presentation transcript:

Universal Hashing When attempting to foil an malicious adversary, randomize the algorithm Universal hashing: pick a hash function randomly when the algorithm begins –Guarantees good performance on average, no matter what keys adversary chooses –Need a family of hash functions to choose from –Think of quick-sort

Universal Hashing Let  be a (finite) collection of hash functions –…that map a given universe U of keys… –…into the range {0, 1, …, m - 1}.  is said to be universal if: –for each pair of distinct keys x, y  U, the number of hash functions h   for which h(x) = h(y) is |  |/m –In other words: With a random hash function from  the chance of a collision between x and y is exactly 1/m (x  y)

Universal Hashing Theorem 11.3: –Choose h from a universal family of hash functions –Hash n keys into a table of m slots, n  m –Then the expected number of collisions involving a particular key x is less than 1 –Proof: For each pair of keys y, z, let c yx = 1 if y and z collide, 0 otherwise E[c yz ] = 1/m (by definition) Let C x be total number of collisions involving key x Since n  m, we have E[C x ] < 1

A Universal Hash Function Choose table size m to be prime Decompose key x into r+1 bytes, so that x = {x 0, x 1, …, x r } –Only requirement is that max value of byte < m –Let a = {a 0, a 1, …, a r } denote a sequence of r+1 elements chosen randomly from {0, 1, …, m - 1} –Define corresponding hash function h a   : –With this definition,  has m r+1 members

A Universal Hash Function  is a universal collection of hash functions (Theorem 11.5) How to use: –Pick r based on m and the range of keys in U –Pick a hash function by (randomly) picking the a’s –Use that hash function on all keys

Example Let m = 5, and the size of each string is 2 bits (binary). Note the maximum value of a string is 3 and m = 5 a = 1,3, chosen at random from 0,1,2,3,4 Example for x = 4 = 01,00 (note r = 1) h a (4) = 1  (01) + 3  (00) = 1

Open Addressing Basic idea (details in Section 12.4): –To insert: if slot is full, try another slot, …, until an open slot is found (probing) –To search, follow same sequence of probes as would be used when inserting the element If reach element with correct key, return it If reach a NULL pointer, element is not in table Good for fixed sets (adding but no deletion) Table needn’t be much bigger than n

Lecture 11: Binary Search Trees Shang-Hua Teng

Data Format KeysEntryKeysSatellite data

Insertion and Deletion on dynamic sets Insert(S,x) –A modifying operation that augments the set S with the element pointed by x Delete –Given a pointer x to an element in the set S, removes x from S –Notice that this operation uses a pointer to an element x, not a key value

Querying on dynamic sets Search(S,k) –given a set S and a key value k, returns a pointer x to an element in S such that key[x] = k, or NIL if no such element belongs S Minimum(S) –on a totally ordered set S that returns a pointer to the element S with the smallest key Maximum(S) Successor(S,x) –Given an element x whose key is from a totally ordered set S, returns a pointer to the next larger element in S, or NIL if x is the maximum element Predecessor(S,x)

subtree Trees Node Edge parent root leaf interior node child path Degree? Depth/Level? Height?

Binary Tree Node –data –left child –right child –parent (optional) Parent Data Left Right Tree root

Trees Full tree of height h –all leaves present at level h –all interior nodes full –total number of nodes in a full binary tree? Complete tree of height h

Applications - Expression Trees * (5*3)+(8-4) To represent infix expressions

Applications - Parse Trees Used in compilers to check syntax statement ifcondthen statementelsestatement ifcondthen statement

Application - Game Trees

Binary Search Trees Binary Search Trees (BSTs) are an important data structure for dynamic sets In addition to satellite data, elements have: –key: an identifying field inducing a total ordering –left: pointer to a left child (may be NULL) –right: pointer to a right child (may be NULL) –p: pointer to a parent node (NULL for root)

Binary Search Trees BST property: key[leftSubtree(x)]  key[x]  key[rightSubtree(x)] Example: F BH KDA

Traversals for a Binary Tree Pre order –visit the node –go left –go right In order –go left –visit the node –go right Post order –go left –go right –visit the node Level order / breadth first –for d = 0 to height visit nodes at level d

Traversal Examples A B D G C E H F I Pre order A B D G H C E F I In order G D H B A E C F I Post order G H D B E I F C A Level order A B C D E F G H I

Traversal Implementation recursive implementation of preorder –base case? –self reference visit node pre-order(left child) pre-order(right child) What changes need to be made for in-order, post-order?

Inorder Tree Walk In order TreeWalk(x) TreeWalk(left[x]); print(x); TreeWalk(right[x]); Prints elements in sorted (increasing) order

In order Tree Walk Example: How long will a tree walk take? In order walk prints in monotonically increasing order. Why? F BH KDA

Evaluating an expression tree Walk the tree in postorder When visiting a node, use the results of its children to evaluate it *

Operations on BSTs: Search Given a key and a pointer to a node, returns an element with that key or NULL: TreeSearch(x, k) if (x = NULL or k = key[x]) return x; if (k < key[x]) return TreeSearch(left[x], k); else return TreeSearch(right[x], k);

BST Search: Example Search for D and C: F BH KDA

Operations of BSTs: Insert Adds an element x to the tree so that the binary search tree property continues to hold The basic algorithm –Like the search procedure above –Insert x in place of NULL –Use a “trailing pointer” to keep track of where you came from (like inserting into singly linked list)

BST Insert: Example Example: Insert C F BH KDA C

BST Search/Insert: Running Time What is the running time of TreeSearch() or TreeInsert()? – O(h), where h = height of tree What is the height of a binary search tree? –worst case: h = O(n) when tree is just a linear string of left or right children We’ll keep all analysis in terms of h for now Later we’ll see how to maintain h = O(lg n)

Sorting With Binary Search Trees Informal code for sorting array A of length n: BSTSort(A) for i=1 to n TreeInsert(A[i]); InorderTreeWalk(root); Argue that this is  (n lg n) What will be the running time in the –Worst case? –Average case? (hint: remind you of anything?)

Sorting With BSTs Average case analysis –It’s a form of quicksort! for i=1 to n TreeInsert(A[i]); InorderTreeWalk(root);

Sorting with BSTs Same partitions are done as with quicksort, but in a different order –In previous example Everything was compared to 3 once Then those items < 3 were compared to 1 once Etc. –Same comparisons as quicksort, different order!

Sorting with BSTs Since run time is proportional to the number of comparisons, same expected time as quicksort: O(n lg n) Which do you think is better, quicksort or BSTsort? Why?

Sorting with BSTs Since run time is proportional to the number of comparisons, same time as quicksort: O(n lg n) Which do you think is better, quicksort or BSTSort? Why? Answer: quicksort –Better constants –Sorts in place –Doesn’t need to build data structure

More BST Operations A priority queue supports –Insert –Minimum –Extract-Min BSTs are good for more than sorting. For example, can implement a priority queue

BST Operations: Minimum How can we implement a Minimum() query? What is the running time? – O(h)

BST Operations: Successor For deletion, we will need a Successor() operation What is the successor of node 3? Node 15? Node 13? What are the general rules for finding the successor of node x? (hint: two cases)

BST Operations: Successor Two cases: –x has a right subtree: successor is minimum node in right subtree –x has no right subtree: successor is first ancestor of x whose left child is also ancestor of x Intuition: As long as you move to the left up the tree, you’re visiting smaller nodes. Predecessor: similar algorithm

BST Operations: Delete Deletion is a bit tricky 3 cases: –x has no children: Remove x –x has one child: Splice out x –x has two children: Swap x with successor Perform case 1 or 2 to delete it F BH KDA C Example: delete K or H or B

BST Operations: Delete Why will case 2 always go to case 0 or case 1? –because when x has 2 children, its successor is the minimum in its right subtree Could we swap x with predecessor instead of successor? –Of course

Next Lecture Up next: guaranteeing an O(lg n) height tree

Animation Animated Binary Tree