l11-graphs

# l11-graphs - CS112 Data Structures Lecture 11 Graphs CS112...

This preview shows pages 1–14. Sign up to view the full content.

CS112: Slides for Prof. Steinberg ʼ s lecture 1 Lecture 11 CS112: Data Structures CS112: Data Structures Lecture 11 Graphs

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS112: Slides for Prof. Steinberg ʼ s lecture 2 Lecture 11 Upcoming Schedule Upcoming Schedule Wed, July 20: to help (bring laptops!) Mon, July 25: 6-7 recitation 7:10 - 8:30 review Wed, July 27: 6 - 7:20 Midterm 2 (info to be posted)
CS112: Slides for Prof. Steinberg ʼ s lecture 3 Lecture 11 Review: Priority Queues Review: Priority Queues Each data item has a priority Add items to queue in any order Highest Priority First Out add A:5, B:3, C:6 remove C add D:8 remove D, remove A

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS112: Slides for Prof. Steinberg ʼ s lecture 4 Lecture 11 Implement as an array Implement as an array Unsorted or sorted: insert or delete is O(n) Can we find a data structure that gives O(insert + delete) bettern than O(n)?
CS112: Slides for Prof. Steinberg ʼ s lecture 5 Lecture 11 Heap Heap A heap is a way to implement a priority queue with O(log n) complexity A heap is a complete binary tree all levels except maybe the last are full last level filled from left to right good bad

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS112: Slides for Prof. Steinberg ʼ s lecture 6 Lecture 11 Heap Heap The number at a node is greater than the number at any descendant 8 7 4 5 6 1 good bad 8 7 4 9 6 1
CS112: Slides for Prof. Steinberg ʼ s lecture 7 Lecture 11 Heap Insert Heap Insert Add node at end of last level Move up restoring order 8 7 3 5 6 1 9 8 7 3 5 9 1 6 9 7 3 5 8 1 6 9 7 3 5 8 1 6 4 9 7 4 5 8 1 6 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS112: Slides for Prof. Steinberg ʼ s lecture 8 Lecture 11 Heap Deletion Heap Deletion Copy out data at root root Move down restoring order 6 7 3 5 8 1 8 7 3 5 6 1
CS112: Slides for Prof. Steinberg ʼ s lecture 9 Lecture 11 Heap Deletion Heap Deletion Compare current node and two children if current node largest, stop if left node largest swap current and left ditto if right largest 1 7 3 5 6 7 1 3 5 6 7 5 3 1 6

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS112: Slides for Prof. Steinberg ʼ s lecture 10 Lecture 11 Heap Representation Heap Representation Store heap in an array For node at index j, children are at 2j+1 and 2j+2 Root at index 0 7 5 3 1 6 7 5 6 3 1 0 1 2 3 4
CS112: Slides for Prof. Steinberg ʼ s lecture 11 Lecture 11 Building a Heap from an Array Building a Heap from an Array Go from last non-leaf to index 0 At each node, do filter-down Work at a node is O(height of node) In a complete binary tree, majority of nodes close to bottom, so adds up to O(n)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CS112: Slides for Prof. Steinberg ʼ s lecture 12 Lecture 11 Building a Heap from an Array Building a Heap from an Array 3 11 5 7 22 18 14 3 11 18 7 22 5 14 3 22 18 7 11 5 14 3 11 18 7 22 5 14
CS112: Slides for Prof. Steinberg ʼ s lecture 13 Lecture 11 Building a Heap from an Array Building a Heap from an Array 22 11 18 7 3 5 14 3 22 18 7 11 5 14 22 3 18 7 11 5 14

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This document was uploaded on 11/01/2011 for the course 198 112 at Rutgers.

### Page1 / 59

l11-graphs - CS112 Data Structures Lecture 11 Graphs CS112...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online