1 CSC 421: Algorithm Design Analysis Spring 2013 Transform & conquer  transform-and-conquer approach  presorting  balanced search trees, heaps  Horner's.

Slides:



Advertisements
Similar presentations
Transform and Conquer Chapter 6. Transform and Conquer Solve problem by transforming into: a more convenient instance of the same problem (instance simplification)
Advertisements

Transform & Conquer Replacing One Problem With Another Saadiq Moolla.
Chapter 6: Transform and Conquer
Design and Analysis of Algorithms - Chapter 61 Transform and Conquer Solve problem by transforming into: b a more convenient instance of the same problem.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2004 Heaps and heap sort  complete tree, heap property  min-heaps & max-heaps  heap operations:
Balanced Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees.
ITEC200 Week 11 Self-Balancing Search Trees. 2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.
CMPT 225 Priority Queues and Heaps. Priority Queues Items in a priority queue have a priority The priority is usually numerical value Could be lowest.
Advanced Tree Data Structures Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
TCSS 343, version 1.1 Algorithms, Design and Analysis Transform and Conquer Algorithms Presorting HeapSort.
Version TCSS 342, Winter 2006 Lecture Notes Priority Queues Heaps.
1 COSC 2P03 Lecture #5 – Trees Part III, Heaps. 2 Today Take up the quiz Assignment Questions Red-Black Trees Binary Heaps Heap sort D-Heaps, Leftist.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
Heapsort CIS 606 Spring Overview Heapsort – O(n lg n) worst case—like merge sort. – Sorts in place—like insertion sort. – Combines the best of both.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2010 transform & conquer  transform-and-conquer approach  balanced search trees o AVL, 2-3 trees,
CSC 172 DATA STRUCTURES. Priority Queues Model Set with priorities associatedwith elements Priorities are comparable by a < operator Operations Insert.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Heapsort Based off slides by: David Matuszek
Compiled by: Dr. Mohammad Alhawarat BST, Priority Queue, Heaps - Heapsort CHAPTER 07.
1 CSC 321: Data Structures Fall 2012 Binary Search Trees  BST property  override binary tree methods: add, contains  search efficiency  balanced trees:
ADT Table and Heap Ellen Walker CPSC 201 Data Structures Hiram College.
Balanced Search Trees Chapter 27 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 CSC 421: Algorithm Design Analysis Spring 2014 Transform & conquer  transform-and-conquer approach  presorting  balanced search trees, heaps  Horner's.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Chapter 21 Priority Queue: Binary Heap Saurav Karmakar.
2-3 Trees, Trees Red-Black Trees
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
1 CSC 427: Data Structures and Algorithm Analysis Fall 2006 Applications:  scheduling applications priority queue, java.util.PriorityQueue min-heaps &
1 Joe Meehean.  We wanted a data structure that gave us... the smallest item then the next smallest then the next and so on…  This ADT is called a priority.
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
Priority Queues and Heaps. October 2004John Edgar2  A queue should implement at least the first two of these operations:  insert – insert item at the.
Chapter 19: Binary Search Trees or How I Learned to Love AVL Trees and Balance The Tree Group 6: Tim Munn.
PRIORITY QUEUES AND HEAPS Slides of Ken Birman, Cornell University.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CIS 068 Welcome to CIS 068 ! Lesson 12: Data Structures 3 Trees.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
Lecture 10COMPSCI.220.FS.T Binary Search Tree BST converts a static binary search into a dynamic binary search allowing to efficiently insert and.
CS 367 Introduction to Data Structures Lecture 8.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Keeping Binary Trees Sorted. Search trees Searching a binary tree is easy; it’s just a preorder traversal public BinaryTree findNode(BinaryTree node,
Red-Black Trees an alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A.
Heaps, Heap Sort, and Priority Queues. Background: Binary Trees * Has a root at the topmost level * Each node has zero, one or two children * A node that.
Priority Queues and Heaps. John Edgar  Define the ADT priority queue  Define the partially ordered property  Define a heap  Implement a heap using.
CSC 421: Algorithm Design Analysis
CSC 421: Algorithm Design Analysis
Sorting With Priority Queue In-place Extra O(N) space
Balanced Search Trees 2-3 Trees AVL Trees Red-Black Trees
COMP261 Lecture 23 B Trees.
AA Trees.
CSC 321: Data Structures Fall 2016 Balanced and other trees
Chapter 6 Transform-and-Conquer
CSC 421: Algorithm Design Analysis
ADT Heap data structure
original list {67, 33,49, 21, 25, 94} pass { } {67 94}
Chapter 6 Transform and Conquer.
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Hassan Khosravi / Geoffrey Tien
Balanced-Trees This presentation shows you the potential problem of unbalanced tree and show two way to fix it This lecture introduces heaps, which are.
Chapter 6: Transform and Conquer
CSC 421: Algorithm Design Analysis
CSC 421: Algorithm Design Analysis
Hash Maps: The point of a hash map is to FIND DATA QUICKLY.
CSC 321: Data Structures Fall 2018 Balanced and other trees
Presentation transcript:

1 CSC 421: Algorithm Design Analysis Spring 2013 Transform & conquer  transform-and-conquer approach  presorting  balanced search trees, heaps  Horner's Rule  problem reduction

Transform & conquer the idea behind transform-and-conquer is to transform the given problem into a slightly different problem that suffices e.g., presorting data in a list can simplify many algorithms  suppose we want to determine if a list contains any duplicates BRUTE FORCE: compare each item with every other item (N-1) + (N-2) + … + 1 = (N-1)N/2  O(N 2 ) TRANSFORM & CONQUER: first sort the list, then make a single pass through the list and check for adjacent duplicates O(N log N) + O(N)  O(N log N)  finding the mode of a list, finding closest points, … 2

Balanced search trees recall binary search trees – we need to keep the tree balanced to ensure O(N log N) search/add/remove  OR DO WE?  it suffices to ensure O(log N) height, not necessarily minimal height transform the problem of "tree balance" to "relative tree balance" several specialized structures/algorithms exist:  AVL trees  2-3 trees  red-black trees 3

4 AVL trees an AVL tree is a binary search tree where  for every node, the heights of the left and right subtrees differ by at most 1  first self-balancing binary search tree variant  named after Adelson-Velskii & Landis (1962) AVL tree AVL property is weaker than perfect balance, but sufficient height of AVL tree with N nodes < 2 log(N+2)  searching is O(log N)

5 Inserting/removing from AVL tree when you insert or remove from an AVL tree, imbalances can occur  if an imbalance occurs, must rotate subtrees to retain the AVL property

6 AVL tree rotations worst case, inserting/removing requires traversing the path back to the root and rotating at each level  each rotation is a constant amount of work  inserting/removing is O(log N) there are two possible types of rotations, depending upon the imbalance caused by the insertion/removal

7 Red-black trees a red-black tree is a binary search tree in which each node is assigned a color (either red or black) such that 1.the root is black 2.a red node never has a red child 3.every path from root to leaf has the same number of black nodes  add & remove preserve these properties (complex, but still O(log N))  red-black properties ensure that tree height < 2 log(N+1)  O(log N) search

8 TreeSets & TreeMaps java.util.TreeSet uses red-black trees to store values  O(log N) efficiency on add, remove, contains java.util.TreeMap uses red-black trees to store the key-value pairs  O(log N) efficiency on put, get, containsKey thus, the original goal of an efficient tree structure is met  even though the subgoal of balancing a tree was transformed into "relatively balancing" a tree

9 Scheduling & priority queues many real-world applications involve optimal scheduling  balancing transmission of multiple signals over limited bandwidth  selecting a job from a printer queue  selecting the next disk sector to access from among a backlog  multiprogramming/multitasking a priority queue encapsulates these three optimal scheduling operations: add item (with a given priority) find highest priority item remove highest priority item  can be implemented as an unordered list  add is O(1), findHighest is O(N), removeHisghest is O(N)  can be implemented as an ordered list  add is O(N), findHighest is O(1), removeHighest is O(1)

10 Heaps Java provides a java.util.PriorityQueue class  the underlying data structure is not a list or queue at all  it is a tree structure called a heap a complete tree is a tree in which  all leaves are on the same level or else on 2 adjacent levels  all leaves at the lowest level are as far left as possible  note: a complete tree with N nodes will have minimal height =  log2 N  +1 a heap is complete binary tree in which  for every node, the value stored is  the values stored in both subtrees (technically, this is a min-heap -- can also define a max-heap where the value is  )

11 Inserting into a heap note: insertion maintains completeness and the heap property  worst case, if add smallest value, will have to swap all the way up to the root  but only nodes on the path are swapped  O(height) = O(log N) swaps to insert into a heap  place new item in next open leaf position  if new value is smaller than parent, then swap nodes  continue up toward the root, swapping with parent, until smaller parent found add 30

12 Finding/removing from a heap note: removing root maintains completeness and the heap property  worst case, if last value is largest, will have to swap all the way down to leaf  but only nodes on the path are swapped  O(height) = O(log N) swaps finding the min value in a heap is O(1) – it's in the root removing the min value requires some work  replace root with last node on bottom level  if new root value is greater than either child, swap with smaller child  continue down toward the leaves, swapping with smaller child, until smallest

13 Implementing a heap a heap provides for O(1) find min, O(log N) insertion and min removal  also has a simple, List-based implementation  since there are no holes in a heap, can store nodes in an ArrayList, level-by-level  root is at index 0  last leaf is at index size()-1  for a node at index i, children are at 2*i+1 and 2*i+2  to add at next available leaf, simply add at end

14 Heap sort the priority queue nature of heaps suggests an efficient sorting algorithm  start with the ArrayList to be sorted  construct a heap out of the elements  repeatedly, remove min element and put back into the ArrayList  N items in list, each insertion can require O(log N) swaps to reheapify  construct heap in O(N log N)  N items in heap, each removal can require O(log N) swap to reheapify  copy back in O(N log N) public static > void heapSort(ArrayList items) { MinHeap itemHeap = new MinHeap (); for (int i = 0; i < items.size(); i++) { itemHeap.add(items.get(i)); } for (int i = 0; i < items.size(); i++) { items.set(i, itemHeap.minValue()); itemHeap.remove(); } thus, overall efficiency is O(N log N), which is as good as it gets!  can also implement so that the sorting is done in place, requires no extra storage

Horner's rule polynomials are used extensively in mathematics and algorithm analysis p(x) = a n x n + a n-1 x n-1 + a n-2 x n a 1 x + a 0 how many multiplications would it take to evaluate this function for some value of x? 15 W.G. Horner devised a new formula that transforms the problem p(x) = ( … (((a n x + a n-1 )x + a n-2 )x a 1 )x + a 0 can evaluate in only n multiplications and n additions

Problem reduction in CSC321, we looked at a number of examples of reducing a problem from one form to another  e.g., generate the powerset (set of all subsets) of an N element set S = { x 1, x 2, x 3, x 4 } powerset S = { {}, {x 1 }, {x 2 }, {x 3 }, {x 4 }, {x 1, x 2 }, {x 1, x 3 }, {x 1, x 4 }, {x 2, x 3 }, {x 2, x 4 }, {x 3, x 4 }, {x 1, x 2, x 3 }, {x 1, x 2, x 4 }, {x 1, x 3, x 4 }, {x 2, x 3, x 4 }, {x 1, x 2, x 3, x 4 } }  PROBLEM REDUCTION: simplify by reducing it to a problem about bit sequences can map each subset into a sequence of N bits: b i = 1  x i in subset { x 1, x 4, x 5 }  …0 much simpler to generate all possible N-bit sequences { 0000, 0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111 } 16

lcm & gcd consider calculating the least common multiple of two numbers m & n  BRUTE FORCE: reduce each number to its prime factors then multiply (factors in both m & n) (factors only in m) (factors only in n) 24 = 2  2  2  3 60 = 2  2  3  5 lcm(24, 60) = (2  2  3)  (2)  (5) = 12  2  5 = 120  PROBLEM REDUCTION: can recognize a relationship between lcm & gcd lcm(m, n) = m x n / gcd(m, n) lcm(24, 60) = 24  60 / 12 = 2  60 = 120 gcd can be calculated efficiently using Euclid's algorithm: gcd(a, 0) = agcd(a, b) = gcd(b, a % b) 17