Recurrence 1 - PowerPoint PPT Presentation

About This Presentation
Title:

Recurrence 1

Description:

You can see this as trying to access a song recorded in a cassette tape ... Rather than a cassette tape, think CD player. Can we say that one is better than another? ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 21
Provided by: csF8
Learn more at: https://cs.fit.edu
Category:

less

Transcript and Presenter's Notes

Title: Recurrence 1


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
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 ltname objectgt
  • 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
Sequential Access
Direct Access
Set
FIFO
LIFO
General
Heterogeneous Components
Homogeneous Components
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(arg
s0) boolean numbers new booleann1
for (int i 2 i lt n i)
numbersi true for (int i 2 i lt
n i) if (numbersi) for
(int j i ji lt n j) if ((ji)
lt n) // takes care of overflow in ji
numbersji false
for (int i 2 i lt n i)
if (numbersi) System.out.println(i)

18
Coin Flipping Simulation(simulation of Bernoulli
trials)
public class CoinFlippingSimulation private
static boolean heads() return
(Math.random() lt 0.5) public static void
main (String args) int cnt 0, j
int n Integer.parseInt(args0) int m
Integer.parseInt(args1) int result new
intn1 for (int i 0 i lt m i)
cnt 0 for (j 0 j lt n j)
if (heads()) cnt resultcnt
for (j 0 j lt n j) if
(resultj 0) System.out.print(".")
for (int i 0 i lt resultj
i10) 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.
Write a Comment
User Comments (0)
About PowerShow.com