l11-graphs

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

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

View Full Document Right Arrow Icon
CS112: Slides for Prof. Steinberg ʼ s lecture 1 Lecture 11 CS112: Data Structures CS112: Data Structures Lecture 11 Graphs
Background image of page 1

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

View Full DocumentRight Arrow Icon
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)
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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)?
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 8
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
Background image of page 9

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

View Full DocumentRight Arrow Icon
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
Background image of page 10
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)
Background image of page 11

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

View Full DocumentRight Arrow Icon
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
Background image of page 12
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
Background image of page 13

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

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 59

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

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online