1 / 20

Recurrence 1

Recurrence 1. …. Recurrence 2. ……. Understanding Recurrences. But where do these recurrences come from? How can we derive one from a real recursive problem? Let's collectively think about a problem. Try first to come up with any algorithm (brute-force)

menefer
Download Presentation

Recurrence 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recurrence 1 ….

  2. Recurrence 2 ……

  3. Understanding Recurrences • But where do these recurrences come from? • How can we derive one from a real recursive problem? • Let's collectively think about a problem. • Try first to come up with any algorithm (brute-force) • Then try to improve it and possibly define a recursive solution for it. • After we have a recursive solution we'll derive the recurrence for the algorithm • What is the problem? • Pancake Flipping • This problem has been posed by Harry Dweighter on American Mathematical Monthly, 1975

  4. 5 4 1 6 3 2 Flipping Pancakes

  5. Two-Flips method(aka Bringing to the top method) • The problem above can be solved by doing the following • Bring the largest pancake to the top of the pile with one flip • With another flip place the largest pancake at the bottom of the pile • Bring the second largest pancake to the top of the pile with one flip • With another flip place the second largest pancake at the second position from the bottom of the pile ... and so on. • The problem above is intrinsicaly recursive and can be stated as: • If the pile has size (n) == 1 then stop • Otherwise, perform a two-flip operation on the nth pancake • Solve the flip problem for (n-1) pancakes • Recurrence relation for this problem is: T(n) = T(n-1) + 2 • The worst case number of raised pancake is T(n) = T(n-1) + 2n-1

  6. Recurrence 3 first remember that:

  7. Recurrence 4 ……

  8. Data Structures • A data structure is the building block of programming • It defines how data is organized (and consequently) how data is allocated in a computer memory • The importance of data structures for algorithm efficiency cannot be overemphasized. • The efficiency of most algorithms is directly linked to the data structure choice • Some algorithms are basically a data structure definition (plus the operations associated with the structure) • For the same amout of data, different data structures can take more or less space in the computer memory

  9. Abstract Data Types (ADT) • A formal specification defines a system independently of implementation by describing its internal state as Abstract Data Types (objects), characterized only by the operations allowed on them. • There are 2 types of specifications: • the algebraic specifications (OBJ) – leading to an algebraic structure (an algebra) • a data set • a set of operations (functions) • a set of properties (axioms) characterizing the operations • the constructive approach (VDM – Vienna Development Method) • explicit specification of operation (e.g. using the set theory)

  10. Abstract Data Types (ADT) • Algebraic specifications follow the pattern: • obj <name object> • obj • {important sub-objects, objects parameters of functions…} • mode • {complete specification of this data type – e.g. with parameters for templates} • funct • {specify functions associated with the object} • vars • {specify universally quantified variables used in the following equations, e.g. “forall bool x”} • eqns • {specify axioms associated with the object} • jbo

  11. Data Structures • We mentioned above the operations associated with the structure. What are these? • A data structure is not passive, it consists of data and operations to manipulate the data • They are implementation of (ADTs)

  12. Elementary Data Structures Elementary Data Structures Linear Nonlinear Direct Access Sequential Access Set Homogeneous Components Heterogeneous Components General LIFO FIFO Array Record List Stack Queue

  13. Linear vs. Nonlinear • For a structure to be linear all of the above has to be true • There is a unique first element • There is a unique last element • Every component has a unique predecessor (except the first) • Every component has a unique successor (except the last) • If one or more of the above is not true, the structure is nonlinear

  14. Direct vs. Sequential Access • In any linear data structure we have two methods of access the stored data • Sequential structures are such that we can only access the Nth element we have to accessed all element preceding N. • This means that all elements from 1 to N-1 will have to be accessed first. • You can see this as trying to access a song recorded in a cassette tape • Direct access structures are such that any element of the structure can be accessed in directly. • There is no need to access any other object besides the element required. • Rather than a cassette tape, think CD player. Can we say that one is better than another?

  15. Arrays • One of the most common types of data structures • Normally pre-defined in most programming languages • Has the advantages: • Direct access to elements • But also disadvantages: • Fixed size • Homogeneous elements • Normally implemented by using contiguous allocation of memory cells • This is not however required in the ADT definition of an array. • The array implementation may give the impression of contiguousness.

  16. Arrays as ADTs • Domain • A collection of fixed number of components of the same type • A set of indexes used to access the data stored in the array. • There is a one-to-one relation between index and objects stored. • Operations • valueAt(i): Index i is used to access the value stored in the corresponding position of the array • Most languages use the [ i ] as the index of an array • store(i,v): Stores the value v into the array position i • Most languages use the = operator

  17. Sieve of Eratosthenes(Prime Testing) public class Sieve { public static void main (String args[]) { int n = Integer.parseInt(args[0]); boolean numbers[] = new boolean[n+1]; for (int i = 2; i <= n; i++) { numbers[i] = true; } for (int i = 2; i <= n; i++) { if (numbers[i]) { for (int j = i; j*i <= n; j++) { if ((j*i) <= n) { // takes care of overflow in j*i numbers[j*i] = false; } } } } for (int i = 2; i <= n; i++) { if (numbers[i]) { System.out.println(i); } } } }

  18. Coin Flipping Simulation(simulation of Bernoulli trials) public class CoinFlippingSimulation { private static boolean heads() { return (Math.random() < 0.5); } public static void main (String args[]) { int cnt = 0, j; int n = Integer.parseInt(args[0]); int m = Integer.parseInt(args[1]); int[] result = new int[n+1]; for (int i = 0; i < m; i++) { cnt = 0; for (j = 0; j < n; j++) { if (heads()) cnt++; } result[cnt]++; } for (j = 0; j <= n; j++) { if (result[j] == 0) { System.out.print("."); } for (int i = 0; i < result[j]; i+=10) { System.out.print("*"); } System.out.println(); } } }

  19. Reading Work • Required • Chapter 2: Sections 2.1 to 2.5 • Highly recommended • Chapter 2: Sections 2.6 to the end of the chapter

  20. Bibliography(used to produce these slides) • [Sedgewick 2003]. Algorithms in Java. Parts 1-4. • [Cormen et al]. Introduction to Algorithms.

More Related