COSC 3100 Transform and Conquer

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

Analysis of Algorithms
Sorting Comparison-based algorithm review –You should know most of the algorithms –We will concentrate on their analyses –Special emphasis: Heapsort Lower.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Transform & Conquer Replacing One Problem With Another Saadiq Moolla.
Transform and Conquer. Algorithms based on the idea of transformation –Transformation stage Problem instance is modified to be more amenable to solution.
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.
Transform & Conquer Lecture 08 ITS033 – Programming & Algorithms
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
Design and Analysis of Algorithms – Chapter 61 Transform and Conquer Dr. Ying Lu RAIK 283: Data Structures & Algorithms.
Sorting Heapsort Quick review of basic sorting methods Lower bounds for comparison-based methods Non-comparison based sorting.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
CS 253: Algorithms Chapter 6 Heapsort Appendix B.5 Credit: Dr. George Bebis.
Analysis of Algorithms CS 477/677
More sorting algorithms: Heap sort & Radix sort. Heap Data Structure and Heap Sort (Chapter 7.6)
TCSS 343, version 1.1 Algorithms, Design and Analysis Transform and Conquer Algorithms Presorting HeapSort.
Analysis of Algorithms CS 477/677 Midterm Exam Review Instructor: George Bebis.
CS 206 Introduction to Computer Science II 12 / 03 / 2008 Instructor: Michael Eckmann.
3-Sorting-Intro-Heapsort1 Sorting Dan Barrish-Flood.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
HEAPSORT COUNTING SORT RADIX SORT. HEAPSORT O(nlgn) worst case like Merge sort. Like Insertion Sort, but unlike Merge Sort, Heapsort sorts in place: Combines.
PQ, binary heaps G.Kamberova, Algorithms Priority Queue ADT Binary Heaps Gerda Kamberova Department of Computer Science Hofstra University.
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 Chapter 8 Priority Queues. 2 Implementations Heaps Priority queues and heaps Vector based implementation of heaps Skew heaps Outline.
Heapsort Based off slides by: David Matuszek
9/17/20151 Chapter 12 - Heaps. 9/17/20152 Introduction ► Heaps are largely about priority queues. ► They are an alternative data structure to implementing.
Chapter 6 Transform-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Heaps, Heapsort, Priority Queues. Sorting So Far Heap: Data structure and associated algorithms, Not garbage collection context.
1 CSC 421: Algorithm Design Analysis Spring 2014 Transform & conquer  transform-and-conquer approach  presorting  balanced search trees, heaps  Horner's.
Binary Heap.
The Binary Heap. Binary Heap Looks similar to a binary search tree BUT all the values stored in the subtree rooted at a node are greater than or equal.
Chapter 21 Binary Heap.
Computer Sciences Department1. Sorting algorithm 3 Chapter 6 3Computer Sciences Department Sorting algorithm 1  insertion sort Sorting algorithm 2.
Sorting. Pseudocode of Insertion Sort Insertion Sort To sort array A[0..n-1], sort A[0..n-2] recursively and then insert A[n-1] in its proper place among.
Data Structure & Algorithm II.  In a multiuser computer system, multiple users submit jobs to run on a single processor.  We assume that the time required.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
CS 206 Introduction to Computer Science II 04 / 22 / 2009 Instructor: Michael Eckmann.
1 Algorithms CSCI 235, Fall 2015 Lecture 14 Analysis of Heap Sort.
Queues, Stacks and Heaps. Queue List structure using the FIFO process Nodes are removed form the front and added to the back ABDC FrontBack.
David Luebke 1 12/23/2015 Heaps & Priority Queues.
Heaps and Heapsort Prof. Sin-Min Lee Department of Computer Science San Jose State University.
Heapsort. What is a “heap”? Definitions of heap: 1.A large area of memory from which the programmer can allocate blocks as needed, and deallocate them.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
Lecture 15 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Lecture 8 : Priority Queue Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
1 Heap Sort. A Heap is a Binary Tree Height of tree = longest path from root to leaf =  (lgn) A heap is a binary tree satisfying the heap condition:
Internal and External Sorting External Searching
Lecture 14 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Priority Queues, Heaps, and Heapsort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Sept Heapsort What is a heap? Max-heap? Min-heap? Maintenance of Max-heaps -MaxHeapify -BuildMaxHeap Heapsort -Heapsort -Analysis Priority queues.
1 CSC 421: Algorithm Design Analysis Spring 2013 Transform & conquer  transform-and-conquer approach  presorting  balanced search trees, heaps  Horner's.
Chapter 6 Transform-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Sorting Cont. Quick Sort As the name implies quicksort is the fastest known sorting algorithm in practice. Quick-sort is a randomized sorting algorithm.
Priority Queues, Heaps, and Heapsort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Priority Queues A priority queue is an ADT where:
"Teachers open the door, but you must enter by yourself. "
Heaps, Heap Sort and Priority Queues
Heap Sort Example Qamar Abbas.
Sorting.
CS200: Algorithm Analysis
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance.
Heap Sort The Heap Data Structure
"Teachers open the door, but you must enter by yourself. "
Chapter 6: Transform and Conquer
Data structure: Heap Representation change: Heapsort Problem reduction
Topic 5: Heap data structure heap sort Priority queue
Data structure: Heap Representation change: Heapsort Problem reduction
Asst. Prof. Dr. İlker Kocabaş
Data structure: Heap Representation change: Heapsort Problem reduction
Presentation transcript:

COSC 3100 Transform and Conquer Instructor: Tanvir

What is Transform and Conquer ? The 4th algorithm design technique we are going to study Three major variations Instance Simplification: Transform to a simpler or more convenient instance of the same problem Representation Change: Transform to a different representation of the same instance Problem Reduction: Transform to an instance of a different problem for which you know an efficient algorithm Two-step process: Step 1: Modify problem instance to something easier to solve Step 2: Conquer!

What is Transform and Conquer ? 1. Presorting Simpler instance Or Another representation Another problem’s instance Problem’s instance Solution 1. Computing Least Common Multiple 2. Counting Paths in a Graph 3. Reduction to Optimization Problems 4. Reduction to Graph Problems 1. Heapsort 2. Horner’s Rule 3. Binary Exponentiation

Trns. & Conq.: Presorting Many questions about a list are easier to answer if the list is sorted We shall see three examples Example 1: Checking element uniqueness in an array T(n) = (n-1)+(n-2)+…+1 = 𝒏−𝟏 𝒏 𝟐 є Θ(n2) E.g., 4 1 8 9 3 7 10 2 What is the brute-force idea ? Take say the first element and check whether it is in the rest of the array, take the second element and check if it is in the rest of the array, and so on…

Trns. & Conq.: Presorting Let us apply sorting first ALGORITHM PresortElementUniqueness(A[0..n-1]) sort the array A for i <- 0 to n-2 do if A[i] = A[i+1] return false return true 4 1 8 9 3 7 10 2 S O R T 1 2 3 4 7 8 9 10 T(n) = Tsort(n) + Tscan(n) є Θ(nlgn) + Θ(n) = Θ(nlgn)

Trns. & Conq.: Presorting Example 2: Computing a mode A mode is a value that occurs most often in a list E.g., in , mode is ? 5 1 7 6 5 Brute-force: scan the list and compute the frequencies of all distinct values, then find the value with the highest frequency How to implement brute-force ? 5 3 1 7 2 6 Value: Frequency: Store the values already encountered along with their frequencies, in a separate list. What is the worst-case input ? An array with no equal elements… C(n) = 0 + 1 + … + (n-1) = 𝒏−𝟏 𝒏 𝟐 є Θ(n2)

Trns. & Conq.: Presorting Example 2: Computing a mode (contd.) If we sort the array first, all equal values will be adjacent to each other. To compute the mode, all we need to know is to find the longest run of adjacent equal values in the sorted array

Trns. & Conq.: Presorting Example 2: Computing a mode (contd.) ALGORITHM PresortMode( A[0..n-1] ) sort the array A i <- 0 modefrequency <- 0 while i ≤ n-1 do runlength <- 1; runvalue <- A[i] while i+runlength ≤ n-1 and A[i+runlength] = runvalue runlength <- runlength+1 if runlength > modefrequency modefrequency <- runlength; modevalue <- runvalue i <- i+runlength return modevalue 1 2 3 7 8 9 10 while loop takes linear time, so the overall runtime is dominated by the time for sorting, Θ(nlgn)

Trns. & Conq.: Heapsort Another O(nlgn) sorting algorithm Uses a clever data structure called “Heap” It transforms an array into a Heap and then sorting becomes very easy Heap has other important uses, like in implementing “priority queue” Recall, priority queue is a multiset of items with an orderable characteristic called “priority”, with the following operations: Find an item with the highest priority Deleting an item with the highest priority Adding a new item to the multiset Let us study Heap first…

Trns. & Conq.: Heap A “heap” can be described as a binary tree, with one key per node, provided the following two conditions are met: Shape property: Binary tree is essentially complete, all levels are full except possibly the last level, where only some right most leaves may be missing Parental dominance (or heap property): key in each node is greater than or equal to the keys in its children (considered automatically satisfied for the leaves) This is actually the “max heap”, There is a corresponding “min heap”…

Trns. & Conq.: Heap Shape and Heap properties… Is it a Heap ? 5 7 2 1 10 5 7 2 1 10 5 2 4 7 1 Is it a Heap ? 10 5 7 2 6 1

Trns. & Conq.: Heap Note: Sequence of values on a path from the root to a leaf is nonincreasing There is no left-to-right order in key values, though 10 8 7 5 2 1 6 ALGORITHM Parent(i) return 𝑖 2 1 3 5 ALGORITHM Left(i) return 2i index: 1 2 3 4 5 6 7 8 9 10 ALGORITHM Right(i) return 2𝑖+1 value: 10 8 7 5 2 1 6 3 parents leaves

Trns. & Conq.: Heap Important properties of heaps: 8 2 5 7 1 6 3 H: 9 There exists one essentially complete binary tree with n nodes, its height is 𝑙𝑔𝑛 2. The root of a heap contains the largest element 3. A node with all its descendants is also a heap 4. Heap can be implemented as an array By recording its elements in the top-down, left-right fashion. H[0] is unused or contains a number bigger than all the rest. 1 2 3 4 5 6 7 8 9 10 H: 9 8 7 5 2 1 6 3 a. Parental node keys will be in first 𝑛 2 positions, leafs will Be in the last 𝑛 2 positions b. Children of key at index i (1 ≤ i ≤ 𝑛 2 ) will be in positions 2i and 2i+1. The parent of a key at index i (2 ≤ i ≤ n) will be in Position 𝑖 2 . Which gives: H[i] ≥ max{ H[2i], H[2i+1] } for i = 1, … , 𝒏 𝟐

Trns. & Conq.: Heap How can we transform an array into a heap? Two ways: 2 9 5 6 8 7 1. Start at the last parent at index 𝒏 𝟐 2 9 7 2. Check if parental dominance holds for this node’s key 2 6 5 8 9 8 3. If it does not, Exchange the node’s key K with the larger key of its children 6 5 7 n 1 2 3 4 5 6 9 4. Check if parental dominance holds for K in its new position and so on… Stop after doing for root. 2 9 7 6 5 8 6 2 8 6 2 5 7 This process is called “Heapifying” This method is called “bottom-up heap construction”

Trns. & Conq.: Heap 9 5 6 7 8 n 2 9 7 6 5 8 one parent Heapify ALGORITHM HeapBottomUp(H[1..n]) //Input: An array H[1..n] of orderable items //Output: A heap H[1..n] for i <- 𝑛 2 downto 1 do k <- i; v <- H[k] heap <- false while not heap and 2*k ≤ n do j <- 2*k if j < n // there are 2 children if H[j] < H[j+1] j <- j+1 if v ≥ H[j] // only left child heap <- true else H[k] <- H[j] k <- j H[k] <- v 2 9 5 6 7 8 n 1 2 3 4 5 6 2 9 7 6 5 8 one parent Heapify

Trns. & Conq.: Heap Let us analyze the HeapBottomUp(H[1..n])’s worst case Let’s take a heap whose binary tree is full Why ? 7 4 5 6 2 3 So, number of nodes, n = 2m-1 largest possible # of nodes at each level… n = 22-1 9 n = 23-1 So what is the height ? h = 𝒍𝒈𝒏 = 𝐥𝐠⁡(𝒏+𝟏) -1 = m-1 Let’s look at a worst-case… A key on level i must travel to the leaf-level h; Level i has (h-i) lower levels. Going down by 1 level requires 2 comparisons: One to determine the larger child, the other to determine if exchange is needed. Cworst(n) = 𝑖=0 ℎ−1 𝑗=1 2 𝑖 2(ℎ−𝑖) = 𝑖=0 ℎ−1 2(ℎ−𝑖) 2 𝑖 =2ℎ 𝑖=0 ℎ−1 2 𝑖 −2 𝑖=0 ℎ−1 𝑖 2 𝑖 So, fewer than 2n comparisons are required = 2h(2h-1) – 2(h-2)2h + 4 = h2h+1-2h-h2h+1+2*2h+1+4 = 2 ( n – lg(n+1) + 4 ) 𝒊=𝟏 𝒏 𝒊 𝟐 𝒊 =(𝒏−𝟏) 𝟐 𝒏+𝟏 +𝟐

Trns. & Conq.: Heap Building a heap, Method 2 It’s called “top-down heap construction” Idea: Successively insert a new key into a previously constructed heap Attach a new node with key K after the last leaf of the existing heap Sift K up until the parental dominance is established 9 9 10 6 8 6 10 6 9 2 5 7 10 2 5 7 8 2 5 7 8 Insertion operation cannot take more than twice the height of the heap’s tree, so it is in O(lgn)

Trns. & Conq.: Heap Maximum key deletion from a heap Exchange root’s key with the last key K in the heap Decrease the heap’s size by 1 “Heapify” the smaller tree by sifting K down establishing “parental dominance” as long as it is needed 1 We want to delete the root 1 8 6 8 6 9 2 5 2 5 9 8 6 2 5 1 8 Efficiency is O(lgn) 8 1 6 5 6 2 5 2 1

Trns. & Conq.: Heapsort An interesting algorithm discovered by J. W. J. Williams in 1964 It has 2 stages Stage 1: Costruct a heap from a given array Stage 2: Apply the root-deletion operation n-1 times to the remaining heap

Trns. & Conq.: Heapsort Stage 1: Heap construction 2 9 7 2 9 7 6 5 8 6 1 2 3 4 5 6 9 7 2 9 7 6 5 8 6 5 8 1 2 3 4 5 6 2 9 8 6 5 7 9 2 6 8 1 2 3 4 5 6 9 8 2 9 8 6 5 7 2 5 7 6 5 7 1 2 3 4 5 6 9 2 8 6 5 7 9 2 1 2 3 4 5 6 2 8 9 6 8 2 5 7 9 8 6 5 7 6 5 7

Trns. & Conq.: Heapsort Stage 2: Maximum deletions 9 6 8 2 5 7 2 6 5 7 1 2 3 4 5 6 1 2 3 4 5 6 9 6 8 2 5 7 2 6 5 7 8 9 1 2 3 4 5 6 1 2 3 4 5 6 7 6 8 2 5 9 6 2 5 7 8 9 1 2 3 4 5 6 1 2 3 4 5 6 8 6 7 2 5 9 5 2 6 7 8 9 1 2 3 4 5 6 1 2 3 4 5 6 5 6 7 2 8 9 2 5 6 7 8 9 1 2 3 4 5 6 7 6 5 2 8 9

Trns. & Conq.: Heapsort What is Heapsort’s worst-case complexity ? Stage 1: Heap construction is in ? O(n) Stage 2: Maximum deletions is in ? Let’s see… Let C(n) be the # of comparisons needed in Stage 2 Recall, height of heap’s tree is 𝒍𝒈𝒏 C(n) ≤ 2 𝐥𝐠⁡(𝒏−𝟏) + 2 𝒍𝒈⁡(𝒏−𝟐) + … … + 2 𝒍𝒈⁡(𝟏) ≤ 2 𝒊=𝟏 𝒏−𝟏 𝐥𝐠⁡(𝒊) C(n) ≤ 2 𝒊=𝟏 𝒏−𝟏 𝒍𝒈⁡(𝒏−𝟏) = 2(n-1)lg(n-1) ≤ 2nlgn So, C(n) є O(nlgn) So, for two stages, O(n) + O(nlgn) = O(nlgn)

Trns. & Conq.: Heapsort More careful analysis shows that Heapsort’s worst and best cases are in Θ(nlgn) like Mergesort Additionally Heapsort is in-place Timing experiments on random files show that Heapsort is slower than Quicksort, but can be competitive with Mergesort What sorting algorithm is used by Collections.sort(List<T> list) of Java?

Trns. & Conq.: Problem Reduction Compute least common multiple of two nonnegative integers, lcm(m, n) Say m = 24, and n = 60 24 = 2*2*2*3 and 60 = 2*2*3*5 Take product of all common factors of m and n, and all factors of m not in n, and all factors of n not in m lcm(24, 60) = (2*2*3)*2*5 = 120 reduction Alg. A Solution to Problem 2 Problem 2 Problem 1 To be solved Solvable by Alg. A So, m*n = lcm(m, n) * gcd(m, n) lcm(m, n) = 𝒎 ∗ 𝒏 𝐠𝐜𝐝⁡(𝒎,𝒏) ! Is it a good algorithm? Notice, 24*60 = (2*2*2*3) * (2*2*3*5) = (2*2*3)2*2*5

THANK YOU…