Initializing A Max Heap. Initializing A Max Heap

Similar documents
Data Structures, Algorithms, & Applications in C++ ( Chapter 9 )

Priority Queues 9/10. Binary heaps Leftist heaps Binomial heaps Fibonacci heaps

Binary and Binomial Heaps. Disclaimer: these slides were adapted from the ones by Kevin Wayne

Heaps. Heap/Priority queue. Binomial heaps: Advanced Algorithmics (4AP) Heaps Binary heap. Binomial heap. Jaak Vilo 2009 Spring

1.6 Heap ordered trees

Heaps

PRIORITY QUEUES. binary heaps d-ary heaps binomial heaps Fibonacci heaps. Lecture slides by Kevin Wayne Copyright 2005 Pearson-Addison Wesley

Algorithms PRIORITY QUEUES. binary heaps d-ary heaps binomial heaps Fibonacci heaps. binary heaps d-ary heaps binomial heaps Fibonacci heaps

Advanced Algorithmics (4AP) Heaps

> asympt( ln( n! ), n ); n 360n n

3/7/13. Binomial Tree. Binomial Tree. Binomial Tree. Binomial Tree. Number of nodes with respect to k? N(B o ) = 1 N(B k ) = 2 N(B k-1 ) = 2 k

Design and Analysis of Algorithms 演算法設計與分析. Lecture 8 November 16, 2016 洪國寶

COSC160: Data Structures Binary Trees. Jeremy Bolton, PhD Assistant Teaching Professor

Ch 10 Trees. Introduction to Trees. Tree Representations. Binary Tree Nodes. Tree Traversals. Binary Search Trees

1 Solutions to Tute09

SET 1C Binary Trees. 2. (i) Define the height of a binary tree or subtree and also define a height balanced (AVL) tree. (2)

Design and Analysis of Algorithms 演算法設計與分析. Lecture 9 November 19, 2014 洪國寶

Binary Tree Applications

COMP Analysis of Algorithms & Data Structures

Design and Analysis of Algorithms. Lecture 9 November 20, 2013 洪國寶

Priority Queues. Fibonacci Heap

AVL Trees. The height of the left subtree can differ from the height of the right subtree by at most 1.

Successor. CS 361, Lecture 19. Tree-Successor. Outline

Fundamental Algorithms - Surprise Test

Meld(Q 1,Q 2 ) merge two sets

Design and Analysis of Algorithms

Splay Trees Goodrich, Tamassia, Dickerson Splay Trees 1

Homework #4. CMSC351 - Spring 2013 PRINT Name : Due: Thu Apr 16 th at the start of class

Priority queue. Advanced Algorithmics (6EAP) Binary heap. Heap/Priority queue. Binomial heaps: Merge two heaps.

CSCE 750, Fall 2009 Quizzes with Answers

Administration CSE 326: Data Structures

Data Structures. Binomial Heaps Fibonacci Heaps. Haim Kaplan & Uri Zwick December 2013

Chapter 16. Binary Search Trees (BSTs)

Outline for this Week

CMPSCI 311: Introduction to Algorithms Second Midterm Practice Exam SOLUTIONS

2 all subsequent nodes. 252 all subsequent nodes. 401 all subsequent nodes. 398 all subsequent nodes. 330 all subsequent nodes

1 Binomial Tree. Structural Properties:

Heap Building Bounds

PARELLIZATION OF DIJKSTRA S ALGORITHM: COMPARISON OF VARIOUS PRIORITY QUEUES

Outline for this Week

BITTIGER #11. Oct

4/8/13. Part 6. Trees (2) Outline. Balanced Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees. to maximum n. Tree A. Tree B.

Heaps. c P. Flener/IT Dept/Uppsala Univ. AD1, FP, PK II Heaps 1

The Tree Data Model. Laura Kovács

CSCI 104 B-Trees (2-3, 2-3-4) and Red/Black Trees. Mark Redekopp David Kempe

Optimal Satisficing Tree Searches

Fibonacci Heaps Y Y o o u u c c an an s s u u b b m miitt P P ro ro b blle e m m S S et et 3 3 iin n t t h h e e b b o o x x u u p p fro fro n n tt..

On the Optimality of a Family of Binary Trees Techical Report TR

COMPUTER SCIENCE 20, SPRING 2014 Homework Problems Recursive Definitions, Structural Induction, States and Invariants

Notes on Natural Logic

Outline. Objective. Previous Results Our Results Discussion Current Research. 1 Motivation. 2 Model. 3 Results

Stanford University, CS 106X Homework Assignment 5: Priority Queue Binomial Heap Optional Extension

Chapter 7 Sorting (Part II)

Binary Search Tree and AVL Trees. Binary Search Tree. Binary Search Tree. Binary Search Tree. Techniques: How does the BST works?

CSE 21 Winter 2016 Homework 6 Due: Wednesday, May 11, 2016 at 11:59pm. Instructions

Priority Queues Based on Braun Trees

9.7 Binomial Queues. This excerpt made available by permission of Robert Sedgewick, and of Pearson Education, Inc.

The potential function φ for the amortized analysis of an operation on Fibonacci heap at time (iteration) i is given by the following equation:

Supporting Information

Chapter wise Question bank

Outline for Today. Quick refresher on binomial heaps and lazy binomial heaps. An important operation in many graph algorithms.

CSE 100: TREAPS AND RANDOMIZED SEARCH TREES

Splay Trees. Splay Trees - 1

Structural Induction

Fibonacci Heaps CLRS: Chapter 20 Last Revision: 21/09/04

Lecture l(x) 1. (1) x X

UNIT VI TREES. Marks - 14

The suffix binary search tree and suffix AVL tree

Lesson 9: Heuristic Search and A* Search

Basic Data Structures. Figure 8.1 Lists, stacks, and queues. Terminology for Stacks. Terminology for Lists. Chapter 8: Data Abstractions

0/1 knapsack problem knapsack problem

Lecture 5: Tuesday, January 27, Peterson s Algorithm satisfies the No Starvation property (Theorem 1)

Q1. [?? pts] Search Traces

Smoothed Analysis of Binary Search Trees

Practical session No. 5 Trees

UNIT 2. Greedy Method GENERAL METHOD

UNIVERSITY OF OSLO. Faculty of Mathematics and Natural Sciences

CSE 417 Algorithms. Huffman Codes: An Optimal Data Compression Method

Practical session No. 5 Trees

Random Tree Method. Monte Carlo Methods in Financial Engineering

CIS 540 Fall 2009 Homework 2 Solutions

What is Greedy Approach? Control abstraction for Greedy Method. Three important activities

On the Optimality of a Family of Binary Trees

NOTES ON FIBONACCI TREES AND THEIR OPTIMALITY* YASUICHI HORIBE INTRODUCTION 1. FIBONACCI TREES

Binomial Heaps. Bryan M. Franklin

Homework solutions, Chapter 8

Information Theory and Coding Prof. S. N. Merchant Department of Electrical Engineering Indian Institute of Technology, Bombay

Recursive Trees for Practical ORAM

An effective perfect-set theorem

Outline. CSE 326: Data Structures. Priority Queues Leftist Heaps & Skew Heaps. Announcements. New Heap Operation: Merge

CEC login. Student Details Name SOLUTIONS

Today s Outline. One More Operation. Priority Queues. New Operation: Merge. Leftist Heaps. Priority Queues. Admin: Priority Queues

Introduction to Greedy Algorithms: Huffman Codes

Algorithmic Game Theory and Applications. Lecture 11: Games of Perfect Information

MSU CSE Spring 2011 Exam 2-ANSWERS

Node betweenness centrality: the definition.

It is used when neither the TX nor RX knows anything about the statistics of the source sequence at the start of the transmission

Lecture 8 Feb 16, 2017

Objec&ves. Review: Graphs. Finding Connected Components. Implemen&ng the algorithms

Rotation and Lighting Invariant Template Matching

Transcription:

Initializing A Max Heap 3 4 5 6 7 8 70 8 input array = [-,,, 3, 4, 5, 6, 7, 8,, 0, ] Initializing A Max Heap 3 4 5 6 7 8 70 8 Start at rightmost array position that has a child. Index is n/.

Initializing A Max Heap 3 4 6 7 8 70 85 Move to next lower array position. Initializing A Max Heap 3 4 6 7 8 70 85

Initializing A Max Heap 3 6 7 8 4 70 85 Initializing A Max Heap 3 6 7 8 4 70 85

Initializing A Max Heap 7 6 3 8 4 70 85 Initializing A Max Heap 7 6 3 8 4 70 85

Initializing A Max Heap 7 6 3 8 4 70 85 Find a home for. Initializing A Max Heap 7 0 6 3 8 4 8 75 Find a home for.

Initializing A Max Heap 7 0 6 3 8 4 7 85 Done, move to next lower array position. Initializing A Max Heap 7 0 6 3 8 4 7 85 Find home for.

Initializing A Max Heap 7 0 6 3 8 4 7 85 Find home for. Initializing A Max Heap 0 7 6 3 8 4 7 85 Find home for.

Initializing A Max Heap 0 7 5 6 3 8 4 7 8 Find home for. Initializing A Max Heap 0 7 5 6 3 8 4 7 8 Done.

Time Complexity 7 8 5 6 3 4 70 8 Height of heap = h. Number of subtrees with root at level j is <= j-. Time for each subtree is O(h-j+). Complexity Time for level j subtrees is <= j- (h-j+) = t(j). Total time is t() + t() + + t(h-) = O(n).

Leftist Trees Linked binary tree. Can do everything a heap can do and in the same asymptotic complexity. Can meld two leftist tree priority queues in O(log n) time. Extended Binary Trees Start with any binary tree and add an external node wherever there is an empty subtree. Result is an extended binary tree.

A Binary Tree An Extended Binary Tree number of external nodes is n+

The Function s() For any node x in an extended binary tree, let s(x) be the length of a shortest path from x to an external node in the subtree rooted at x. s() Values Example

s() Values Example 0 0 0 0 0 0 0 0 0 0 Properties Of s() If x is an external node, then s(x) = 0. Otherwise, s(x) = min {s(leftchild(x)), s(rightchild(x))} +

Height Biased Leftist Trees A binary tree is a (height biased) leftist tree iff for every internal node x, s(leftchild(x)) >= s(rightchild(x)) A Leftist Tree 0 0 0 0 0 0 0 0 0 0

Leftist Trees--Property In a leftist tree, the rightmost path is a shortest root to external node path and the length of this path is s(root). A Leftist Tree 0 0 0 0 0 0 0 0 0 0 Length of rightmost path is.

Leftist Trees Property The number of internal nodes is at least s(root) - Because levels through s(root) have no external nodes. So, s(root) <= log(n+) A Leftist Tree 0 0 0 0 0 0 0 0 0 0 Levels and have no external nodes.

Leftist Trees Property 3 Length of rightmost path is O(log n), where n is the number of nodes in a leftist tree. Follows from Properties and. Leftist Trees As Priority Queues Min leftist tree leftist tree that is a min tree. Used as a min priority queue. Max leftist tree leftist tree that is a max tree. Used as a max priority queue.

A Min Leftist Tree 4 3 6 8 5 8 6 Some Min Leftist Tree Operations put() remove() meld() initialize() put() and remove() use meld().

Put Operation put(7) 4 3 6 8 5 8 6 Put Operation put(7) 4 3 6 8 5 8 6 Create a single node min leftist tree. 7

Put Operation put(7) 4 3 6 8 5 8 6 Create a single node min leftist tree. Meld the two min leftist trees. 7 Remove Min 4 3 6 8 5 8 6

Remove Min 4 3 6 8 5 8 6 Remove the root. Remove Min 4 3 6 8 5 8 6 Remove the root. Meld the two subtrees.

Meld Two Min Leftist Trees 4 3 6 8 5 6 8 6 Traverse only the rightmost paths so as to get logarithmic performance. Meld Two Min Leftist Trees 4 3 6 8 5 6 8 6 Meld right subtree of tree with smaller root and all of other tree.

Meld Two Min Leftist Trees 4 3 6 8 5 6 8 6 Meld right subtree of tree with smaller root and all of other tree. Meld Two Min Leftist Trees 4 6 6 8 8 6 Meld right subtree of tree with smaller root and all of other tree.

Meld Two Min Leftist Trees 8 6 Meld right subtree of tree with smaller root and all of other tree. Right subtree of 6 is empty. So, result of melding right subtree of tree with smaller root and other tree is the other tree. Meld Two Min Leftist Trees 8 6 Make melded subtree right subtree of smaller root. 6 8 Swap left and right subtree if s(left) < s(right). 6 8

Meld Two Min Leftist Trees 4 6 4 6 8 6 6 8 6 8 6 8 Make melded subtree right subtree of smaller root. Swap left and right subtree if s(left) < s(right). Meld Two Min Leftist Trees 3 5 4 6 6 8 6 8 Make melded subtree right subtree of smaller root. Swap left and right subtree if s(left) < s(right).

Meld Two Min Leftist Trees 3 4 5 6 6 8 6 8 Initializing In O(n) Time create n single node min leftist trees and place them in a FIFO queue repeatedly remove two min leftist trees from the FIFO queue, meld them, and put the resulting min leftist tree into the FIFO queue the process terminates when only min leftist tree remains in the FIFO queue analysis is the same as for heap initialization