Download presentation
Presentation is loading. Please wait.
1
CISC220 Fall 2009 James Atlas Nov 13: Heap Implementations, Graphs
2
Heaps Specialized Tree Often are Binary Heaps Satisfy Heap Property –if B is a child node of A, then key(A) ≥ key(B) [in a Max-Heap] –if B is a child node of A, then key(A) key(B) [in a Min-Heap]
3
Binary Max Heap
4
Heap operations insert: –adding a new key to the heap delete-max or delete-min: –removing the root node increase-key or decrease-key: –updating a key within a max- or min-heap, respectively merge: –joining two heaps to form a valid new heap containing all the elements of both.
5
Heap Insert 1.Add the element on the bottom level of the heap. 2.Compare the added element with its parent; if they are in the correct order, stop. 3.If not, swap the element with its parent and return to the previous step.
6
Heap Insert (example) 2 1 3
7
Heap Delete Replace root with last element Find max (or min) between root and children Swap if child > root, recursively do find max on subtree now rooted at new child
8
Heap Delete (example) 12
9
Implementations Array
10
Operation Running Times OperationBinary findMaxO(1) deleteMaxO(log n) insertO(log n) decreaseKeyO(log n) mergeO(n)
11
Seven Bridges of Königsberg solved by Leonhard Euler c1735 Find a path that crosses all bridges exactly once
12
Seven Bridges of Königsberg (cont’)
13
vertex edge Undirected Degree at each node is 3
14
Implementation/Storage Details How do we represent a graph?
15
List Structures V = {A, B, C, D, E} E = {{A, B}, {A, D}, {C, E}, {D, E}} Incidence List –E = {{A, B}, {A, D}, {C, E}, {D, E}} Adjacency List –A = {{A, B, D}, {B, A}, {C, E}, {D, A, E}, {E, C, D}} A B E D C
16
Matrix Structures V = {A, B, C, D, E} E = {{A, B}, {A, D}, {C, E}, {D, E}} Adjacency Matrix A B E D C ABCDE A11010 B11000 C00101 D10011 E00111
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.