Mon Tue Wed Thurs Fri

Similar documents
Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes.

Fibonacci (Min-)Heap. (I draw dashed lines in place of of circular lists.) 1 / 17

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

Fibonacci Heaps These lecture slides are adapted from CLRS, Chapter 20.

Algorithms Theory. 08 Fibonacci Heaps

2-INF-237 Vybrané partie z dátových štruktúr 2-INF-237 Selected Topics in Data Structures

L feb abhi shelat

25. Minimum Spanning Trees

25. Minimum Spanning Trees

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm

searching algorithms

CSE548, AMS542: Analysis of Algorithms, Fall 2017 Date: Oct 26. Homework #2. ( Due: Nov 8 )

FIBONACCI HEAPS. preliminaries insert extract the minimum decrease key bounding the rank meld and delete. Copyright 2013 Kevin Wayne

Heaps and Priority Queues

INF2220: algorithms and data structures Series 1

ENS Lyon Camp. Day 2. Basic group. Cartesian Tree. 26 October

past balancing schemes require maintenance of balance info at all times, are aggresive use work of searches to pay for work of rebalancing

Graphs and Trees Binary Search Trees AVL-Trees (a,b)-trees Splay-Trees. Search Trees. Tobias Lieber. April 14, 2008

Ordered Dictionary & Binary Search Tree

Part IA Algorithms Notes

Another way of saying this is that amortized analysis guarantees the average case performance of each operation in the worst case.

CS213d Data Structures and Algorithms

Binary Search Trees. Lecture 29 Section Robb T. Koether. Hampden-Sydney College. Fri, Apr 8, 2016

CS Data Structures and Algorithm Analysis

Fundamental Algorithms

Shortest Path Algorithms

Dijkstra s Single Source Shortest Path Algorithm. Andreas Klappenecker

AMORTIZED ANALYSIS. binary counter multipop stack dynamic table. Lecture slides by Kevin Wayne. Last updated on 1/24/17 11:31 AM

Algorithm Design and Analysis

CPSC 320 Sample Final Examination December 2013

Slides credited from Hsueh-I Lu & Hsu-Chun Hsiao

CS781 Lecture 3 January 27, 2011

Priority queues implemented via heaps

Data Structures 1 NTIN066

CSE 202: Design and Analysis of Algorithms Lecture 3

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

CS 4407 Algorithms Lecture: Shortest Path Algorithms

CS 580: Algorithm Design and Analysis

Binary Search Trees. Motivation

Analysis of Algorithms. Outline. Single Source Shortest Path. Andres Mendez-Vazquez. November 9, Notes. Notes

Weak Heaps and Friends: Recent Developments

Greedy Algorithms and Data Compression. Curs 2018

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

Weight-balanced Binary Search Trees

9 Union Find. 9 Union Find. List Implementation. 9 Union Find. Union Find Data Structure P: Maintains a partition of disjoint sets over elements.

Data Structures 1 NTIN066

B- TREE. Michael Tsai 2017/06/06

Assignment 5: Solutions

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

Greedy Alg: Huffman abhi shelat

Lecture VI AMORTIZATION

Each internal node v with d(v) children stores d 1 keys. k i 1 < key in i-th sub-tree k i, where we use k 0 = and k d =.

Weight-balanced Binary Search Trees

Design and Analysis of Algorithms

Algorithm Design and Analysis

Matching Residents to Hospitals

CS 161: Design and Analysis of Algorithms

Disjoint-Set Forests

Outline for Today. Static Optimality. Splay Trees. Properties of Splay Trees. Dynamic Optimality (ITA) Balanced BSTs aren't necessarily optimal!

Lower Bounds for Dynamic Connectivity (2004; Pǎtraşcu, Demaine)

CS 240 Data Structures and Data Management. Module 4: Dictionaries

Introduction to Algorithms

(v,w) disconnected? bridge? articulatiion point? Update MST during insertion and deletion

Introduction to Algorithms

Objec&ves. Review. Data structure: Heaps Data structure: Graphs. What is a priority queue? What is a heap?

CS 410/584, Algorithm Design & Analysis, Lecture Notes 4

Slides for CIS 675. Huffman Encoding, 1. Huffman Encoding, 2. Huffman Encoding, 3. Encoding 1. DPV Chapter 5, Part 2. Encoding 2

Splay Trees. CMSC 420: Lecture 8

Discrete Event Simulation. Motive

A Simple Implementation Technique for Priority Search Queues

Review Of Topics. Review: Induction

Chapter 4. Greedy Algorithms. Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Shortest Paths. CS 320, Fall Dr. Geri Georg, Instructor 320 ShortestPaths 3

Algorithm Design and Analysis (NTU CSIE, Fall 2017) Homework #3. Homework #3. Due Time: 2017/12/14 (Thu.) 17:20 Contact TAs:

Notes on Logarithmic Lower Bounds in the Cell Probe Model

CS60007 Algorithm Design and Analysis 2018 Assignment 1

Single Source Shortest Paths

Introduction to Algorithms

CS 253: Algorithms. Chapter 24. Shortest Paths. Credit: Dr. George Bebis

4.8 Huffman Codes. These lecture slides are supplied by Mathijs de Weerd

Advanced Implementations of Tables: Balanced Search Trees and Hashing

Amortized analysis. Amortized analysis

Divide-and-Conquer Algorithms Part Two

Computing Connected Components Given a graph G = (V; E) compute the connected components of G. Connected-Components(G) 1 for each vertex v 2 V [G] 2 d

Undirected Graphs. V = { 1, 2, 3, 4, 5, 6, 7, 8 } E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 } n = 8 m = 11

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Design and Analysis of Algorithms

CS 466/666 Course Notes

Functional Data Structures

Algorithm Design CS 515 Fall 2015 Sample Final Exam Solutions

BFS Dijkstra. Oct abhi shelat

Algorithms and Theory of Computation. Lecture 2: Big-O Notation Graph Algorithms

IS 709/809: Computational Methods in IS Research Fall Exam Review

A Tight Lower Bound for Top-Down Skew Heaps

Design of discrete-event simulations

! Insert. ! Remove largest. ! Copy. ! Create. ! Destroy. ! Test if empty. ! Fraud detection: isolate $$ transactions.

Verified Analysis of Functional Data Structures

Dictionary: an abstract data type

1 Introduction A priority queue is a data structure that maintains a set of elements and supports operations insert, decrease-key, and extract-min. Pr

CMPS 6610 Fall 2018 Shortest Paths Carola Wenk

Transcription:

In lieu of recitations 320 Office Hours Mon Tue Wed Thurs Fri 8 Cole 9 Dr. Georg Dr. Georg 10 Dr. Georg/ Jim 11 Ali Jim 12 Cole Ali 1 Cole/ Shannon Ali 2 Shannon 3 Dr. Georg Dr. Georg Jim 4 Upcoming Check Progress page, Piazza postings for updates Topological sort program due Oct 28 MST Canvas Quiz due 11:59pm Oct 29 320 Graphs&GraphSearches 1

Prim(G,r) foreach (u V) a[u] ; u. = a[r]= 0 min priority queue Q = {} foreach (u V) insert u onto Q (key: a value) set S {} while (Q is not empty) { u extract min element from Q S S {u} foreach (edge e = (u, v) incident to u) if ((v S) and (c e < a[v])) decrease priority a[v] to c e v. = u 320 MST 2

Complexity and Data Structures Prim s: V = n and E = m m reduce key operations n extract min operations With a binary heap: O(m lg n + n lg n) = O(m lg n) As the graph gets dense, m n 2 : O(n 2 lg n) Flat array: O(1) for reduce key, O(n) for extract min O(m + n 2 ) = O(n 2 ) Trade off point? m lg n = n 2, m= n 2 /lg n, m > n 2 /lg n flat array is better than bin heap 320 MST 3

A complete binary tree Has the heap property: Binary Heaps all nodes have a lower value than the nodes in their subtrees. We insert new nodes to maintain a complete binary tree, then swap nodes as necessary to restore the heap property. 320 MST 4

Example 320 MST 5

Example 320 MST 6

Fibonacci Heap Mergeable Heap supports 5 operations: Make Heap, Insert, Minimum, Extract Min, Union Fibonacci heaps also support: Decrease Key, Delete Operations run in constant amortized time: Lazy delay work as long as possible so that time over multiple operations is minimized 320 MST 7

Binary, Fibonacci Heap Complexities 320 MST 8

Good when: Trade offs number of Extract Min and Delete small compared to other ops dense graphs: lots of Decrease Key ops Downsides: large constants and complexity! Conclusions: binary heaps usually better theoretically interesting 320 MST 9

Terminology 1 Fibonacci heap H: forest of min heap ordered trees t(h): number of trees in H m(h): number of marked nodes in H Minimum node H.min: pointer to the root of a tree with the minimum key Node degree (or rank): number of children; max degree D(n) in n node Fibonacci heap is O(lg n) Node size: num nodes (including x) in subtree rooted at x Node mark: Boolean whether node has lost a child since the last time it was made the child of another node 320 MST 10

Side Track The Golden Ratio = (1 + 5)/2: positive root of x 2 = x + 1 divide a line into 2 parts: longer / smaller = whole / longer Fibonacci numbers: ratio close to Golden ratio, gets closer with larger nums Golden rectangle Golden spiral: adjacent squares of Fibonacci dimensions 320 MST 11

Terminology 2 Where does the Fibonacci come from? k= x.degree, s k is minimum possible size of any node of degree k s k F k+2 where F k+2 = (1 + 0 k = 0 And F i = 1 k = 1 F i 1 + F i 2 k 2 ) = k 320 MST 12

Terminology 3 Potential: method to analyze op complexity Potential function: Φ(H) = t(h) + 2m(H) A unit of potential can pay for a constant amount of work, where the constant is large enough to cover the cost of any specific constant time pieces of work we need to do 320 MST 13

Structure x.left x/ 23 y/ 7 H.min z/ 3 w.right root list w/ 17 t.child t/ 8 v/ 25 v.p r/ 14 u/ 12 child list Maximum degree (num children) for any node in n node heap D(n): O(lg n) Circular, doubly linked lists for root, child lists insert, remove, concatenate: O(1). Only 1 child pointer, to any of the children. t(h) = num root list nodes 320 MST 14

Where is the laziness? 1. Inserting nodes! Nodes just get inserted into the root list next to H.min. So this list can get pretty big. 2. Decreasing a key; if the key becomes less than the parent key it gets added to the root list. If the parent isn t in the root list, it either gets marked (because it lost a child) or if it was already marked it gets added to the root list and it s parent is considered. Again, the root list can get pretty big. 320 MST 15

Where is restructuring work done? When we extract the minimum! Nodes in the root list are consolidated so that we end up with D(H.n) = O(lg n) tree roots in the root list, one for each degree up to lg n: 320 MST 16

Example MAKE HEAP: H.n = 0; H.min = NIL; t(h) = 0; m(h) = 0; Φ(H) = 0 so amortized cost = actual cost = O(1) 320 MST 17

INSERT(H, x 1 ): x 1.degree = 0; x 1.p = NIL; x 1.child = NIL; x 1.mark = FALSE; H.n = H.n +1; create a root list with x 1 (set right and left pointers to x 1 ); if H.min == then H.min = x 1 Creates H, so increase in Φ is: ((t(h) + 1 + 2m(H)) (t(h) + 2m(H)) = 1; add to actual cost to get amortized cost O(1) + 1 = O(1) 320 MST 18

INSERT(H, x 2 ): x 2.degree = 0; x 2.p = NIL; x 2.child = NIL; x 2.mark = FALSE; H.n = H.n +1; insert x 2 into root list; if x 2.key < H.min.key then H.min = x 2 H.min 320 MST 19

INSERT(H, x 3 ): x 3.degree = 0; x 3.p = NIL; x 3.child = NIL; x 3.mark = FALSE; H.n = H.n +1; insert x 3 into root list; ; if x 3.key < H.min.key then H.min = x 3 H.min 320 MST 20

INSERT(H, x 4 ): x 4.degree = 0; x 4.p = NIL; x 4.child = NIL; x 4.mark = FALSE; H.n = H.n +1; insert x 4 into root list; if x 4.key < H.min.key then H.min = x 4 H.min 320 MST 21

EXTRACT MIN(H): z= H.min; H.min = z.right; CONSOLIDATE(H); H.n = H.n 1; return z H.min z 320 MST 22

EXTRACT MIN(H): z= H.min; H.min = z.right; CONSOLIDATE(H); H.n = H.n 1; return z H.min z 320 MST 23

CONSOLIDATE(H): create A[0..1] = [, ] (size is D(H.n) = log 3) Start at H.min and set A[H.min.degree] to point to it. Go right and either set another A[] or use FIB HEAP LINK to join the trees (same degree). Move H.min if needed. H.min A[0] 320 MST 24

FIB HEAP LINK (H, y, x): Remove y from root list of H; make y a child of x; increment x.degree; y.mark = FALSE A[0] H.min A[1] 320 MST 25

DECREASE KEY (H, x,k): If k < x.key then set x.key to k; y = x.p; if y.key larger, CUT(H,x,y); CASCADING CUT(H,y) ; fix H.min if needed H.min 320 MST 26

DECREASE KEY (H, x,k): If k < x.key then set x.key to k; y = x.p; if y.key larger: CUT(H,x,y); CASCADING CUT(H,y) fix H.min if needed CUT: remove x from child list of y; decrement y.degree; add x to root list; x.p = ; x.mark = FALSE H.min 320 MST 27

DECREASE KEY (H, x,k): If k < x.key then set x.key to k; y = x.p; if y.key larger, CUT(H,x,y); CASCADING CUT(H,y); fix H.min if needed CASCADING CUT: z = y.p; if z : set mark if not set & otherwise: CUT (H,y,z); CASCADING CUT (H,z) H.min 320 MST 28

Prim(G,r) foreach (u V) a[u] ; u. = a[r]= 0 min priority queue Q = {} foreach (u V) insert u onto Q (key: a value) set S {} while (Q is not empty) { u extract min element from Q S S {u} foreach (edge e = (u, v) incident to u) if ((v S) and (c e < a[v])) decrease priority a[v] to c e v. = u 320 MST 29

Muddy City 2 Prim s k g 2 4 3 4 a 5 3 f 5 4 4 4 h 2 i 3 3 e 3 2 b 4 3 4 3 d 2 c 320 MST 30

min priority queue Q = {} foreach (u V) insert u in Q set S {} while (Q is not empty) { u Q min element(delete) S S {u} foreach edge e = (u, v) if ((v S) and (c e <a[v])) a[v] = c e v. = u Using Fibonacci Heaps k g 2 5 a 3 4 f 5 4 4 4 3 h 4 2 3 3 i 3 e 2 b 4 3 4 3 d 2 c a/ 0 k/ k/ k/ k/ k/ k/ k/ k/ k/ 320 MST 31

Extract Min/Consolidate 1 320 MST 32

Extract Min/Consolidate 2 320 MST 33

Decrease Key 320 MST 34

Image Credits bean: http://clipart library.com/bean people.html goldenratio: https://www.livescience.com/37704 phi golden ratio.html Fibonacci Heap diagrams from Cormen, et. al., Introduction to Algorithms, 3 rd Edition, 2009, MIT 320 MST 35