Questions from the material presented in this lecture

Similar documents
Algorithms, Spring 2014, CSE, OSU Lecture 2: Sorting

Comparisons. Θ(n 2 ) Θ(n) Sorting Revisited. So far we talked about two algorithms to sort an array of numbers. What is the advantage of merge sort?

Lecture 3: B-Trees. October Lecture 3: B-Trees

Comparisons. Heaps. Heaps. Heaps. Sorting Revisited. Heaps. So far we talked about two algorithms to sort an array of numbers

Lecture 5: Sorting Part A

Algorithms and Data Structures

CS 241 Analysis of Algorithms

Heapsort. Algorithms.

Introduction to Algorithms 3 rd edition

Data Structures and Algorithms Week 4

Heaps and Priority Queues

403: Algorithms and Data Structures. Heaps. Fall 2016 UAlbany Computer Science. Some slides borrowed by David Luebke

Basic Data Structures and Heaps

Lecture: Analysis of Algorithms (CS )

Data Structures and Algorithms Chapter 4

Data Structures and Algorithms. Roberto Sebastiani

Advanced Data Structures Summary

Chapter 6 Heap and Its Application

quiz heapsort intuition overview Is an algorithm with a worst-case time complexity in O(n) data structures and algorithms lecture 3

The Heap Data Structure

Topic: Heaps and priority queues

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

V Advanced Data Structures

V Advanced Data Structures

BM267 - Introduction to Data Structures

Partha Sarathi Manal

Heapsort. Heap data structure

18.3 Deleting a key from a B-tree

Lecture 6: Analysis of Algorithms (CS )

Properties of a heap (represented by an array A)

COSC Advanced Data Structures and Algorithm Analysis Lab 3: Heapsort

Data structures. Organize your data to support various queries using little time and/or space

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

HEAP. Michael Tsai 2017/4/25

Lecture 3. Recurrences / Heapsort

ADVANCED DATA STRUCTURES

Outline. Computer Science 331. Heap Shape. Binary Heaps. Heap Sort. Insertion Deletion. Mike Jacobson. HeapSort Priority Queues.

Data Structure and Algorithm, Spring 2017 Final Examination

Design and Analysis of Algorithms

Sorting and Searching

Algorithms and Theory of Computation. Lecture 7: Priority Queue

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Lecture: Analysis of Algorithms (CS )

Sorting and Searching

CS521 \ Notes for the Final Exam

CS102 Binary Search Trees

Advanced algorithms. binary heap, d-ary heap, binomial heap, amortized analysis, Fibonacci heap. Jiří Vyskočil, Radek Mařík 2013

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Algorithms. Deleting from Red-Black Trees B-Trees

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

Heaps, Heapsort, Priority Queues

Chapter 6 Heapsort 1

Binary Trees. Recursive definition. Is this a binary tree?

A data structure and associated algorithms, NOT GARBAGE COLLECTION

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

Data Structures Week #6. Special Trees

Augmenting Data Structures

Programming II (CS300)

CS301 - Data Structures Glossary By

Data Structures Week #6. Special Trees

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

CS711008Z Algorithm Design and Analysis

CS711008Z Algorithm Design and Analysis

l Heaps very popular abstract data structure, where each object has a key value (the priority), and the operations are:

Binary Heaps in Dynamic Arrays

Chapter 5 Data Structures Algorithm Theory WS 2016/17 Fabian Kuhn

CSCI Trees. Mark Redekopp David Kempe

Total Points: 60. Duration: 1hr

W4231: Analysis of Algorithms

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Advanced Data Structures. Summary. January 9, Remember that binary search trees are binary trees such that:

l So unlike the search trees, there are neither arbitrary find operations nor arbitrary delete operations possible.

Binary heaps (chapters ) Leftist heaps

Basic Data Structures (Version 7) Name:

Data Structures Week #6. Special Trees

CMPS 2200 Fall 2017 Red-black trees Carola Wenk

CS 310 Advanced Data Structures and Algorithms

Algorithm Design (8) Graph Algorithms 1/2

1 Heaps. 1.1 What is a heap? 1.2 Representing a Heap. CS 124 Section #2 Heaps and Graphs 2/5/18

Lower Bound on Comparison-based Sorting

Algorithms. AVL Tree

Department of Computer Applications. MCA 312: Design and Analysis of Algorithms. [Part I : Medium Answer Type Questions] UNIT I

Sorting. Popular algorithms: Many algorithms for sorting in parallel also exist.

INF2220: algorithms and data structures Series 1

Algorithms and Data Structures for Mathematicians

Data Structures and Algorithms for Engineers

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

O(n): printing a list of n items to the screen, looking at each item once.

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Problem Set 5 Solutions

Amortized Analysis. Andreas Klappenecker. [partially based on the slides of Prof. Welch]

Next. 1. Covered basics of a simple design technique (Divideand-conquer) 2. Next, more sorting algorithms.

8. Write an example for expression tree. [A/M 10] (A+B)*((C-D)/(E^F))

Binary Search Trees, etc.

Lecture 13: AVL Trees and Binary Heaps

Week 10. Sorting. 1 Binary heaps. 2 Heapification. 3 Building a heap 4 HEAP-SORT. 5 Priority queues 6 QUICK-SORT. 7 Analysing QUICK-SORT.

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Transcription:

Advanced Data Structures Questions from the material presented in this lecture January 8, 2015 This material illustrates the kind of exercises and questions you may get at the final colloqium. L1. Introduction. Structures for graphs You should be able to indicate The main data structures for graphs, Algorithms to test connectivity in graphs represented by these data structures, What is the definition of matrix A of a graph with adjacency matrix A? How can we compute A iteratively and recursively? The runtime complexity for an algorithm that computes A? The purpose of Warshall s algorithm the pseudo-code and its runtime complexity The runtime complexity of various algorithms. Typical questions 1. What is the worst-case running time for the following procedure as a function of n: procedure mystery(n : integer) var i, j, k : integer begin for i := 1 to n do for j := i + 1 to n do for k := 1 to j do print (i + j k) end 1

Answer: The runtime complexity of this function is proportional to the number of executions of the print operation, which is n n i=1 j=i+1 k=1 j 1 n n i=1 j=1 k=1 n 1 = n 3. Thus, the runtime complexity of this procedure is O(n 3 ). 2. Let A[1..n] be an array of floating point values. (a) Define a procedure amplitude(a) which computes the maximum difference between two elements of the array A. (b) Give, using the big oh notation, the worst-case running time T (n) of the procedure defined by you. Answer: Note that the desired value is the difference between the maximum and minimum element of the given array, thus it can be computed with the following pseudocode: float amplitude(float A[1..n]) float m, M; m = A[1]; M = A[1]; i = 2; while i <= n if A[i] < m then m = A[i] if A[i] > M then M = A[i] i = i + 1 return M m T (n) is proportional to the number of times we execute the while loop, which is n. Thus T (n) = O(n). 3. Consider list elements with the following structure (in C++): struct node { int data; node* next; Write down the (pseudo)code for the function node* inverse(node* x) which takes as input a pointer x to the first element of a simply linked list with n elements, and does the following: It inverts the list in-place, by changing the next pointers of its nodes. It returns a pointer y to the first element of the reverted list. 2

x... k 1 k 2 k n... k 1 k 2 k n y Answer: The inversion can be carried out in one traversal of the list from left to right. At every step of the traversal, we maintain 2 pointers: y is a pointer to the node which is currently visited. p is a pointer to the node before y, which is the head of the list reverted so far. Initially, y = x, and p is the null pointer. node* inverse(node* x) { node* y, p, tmp; y = x; p = 0; while(y->next!= 0) { tmp = y->next; y->next = p; p = y; y = tmp; // y->next is null here y->next = p; return y; The runtime complexity of this implementation is proportional with the number of executions of the while loop, which is the length of the list with head x. Thus, the runtime complexity is O(n). L2: Single-source shortest paths This lecture was about Dijkstra algorithm and about Bellman-Ford algorithm. Indicate the pseudocode an the runtime complexity of these algorithms. Indicate the differences between these algorithms. What is the purpose of the relaxation method? Indicate its psudocode and he meaning of the auxiliary data structures on which it acts. Illustrate the outcome of a while loop of Dijkstra s algorithm on some particular configuration of a weighted graph. For example, assume the following digraph with upper bounds d[v] and predecessors π[v] of nodes as shown in the figure: 3

3 d:3 u π:s d: v π:s d:0 s π:undef 5 5 1 d: x π:s 4 3 2 7 d: y π:s Draw the outcome of relaxing all edges which leave node u. L3: Red-black trees 1. What is a balanced red-black tree? Which are the red-black properties that must be satisfied? 2. What operations are well supported by Red-Black trees, and what is their runtime complexity? 3. The operations LeftRotate(y) and RightRotate(x) transform a node in an RB-tree as depicted in the following picture: y RightRotate(y) x x γ LeftRotate(x) α y α β β γ where the nodes referred by x and y may appear anywhere in the red-black tree T. Assume a node of an RB-tree is an instance of the C++ class enum rbcolor { RED, BLACK ; class rbnode { public: int key; rbnode *left; // pointer to left child rbnode *right; // pointer to right child rbnode *p; // pointer to parent rbnode color; // node color (a) Write down the C++ code for the method rbtree* leftrotate(rbtree *x) (Note that in this case, we must have y=x.right) (b) Write down the C++ code for the method 4

rbtree* rightrotate(rbtree *y) Answer: (Note that in this case, we must have x=y.left) rbnode* rightrotate(rbnode* y) { rbnode* x = y->left; rbnode *alpha = x->left, *beta = x->right, *gamma = y->right; x->p = y->p; y->p = x; x->left = alpha; y->left = beta; y->right = gamma; // reset the parent of gamma, if gamma!= null if (beta!= null) beta->p = y; return x; The method leftrotate can be defined similarly. 4. Consider the following trees: 10 7 12 10 7 12 10 7 12 3 3 8 3 1 (a) (b) (c) 14 10 1 1 7 12 15 15 12 3 2 (d) (e) 1. Which trees satisfy the red-black properties and which do not? For the trees which do not satisfy the red-black properties, indicate which are not satisfied. 5

2. For the trees which satisfy the red-black properties, indicate the black-height of their root. L4: B-trees 1. Give the formal definition of a B-tree with minimum degree t. Answer: A B-tree with minimum degree t is a rooted tree whose structure can be described in C++ as follows: The nodes are structures of the following kind: struct node { int n; // the number of keys currently stored in this node int key[2 t 1]; // array that stores the keys in this node bool leaf; // true if this node has no children, and false otherwise node* c[2 t]; // array of pointers to its children The following properties must be satisfied by every node x: If x is a non-leaf node with n keys (that is, x.n==n and x.leaf ==false), then it has n+1 children and k 0 x.key[0] k 1 x.key[1]... x.key[n 1] k n whenever k i is any key stored in the subtree pointed to by x.c[i]. All leaves have the same height h, which is the height of the tree. If x is not the root of the tree, then x.n t 1. (Every node other than the root must have at least t 1 keys) x.n 2 t 1. (Every node other than the root must have at most 2 t 1 keys) 2. What operations can be performed efficiently with B-trees and RB-trees? What is the complexity of these operations? Answer: RB-trees are data structures design to support the efficient execution of the following operations on a dynamic set S with n elements: Search(S, k): returns a pointer p to an element in S such that p x = k, or Nil if no such element exists. Minimum(S): returns a pointer to the element of S whose key is minimum, or Nil is S has no elements. Maximum(S): returns a pointer to the element of S whose key is maximum, or Nil is S has no elements. Successor(S, x): returns the next element larger than x in S, or Nil if x is the maximum element. Predecessor(S, x): returns the next element smaller than x in S, or Nil if x is the minimum element.

Insert(S, p): augment S with the element pointed to by p. If p is Nil, do nothing. Delete(S, p): remove the element pointed to by p from S. If p is Nil, do nothing. The runtime complexity of all these operations is O(log n). B-trees are data structures designed to support the efficient execution of the following operations: Search(x, k): searches in the tree with root pointed by x, for the existence of a node that contains key k. Runtime complexity is O(t log t n). Create(): creates an empty tree. Runtime complexity is O(1). Insert(T, k) : insert key k in tree with root x. Runtime complexity is O(t log t n). Delete(x, k): delete key k from tree with root x. Runtime complexity is O(t log t n). 3. Draw a B-tree with minimal degree t = 3 whose nodes contain the keys 1, 2, 3, 4, 5,, 8, 9, 11, 1, 24. Answer: First, note that a node of such a B-tree can contain at most 2 t 1 = 5 keys, and all nodes except the root must contain at least t 1 = 2 keys. For example, we can have 1 2 3 4 5 8 9 11 1 24 4. What is the minimum number of nodes in a B-tree with height 4 and minimum degree t = 10? What is the maximum number of nodes in a B-tree with height 4 and minimum degree t = 10? Answer: A B-tree with minimum degree t = 10 and depth 4 has minimum number of nodes if the root has 2 children, and the nodes at depths 1, 2, and 3 have minimum number of children. The minimum number of children of a non-root non-leaf node is t = 10. Thus the minimum number of nodes in such a B-tree is 1 + 2 1 + 2 t + 2 t 2 + 2 t 3 = 2223 where every i-th summand represents the number of nodes at depth i in the B-tree. Note also that the minimal number of keys stored in such a minimal B-tree can be computed as follows: 1 + 2 (t 1) + 2 t (t 1) + 2 t 2 (t 1) + 2 t 3 (t 1) = 19999 7

A B-tree with minimum degree t = 10 and depth 4 has maximum number of nodes if the nodes at depths 0, 1, 2, and 3 have maximum number of children, that is, 2 t = 20. Thus the maximum number of nodes in such a B-tree is 1 + 2 t + (2 t) 2 + (2 t) 3 + (2 t) 4 = 18421 where every i-th summand represents the number of nodes at depth i in the B-tree. Note also that the maximum number of keys stored on such a maximal subtree is 18421 (2 t 1) = 3199999, and this happens when every node contains 2 t 1 = 19 keys. 5. Explain how to search for a node with key k in a B-tree. What is the runtime complexity of this operation?. The operation B-Tree-Split-Child(x, i, y) is sometimes performed in a B-tree T with minimum degree t, when x is a nonfull internal node of T (that is, x contains less than 2 t 1 keys), and i is an index such that y is the i + 1-th child of x, and y is a full node (that is, y contains 2 t 1 keys). In this case, the median key of y is moved into the proper place in x, and the remaining keys of y are split in 2 halves to be stored in 2 new children of x. Illustrate the structure of the B-tree after performing B- Tree-Split-Child(x, 2, y) for the nodes x and y depicted below, where t = 4: x x->key[0] x->key[1] x->key[2] 5 14 23 x->key[3]...... y 12 17 18 19 20 21 22...... L5: Binary heaps 1. What is a binary heap? How to compute the parent, left child, and right child of a node with index i in a binary heap? Write down the heap property of binary heap. Answer: A binary heap is a data structure made of an array A of objects together with two special attributes: (a) A.length: the length, or capacity, of the array. (b) A.heap size: the actual number of elements in the heap 8

such that A.heap size A.length and the heap elements are stored, from left to right, in the array A. Such a binary head represents a complete binary array with A.heap size nodes, in which The parent of the element stored { in A[i] is the element stored in (i 1)/2 if i 0 A[parent(i)] where parent(i) = 1 if i = 0 The left child of the element stored in A[i] is the element stored in A[left(i)] where left(i) = 2 i + 1. The right child of the element stored in A[i] is the element stored in A[right(i)] where right(i) = 2 i + 2. and must satisfy the following property (called the heap property) for all i 0: A[parent(i)] A[i]. Intuitively, this means that, in the binary tree representation of the heap, the key of any node is larger than the keys of its children. 2. What operations can be performed efficiently with binary heaps? What is the complexity of these operations? 3. You should be able to write down the array representation of a binary heap depicted as a binary array, and vice versa. 4. Is the following binary tree a valid representation of a binary heap? 0 42 10 5 40 9 8 3 2 Explain your answer. Answer: No, because the heap property is not satisfied: the key of the node with index 0 is smaller then the keys of its children. 5. The operation Heapify(A, i) is defined for an array A and an index i with the following properties: the subtrees rooted at left(i) and right(i) are binary heaps, the subtree rooted at i may not be a binary heap, and it rearranges the elements of A until it becomes a binary heap. Illustrate the binary heap produced by Heapify(A, i) for the array depicted below and i = 0. 9

0 1 0 2 3 42 4 5 10 7 5 8 40 9 8 3 2 Answer: The left and right subtrees of node with index 0 are binary heaps, but A is not a binary heap. Heapify(A, 0) will push the key of the node with index 0 downwards until the tree becomes a binomial heap: 0 1 42 2 3 40 4 5 10 7 5 8 0 9 8 3 2. Explain how can we use binary heaps and Heapify() to sort an array A with n elements in O(n log 2 n) time. Answer: First, the elements of the array A are inserted, one by one, in a binary heap with heap size = length = n. We name this operation BuildHeap(A). This operation takes O(n) time. Afterwards, we repeatedly swap the maximum element A[0] with the last element A[heap size 1], reduce the heap size by 1, and ensure that the shrinked array is a binary heap by calling Heapify(A, 0). When the heap size becomes 1, A will contain the elements in increasing order. The pseudo-code of this algorithm is HeapSort(A) 1 BuildHeap(A) 2 for i := A.length 1 downto 1 3 exchange A[0] A[i] 4 A.heap size := A.heap size 1 5 Heapify(A, 0) 7. Illustrate how HeapSort performs the sorting of the elements of the array A with element values A[0] = 7, A[1] = 2, A[2] = 1, A[3] = 9, A[4] =. Answer: First, BuildHeap(A) constructs a binary heap by successively inserting the elements 7,2,1,9, into an empty heap. The initial binary heap, and its evolution during shrinking are depicted below: 9 7 1 2 7 1 2 9 2 1 7 9 2 1 7 9 1 2 7 9 10

L: Binomial heaps 1. What is a heap-ordered binomial tree? Illustrate a heap-ordered binomial tree with depth 3, and indicate some remarkable properties of binomial trees. Answer: A binomial tree is an element of the set of ordered trees {B k k N defined recursively as follows: B 0 consists of a single node. B k consists of two binomial trees B k 1 that are linked such that one of them has the other one as left child of its root. A binomial tree is heap-ordered if the key of any node is greater than or equal to the key of its parent. The following is a heap-ordered binomial tree with depth 3: 8 14 7 11 17 38 27 Remarkable properties of binomial trees: For every binomial tree B k the following hold: (a) It has 2 k nodes. (b) It has height k. (c) There are exactly ( k i) nodes at depth i for i = 0, 1,..., k. (d) The root has degree k, which is greater than that of any other node. Moreover, if the children of the root are numbered from left to right by k 1, k 2,..., 0, then child i is the root of a subtree B i. 2. What is a binomial heap? Which operations are well supported by a binomial heap? 3. Write down a suitable C++ class for the implementation of a node in a binomial heap. Explain the meaning of every field in the class. Answer: struct bnode { bnode *p; // pointer to parent node bnode *child; // pointer to the left child of the node bnode *sibling; // pointer to the right sibling of the node int key; // the key of the node int degree; // the number of children of the node 11

4. Draw a binomial heap with 11 elements and the binomial heap resulted of removing its minimum element. Answer: Te root list of a binomial heap consists of binomial trees with different number of nodes, and every binomial tree contains 2 k nodes for some k N. Note that 11 = 1 + 2 + 8, thus we can have a binomial heap made of 3 binomial trees: the first with one element, the second with 2 elements, and the third with 8 elements. For example: H.head 10 1 25 8 12 29 11 17 38 1 5. The following diagram does not represent a binomial heap. Indicate the binomial heap conditions which are fulfilled, and those which are violated. H.head 25 12 4 90 3 37 10 8 14 29 42 1 28 13 11 17 38 2 23 77 27 Answer: The second tree from the root list from is not heap-ordered: the node with key 12 has a child with a smaller key.. Consider the following C++ class definition to represent nodes in a binomial heap: class bnode { public: bnode *p; // pointer to the parent bnode *child; // pointer to the left child bnode *sibling; // pointer to the right sibling int key; // key value int degree; // number of children (a) Define the method bool isbtree(bnode* t, int n) {... which returns true if t is a pointer to the root of a binomial tree B n, and false otherwise. 12

(b) Define the C++ method Answer bool isheapordered(bnode* t, int n) {... which returns true if t is a pointer to the root of a heap-ordered binomial tree B n, and false otherwise. (a) bool isbtree(bnode* t, int n) { if (t == 0) // t should be non-null return false; bnode* tmp = t->child; for (int i = n-1; i>=0,i--) { // check if the children of node t are B n 1,..., B 1, B 0 if (!isbtree(tmp,i)) return false; tmp = tmp->sibling; // tmp should be null here return (tmp == 0); In the worst case, this procedure is called recursively for every node in the binomial tree, thus its runtime complexity is O(N), where N is the number of nodes in the binomial tree. (b) bool isheapordered(bnode* t, int n) { if (t == 0) // t should be non-null return false; bnode* tmp = t->child; for (int i = n-1; i>=0,i--) { // check if the children of node t are B n 1,..., B 1, B 0 // and that their keys are larger than the key of t if (!(isheapordered(tmp,i) && (t->key <= tmp->key))) return false; tmp = tmp->sibling; // tmp should be null here return (tmp == 0); In the worst case, this procedure is called recursively for every node in the binomial tree, thus its runtime complexity is O(N), where N is the number of nodes in the binomial tree. 7. Suppose a binomial heap is represented by an instance of the class struct bheap { bnode* head; 13

(a) Define the C++ method bool isbheap(bheap* h) {... which returns true if h is an instance of a binomial heap, and false otherwise. (Note: Check that the root list of h consists of binomial trees which are heap-ordered.) (b) Define the C++ method bnode* getminimum(bheap* h) which returns a pointer to the node with minimum key in the binary heap h. (c) Define the C++ method bnode *btreemerge(btree* t1, btree* b2) which merges 2 heap-ordered binomial trees t 1 and t 2 from B n and produces a heap-ordered binomial tree from B n+1. That is the time complexity of this operation? (Note: either t 1 becomes the left child of the root of t 2, or t 2 becomes the left child of the root of t 1. The distinction is made by comparing the keys of t 1 and t 2.) L7: Fibonacci heaps and amortized analysis 1. What is a Fibonacci heap? What operations can be performed efficiently with Fibonacci heaps? 2. What is an unordered binomial tree? Indicate some interesting properties of the set U n of unordered binomial trees. 3. Draw a Fibonacci heap with 7 elements, and the Fibonacci heap that results after removing its minimum element. 4. Write down a suitable C++ class for the implementation of a node in a Fibonacci heap. Explain the meaning of every field in the class. 5. What conditions must be fulfilled by an unordered heap-ordered tree in a Fibonacci heap?. What are the differences between a binomial heap and a Fibonacci heap? 7. The following diagram does not represent a Fibonacci heap. Indicate which conditions of a Fibonacci heap are fulfilled, and which conditions are violated. 14

H.min 23 17 3 0 30 18 21 33 4 19 40 8. Consider the following C++ class to describe nodes in a Fibonacci heap: class unode { public: bnode *p; // pointer to the parent; 0 if it has no parent bnode *child; // pointer to a child int degree; // number of children bool mark; // a boolean value int key; // key value bnode *left; // pointer to the left sibling bnode *right; // pointer to the right sibling and the following class for the representation of Fibonacci heaps: class fibheap { public: bnode *min; // pointer to the root of tree in the Fibonacci heap int n; // number of nodes in the heap Define the method int rootlistlength(fibheap h) {... which computes the length of the root list of the Fibonacci heap h. Amortized analysis 1. What is amortized analysis? Indicate 3 common techniques for amortized analysis. 2. Assume A is an 8-bit counter with A[7] = 0. The number stored in A is x := 7 k=0 A[k] 2k. (a) For what value of x is the number of bit flips of Increment(A) maximum? (b) For what values of x is the number of bit flips of Increment(A) minimum? 15

3. Consider the data structure consisting of a bit counter whose bits are initially all 1, together with the Decrement operation that decrements the value of the bit counter by 1. (a) Write down the pseudocode for Decrement. (b) Compute the amortized cost of Decrement with the Aggregate method. 4. What is the average number of bit flips of A[i] in a sequence of n Increment operations of a bit counter whose bits are initially all 0? 1