Brought to you by Max (ICQ:31252512 TEL:61337706) February 5, 2005 Advanced Data Structures Introduction.

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:
CMSC 341 Binary Heaps Priority Queues. 8/3/2007 UMBC CSMC 341 PQueue 2 Priority Queues Priority: some property of an object that allows it to be prioritized.
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.
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
CSE 332 Review Slides Tyler Robison Summer
Binary Heaps CSE 373 Data Structures Lecture 11. 2/5/03Binary Heaps - Lecture 112 Readings Reading ›Sections
BST Data Structure A BST node contains: A BST contains
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
Tirgul 7 Heaps & Priority Queues Reminder Examples Hash Tables Reminder Examples.
Dr. Andrew Wallace PhD BEng(hons) EurIng
1 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
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:
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
1 Search Trees - Motivation Assume you would like to store several (key, value) pairs in a data structure that would support the following operations efficiently.
CSCE 3110 Data Structures & Algorithm Analysis Binary Search Trees Reading: Chap. 4 (4.3) Weiss.
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
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.
9/17/20151 Chapter 12 - Heaps. 9/17/20152 Introduction ► Heaps are largely about priority queues. ► They are an alternative data structure to implementing.
Sorting with Heaps Observation: Removal of the largest item from a heap can be performed in O(log n) time Another observation: Nodes are removed in order.
Hashing Chapter 20. Hash Table A hash table is a data structure that allows fast find, insert, and delete operations (most of the time). The simplest.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
Advanced Data Structure Hackson Leung
data ordered along paths from root to leaf
Data Structures Week 8 Further Data Structures The story so far  Saw some fundamental operations as well as advanced operations on arrays, stacks, and.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
CSE 250 September 29 – October 3, A NNOUNCEMENTS Homework 4 due 10/5 Project 1 posted for 10/6 Exam 2 10/8 No classes meet 10/9 Project 1 due 10/26.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Data Structure II So Pak Yeung Outline Review  Array  Sorted Array  Linked List Binary Search Tree Heap Hash Table.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Data Structure II. Outline Heap Binary Search Tree Hash Table Binary Indexed Tree Segment Tree.
Hashing 1 Hashing. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
CSE373: Data Structures & Algorithms Lecture 6: Priority Queues Kevin Quinn Fall 2015.
Week 10 - Friday.  What did we talk about last time?  Graph representations  Adjacency matrix  Adjacency lists  Depth first search.
Advanced Data Structure By Kayman 21 Jan Outline Review of some data structures Array Linked List Sorted Array New stuff 3 of the most important.
FALL 2005CENG 213 Data Structures1 Priority Queues (Heaps) Reference: Chapter 7.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
CS 367 Introduction to Data Structures Lecture 8.
1 Chapter 6 Heapsort. 2 About this lecture Introduce Heap – Shape Property and Heap Property – Heap Operations Heapsort: Use Heap to Sort Fixing heap.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
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.
Priority Queues and Heaps Tom Przybylinski. Maps ● We have (key,value) pairs, called entries ● We want to store and find/remove arbitrary entries (random.
CSE373: Data Structures & Algorithms Priority Queues
CSC317 Selection problem q p r Randomized‐Select(A,p,r,i)
CSCE 3110 Data Structures & Algorithm Analysis
BCA-II Data Structure Using C
CSCE 3110 Data Structures & Algorithm Analysis
Hashing CSE 2011 Winter July 2018.
Programming Abstractions
Week 11 - Friday CS221.
Hashing Exercises.
Heap Sort Example Qamar Abbas.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Advanced Associative Structures
Heaps, Heapsort, and Priority Queues
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
CS Data Structure: Heaps.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
CS202 - Fundamental Structures of Computer Science II
Advanced Implementation of Tables
Priority Queues CSE 373 Data Structures.
Hashing.
Data Structures and Algorithm Analysis Priority Queues (Heaps)
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Presentation transcript:

Brought to you by Max (ICQ: TEL: ) February 5, 2005 Advanced Data Structures Introduction

Page 2 Outline Review of some data structures Array Linked List Sorted Array New stuff 3 of the most important data structures in OI (and your own programming) Binary Search Tree Heap (Priority Queue) Hash Table

Page 3 Review How to measure the merits of a data structure? Time complexity of common operations Function Find(T : DataType) : Element Function Find_Min() : Element Procedure Add(T : DataType) Procedure Remove(E : Element) Procedure Remove_Min()

Page 4 Review - Array Here Element is simply the integer index of the array cell Find(T) Must scan the whole array, O(N) Find_Min() Also need to scan the whole array, O(N) Add(T) Simply add it to the end of the array, O(1) Remove(E) Deleting an element creates a hole Copy the last element to fill the hole, O(1) Remove_Min() Need to Find_Min() then Remove(), O(N)

Page 5 Review - Linked List Element is a pointer to the object Find(T) Scan the whole list, O(N) Find_Min() Scan the whole list, O(N) Add(T) Just add it to a convenient position (e.g. head), O(1) Remove(E) With suitable implementation, O(1) Remove_Min() Need to Find_Min() then Remove(), O(N)

Page 6 Review - Sorted Array Like array, Element is the integer index of the cell Find(T) We can use binary search, O(logN) Find_Min() The first element must be the minimum, O(1) Add(T) First we need to find the correct place, O(logN) Then we need to shift the array by 1 cell, O(N) Remove(E) Deleting an element creates a hole Need to shift the of array by 1 cell, O(N) Remove_Min() Can be O(1) or O(N) depending on choice of implementation

Page 7 Review - Summary If we are going to perform a lot of these operations (e.g. N=100000), none of these is fast enough! ArrayLinked ListSorted Array FindO(N) O(logN) Find_MinO(N) O(1) AddO(1) O(N) RemoveO(1) O(N) Remove_MinO(N) O(1) or O(N)

Brought to you by Max (ICQ: TEL: ) February 5, 2005 Advanced Data Structures Binary Search Tree

Page 9 What is a Binary Search Tree? Use a binary tree to store the data Maintain this property Left Subtree < Node < Right Subtree

Page 10 Binary Search Tree - Implementation Definition of a Node: Node = Record Left, Right : ^Node; Value : Integer; End; To search for a value (pseudocode) Node Find(Node N, Value V) :- If (N.Value = V) Return N; Else If (V < N.Value) and (V.Left != NULL) Return Find(N.Left); Else If (V > N.Value) and (V.Right != NULL) Return Find(N.Right); Else Return NULL; // not found

Page 11 Binary Search Tree - Find

Page 12 Binary Search Tree - Remove Case I : Removing a leaf node Easy Case II : Removing a node with a single child Replace the removed node with its child Case III : Removing a node with 2 children Replace the removed node with the minimum element in the right subtree (or maximum element in the left subtree) This may create a hole again Apply Case I or II Sometimes you can avoid this by using “Lazy Deletion” Mark a node as removed instead of actually removing it Less coding, performance hit not big if you are not doing this frequently (may even save time)

Page 13 Binary Search Tree - Remove

Page 14 Binary Search Tree - Summary Add() is similar to Find() Find_Min() Just walk to the left, easy Remove_Min() Equivalent to Find_Min() then Remove() Summary Find() : O(logN) Find_Min() : O(logN) Remove_Min() : O(logN) Add() : O(logN) Remove() : O(logN) The BST is “supposed” to behave like that

Page 15 Binary Search Tree - Problems In reality… All these operations are O(logN) only if the tree is balanced Inserting a sorted sequence degenerates into a linked list The real upper bounds Find() : O(N) Find_Min() : O(N) Remove_Min() : O(N) Add() : O(N) Remove() : O(N) Solution AVL Tree, Red Black Tree Use “rotations” to maintain balance Both are difficult to implement, rarely used

Brought to you by Max (ICQ: TEL: ) February 5, 2005 Advanced Data Structures Heap (Priority Queue)

Page 17 What is a Heap? A (usually) complete binary tree for Priority Queue Enqueue = Add Dequeue = Find_Min and Remove_Min Heap Property Every node’s value is greater than those of its decendants

Page 18 Heap - Implementation Usually we use an array to simulate a heap Assume nodes are indexed 1, 2, 3,... Parent = [Node / 2] Left Child = Node*2 Right Child = Node*2 + 1

Page 19 Heap - Add Append the new element at the end Shift it up until the heap property is restored Why always works?

Page 20 Heap - Remove_Min Replace the root with the last element Shift it down until the heap property is restored Again, why it always works?

Page 21 Heap - Build_Heap There is a special operation called Build_Heap Transform an ordinary into a heap without using extra memory The Remove_Min operation has two steps Replace the root with a leaf node Restore the heap structure by shifting the node down This is called “Heapify” If we apply the Heapify step to ALL internal nodes, bottom to up, we get a heap

Page 22 Heap - Build_Heap

Page 23 Heap - Summary Find() is usually not supported by a heap You may scan the whole tree / array if you really want Remove() is equivalent to applying Remove_Min() on a subtree Remember that any subtree of a heap is also a heap Summary Find() : O(N)// We usually don’t use Heap for this Find_Min() : O(1) Remove_Min() : O(logN) Add() : O(logN) Remove() : O(logN)

Brought to you by Max (ICQ: TEL: ) February 5, 2005 Advanced Data Structures Hash Table

Page 25 What is a Hash Table? Question We have a Mark Six result (6 integers in the range 1..49) We want to check if our bet matches it What is the most efficient way? Answer Use a boolean array with 49 cells Checking a number is O(1) Problem What if the range of number is very large? What if we need to store strings? Solution Use a “Hash Function” to compress the range of values

Page 26 Hash Table Suppose we need to store values between 0 and 99, but only have an array with 10 cells We can map the values [0,99] to [0,9] by taking modulo 10. The result is the “Hash Value” Adding, finding and removing an element are O(1) It is even possible to map the strings to integers, e.g. “ATE” to (1*26*26+20*26+5) mod 10

Page 27 Hash Table - Collision But this approach has an inherent problem What happens if two data has the same hash value? Two major methods to deal with this Chaining (Also called Open Hashing) Open Addressing (Also called Closed Hashing)

Page 28 Hash Table - Chaining Keep a link list at each hash table cell On average, Add / Find / Remove is O(1+  )  = Load Factor = # of stored elements / # of cells If hash function is “random” enough, usually can get the average case

Page 29 Hash Table - Open Addressing If you don’t want to implement a linked list… An alternative is to skip a cell if it is occupied The following diagram illustrates “Linear Probing”

Page 30 Hash Table - Open Addressing Find() must continue until a blank cell is reached Remove() must use Lazy Deletion, otherwise further operations may fail

Page 31 Hash Table - Summary Find_Min() and Remove_Min() are usually not supported in a Hash Table You may scan the whole tree / array if you really want For Chaining Find() : O(1+  ) Add() : O(1+  Remove() : O(1+  ) For Open Adressing Find() : O(1 / 1-  ) Add() : O(1 / 1-  ) Remove() : O(ln(1/1-  )/  + 1/  ) Both are close to O(1) if  is kept small (< 50%)

Page 32 Miscellaneous Stuff Judge problems 1020 – Left Join 1021 – Inner Join 1019 – Addition II Past contest problems NOI2004 Day 1 – Cashier Any more? Good place to find related information - Wikipedia