Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf ·...
Transcript of Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf ·...
![Page 1: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/1.jpg)
Data Structures for Minimum Spanning Trees:Graphs & Heaps
Slides by Carl Kingsford
Jan. 17, 2014
KT 2.5,3.1
![Page 2: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/2.jpg)
Recall Prim’s Algorithm
1: # distToT[u] is distance from current tree to u
2: for u 2 V do distToT[u] 13: u s4: while u 6= null do
5: # We put u in the tree, so distance is �16: distToT[u] �17: # Each of u’s neighbors v are now incident to the current tree
8: for v 2 Neighbors(u) do9: # If the distance is smaller than before, we have to update
10: if d(u,v) < distToT[v] then11: distToT[v] d(u,v)12: parent[v] u
13: u ClosestVertex(distToT)
14: return parent
![Page 3: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/3.jpg)
RAM = Symbols + Pointers(for our purposes)
RAM is an array of bits, broken up into words, where each wordhas an address.
0100101001111011
0010000100100011
0110111010100000
1000010001010001
0000000000000100
1001010110001010
1000000111000001
1111111111111111
16-bit words
0
2
4
6
8
10
12
14
We may interpret the bits as numbers, or letters, or other symbols
We may interpret bits as a memory address (pointer)
Add
ress
es
We can store and manipulate arbitrary symbols (like letters) andassociations between them.
![Page 4: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/4.jpg)
Storing a list
Store a list of numbers such as 3, 7, 32, 8:
3 next
7 next
32 next
8 nextFirst
0
I Records located anywhere in memory
I Don’t have to know the size of data at the start
I Pointers let us express relationships between pieces ofinformation
![Page 5: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/5.jpg)
What is a data structure anyway?
It’s an agreement about:
I how to store a collection of objects in memory,
I what operations we can perform on that data,
I the algorithms for those operations, and
I how time and space e�cient those algorithms are.
Data structures ! Data structurING:
How do we organize information in memory so that we can find,update, add, and delete portions of it e�ciently?
Often, the key to a fast algorithm is an e�cient data structure.
![Page 6: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/6.jpg)
Abstract data types (ADT)
ADT specifies permitted operations as well as time and spaceguarantees.
Example Graph ADT (without time/space guarantees):
I G.add vertex(u) — adds a vertex to graph G.
I G.add edge(u, v, d) — adds an edge of weight d betweenvertices u and v.
I G.has edge(u, v) — returns True i↵ edge {u,v} exists in G.
I G.neighbors(u) — gives a list of vertices adjacent to u in G.
I G.weight(u,v) — gives the weight of edge u and v
![Page 7: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/7.jpg)
Representing Graphs
Adjacency matrix:
0
BBBB@
0 1 1 0 01 0 0 0 11 0 0 1 10 0 1 0 00 1 1 0 0
1
CCCCA
Adjacency list:
2 3
1 5
1 4 5
3
2 3
1
2
3
4
5
![Page 8: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/8.jpg)
Representing Graphs
Adjacency matrix:
0
BBBB@
0 1 1 0 01 0 0 0 11 0 0 1 10 0 1 0 00 1 1 0 0
1
CCCCA
Adjacency list:
2 3
1 5
1 4 5
3
2 3
1
2
3
4
5
How long doesG.neighbors(u) take?
![Page 9: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/9.jpg)
Representing Graphs
Adjacency matrix:
0
BBBB@
0 1 1 0 01 0 0 0 11 0 0 1 10 0 1 0 00 1 1 0 0
1
CCCCA
Adjacency list:
2 3
1 5
1 4 5
3
2 3
1
2
3
4
5
How long doesG.neighbors(u) take?
How would you implementG.weight(u,v)?
![Page 10: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/10.jpg)
Implementing ClosestVertex:
Priority Queue ADT & d-Heaps
![Page 11: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/11.jpg)
Priority Queue ADT
A priority queue, also called a heap, holds items i that have keyskey(i).
They support the following operations:
I H.insert(i) — put item i into heap H
I H.deletemin() — return item with smallest key in H anddelete it from H
I H.makeheap(S) — create a heap from a set S of items
I H.findmin() — return item with smallest key in H
I H.delete(i) — remove item i from H
![Page 12: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/12.jpg)
ClosestVertex via Heaps
Items: vertices that are on the “frontier” (incident to the currenttree)
Key(v) = distToT[v]
ClosestVertex: return H.deletemin()
How can we e�ciently implement the heap ADT?
![Page 13: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/13.jpg)
Priority Queue ADT
• Efficiently support the following operations on a set of keys:!- findmin: return the smallest key!
- deletemin: return the smallest key & delete it!
- insert: add a new key to the set!
- delete: delete an arbitrary key!
• Would like to be able to do findmin faster (say in time independent of the # of items in the set).
![Page 14: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/14.jpg)
Job Scheduling: UNIX process prioritiesPRI COMM! 14 /System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Versions/A/Support/mdworker! 31 -bash! 31 /Applications/iTunes.app/Contents/Resources/iTunesHelper.app/Contents/MacOS/iTunesHelper! 31 /System/Library/CoreServices/Dock.app/Contents/MacOS/Dock! 31 /System/Library/CoreServices/FileSyncAgent.app/Contents/MacOS/FileSyncAgent! 31 /System/Library/CoreServices/RemoteManagement/AppleVNCServer.bundle/Contents/MacOS/AppleVNCServer! 31 /System/Library/CoreServices/RemoteManagement/AppleVNCServer.bundle/Contents/Support/RFBRegisterMDNS! 31 /System/Library/CoreServices/RemoteManagement/AppleVNCServer.bundle/Contents/Support/VNCPrivilegeProxy! 31 /System/Library/CoreServices/Spotlight.app/Contents/MacOS/Spotlight! 31 /System/Library/CoreServices/coreservicesd!...! 31 /System/Library/PrivateFrameworks/MobileDevice.framework/Versions/A/Resources/usbmuxd! 31 /System/Library/Services/AppleSpell.service/Contents/MacOS/AppleSpell! 31 /sbin/launchd! 31 /sbin/launchd! 31 /usr/bin/ssh-agent! 31 /usr/libexec/ApplicationFirewall/socketfilterfw! 31 /usr/libexec/hidd! 31 /usr/libexec/kextd!...! 31 /usr/sbin/mDNSResponder! 31 /usr/sbin/notifyd! 31 /usr/sbin/ntpd! 31 /usr/sbin/pboard! 31 /usr/sbin/racoon! 31 /usr/sbin/securityd! 31 /usr/sbin/syslogd! 31 /usr/sbin/update! 31 autofsd! 31 login! 31 ps! 31 sort! 46 /Applications/Preview.app/Contents/MacOS/Preview! 46 /Applications/iCal.app/Contents/MacOS/iCal! 47 /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal! 50 /System/Library/Frameworks/CoreServices.framework/Frameworks/Metadata.framework/Support/mds! 50 /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CarbonCore.framework/Versions/A/Support/fseventsd! 62 /System/Library/CoreServices/Finder.app/Contents/MacOS/Finder! 63 /Applications/Safari.app/Contents/MacOS/Safari! 63 /Applications/iWork '08/Keynote.app/Contents/MacOS/Keynote! 63 /System/Library/CoreServices/Dock.app/Contents/Resources/DashboardClient.app/Contents/MacOS/DashboardClient! 63 /System/Library/CoreServices/SystemUIServer.app/Contents/MacOS/SystemUIServer! 63 /System/Library/CoreServices/loginwindow.app/Contents/MacOS/loginwindow! 63 /System/Library/Frameworks/ApplicationServices.framework/Frameworks/CoreGraphics.framework/Resources/WindowServer! 63 /sbin/dynamic_pager! 63 /usr/sbin/UserEventAgent! 63 /usr/sbin/coreaudiod!
When scheduler asks “What should I run next?” it could findmin(H).
![Page 15: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/15.jpg)
Plane Sweep: Process points left to right:
Store points in a priority queue, ordered by their x coordinate.
![Page 16: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/16.jpg)
Plane Sweep: Process points left to right:
Store points in a priority queue, ordered by their x coordinate.
![Page 17: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/17.jpg)
Plane Sweep: Process points left to right:
Store points in a priority queue, ordered by their x coordinate.
![Page 18: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/18.jpg)
Plane Sweep: Process points left to right:
Store points in a priority queue, ordered by their x coordinate.
![Page 19: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/19.jpg)
Plane Sweep: Process points left to right:
Store points in a priority queue, ordered by their x coordinate.
![Page 20: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/20.jpg)
Heap-Ordered Trees
• The keys of the children of u are ≥ the key(u), for all nodes u. !
• (This “heap” has nothing to do with the “heap” part of computer memory.)
2
8 3
12 9 7
10
![Page 21: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/21.jpg)
Heap-Ordered Trees
• The keys of the children of u are ≥ the key(u), for all nodes u. !
• (This “heap” has nothing to do with the “heap” part of computer memory.)
2
8 3
12 9 7
10
Along each path!keys are monotonically non-decreasing
![Page 22: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/22.jpg)
Heap – Find min
2
8 3
12 9 7
10
The minimum element is always
the root
![Page 23: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/23.jpg)
Heap – Insert
2
3
12 7
10
9
8
![Page 24: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/24.jpg)
1. Add node as a leaf (we’ll see where later)
Heap – Insert
2
3
12 7
106
9
8
![Page 25: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/25.jpg)
1. Add node as a leaf (we’ll see where later)
Heap – Insert
2
3
12 7
10
2. “sift up:” while current node is < its parent, swap them.
6
9
8
![Page 26: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/26.jpg)
1. Add node as a leaf (we’ll see where later)
Heap – Insert
2
3
12 7
10
2. “sift up:” while current node is < its parent, swap them.
6
9
8
![Page 27: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/27.jpg)
1. Add node as a leaf (we’ll see where later)
Heap – Insert
2
3
12 7
10
2. “sift up:” while current node is < its parent, swap them.
6
9
8
![Page 28: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/28.jpg)
Heap – Delete(i)
2
3
12 7
10
6
9
8
![Page 29: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/29.jpg)
Heap – Delete(i)
2
3
12 7
10
6
9
8
1. need a pointer to node containing key i
![Page 30: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/30.jpg)
Heap – Delete(i)
2
3
12 7
10
2. replace key to delete i with key j at a leaf node (we’ll see how to find a leaf soon)
9
8
1. need a pointer to node containing key i
![Page 31: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/31.jpg)
Heap – Delete(i)
2
3
12 7
10
2. replace key to delete i with key j at a leaf node (we’ll see how to find a leaf soon)
9
8
1. need a pointer to node containing key i
3. Delete leaf
![Page 32: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/32.jpg)
Heap – Delete(i)
2
3
12 7
10
2. replace key to delete i with key j at a leaf node (we’ll see how to find a leaf soon)
9
8
4. If i > j then sift up, moving j up the tree.!
If i < j then “sift down”: swap current node with smallest of children until its bigger than all of its children.
1. need a pointer to node containing key i
3. Delete leaf
![Page 33: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/33.jpg)
Running Times
• findmin takes constant time [O(1)]!
• insert, delete take time ∝ tree height plus the time to find the leaves.!
• deletemin: same as delete!
• Q1: How do we find leaves used in insert and delete?!- delete: use the last inserted node.!
- insert: choose node so tree remains complete.!
• Q2: How do we ensure the tree has low height?
![Page 34: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/34.jpg)
2115
Store Heap in a Complete Tree
2
8 3
12 9 7 10
![Page 35: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/35.jpg)
A2115
Store Heap in a Complete Tree
2
8 3
12 9 7 10
![Page 36: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/36.jpg)
BA2115
Store Heap in a Complete Tree
2
8 3
12 9 7 10
![Page 37: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/37.jpg)
CBA2115
Store Heap in a Complete Tree
2
8 3
12 9 7 10
![Page 38: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/38.jpg)
C DBA2115
Store Heap in a Complete Tree
2
8 3
12 9 7 10
![Page 39: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/39.jpg)
C D EBA2115
Store Heap in a Complete Tree
2
8 3
12 9 7 10
![Page 40: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/40.jpg)
C D E FBA2115
Store Heap in a Complete Tree
2
8 3
12 9 7 10
![Page 41: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/41.jpg)
Arrays ⇔ Complete Binary Trees
C D E FBA2115
2
8 3
12 9 7 10
![Page 42: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/42.jpg)
2115
Store Heap in a Complete Tree
2
8 3
12 9 7 10
2 8 3 12 9 7 10 15 21 A B C D E F1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
left(i): 2i if 2i ≤ n otherwise None!right(i): (2i + 1) if 2i + 1 ≤ n otherwise None!parent(i): #i/2$ if i ≥ 2 otherwise None
![Page 43: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/43.jpg)
A2115
Store Heap in a Complete Tree
2
8 3
12 9 7 10
2 8 3 12 9 7 10 15 21 A B C D E F1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
left(i): 2i if 2i ≤ n otherwise None!right(i): (2i + 1) if 2i + 1 ≤ n otherwise None!parent(i): #i/2$ if i ≥ 2 otherwise None
![Page 44: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/44.jpg)
BA2115
Store Heap in a Complete Tree
2
8 3
12 9 7 10
2 8 3 12 9 7 10 15 21 A B C D E F1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
left(i): 2i if 2i ≤ n otherwise None!right(i): (2i + 1) if 2i + 1 ≤ n otherwise None!parent(i): #i/2$ if i ≥ 2 otherwise None
![Page 45: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/45.jpg)
CBA2115
Store Heap in a Complete Tree
2
8 3
12 9 7 10
2 8 3 12 9 7 10 15 21 A B C D E F1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
left(i): 2i if 2i ≤ n otherwise None!right(i): (2i + 1) if 2i + 1 ≤ n otherwise None!parent(i): #i/2$ if i ≥ 2 otherwise None
![Page 46: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/46.jpg)
C DBA2115
Store Heap in a Complete Tree
2
8 3
12 9 7 10
2 8 3 12 9 7 10 15 21 A B C D E F1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
left(i): 2i if 2i ≤ n otherwise None!right(i): (2i + 1) if 2i + 1 ≤ n otherwise None!parent(i): #i/2$ if i ≥ 2 otherwise None
![Page 47: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/47.jpg)
C D EBA2115
Store Heap in a Complete Tree
2
8 3
12 9 7 10
2 8 3 12 9 7 10 15 21 A B C D E F1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
left(i): 2i if 2i ≤ n otherwise None!right(i): (2i + 1) if 2i + 1 ≤ n otherwise None!parent(i): #i/2$ if i ≥ 2 otherwise None
![Page 48: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/48.jpg)
C D E FBA2115
Store Heap in a Complete Tree
2
8 3
12 9 7 10
2 8 3 12 9 7 10 15 21 A B C D E F1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
left(i): 2i if 2i ≤ n otherwise None!right(i): (2i + 1) if 2i + 1 ≤ n otherwise None!parent(i): #i/2$ if i ≥ 2 otherwise None
![Page 49: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/49.jpg)
d-Heaps – Don’t have to use binary trees
• What about complete non-binary trees (e.g. every node has d children)?!- insert takes O(logd n) [because height O(logd n)]!
- delete takes O(d logd n) [why?]!
!
• Can still store in an array.!!
• If you have few deletions, make d bigger so that tree is shorter.!
• Can tune d to fit the relative proportions of inserts / deletes.
![Page 50: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/50.jpg)
3-Heap Example
![Page 51: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/51.jpg)
d-Heap Runtime Summary
!
- findmin takes O(1) time!
- insert takes O(logd n) time !
- delete takes O(d logd n) time!
- deletemin takes time O(d logd n)!
- makeheap takes O(n) time
Reason: height of a complete!binary tree with n nodes is about!log n.
![Page 52: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/52.jpg)
One more operation needed for Prim’s
• H.decreaseKey(u,j): reduce the key for item u by j.
![Page 53: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/53.jpg)
One more operation needed for Prim’s
• H.decreaseKey(u,j): reduce the key for item u by j.
Why is this needed?
![Page 54: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/54.jpg)
One more operation needed for Prim’s
• H.decreaseKey(u,j): reduce the key for item u by j.
Why is this needed?
How can we implement it?
![Page 55: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/55.jpg)
One more operation needed for Prim’s
• H.decreaseKey(u,j): reduce the key for item u by j.
Why is this needed?
How can we implement it?
1. Reduce the key by j!2. siftup to put it in the right place.!3. Takes time proportional to the height of the tree ≈ log n
![Page 56: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/56.jpg)
Make Heap – Create a heap from n items
• n inserts take time ∝ n log n.!
• Better: !
- put items into array arbitrarily.!
- for i = n ... 1, siftdown(i).!
• Each element trickles down to its correct place.
By the time you sift level i, all levels i + 1 and greater are already heap ordered.
![Page 57: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/57.jpg)
Make Heap – Time Bound
There are at most n/2h items at height h.
Siftdown for all height h nodes is ≈ hn/2h time
Total time ! ≈ ∑h hn/2h [sum of time for each height]! = n ∑h (h / 2h) [factor out the n]! ≈ n [sum bounded by const]
2H-h nodes at height h!in a tree with total height H = 2H / 2h!!H ≈ log n; 2H = n
Height counts from bottom!Level counts from top
![Page 58: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/58.jpg)
Heapsort – Another application of Heaps
2 12 10 7 15 21 91 2 3 4 5 6 7 8 9
8 3Given unsorted array of integers
end
![Page 59: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/59.jpg)
Heapsort – Another application of Heaps
3 12 8 7 15 21 91 2 3 4 5 6 7 8 9
2 12 10 7 15 21 91 2 3 4 5 6 7 8 9
8 3
2 10
Given unsorted array of integers
makeheap – O(n)!Now first position has smallest item.
end
end
Swap first & last items.
![Page 60: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/60.jpg)
Heapsort – Another application of Heaps
3 12 8 7 15 21 91 2 3 4 5 6 7 8 9
2 12 10 7 15 21 91 2 3 4 5 6 7 8 9
8 3
210
Given unsorted array of integers
makeheap – O(n)!Now first position has smallest item.
end
end
Swap first & last items.
![Page 61: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/61.jpg)
Heapsort – Another application of Heaps
3 12 8 7 15 21 91 2 3 4 5 6 7 8 9
2 12 10 7 15 21 91 2 3 4 5 6 7 8 9
8 3
210
Given unsorted array of integers
makeheap – O(n)!Now first position has smallest item.
Delete last item from heap. 3 12 8 7 15 21 91 2 3 4 5 6 7 8 9
210
end
end
end
![Page 62: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/62.jpg)
Heapsort – Another application of Heaps
3 12 8 7 15 21 91 2 3 4 5 6 7 8 9
2 12 10 7 15 21 91 2 3 4 5 6 7 8 9
8 3
210
Given unsorted array of integers
makeheap – O(n)!Now first position has smallest item.
Delete last item from heap. 3 12 8 7 15 21 91 2 3 4 5 6 7 8 9
210
end
end
end
![Page 63: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/63.jpg)
Heapsort – Another application of Heaps
3 12 8 7 15 21 91 2 3 4 5 6 7 8 9
2 12 10 7 15 21 91 2 3 4 5 6 7 8 9
8 3
10 2
Given unsorted array of integers
makeheap – O(n)!Now first position has smallest item.
Delete last item from heap. 3 12 8 7 15 21 91 2 3 4 5 6 7 8 9
210
end
end
end
8 21 91 2 3 4 5 6 7 8 9
210
end
12 15siftdown new root key down 3 7
![Page 64: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/64.jpg)
Heapsort – Another application of Heaps
3 12 8 7 15 21 91 2 3 4 5 6 7 8 9
2 12 10 7 15 21 91 2 3 4 5 6 7 8 9
8 3
10 2
Given unsorted array of integers
makeheap – O(n)!Now first position has smallest item.
Delete last item from heap. 3 12 8 7 15 21 91 2 3 4 5 6 7 8 9
210
end
end
end
8 21 91 2 3 4 5 6 7 8 9
210
end
12 15siftdown new root key down 3 7
![Page 65: Data Structures for Minimum Spanning Trees: Graphs & Heapsckingsf/.../lectures/lec02-mstdata.pdf · Data Structures for Minimum Spanning Trees: Graphs & Heaps Slides by Carl Kingsford](https://reader033.fdocuments.in/reader033/viewer/2022042322/5f0bdea97e708231d4329e37/html5/thumbnails/65.jpg)
Heapsort – Another application of Heaps
3 12 8 7 15 21 91 2 3 4 5 6 7 8 9
2 12 10 7 15 21 91 2 3 4 5 6 7 8 9
8 3
10 2
Given unsorted array of integers
makeheap – O(n)!Now first position has smallest item.
Delete last item from heap. 3 12 8 7 15 21 91 2 3 4 5 6 7 8 9
210
end
end
end
8 21 91 2 3 4 5 6 7 8 9
210
end
12 15siftdown new root key down 3 7