CSC 421: Algorithm Design Analysis. Spring 2013

Similar documents
CSC 421: Algorithm Design Analysis. Spring 2017

CSC 321: Data Structures. Fall 2016

CSC 321: Data Structures. Fall 2017

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

CSC Design and Analysis of Algorithms. Lecture 8. Transform and Conquer II Algorithm Design Technique. Transform and Conquer

Priority Queues. e.g. jobs sent to a printer, Operating system job scheduler in a multi-user environment. Simulation environments

Lecture 7. Transform-and-Conquer

2. Instance simplification Solve a problem s instance by transforming it into another simpler/easier instance of the same problem

Self-Balancing Search Trees. Chapter 11

Analysis of Algorithms

Advanced Tree Data Structures

CPS 616 TRANSFORM-AND-CONQUER 7-1

Lecture 13: AVL Trees and Binary Heaps

Design and Analysis of Algorithms - Chapter 6 6

AVL Trees Heaps And Complexity

Sorting and Searching

Sorting and Searching

Transform & Conquer. Presorting

9. Heap : Priority Queue

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

CS350: Data Structures Heaps and Priority Queues

Data Structures and Algorithms

CSC Design and Analysis of Algorithms

Algorithms. AVL Tree

Recall: Properties of B-Trees

CS 261 Data Structures. AVL Trees

CSC Design and Analysis of Algorithms. Lecture 7. Transform and Conquer I Algorithm Design Technique. Transform and Conquer

CSE 214 Computer Science II Heaps and Priority Queues

COMP : Trees. COMP20012 Trees 219

Section 4 SOLUTION: AVL Trees & B-Trees

Priority queues. Priority queues. Priority queue operations

AVL Tree Definition. An example of an AVL tree where the heights are shown next to the nodes. Adelson-Velsky and Landis

Trees. A tree is a directed graph with the property

CSCI2100B Data Structures Heaps

Balanced Search Trees. CS 3110 Fall 2010

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

COMP171. AVL-Trees (Part 1)

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

3137 Data Structures and Algorithms in C++

Lecture 9: Balanced Binary Search Trees, Priority Queues, Heaps, Binary Trees for Compression, General Trees

CS Transform-and-Conquer

Hierarchical data structures. Announcements. Motivation for trees. Tree overview

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

Heap: A binary heap is a complete binary tree in which each, node other than root is smaller than its parent. Heap example: Fig 1. NPTEL IIT Guwahati

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

3. Priority Queues. ADT Stack : LIFO. ADT Queue : FIFO. ADT Priority Queue : pick the element with the lowest (or highest) priority.

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Balanced Binary Search Trees. Victor Gao

Binary search trees (chapters )

Properties of a heap (represented by an array A)

Data Structure. A way to store and organize data in order to support efficient insertions, queries, searches, updates, and deletions.

ADVANCED DATA STRUCTURES STUDY NOTES. The left subtree of each node contains values that are smaller than the value in the given node.

Computer Science 210 Data Structures Siena College Fall Topic Notes: Priority Queues and Heaps

Balanced Binary Search Trees

CSI33 Data Structures

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

Data Structures and Algorithms for Engineers

Module 2: Classical Algorithm Design Techniques

CIS265/ Trees Red-Black Trees. Some of the following material is from:

CISC 235: Topic 4. Balanced Binary Search Trees

CSC 1700 Analysis of Algorithms: Heaps

Priority Queues. Chapter 9

Algorithms and Data Structures

Definition of a Heap. Heaps. Priority Queues. Example. Implementation using a heap. Heap ADT

8.1. Optimal Binary Search Trees:

CE 221 Data Structures and Algorithms

Priority Queues (Heaps)

Priority Queues and Binary Heaps

CS 240 Fall Mike Lam, Professor. Priority Queues and Heaps

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

Binary search trees (chapters )

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

HEAPS: IMPLEMENTING EFFICIENT PRIORITY QUEUES

Data Structures in Java

CSC Design and Analysis of Algorithms. Lecture 7. Transform and Conquer I Algorithm Design Technique. Transform and Conquer

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

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

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CSCE 411 Design and Analysis of Algorithms

Trees. Eric McCreath

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CSE 373: Data Structures and Algorithms

Data Structures and Algorithms

Heaps 2. Recall Priority Queue ADT. Heaps 3/19/14

Heaps. 2/13/2006 Heaps 1

Heaps, Heap Sort, and Priority Queues.

Binary Heaps in Dynamic Arrays

Trees & Tree-Based Data Structures. Part 4: Heaps. Definition. Example. Properties. Example Min-Heap. Definition

Heaps Goodrich, Tamassia. Heaps 1

Dynamic Access Binary Search Trees

Stores a collection of elements each with an associated key value

& ( D. " mnp ' ( ) n 3. n 2. ( ) C. " n

Data Structures Lesson 7

Algorithms and Data Structures

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

CS S-08 Priority Queues Heaps 1

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

Transcription:

CSC 421: Algorithm Design Analysis Spring 2013 Transform & conquer transform-and-conquer approach presorting balanced search trees, heaps Horner's Rule problem reduction 1 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 1

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 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) 4 2

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 5 AVL tree rotations there are two possible types of rotations, depending upon the imbalance caused by the insertion/removal 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) 6 3

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 7 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 8 4

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) 9 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 ) 10 5

Inserting into a heap 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 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 11 Finding/removing from a heap 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 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 12 6

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 30 34 60 36 71 66 71 83 40 94 to add at next available leaf, simply add at end 13 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 public static <E extends Comparable<? super E>> void heapsort(arraylist<e> items) { MinHeap<E> itemheap = new MinHeap<E>(); } 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(); } 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) 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 14 7

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-2 +... + a 1 x + a 0 how many multiplications would it take to evaluate this function for some value of x? 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 15 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 } 10011000 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 8

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) = a gcd(a, b) = gcd(b, a % b) 17 9