Understanding Recursion /1 Powerful computing/problem-solving techniques Examples Factorial: f(n) = 1, if n = 1 f(n) = f(n-1) * n, if n ≥ 1 Quick sort:

Slides:



Advertisements
Similar presentations
An Array-Based Algorithm for Simultaneous Multidimensional Aggregates By Yihong Zhao, Prasad M. Desphande and Jeffrey F. Naughton Presented by Kia Hall.
Advertisements

Materialization and Cubing Algorithms. Cube Materialization Each cell of the data cube is a view consisting of an aggregation of interest. The values.
Recursion Genome 559: Introduction to Statistical and Computational Genomics Elhanan Borenstein.
Equality Join R X R.A=S.B S : : Relation R M PagesN Pages Relation S Pr records per page Ps records per page.
CSC 421: Algorithm Design & Analysis
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
A simple example finding the maximum of a set S of n numbers.
Recursion, Divide and Conquer Lam Chi Kit (George) HKOI2007.
5/15/ Algorithms1 Algorithms – Ch4 - Divide & Conquer Recurrences: as we saw in another lecture, the Divide and Conquer approach leads to Recurrence.
CS4413 Divide-and-Conquer
1 Divide & Conquer Algorithms. 2 Recursion Review A function that calls itself either directly or indirectly through another function Recursive solutions.
Introduction to Algorithms
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
Algorithm Design Strategy Divide and Conquer. More examples of Divide and Conquer  Review of Divide & Conquer Concept  More examples  Finding closest.
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
Midterm Review Spring Overview Sorting Hashing Selections Joins.
SPRING 2004CENG 3521 Join Algorithms Chapter 14. SPRING 2004CENG 3522 Schema for Examples Similar to old schema; rname added for variations. Reserves:
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
External Sorting 198:541. Why Sort?  A classic problem in computer science!  Data requested in sorted order e.g., find students in increasing gpa order.
1 External Sorting for Query Processing Yanlei Diao UMass Amherst Feb 27, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
Evaluation of Relational Operations. Relational Operations v We will consider how to implement: – Selection ( ) Selects a subset of rows from relation.
Divide-And-Conquer Sorting Small instance.  n
Algorithms and Data Structures Lecture X
Lecture 5 Dynamic Programming. Dynamic Programming Self-reducibility.
Efficient Methods for Data Cube Computation and Data Generalization
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
Nattee Niparnan. Recall  Complexity Analysis  Comparison of Two Algos  Big O  Simplification  From source code  Recursive.
HKOI 2006 Intermediate Training Searching and Sorting 1/4/2006.
1 Recurrences Algorithms Jay Urbain, PhD Credits: Discrete Mathematics and Its Applications, by Kenneth Rosen The Design and Analysis of.
1 Fast Computation of Sparse Datacubes Vicky :: Cao Hui Ping Sherman :: Chow Sze Ming CTH :: Chong Tsz Ho Ronald :: Woo Lok Yan Ken :: Yiu Man Lung.
Introduction to Algorithms Jiafen Liu Sept
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
CS411 Database Systems Kazuhiro Minami 11: Query Execution.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2.Solve smaller instances.
Lecture 7. Solution by Substitution Method T(n) = 2 T(n/2) + n Substitute n/2 into the main equation 2T(n/2) = 2(2(T(n/4)) + n/2) = 4T(n/4) + n And T(n)
Implementing Data Cube Construction Using a Cluster Middleware: Algorithms, Implementation Experience, and Performance Ge Yang Ruoming Jin Gagan Agrawal.
Divide And Conquer A large instance is solved as follows:  Divide the large instance into smaller instances.  Solve the smaller instances somehow. 
4/3/2003CSE More Math CSE Algorithms Euclidean Algorithm Divide and Conquer.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
CPSC 404, Laks V.S. Lakshmanan1 Evaluation of Relational Operations – Join Chapter 14 Ramakrishnan and Gehrke (Section 14.4)
1 Ch.19 Divide and Conquer. 2 BIRD’S-EYE VIEW Divide and conquer algorithms Decompose a problem instance into several smaller independent instances May.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Sorting.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
CS 540 Database Management Systems
Sorting Quick, Merge & Radix Divide-and-conquer Technique subproblem 2 of size n/2 subproblem 1 of size n/2 a solution to subproblem 1 a solution to.
IT 60101: Lecture #181 Foundation of Computing Systems Lecture 18 Sorting Algorithms III.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
CSC 421: Algorithm Design & Analysis
Divide-and-Conquer The most-well known algorithm design strategy:
Evaluation of Relational Operations
Chapter 4: Divide and Conquer
Sorting Algorithms Ellysa N. Kosinaya.
Divide-and-Conquer The most-well known algorithm design strategy:
CSC 421: Algorithm Design & Analysis
Functional Dependencies
DYNAMIC PROGRAMMING.
Programming with Recursion
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

Understanding Recursion /1 Powerful computing/problem-solving techniques Examples Factorial: f(n) = 1, if n = 1 f(n) = f(n-1) * n, if n ≥ 1 Quick sort: Sort([x]) = [x] Sort([x1, …, pivot, … xn]) = sort[ys] ++ sort[zs]), where ys = [ x | x in xi, x ≤ pivot ] zs = [ x | x pivot ] 9/06/ f(0) = 0! = ??? List comprehension in Haskell or python

Understanding Recursion /2 Let C(n, m) be the number of ways to select m balls from n numbered balls Show that C(n, m) = C(n-1, m-1) + C(n-1, m) Example: m = 3, n = 5 Consider any ball in the 5 balls, e.g., ‘d’ 9/06/ entire solution space ?1?2?3 d?1?2 ?1 (not d)?2?3 abcde ?i in {}

Key Points Sub-problems need to be “smaller”, so that a simple/trivial boundary case can be reached Divide-and-conquer There may be multiple ways the entire solution space can be divided into disjoint sub-spaces, each of which can be conquered recursively. 9/06/2015 3

Outline We offer two ways to derive/interpret a BUC-like algorithm Slides 5-8: Slides 9-11: geometric derivation Slides 12-17: Simplified pseudo-code of BUC Misc stuffs Example 9/06/2015 4

Naïve Relational Cubing Method /1 All tuples in the cube has the format (a i, b j, c k, …, M l ) a i must be one of the values on dim A observed from the base cuboid + * (i.e. ALL)  collectively denoted as Dom*(A) Cubing R ABC = foreach (a i, b j, c k ) in Dom*(A) X Dom*(B) X Dom*(C) return ( a i, b j, c k, aggregate(R, (a i, b j, c k )) ) 9/06/ i.e., Cubing(R, ABC). We omit () for clarity. No confusion as Cubing(.,.) always takes 2 parameters

Naïve Relational Cubing Method /2 Rather than hardcode 3 nested loops, use recursion Cubing R ABC = [Cubing R a i BC | a i in Dom*(A)] Cubing R a i BC = [a i ] Cubing R BC is the Cartesian product; effectively prepending a i to every tuple from the recursion call Assertion (which is easy to prove): Cubing(.,.) returns the (almost) correct cube from R wrt the given set of dimensions 9/06/ Boundary case omitted. Try to write it by yourself.

Improved Relational Cubing Method /1 Cubing R ABC = [ [a i ] Cubing R BC | a i in Dom*(A)] Problem: may generate non- observed dimension value combinations. The choice of b j should depend on a i Fix: (1) pass tuples with A = ai to recursive calls (2) take bj values from those observed in the set of tuples passed in. 9/06/ [1] [2] [*] (1, 1, *, 0) is spurious

Improved Relational Cubing Method /2 Cubing R ABC = [ [a i ] Cubing ∏ BC σ A=ai (R) BC | a i in Dom*(R.A) ] 9/06/ [1] [2] [*]

Reduce Cube(in 2D) to Cube(in 1D) 9/06/ Geometric Interpretation /1 M11M12M13[Step 1] M21M22M23[Step 1] [Step 2] [Step 3] a1 a2 b1b2b3 M11M12M13[Step 1] b1b2b3 M21M22M23[Step 1] [Step 2] [Step 3] [a1] [a2] [a*]

Reduce Cube(in 3D) to Cube(in 2D) 9/06/ Geometric Interpretation /2

Reduce Cube(in 3D) to Cube(in 2D) 9/06/ Geometric Interpretation /3

12 Scaffolding BUC Alg: BottomUpCube(input, d)

BUC (Scaffolded) Explained Essentially the same as the improved recursive cubing algorithm Some recursion manually unfolded Computes coarse aggregation first (Line 1), mainly for iceberg cube computation Computes aggregates from cuboids interleavingly and in the order shown on the right 9/06/ ABC ABACBC AB C  Our: ABC, AB, AC, A, BC, B, C, ɸ BUC: ɸ, A, AB, ABC, AC, B, BC, C Cuboid AB = GROUP BY A, B = (a i, b j, [….])

An Alternative View of BUC’s Algorithm Divide the solution space (all tuples in the cube) in the following manner: A=a i A=*, B=b j A=*, B=*, C=c k … … ??? 9/06/ disjoint & complete (why?  write out the last bullet) Compute (d-1) dim cube Compute (d-2) dim cube … ???

15 Additional Advantage of Divide and Conquer: Locality of Access Increasingly important when dealing with large datasets, residing on disk (disk is slower than memory) in the memory (memory is slower than L2/1 cache; TLB misses) Each chunk of data is loaded once into the memory, and then we perform all the computation depending on it If (1, _, _ …) fits in the memory Compute all (1, _, _, …) without additional I/O cost Write out (*, _, _, …) No longer needed afterwards c.f., external memory sort

9/06/ BUC Example MCBA ABC ABACBC AB C  (*, *, *) = 150 AB C  (1, *, *) (2, *, *) AB (1, 1, *) (1, 2, *) (1, 3, *) AB ABC (1, 1, 1) ABC (1, 2, 1) ABC (1, 3, 1) (2, 1, *) ABC (2, 1, 1) AC (1, *, 1) (1, *, 2) AC (2, *, 1) (1, 1, 2)

9/06/ BUC Example (*, *, *) = 150 B C  (*, 1, *) BC (*, 1, 1) (*, 1, 2) MCB (*, 2, *) BC (*, 2, 1) (*, 3, *) BC (*, 3, 1) (*, *, 1) (*, *, 2) ABC ABACBC AB C  A ABC ABAC Note: strictly speaking, BUC uses depth-first traversal order and it is slightly different from what is shown in the animation here. E.g., when partitioning on B, it discovers three partitions, and will delve into the first partition (and calculate (*,1,*)); it will only access and perform computation for other partitions after all the (recursive) computation of the first partition is completed.