Cpt S 223 – Advanced Data Structures Course Review Midterm Exam # 2

Slides:



Advertisements
Similar presentations
COL 106 Shweta Agrawal and Amit Kumar
Advertisements

The Dictionary ADT Definition A dictionary is an ordered or unordered list of key-element pairs, where keys are used to locate elements in the list. Example:
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
B+-Trees (PART 1) What is a B+ tree? Why B+ trees? Searching a B+ tree
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 18: Hash Tables.
Hashing Techniques.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
B+-tree and Hashing.
CSE 326: Data Structures Lecture #11 B-Trees Alon Halevy Spring Quarter 2001.
Hashing Text Read Weiss, §5.1 – 5.5 Goal Perform inserts, deletes, and finds in constant average time Topics Hash table, hash function, collisions Collision.
Chapter 6: Priority Queues Priority Queues Binary Heaps Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
Course Review COMP171 Spring Hashing / Slide 2 Elementary Data Structures * Linked lists n Types: singular, doubly, circular n Operations: insert,
CSE 373 Data Structures Lecture 12
Priority Queues  Queues: first-in first-out in printer schedule  Disadvantage: short job, important job need to wait  Priority queue is a data structure.
Hashing COMP171 Fall Hashing 2 Hash table * Support the following operations n Find n Insert n Delete. (deletions may be unnecessary in some applications)
Quick Review of material covered Apr 8 B+-Tree Overview and some definitions –balanced tree –multi-level –reorganizes itself on insertion and deletion.
Unit 11a 1 Unit 11: Data Structures & Complexity H We discuss in this unit Graphs and trees Binary search trees Hashing functions Recursive sorting: quicksort,
B + -Trees (Part 1). Motivation AVL tree with N nodes is an excellent data structure for searching, indexing, etc. –The Big-Oh analysis shows most operations.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
Balanced Trees. Binary Search tree with a balance condition Why? For every node in the tree, the height of its left and right subtrees must differ by.
Cpt S 223 – Advanced Data Structures Hashing
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
CSE 326: Data Structures Binomial Queues Ben Lerner Summer 2007.
Fundamental Structures of Computer Science March 02, 2006 Ananda Guna Binomial Heaps.
1 Priority Queues (Heaps)  Sections 6.1 to The Priority Queue ADT  DeleteMin –log N time  Insert –log N time  Other operations –FindMin  Constant.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
IntroductionIntroduction  Definition of B-trees  Properties  Specialization  Examples  2-3 trees  Insertion of B-tree  Remove items from B-tree.
B-trees (Balanced Trees) A B-tree is a special kind of tree, similar to a binary tree. However, It is not a binary search tree. It is not a binary tree.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
PRIORITY QUEUES (HEAPS). Queues are a standard mechanism for ordering tasks on a first-come, first-served basis However, some tasks may be more important.
1 B Trees - Motivation Recall our discussion on AVL-trees –The maximum height of an AVL-tree with n-nodes is log 2 (n) since the branching factor (degree,
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Oct 29, 2001CSE 373, Autumn External Storage For large data sets, the computer will have to access the disk. Disk access can take 200,000 times longer.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Midterm.
1 Road Map Associative Container Impl. Unordered ACs Hashing Collision Resolution Collision Resolution Open Addressing Open Addressing Separate Chaining.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
1 Hash table. 2 Objective To learn: Hash function Linear probing Quadratic probing Chained hash table.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
Chapter 21 Binary Heap.
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
COSC 2007 Data Structures II Chapter 15 External Methods.
1 CSE 326: Data Structures: Hash Tables Lecture 12: Monday, Feb 3, 2003.
1 G64ADS Advanced Data Structures Hashing. 2 Overview oHashing oTechnique supporting insertion, deletion and search in average-case constant time oOperations.
Cpt S 223 – Advanced Data Structures Priority Queues
1 Heaps (Priority Queues) You are given a set of items A[1..N] We want to find only the smallest or largest (highest priority) item quickly. Examples:
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees (Including Balanced Trees) Priority.
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
Chapter 5: Hashing Part I - Hash Tables. Hashing  What is Hashing?  Direct Access Tables  Hash Tables 2.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Course Review Final.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
COSC 1030 Lecture 10 Hash Table. Topics Table Hash Concept Hash Function Resolve collision Complexity Analysis.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
CMSC 341 Binomial Queues and Fibonacci Heaps. Basic Heap Operations OpBinary Heap Leftist Heap Binomial Queue Fibonacci Heap insertO(lgN) deleteMinO(lgN)
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
Internal and External Sorting External Searching
Exam 3 Review Data structures covered: –Hashing and Extensible hashing –Priority queues and binary heaps –Skip lists –B-Tree –Disjoint sets For each of.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
1 Priority Queues (Heaps). 2 Priority Queues Many applications require that we process records with keys in order, but not necessarily in full sorted.
COMP261 Lecture 23 B Trees.
Hashing Exercises.
Data Structures for Shaping and Scheduling
Heaps & Multi-way Search Trees
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

Cpt S 223 – Advanced Data Structures Course Review Midterm Exam # 2 Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

Midterm Exam 2 When: Wednesday (04/04) 12:10 -1pm Where: In Class Cpt S 223 Midterm Exam 2 When: Wednesday (04/04) 12:10 -1pm Where: In Class Closed book, Closed notes Comprehensive Material for preparation: Lecture slides primarily covered after Midterm Exam # 1 B-Trees, Hashing and Priority Queues Questions might be indirectly related to previous topics Homework 4 and Programming assignment 3 (Hash Table) Weiss Textbook Washington State University

Course Overview B-Trees (Chapter 4.7) Hashing (Chapter 5) Insertion and Deletion Hashing (Chapter 5) Hash Function Separate Chaining Linear Probing, Quadratic probing, Double Hashing Priority Queues/Heaps (Chapter 6) Binary Heap Insert, deleteMin Leftist Heaps Binomial Queues (Insert, deleteMin, Merge)

B-Trees Review (Chapter 4) A B-tree (also called a B+ Tree) of order M is an M-ary tree with the following properties: Data items are stored at the leaves Non-leaf nodes store up to M-1 keys Key i represents the smallest key in subtree i + 1 Root node is either a leaf or has between 2 and M children Non-leaf nodes have between M / 2 and M children All leaves at same depth and have between L / 2 and L items Requiring nodes to be half full avoids degeneration into binary tree

B-Tree B-tree of order 5 (i.e. M = 5) Node has 2 to 4 keys and 3 to 5 children Leaves have 3 to 5 data elements

B-Tree: Insertion Case 1: Insert into a non-full leaf node E.g., insert 57 into the previous order 5 tree

B-Tree: Insertion (cont’d) Case 2: Insert into a full leaf node, but parent has room Split leaf and promote middle element to parent E.g., insert 55 into previous tree

B-Tree: Insertion (cont’d) Case 3: Insert into full leaf, parent has no room Split parent, promote parent’s middle element to grandparent Continue until non-full parent or split root E.g., insert 40 into previous tree

B-Tree: Deletion Case 1: Leaf node containing item not at minimum E.g., remove 16 from previous tree

B-Tree: Deletion (cont’d) Case 2: Leaf node containing item has minimum elements, neighbor not at minimum Adopt element from neighbor E.g., remove 6 from previous tree 10 8

B-Tree: Deletion (cont’d) Case 3: Leaf node containing item has minimum elements, neighbors have minimum elements Merge with neighbor and intermediate key If parent now below minimum, continue up the tree E.g., remove 99 from previous tree

B-Tree: Case 3 Deletion (cont’d)

Hashing Review (Chapter 5)

h(key) ==> hash table index Hash Function Mapping from key to array index is called a hash function Typically, many-to-one mapping Different keys map to different indices Distributes keys evenly over table Hash function h(key) = key mod TableSize with integer keys Add up character ASCII values (0-127) to produce integer keys Treat first 3 characters of string as base-27 integer Collision occurs when hash function maps two keys to same array index h(key) ==> hash table index

Collision Resolution by Chaining Hash table T is a vector of lists Only singly-linked lists needed if memory is tight Key k is stored in list at T[h(k)] E.g. TableSize = 10 h(k) = k mod 10 Insert first 10 perfect squares Insertion sequence = 0, 1, 4, 9, 16, 25, 36, 49, 64, 81

Collision Resolution by Chaining: Analysis Load factor  of a hash table T N = number of elements in T M = size of T  = N / M Average length of a chain is  Unsuccessful search O() Successful search O( / 2) Ideally, we want   1 (not a function of N) i.e., TableSize = number of elements you expect to store in the table Keep the TableSize prime to ensure a good distribution

Collision Resolution by Open Addressing Probe sequence Sequence of slots in hash table to search h0(x), h1(x), h2(x), … Needs to visit each slot exactly once Hash function hi(x) = (hash(x) + f(i)) mod TableSize f(0) = 0 ==> first try Three common collision resolution strategies Linear Probing: hi(x) = (hash(x) + f(i)) mod TableSize, f(i)=i Quadratic Probing: hi(x) = (hash(x) + f(i)) mod TableSize; f(i) = i2 Double Hashing: hi(x) = (hash(x) + f(i)) mod TableSize; f(i) = i * hash2(x)

Extendible Hashing Example Extendible hash table Contains N = 12 data elements First D = 2 bits of key used by root node keys 2D entries in directory Each leaf contains up to M = 4 data elements As determined by disk page size Each leaf stores number of common starting bits (dL)

Extendible Hashing Example (cont’d) After inserting 100100 Directory split and rewritten Leaves not involved in split now pointed to by two adjacent directory entries. These leaves are not accessed.

Priority Queues/Heaps (Chapter 6)

Structure Property of Binary Heap A binary heap is a complete binary tree Each level is completely filled Bottom level may be partially filled from left to right Height of a complete binary tree with N elements is log2 N

Binary Heap Example N = 10 Every level (except last) completely filled Array representation of the binary heap above:

Heap-Order Property of Binary Heap Heap-order property (for a “min binary heap”) For every node X, key(parent(X))  key(X) Except root node, which has no parent Thus, minimum key always at the root Alternatively, for a “max binary heap”, always keep the maximum key at the root insert and deleteMin must always maintain heap-order property

Heap Insert: Example 14 vs. 31 Insert 14: 14 vs. 21 14 vs. 13 Heap-order okay; structure okay Percolating up

Heap DeleteMin Minimum element is always at the root A hole is created at the root when minimum element is removed Heap decreases by one in number of elements (or size) Last element X should be moved somewhere in the heap If it can be placed in the hole, then we are done Push smaller of the hole’s children into the hole, thus pushing the hole down one level Repeat this step until X can be placed in the hole Percolate down until the heap-order property is not restored

Heap DeleteMin: Example Remove value at the root Move 31 (last element) to the root Is 31 > min(14, 16)? If yes, swap 31 with min(14, 16) If no, leave 31 in hole

Heap DeleteMin: Example (cont’d) Percolating down… Is 31 > min(19, 21)? If yes, swap 31 with min(19, 21) If no, leave 31 in hole Is 31 > min(65, 26)? If yes, swap 31 with min(65, 26) If no, leave 31 in hole

Heap DeleteMin: Example (cont’d) Percolating down… Heap-order property okay; Structure okay; Done.

Building a Heap What if all N elements are all available upfront to build the heap? To build a heap with N elements Default method takes O(N log N) time. New method will take O(N) time – i.e., optimal

Building a Heap (cont’d) Construct a heap from an initial set of N items Solution 1 Perform N successive inserts O(1) constant time on average and O(log N) worst-case O(N) average case, but O(N log2 N) worst-case Solution 2 Randomly populate initial heap with structure property Perform a percolate-down from each internal node (i.e., H[size/2] to H[1]) To take care of heap-order property Do practice the buildheap() example from the lecture notes with linear time O(N)

Binomial Heap A forest of heap-ordered binomial trees Structure property Heap-order property The structural property of each heap-ordered tree in binomial heap is different from binary heap The heap-order property applies to each binomial tree

Definition: A “Binomial Tree” Bk A binomial tree of height k is called Bk It has 2k nodes The number of nodes at depth d is the binomial co-efficient A binomial tree need not to be a binary tree is the form of the coefficients in binomial theorem Depth B3 # of nodes d = 0 d = 1 d = 2 d = 3

Binomial Heap with 31 Nodes We know: Binomial heap should be a forest of binomial trees Each binomial tree has power of 2 elements How many binomial trees do we need? n = 31 = 1 1 1 1 12 B4 B3 B2 B1 B0

Binomial Heap with 31 Nodes (cont’d) Bk == Bk-1 + Bk-1 B4 B2 B2 B3 B3 A binomial tree Bk consists of a root with children B0, B1, B2 ….Bk-1 How many trees are there in this forest in worst case if the # of nodes are N?

merge(H1, H2) Let n1 be the number of nodes in H1 Therefore, the new heap is going to have n1 + n2 nodes Logic: Merge trees of same height, starting from lowest height trees If only one tree of a given height, then just copy that May need carryover (just like adding two binary numbers)

merge(H1, H2): Example carryover + B0 B1 B2 13 14 26 16 ? B2

insert(x) in Binomial Heap Goal To insert a new element x into a binomial heap H Observation Element x can be viewed as a single element binomial heap insert(x) == merge(H, {x}) So, if we know how to do merge we will automatically figure out how to implement both insert() and deleteMin().

deleteMin() Example Find the binomial tree with smallest root say Bk and original priority queue let be H Remove Bk from the forest of trees in H, forming the new binomial queue H’ Remove the root of Bk creating binomial trees B0, B1, B2 ….Bk-1 which collectively forms H’’ Merge H’ and H’’ B0 B2 B3 B0’ B1’ B2’ For deleteMin(): After delete, how to adjust the heap? New heap: Merge {B0, B2} and {B0’, B1’, B2’}.

Cpt S 223 Good Luck ! Washington State University