i206: Lecture 14: Heaps, Graphs intro.

Slides:



Advertisements
Similar presentations
 Chapter 11 has several programming projects, including a project that uses heaps.  This presentation shows you what a heap is, and demonstrates two.
Advertisements

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.
Heapsort.
Data Structures Heaps and Graphs i206 Fall 2010 John Chuang Some slides adapted from Marti Hearst, Brian Hayes, or Glenn Brookshear.
Heapsort. 2 Why study Heapsort? It is a well-known, traditional sorting algorithm you will be expected to know Heapsort is always O(n log n) Quicksort.
CS 206 Introduction to Computer Science II 10 / 31 / 2008 Happy Halloween!!! Instructor: Michael Eckmann.
1 Foundations of Software Design Fall 2002 Marti Hearst Lecture 17: Binary Search Trees; Heaps.
Heapsort Based off slides by: David Matuszek
1 HEAPS & PRIORITY QUEUES Array and Tree implementations.
Heapsort CSC Why study Heapsort? It is a well-known, traditional sorting algorithm you will be expected to know Heapsort is always O(n log n)
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.
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
Heaps and Heapsort Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
INTRO TO HEAP & Adding and Removing a NODE Presented to: Sir AHSAN RAZA Presented by: SHAH RUKH Roll #07-22 Semester BIT 3 rd Session 2007—2011 Department.
CSC212 Data Structure Lecture 16 Heaps and Priority Queues Instructor: George Wolberg Department of Computer Science City College of New York.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
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.
Heaps © 2010 Goodrich, Tamassia. Heaps2 Priority Queue ADT  A priority queue (PQ) stores a collection of entries  Typically, an entry is a.
Sorting Cont. Quick Sort As the name implies quicksort is the fastest known sorting algorithm in practice. Quick-sort is a randomized sorting algorithm.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
"Teachers open the door, but you must enter by yourself. "
Top 50 Data Structures Interview Questions
Heaps, Heapsort, and Priority Queues
Priority Queues © 2010 Goodrich, Tamassia Priority Queues 1
Heaps 8/2/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Hashing Exercises.
Source: Muangsin / Weiss
Heaps © 2010 Goodrich, Tamassia Heaps Heaps
Bohyung Han CSE, POSTECH
Heap Sort Example Qamar Abbas.
Design and Analysis of Algorithms
Phil Tayco Slide version 1.0 May 7, 2018
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
7/23/2009 Many thanks to David Sun for some of the included slides!
Part-D1 Priority Queues
Data Structures & Algorithms Priority Queues & HeapSort
Heapsort Heap & Priority Queue.
Dr. David Matuszek Heapsort Dr. David Matuszek
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.
ITEC 2620M Introduction to Data Structures
Tree Representation Heap.
CSC212 Data Structure - Section AB
© 2013 Goodrich, Tamassia, Goldwasser
Heaps 12/4/2018 5:27 AM Heaps /4/2018 5:27 AM Heaps.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Ch. 8 Priority Queues And Heaps
Computer Science 2 Heaps.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
"Teachers open the door, but you must enter by yourself. "
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
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.
Heapsort.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Heaps © 2014 Goodrich, Tamassia, Goldwasser Heaps Heaps
Priority Queues & Heaps
CSE 12 – Basic Data Structures
HEAPS.
CSC 380: Design and Analysis of Algorithms
Heapsort.
Important Problem Types and Fundamental Data Structures
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
Heapsort.
Data Structures and Algorithm Analysis Priority Queues (Heaps)
Heaps & Multi-way Search Trees
CO 303 Algorithm Analysis and Design
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps.
CS210- Lecture 13 June 28, 2005 Agenda Heaps Complete Binary Tree
Presentation transcript:

i206: Lecture 14: Heaps, Graphs intro. Marti Hearst Spring 2012

Heap A specialized binary tree that satisfies Heap property Complete binary tree property Useful for implementing priority queue, heap-sort algorithm The heap property: Each node’s key is larger than its childrens’ keys.

Heap Methods Insert Remove Insert element as last node of the heap May need to perform up-heap bubbling to restore heap-order property Remove Remove and return element at root node Move last node to root node May need to perform down-heap bubbling to restore heap-order property

Heaps Root A heap is a certain kind of complete binary tree. The first node of a complete binary tree is always the root... When a complete binary tree is built, its first node must be the root. Slide copyright 1999 Addison Wesley Longman

Left child of the root The second node is always the left child ...the second node is always the left child of the root... The second node is always the left child of the root. Slide copyright 1999 Addison Wesley Longman

Right child of the root The third node is always the right child ...then the right child of the root... The third node is always the right child of the root. Slide copyright 1999 Addison Wesley Longman

level from left-to-right. ...and so on. The nodes always fill each level from left-to-right... The next nodes always fill the next level from left-to-right. Slide copyright 1999 Addison Wesley Longman

level from left-to-right. The next nodes always fill the next level from left-to-right. Slide copyright 1999 Addison Wesley Longman

Slide copyright 1999 Addison Wesley Longman ...from left-to-right... Slide copyright 1999 Addison Wesley Longman

Slide copyright 1999 Addison Wesley Longman ...from left-to-right... Slide copyright 1999 Addison Wesley Longman

Slide copyright 1999 Addison Wesley Longman ...and when a level is filled you start the next level at the left. Slide copyright 1999 Addison Wesley Longman

45 35 23 27 21 22 4 19 Each node in a heap contains a key that So, a heap is a complete binary tree. Each node in a heap contains a key, and these keys must be organized in a particular manner. Notice that this is not a binary search tree, but the keys do follow some semblance of order. Can you see what rule is being enforced here? Each node in a heap contains a key that can be compared to other nodes' keys. Slide copyright 1999 Addison Wesley Longman

Largest node is always on the top. 45 Largest node is always on the top. 35 23 27 21 22 4 19 The heap property requires that each node's key is >= to the keys of its children. This is a handy property because the biggest node is always at the top. Because of this, a heap can easily implement a priority queue (where we need quick access to the highest priority item). The "heap property" requires that each node's key is >= the keys of its children Slide copyright 1999 Addison Wesley Longman

Adding a Node to a Heap 45 Put the new node in the next available spot. Move the new node upward, swapping with its parent until the new node reaches an acceptable location. 35 23 27 21 22 4 19 42 We can add new elements to a heap whenever we like. Because the heap is a complete binary search tree, we must add the new element at the next available location, filling in the levels from left-to-right. In this example, I have just added the new element with a key of 42. Of course, we now have a problem: The heap property is no longer valid. The 42 is bigger than its parent 27. To fix the problem, we will push the new node upwards until it reaches an acceptable location. Slide copyright 1999 Addison Wesley Longman

45 35 23 42 21 22 4 19 27 Slide copyright 1999 Addison Wesley Longman Here we have pushed the 42 upward one level, swapping it with its smaller parent 27. We can't stop here though, because the parent 35 is still smaller than the new node 42. Slide copyright 1999 Addison Wesley Longman

45 42 23 35 21 22 4 19 27 Slide copyright 1999 Addison Wesley Longman Can we stop now? Yes, because the 42 is less than or equal to its parent. Slide copyright 1999 Addison Wesley Longman

45 The parent has a key that is >= new node, or The node reaches the root. The process of moving the new node upward is called reheapification upward. 42 23 35 21 22 4 19 27 In general, there are two conditions that can stop the pushing upward: 1. We reach a spot where the parent is >= the new node, or 2. We reach the root. This process is called reheapification upward (I didn't just make up that name, really). Slide copyright 1999 Addison Wesley Longman

Removing the Top of the Heap 45 Move the last node onto the root. 42 23 35 21 22 4 19 27 We can also remove the top node from a heap. The first step of the removal is to move the last node of the tree onto the root. In this example we move the 27 onto the root. Slide copyright 1999 Addison Wesley Longman

27 42 23 35 21 22 4 19 Slide copyright 1999 Addison Wesley Longman Now the 27 is on top of the heap, and the original root (45) is no longer around. But the heap property is once again violated. Slide copyright 1999 Addison Wesley Longman

27 42 23 35 21 22 4 19 Move the last node onto the root. Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 42 23 35 21 22 4 19 We'll fix the problem by pushing the out-of-place node downward. Perhaps you can guess what the downward pushing is called....reheapification downward. Slide copyright 1999 Addison Wesley Longman

42 27 23 35 21 22 4 19 Move the last node onto the root. Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 27 23 35 21 22 4 19 When we push a node downward it is important to swap it with its largest child. (Otherwise we are creating extra problems by placing the smaller child on top of the larger child.) This is what the tree looks like after one swap. Should I continue with the reheapification downward? Slide copyright 1999 Addison Wesley Longman

42 35 23 27 21 22 4 19 Move the last node onto the root. Push the out-of-place node downward, swapping with its larger child until the new node reaches an acceptable location. 35 23 27 21 22 4 19 Yes, I swap again, and now the 27 is in an acceptable location. Slide copyright 1999 Addison Wesley Longman

42 The children all have keys <= the out-of-place node, or The node reaches the leaf. The process of pushing the new node downward is called reheapification downward. 35 23 27 21 22 4 19 Reheapification downward can stop under two circumstances: 1. The children all have keys that are <= the out-of-place node. 2. The out-of-place node reaches a leaf. Slide copyright 1999 Addison Wesley Longman

Implementing a Heap 42 35 23 27 21 An array of data Store the data from the nodes in a partially-filled array. 35 23 27 21 This slide shows the typical way that a heap is implemented. For the most part, there is nothing new here, because you already know how to implement a complete binary tree using a partially-filled array. That is what we are doing with the heap. An array of data Slide copyright 1999 Addison Wesley Longman

Data from the root goes in the first location of the array. 42 35 23 27 21 Following the usual technique for implementing a complete binary tree, the data from the root is stored in the first entry of the array. 42 An array of data Slide copyright 1999 Addison Wesley Longman

Data from the next row goes in the next two array locations. 42 35 23 27 21 The next two nodes go in the next two locations of the array. 42 35 23 An array of data Slide copyright 1999 Addison Wesley Longman

Data from the next row goes in the next two array locations. 42 35 23 27 21 and so on. 42 35 23 27 21 An array of data Slide copyright 1999 Addison Wesley Longman

Data from the next row goes in the next two array locations. 42 35 23 27 21 As with any partially-filled array, we are only concerned with the front part of the array. If the tree has five nodes, then we are only concerned with the entries in the first five components of the array. 42 35 23 27 21 An array of data Slide copyright 1999 Addison Wesley Longman

The links between the tree's nodes are not actually stored as pointers, or in any other way. The only way we "know" that "the array is a tree" is from the way we manipulate the data. 42 35 23 27 21 With this implementation of a heap, there are no pointers. The only way that we know that the array is a heap is the manner in which we manipulate it. 42 35 23 27 21 An array of data Slide copyright 1999 Addison Wesley Longman

If you know the index of a node, then it is easy to figure out the indexes of that node's parent and children. if i = location of current node: location of left(i) = 2i + 1 location of right(i) = 2i + 2 42 35 23 27 21 The manipulations are the same manipulations that you've used for a complete binary tree, making it easy to compute the index where various nodes are stored. 42 35 23 27 21 [0] [1] [2] [3] [4]

if i = location of current node: location of left(i) = 2i + 1 If you know the index of a node, then it is easy to figure out the indexes of that node's parent and children. if i = location of current node: location of left(i) = 2i + 1 location of right(i) = 2i + 2 The manipulations are the same manipulations that you've used for a complete binary tree, making it easy to compute the index where various nodes are stored.

Heap Running Times What is the running time of each operation? Insert O(log n) Remove

Note: Heap is NOT sorted 42 It satisfies the heap property, but does not fully sort the keys. Heap property? 35 23 27 21

Heapsort Given input items: Build a heap from the input items sortedlist = [] While the heap is not empty: Remove the largest item (the root), and place it at the end of sortedlist Re-heapify the heap Return sortedlist.

Heapsort Running time is O(n log n) Almost as fast as quicksort, but doesn’t have as bad a worst case running time Quicksort worst case is O(n^2) Heapsort can be done in place in one array of length n. Also competes with merge sort Heapsort faster, but merge sort easier to parallelize John Chuang

Outline What is a data structure Basic building blocks: arrays and linked lists Data structures (uses, methods, performance): List, stack, queue Dictionary Tree Graph

Internet Graphs Source: Cheswick and Burch

Social Network Graphs American Journal of Sociology, Vol. 100, No. 1. "Chains of affection: The structure of adolescent romantic and sexual networks," Bearman PS, Moody J, Stovel K.

Graph A graph consists of a set of nodes (vertices) and a set of links (edges) that establish relationships (connections) between the nodes Represented/stored using adjacency list or adjacency matrix data structures Adjacency list for Graph 1: {a,b}, {a,c}, {b,c} Adjacency matrix for Graph 2: Edges can be directed/undirected Edges can have weights Tree is a special case of graph

Graph Algorithms Search/traversal: breadth-first or depth-first -- O(|V|+|E|) Routing: shortest path between two points (Dijkstra’s algorithm) -- O(|V|2+|E|) Minimum spanning tree -- O(|E|) Maximum Flow -- O(|V|3), O(|V|2|E|), O(|V||E|2)