CS 381 – Spring 2017 Week 9.1, Monday, March 6 1 Assignment 5: submit one problem by 3/10 Problem 1 Determine and efficient approach and analyze your algorithm Problem 2 and 3 Review basic graph and tree representations and traversals (DFS, BFS, topological search) We will cover finding strongly connected components using DFS Problem 4 Review Union/Find material covered Problem 5 Read problem carefully and explore your greedy ideas Problem 6 Review amortized analysis covered in class 2 Three types of amortized analysis Aggregate analysis Determine total time T(n) for all n operations Amortized cost per operation is T(n)/n Accounting method Assign charges to operations Never overspend Potential method Define a potential function which maps a data structure to a real number representing a cost Amortized cost = actual cost + change in potential 3 Potential function: Dynamic tables (17.4.1) Potential function P P(T) = 2 number of items in table - size of the table = 2 n(T) - s(T) i-th operation when the table size does not change 𝒄(i) = c(i) + P(Ti) – P(Ti-1) = 1 + (2n(Ti) - s(Ti)) – (2n(Ti-1) - s(Ti-1)) = 1 + 2n(Ti) - s(Ti) – 2(n(Ti) - 1) + s(Ti)) = 1 + 2n(Ti) – 2n(Ti) + 2 =3 4 Potential function P P(T) = 2 n(T) - s(T) i-th operation when the table size does change The size of the table doubles; we have s(Ti) = 2(n(Ti) -1) 𝒄(i) = c(i) + P(Ti) – P(Ti-1) = n(Ti)+ (2 n(Ti) - s(Ti)) – (2 n(Ti-1) - s(Ti-1)) = n(Ti) + (2 n(Ti) - 2 (n(Ti)-1)) – (2[n(Ti) – 1] - (n(Ti) -1)) = n(Ti) + 2 n(Ti) - 2 n(Ti) +2 - 2 n(Ti) + 2 + n(Ti) - 1 = n(Ti) + 2 – n(Ti) + 1 =3 5 Using the potential function P(T) = 2n(T) - s(T) shows that the amortized cost of an insertion into a dynamic table is O(1). Aggregate and accounting methods are more intuitive, but don’t always work Analysis of Fibonacci Heap uses a potential function Analysis of Union/Find with path compression uses a potential function 6 Analysis of Prim’s mspt Algorithm Time = Q(V)·TEXTRACT-MIN + Q(E)·TDECREASE-KEY Q TEXTRACT-MIN TDECREASE-KEY Total array O(V) O(1) O(V2) O(lg V) O(lg V) O(E lg V) binary heap Fibonacci O(lg V) heap amortized O(1) O(E + V lg V) amortized worst case Fibonacci Heap (Chapter 20) Decrease-key costs O(log n) time in a regular min-heap Assume we have an index to each element in the heap Decrease-key done up to m times in Prim’s algorithm Is there a priority structure that achieves O(1) time for decrease-key? Prim’s algorithm Use Fibonacci heap to achieve O(m+n log n) Slides modified from www.cs.princeton.edu/~wayne/kleinberg-tardos/ 8 Fibonacci Heaps Maintain a collection of heaps Heaps are stored as trees, not in an array They are not binary heaps Insert: O(1) Delete-min: O(log n) Decrease-key: O(1) Delete-min and decrease-key costs are amortized over n operations 9 Fibonacci Heaps: Structure Set of heap-ordered trees. Maintain pointer to minimum element. Some nodes are marked nodes. min 17 30 Heap H 24 26 35 23 7 3 46 18 marked 39 52 41 44 10 Fibonacci Heaps: Notation n = number of nodes in heap. rank(x) = number of children of node x. rank(H) = maximum rank of any node in heap H. trees(H) = number of trees in heap H. marks(H) = number of marked nodes in heap H. trees(H) = 5 17 30 marks(H) = 3 24 26 35 n = 14 23 min rank = 3 7 3 46 18 marked 39 52 41 44 11 Fibonacci Heaps: Potential Function (H) = trees(H) + 2 marks(H) potential of heap H (H) = 5 + 23 = 11 trees(H) = 5 marks(H) = 3 17 30 Heap H min 24 26 35 23 7 3 46 18 marked 39 52 41 44 12 Fibonacci Heaps: Insert Insert. Create a new singleton tree. Add to root list; update min pointer (if necessary). insert 21 21 17 30 Heap H 24 26 35 46 23 min 7 3 18 39 52 41 44 13 Fibonacci Heaps: Insert Insert. Create a new singleton tree. Add to root list; update min pointer (if necessary). insert 21 min 17 30 Heap H 24 26 35 46 23 7 3 21 18 39 52 41 44 14 Fibonacci Heaps: Insert Analysis Actual cost. O(1) Change in potential. +1 (one tree is added) Amortized cost. O(1) (H) = trees(H) + 2 marks(H) min 17 30 Heap H 24 26 35 46 23 7 3 21 18 39 52 41 44 15 Linking Operation Linking operation. Make larger root a child of smaller root. larger root 24 77 tree T1 3 3 15 56 still heap-ordered smaller root 18 52 39 41 44 tree T2 56 15 18 24 39 52 41 44 77 tree T' 16 Fibonacci Heaps: Delete Min Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same rank. min 7 30 24 26 35 46 23 17 3 18 39 52 41 44 17 Fibonacci Heaps: Delete Min Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same rank. min 7 30 24 26 46 23 17 18 39 52 41 44 35 18 Fibonacci Heaps: Delete Min Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same rank. min current 7 30 24 26 46 23 17 18 39 52 41 44 35 19 Fibonacci Heaps: Delete Min Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same rank. rank 0 min 1 2 3 current 7 30 24 26 46 23 17 18 39 52 41 44 35 20 Fibonacci Heaps: Delete Min Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same rank. rank 0 min 1 2 3 current 7 30 24 26 46 23 17 18 39 52 41 44 35 21 Fibonacci Heaps: Delete Min Delete min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same rank. rank 0 1 2 3 min 7 30 24 26 46 23 current 17 18 39 52 41 44 35 22 Fibonacci Heaps: Delete Min Delete min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same rank. rank 0 1 2 3 min 7 30 24 26 46 23 17 current 18 52 39 41 44 35 link 23 into 17 23 Fibonacci Heaps: Delete Min Delete min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same rank. rank 0 1 2 3 min 7 30 24 26 46 current 17 18 23 39 52 41 44 35 link 17 into 7 24 Fibonacci Heaps: Delete Min Delete min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same rank. rank 0 1 2 3 current min 24 26 35 46 17 7 18 30 39 52 41 44 23 link 24 into 7 25 Fibonacci Heaps: Delete Min Delete min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same rank. rank 0 1 2 3 current min 26 24 17 46 23 7 18 30 39 52 41 44 35 26 Fibonacci Heaps: Delete Min Delete min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same rank. rank 0 1 2 3 current min 26 24 17 46 23 7 18 30 39 52 41 44 35 27 Fibonacci Heaps: Delete Min Delete min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same rank. rank 0 1 2 3 current min 26 24 17 46 23 7 18 30 39 52 41 44 35 28 Fibonacci Heaps: Delete Min Delete min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same rank. rank 0 1 2 3 current min 26 24 17 46 23 7 18 30 39 52 41 44 link 41 into 18 35 29 Fibonacci Heaps: Delete Min Delete min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same rank. rank 0 1 2 3 current min 7 26 24 17 46 23 30 52 18 41 39 44 35 30 Fibonacci Heaps: Delete Min Delete min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same rank. rank 0 1 2 3 current min 7 26 24 17 46 23 30 52 18 41 39 44 35 31 Fibonacci Heaps: Delete Min Delete min. Delete min; meld its children into root list; update min. Consolidate trees so that no two roots have same rank. min 7 26 24 17 46 23 52 18 41 30 39 44 stop 35 32 Fibonacci Heaps: Decrease Key Intuition for deceasing the key of node x. If heap-order is not violated, just decrease the key of x. Otherwise, cut tree rooted at x and meld into root list. To keep trees flat: as soon as a node has its second child cut, cut it off and meld into root list (and unmark it). min 7 marked node: one child already cut 35 24 17 26 46 30 88 72 23 21 18 38 39 41 52 33 Fibonacci Heaps: Decrease Key Case 1. [heap order not violated] Decrease key of x. Change heap min pointer (if necessary). min 7 26 24 17 46 29 30 23 21 18 38 39 41 52 x 35 88 72 decrease-key of x from 46 to 29 34 Fibonacci Heaps: Decrease Key Case 2a. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child). min 7 24 17 23 21 18 38 39 41 p 26 29 15 30 52 x 35 88 72 decrease-key of x from 29 to 15 35 Fibonacci Heaps: Decrease Key Case 2a. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child). x min 15 7 72 24 17 23 21 18 38 39 41 p 26 35 88 30 52 decrease-key of x from 29 to 15 36 Fibonacci Heaps: Decrease Key Case 2a. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child). x min 15 7 72 24 23 21 38 39 41 p mark parent 26 35 17 18 88 30 52 decrease-key of x from 29 to 15 37 Fibonacci Heaps: Decrease Key Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child). min 15 7 72 24 p x 35 5 26 88 17 30 23 21 18 38 39 41 52 decrease-key of x from 35 to 5 38 Fibonacci Heaps: Decrease Key Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child). min 15 7 72 24 p x 5 26 88 17 30 23 21 18 38 39 41 52 decrease-key of x from 35 to 5 39 Fibonacci Heaps: Decrease Key Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child). min 15 x 5 7 72 24 p 26 88 17 30 23 21 18 38 39 41 52 decrease-key of x from 35 to 5 40 Fibonacci Heaps: Decrease Key Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child). min 15 72 x 5 7 24 second child cut p 26 88 17 30 23 21 18 38 39 41 52 decrease-key of x from 35 to 5 41 Fibonacci Heaps: Decrease Key Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child). min 15 72 x p 5 26 88 7 24 17 30 23 21 18 38 39 41 52 decrease-key of x from 35 to 5 42 Fibonacci Heaps: Decrease Key Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child). min 15 72 x p 5 26 88 7 p' second child cut 24 17 30 23 21 18 38 39 41 52 decrease-key of x from 35 to 5 43 Fibonacci Heaps: Decrease Key Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child). min 15 72 x p p' p'' 5 26 24 7 88 don't mark parent if it's a root 17 30 23 21 18 38 39 41 52 decrease-key of x from 35 to 5 44 Analysis Summary Insert. Delete-min. Decrease-key. O(1) O(rank(H)) amortized time O(1) amortized time Key Lemma. rank(H) = O(log n). Proof in textbook 45 Using a Fibonacci Heap Prim’s algorithm Use Fibonacci heap to achieve O(m+n log n) Dijkstra’s algorithm for finding the single source shortest path tree Use Fibonacci heap to achieve O(m+n log n) 46
© Copyright 2026 Paperzz