SlideShare una empresa de Scribd logo
1 de 49
Analysis of Algorithms



                                                                ‣    estimating running time
                                                                ‣    mathematical analysis
                                                                ‣    order-of-growth hypotheses
                                                                ‣    input models
                                                                ‣    measuring space
Reference:
  Algorithms in Java, Chapter 2
  Intro to Programming in Java, Section 4.1                      Except as otherwise noted, the content of this presentation
  http://www.cs.princeton.edu/algs4                              is licensed under the Creative Commons Attribution 2.5 License.



 Algorithms in Java, 4th Edition   · Robert Sedgewick and Kevin Wayne · Copyright © 2008             ·    September 2, 2009 2:46:54 AM
Running time




      “ As soon as an Analytic Engine exists, it will necessarily guide the
      future
        course of the science. Whenever any result is sought by its aid,
      the question




                                                                        how many
                                                                        times do you
                                                                        have to turn
                                                                        the crank?




          Charles Babbage (1864)              Analytic Engine



                                                                                       2
Reasons to analyze algorithms


Predict performance.

                                             this course (COS 226)
Compare algorithms.


Provide guarantees.


Understand theoretical basis.                theory of algorithms (COS 423)




              client gets poor performance because programmer
               did not understand performance characteristics




Primary practical reason: avoid performance bugs.
                                                                              3
Some algorithmic successes


Discrete Fourier transform.
•   Break down waveform of N samples into periodic components.
•   Applications: DVD, JPEG, MRI, astrophysics, ….
•   Brute force: N2 steps.
                                                                 Freidrich Gauss
•   FFT algorithm: N log N steps, enables new technology.        1805




            time
                                            quadratic
            64T




            32T



            16T
                                           linearithmic
             8T
                                              linear

           size      1K 2K      4K             8K

                  Linear, linearithmic, and quadratic
                                                                                   4
Some algorithmic successes


N-body Simulation.
•   Simulate gravitational interactions among N bodies.
•   Brute force: N2 steps.
•   Barnes-Hut: N log N steps, enables new research.
                                                          Andrew Appel
                                                          PU '81




            time
                                            quadratic
            64T




            32T



            16T
                                           linearithmic
             8T
                                              linear

           size      1K 2K      4K             8K

                  Linear, linearithmic, and quadratic
                                                                         5
‣   estimating running time
‣   mathematical analysis
‣   order-of-growth hypotheses
‣   input models
‣   measuring space




                                 6
Scientific analysis of algorithms


A framework for predicting performance and comparing algorithms.


Scientific method.
•   Observe some feature of the universe.
•   Hypothesize a model that is consistent with observation.
•   Predict events using the hypothesis.
•   Verify the predictions by making further observations.
•   Validate by repeating until the hypothesis and observations agree.



Principles.
•   Experiments must be reproducible.
•   Hypotheses must be falsifiable.



Universe = computer itself.
                                                                         7
Experimental algorithmics


Every time you run a program you are doing an experiment!


                                         Why is
                                  my program so slow ?




First step. Debug your program!
Second step. Choose input model for experiments.
Third step. Run and time the program for problems of increasing size.


                                                                        8
Example: 3-sum


3-sum. Given N integers, find all triples that sum to exactly zero.
Application. Deeply related to problems in computational geometry.




           % more 8ints.txt
           30 -30 -20 -10 40 0 10 5

           % java ThreeSum < 8ints.txt
            4
            30 -30   0
            30 -20 -10
           -30 -10 40
           -10   0 10




                                                                      9
3-sum: brute-force algorithm



      public class ThreeSum
      {
         public static int count(long[] a)
         {
            int N = a.length;
            int cnt = 0;
             for (int i = 0; i < N; i++)
                for (int j = i+1; j < N; j++)
                   for (int k = j+1; k < N; k++)     check each triple

                      if (a[i] + a[j] + a[k] == 0)
                         cnt++;
            return cnt;
         }

          public static void main(String[] args)
          {
             int[] a = StdArrayIO.readLong1D();
             StdOut.println(count(a));
          }
      }


                                                                         10
Measuring the running time


Q. How to time a program?
A. Manual.




                             11
Measuring the running time


Q. How to time a program?
A. Automatic.

                Stopwatch stopwatch = new Stopwatch();

                ThreeSum.count(a);

                double time = stopwatch.elapsedTime();
                StdOut.println("Running time: " + time + " seconds");

                                          client code



                public class Stopwatch
                {
                   private final long start = System.currentTimeMillis();

                    public double elapsedTime()
                    {
                       long now = System.currentTimeMillis();
                       return (now - start) / 1000.0;
                    }
                }

                                        implementation
                                                                            12
3-sum: initial observations


Data analysis. Observe and plot running time as a function of input size N.




        N           time (seconds) †

       1024                0.26

       2048                2.16

       4096                17.18

       8192               137.76


     † Running Linux on Sun-Fire-X4100




                                                                              13
Empirical analysis


Log-log plot. Plot running time vs. input size N on log-log scale.




                                       slope = 3




                                                              power law

Regression. Fit straight line through data points: c N a.                 slope

Hypothesis. Running time grows cubically with input size: c N 3.
                                                                                  14
Prediction and verification


Hypothesis. 2.5 × 10 –10 × N 3 seconds for input of size N.




Prediction. 17.18 seconds for N = 4,096.


Observations.          N        time (seconds)

                      4096          17.18

                      4096          17.15                     agrees
                      4096          17.17




Prediction. 1100 seconds for N = 16,384.


Observation.           N        time (seconds)
                                                              agrees
                     16384         1118.86


                                                                       15
Doubling hypothesis


Q. What is effect on the running time of doubling the size of the input?




                N          time (seconds) †             ratio

               512                0.03                    -

              1024                0.26                  7.88

              2048                2.16                  8.43

              4096                17.18                 7.96

              8192               137.76                 7.96



           numbers increases   running time increases
           by a factor of 2    by a factor of 8
                                                                lg of ratio is
                                                                exponent in power law
                                                                (lg 7.96 ≈ 3)



Bottom line. Quick way to formulate a power law hypothesis.
                                                                                        16
Experimental algorithmics


Many obvious factors affect running time:
•   Machine.
•   Compiler.
•   Algorithm.
•   Input data.


More factors (not so obvious):
•   Caching.
•   Garbage collection.
•   Just-in-time compilation.
•   CPU use by other applications.


Bad news. It is often difficult to get precise measurements.
Good news. Easier than other sciences.

                          e.g., can run huge number of experiments


                                                                     17
‣   estimating running time
‣   mathematical analysis
‣   order-of-growth hypotheses
‣   input models
‣   measuring space




                                 18
Mathematical models for running time


Total running time: sum of cost × frequency for all operations.
•   Need to analyze program to determine set of operations.
•   Cost depends on machine, compiler.
•   Frequency depends on algorithm, input data.




                                                              Donald Knuth
                                                              1974 Turing Award




In principle, accurate mathematical models are available.


                                                                                  19
Cost of basic operations




                 operation                         example                nanoseconds †

                integer add                         a + b                       2.1

              integer multiply                      a * b                       2.4

               integer divide                       a / b                       5.4

             floating point add                     a + b                       4.6

           floating point multiply                  a * b                       4.2

            floating point divide                   a / b                      13.5

                    sine                      Math.sin(theta)                  91.3

                arctangent                   Math.atan2(y, x)                  129.0

                     ...                               ...                      ...




                           † Running OS X on Macbook Pro 2.2GHz with 2GB RAM




                                                                                          20
Cost of basic operations




                 operation               example         nanoseconds †

            variable declaration          int a                c1

           assignment statement           a = b                c2

              integer compare             a < b                c3

            array element access          a[i]                 c4

                array length            a.length               c5

             1D array allocation       new int[N]            c6 N

             2D array allocation      new int[N][N]          c7 N 2

                string length          s.length()              c8

            substring extraction   s.substring(N/2, N)         c9

            string concatenation          s + t              c10 N




Novice mistake. Abusive string concatenation.
                                                                         21
Example: 1-sum


Q. How many instructions as a function of N?


         int count = 0;
         for (int i = 0; i < N; i++)
            if (a[i] == 0) count++;




                 operation         frequency

            variable declaration        2

           assignment statement         2

           less than comparison        N+1

            equal to comparison         N
                                               between N (no zeros)
                                               and 2N (all zeros)
               array access             N

                 increment           ≤ 2N



                                                                      22
Example: 2-sum


Q. How many instructions as a function of N?


         int count = 0;
         for (int i = 0; i < N; i++)
            for (int j = i+1; j < N; j++)
               if (a[i] + a[j] == 0) count++;



                                                                                           1
                                                         0 + 1 + 2 + . . . + (N − 1)   =     N (N − 1)
                 operation             frequency                                           2
                                                                                             N
                                                                                       =
                                                                                             2
            variable declaration         N+2

           assignment statement          N+2

           less than comparison    1/2 (N + 1) (N + 2)

            equal to comparison      1/2 N (N − 1)
                                                               tedious to count exactly
               array access            N (N − 1)

                 increment               ≤ N2



                                                                                                         23
Tilde notation


•   Estimate running time (or memory) as a function of input size N.
•   Ignore lower order terms.
    - when N is large, terms are negligible
    - when N is small, we don't care

Ex 1.     6 N 3 + 20 N + 16
 
                      ~ 6N3
Ex 2.     6 N 3 + 100 N 4/3 + 56
                   ~ 6N3
Ex 3.     6 N 3 + 17 N    2   lg N + 7 N
           ~ 6N3


                 discard lower-order terms
                 (e.g., N = 1000 6 trillion vs. 169 million)



                                                                      f (N)
      Technical definition. f(N) ~ g(N) means                  lim          = 1
                                                               N→   ∞ g(N)




                                                €
                                                                                  24
Example: 2-sum


Q. How long will it take as a function of N?


          int count = 0;
          for (int i = 0; i < N; i++)
             for (int j = i+1; j < N; j++)
                if (a[i] + a[j] == 0) count++;           "inner loop"




                 operation           frequency    cost   total cost

            variable declaration        ~ N        c1      ~ c1 N

           assignment statement         ~ N        c2      ~ c2 N

            less than comparison      ~ 1/2 N 2
                                                   c3     ~ c3 N 2
            equal to comparison       ~ 1/2 N 2

               array access            ~ N2        c4     ~ c4 N 2

                 increment             ≤ N2        c5     ≤ c5 N 2
                   total                                   ~ cN2

                                                                        25
Example: 3-sum
       478                                                              Algorithms and Data
Q. How many instructions as a function of N?

       public class ThreeSum
       {                                                            the leading term of math
          public static int count(int[] a)
          {
                                                                    pressions by using a m
             int N = a.length;                                      device known as the til
             int cnt = 0;                                    1
                                                                    We write f (N) to re
              for (int i = 0; i < N; i++)
                                                                    quantity that, when divid
                 for (int j = i+1; j < N; j++)               N
     inner
                    for (int k = j+1; k < N; k++)            ~N / 2
                                                               2
                                                                    approaches 1 as N grow
      loop
                       if (a[i] + a[j] + a[k] == 0)          ~N / 6
                                                               3    write N g (N)N (Nf − 1)(N −to in
                                                                               =       (N) 2)
                                                                          3              3!
                          cnt++;                                    g (N) f (N)1approaches 1
                                                                               ∼   N 3
                                                                                 6
              return cnt;              depends on input data        With this notation, we
           }
                                                                    complicated parts of an
           public static void main(String[] args)                   that represent small val
           {
              int[] a = StdArrayIO.readInt1D();                     ample, the if statement
              int cnt = count(a);                                   is executed N 3/6 tim
              StdOut.println(cnt);
           }
                                                                    N (N 1)(N 2)/6 N 3/
      }
Remark. Focus on instructions in inner loop; ignore everything else!N/3, which certainly, whe
        Anatomy of a program’s statement execution frequencies      N 3/6, approaches 1 as N
                                                                    notation is useful when t
                                                                    ter the leading term are r   26
Mathematical models for running time


In principle, accurate mathematical models are available.


In practice,
•   Formulas can be complicated.
•   Advanced mathematics might be required.
•   Exact models best left for experts.

                 costs (depend on machine, compiler)



          TN = c1 A + c2 B + c3 C + c4 D + c5 E
            A = variable declarations
            B = assignment statements                         frequencies
            C = compare                                (depend on algorithm, input)
            D = array access
            E = increment



Bottom line. We use approximate models in this course: TN ~ c N3.
                                                                                      27
‣   estimating running time
‣   mathematical analysis
‣   order-of-growth hypotheses
‣   input models
‣   measuring space




                                 28
Common order-of-growth hypotheses


To determine order-of-growth:
•   Assume a power law TN ~ c N a.
•   Estimate exponent a with doubling hypothesis.
•   Validate with mathematical analysis.

                                                           N        time (seconds) †
Ex. ThreeSumDeluxe.java
                                                         1,000             0.43
Food for thought. How is it implemented?
                                                         2,000             0.53

                                                         4,000             1.01

                                                         8,000             2.87

                                                         16,000           11.00

                                                         32,000           44.64

                                                         64,000          177.48

                                                                  observations




Caveat. Can't identify logarithmic factors with doubling hypothesis.

                                                                                       29
Common order-of-growth hypotheses
                         Linearithmic. We use the term linearithmic to describe programs whose running
          time for a problem of size N has order of growth N log N. Again, the base of the
Good news. the smallrelevant. For example, CouponCollector (PROGRAM 1.4.2) is lin-
          logarithm is not set of functions
          earithmic.N, prototypical example 2, mergesort (see NROGRAM 4.2.6). Several im-
           1, log The N, N log N, N is N3, and 2P
          portant problems have natural solutions that are quadratic but clever algorithms
suffices to describe order-of-growth of typical algorithms. impor-
          that are linearithmic. Such algorithms (including mergesort) are critically
          tant in practice because they enable us to address problem sizes far larger than
          could be addressed with quadratic solutions. In the next section, we consider a
                                                   general design technique for developing
   time                                                      growth rate
                                                   linearithmic algorithms.       name                              T(2N) / T(N)
  1024T
                                                                      Quadratic. A 1           constant                  1
           exponential




                                                                                   typical program whose
                                 c

                                            tic
                                cubi




                                                         ea c
                                                              i
                                                            hm
                                        dra



   512T
                                                                      running time has order of growth N 2



                                                           r
                                                     rit
                                       qua




                                                                                    log N          logarithmic          ~1
                                                   ea

                                                           lin
                                                  lin                 has two nested for loops, used for some
                                                                      calculation involving all pairs linear ele-
                                                                                      N                 of N             2
    64T                                                               ments. The force update double loop in
                                                                      NBody (PROGRAM 3.4.2) is a linearithmicof
                                                                                   N log N          prototype           ~2
                                                                      the programs in this classification, as is
                                                                      the elementaryN2              quadratic
                                                                                      sorting algorithm Inser-           4
     8T                                                               tion (PROGRAM 4.2.4).
     4T
                                                                                     N 3
                                                                                                      cubic              8

     2T                                                               Cubic. Our example for this section,
                                                        logarithmic
                                                                                   N
                                                                                     2             exponential         T(N)
                                                                      ThreeSum,
                                                                    is cubic (its running time has
       T
                                              constant   order of growth N 3) because it has three
    size    1K 2K 4K 8K                            1024K
                                                         nested for loops, to process all triples of
                                                         N elements. The running time of matrix      factor for
              Orders of growth (log-log plot)
                                                         multiplication, as implemented in SEC- doubling hypothesis
                                                         TION 1.4 has order of growth M 3 to mul-
              tiply two M-by-M matrices, so the basic matrix multiplication algorithm is often
              considered to be cubic. However, the size of the input (the number of entries in the                                 30
Common order-of-growth hypotheses


growth
             name              typical code framework       description          example
 rate


   1       constant                a = b + c;                statement       add two numbers


                                 while (N > 1)
 log N    logarithmic      {    N = N / 2; ...          }   divide in half    binary search



                         for (int i = 0; i < N; i++)
  N          linear             { ...        }                  loop         find the maximum



                                                               divide
N log N   linearithmic             [see lecture 5]                              mergesort
                                                            and conquer


                         for (int i = 0; i < N; i++)
  N2       quadratic      for (int j = 0; j < N; j++)       double loop       check all pairs
                                 { ...        }


                         for (int i = 0; i < N; i++)
                          for (int j = 0; j < N; j++)
  N3         cubic          for (int k = 0; k < N; k++)      triple loop     check all triples
                                   { ...        }

                                                             exhaustive         check all
  2N      exponential             [see lecture 24]
                                                               search          possibilities

                                                                                                 31
Practical implications of order-of-growth


Q. How long to process millions of inputs?
Ex. Population of NYC was "millions" in 1970s; still is.
                                                           seconds     equivalent

                                                              1         1 second
Q. How many inputs can be processed in minutes?
                                                             10        10 seconds
Ex. Customers lost patience waiting "minutes" in 1970s;
                                                             102      1.7 minutes
they still do.
                                                             103       17 minutes

                                                             104       2.8 hours

                                                             105        1.1 days
For back-of-envelope calculations, assume:
                                                             106       1.6 weeks

                                                             107       3.8 months
                          processor   instructions
                 decade
                            speed      per second            108        3.1 years

                 1970s     1 MHz          106                109      3.1 decades

                 1980s     10 MHz         107               1010      3.1 centuries

                 1990s    100 MHz         108                …          forever

                 2000s     1 GHz          109               1017     age of universe


                                                                                       32
Practical implications of order-of-growth




                       problem size solvable in minutes                         time to process millions of inputs
growth
 rate
            1970s           1980s          1990s           2000s      1970s           1980s          1990s            2000s



   1          any             any           any              any      instant        instant        instant          instant



 log N        any             any           any              any      instant        instant        instant          instant


                            tens of     hundreds of
  N         millions                                       billions   minutes        seconds        second           instant
                            millions      millions

          hundreds of                                 hundreds of                                   tens of
N log N                     millions      millions                     hour          minutes                         seconds
           thousands                                    millions                                    seconds

                                                           tens of
  N2       hundreds        thousand      thousands                    decades         years         months            weeks
                                                          thousands


  N3       hundred         hundreds      thousand         thousands    never          never          never           millennia




                                                                                                                                 33
Practical implications of order-of-growth



                                                                    effect on a program that
                                                                     runs for a few seconds
growth
              name                   description
 rate
                                                              time for 100x          size for 100x
                                                                more data           faster computer


   1        constant          independent of input size             -                          -


 log N     logarithmic     nearly independent of input size         -                          -


  N           linear            optimal for N inputs          a few minutes               100x


N log N    linearithmic      nearly optimal for N inputs      a few minutes               100x


  N2        quadratic      not practical for large problems   several hours               10x


  N3          cubic       not practical for medium problems   several weeks               4-5x


  2N       exponential      useful only for tiny problems       forever                    1x




                                                                                                      34
‣   estimating running time
‣   mathematical analysis
‣   order-of-growth hypotheses
‣   input models
‣   measuring space




                                 35
Types of analyses


Best case. Running time determined by easiest inputs.
Ex. N-1 compares to insertion sort N elements in ascending order.


Worst case. Running time guarantee for all inputs.
Ex. No more than ½N2 compares to insertion sort any N elements.


Average case. Expected running time for "random" input.
Ex. ~ ¼ N2 compares on average to insertion sort N random elements.




                                                                      36
Commonly-used notations




 notation       provides          example       shorthand for               used to


                                                     10 N 2
                                                                            provide
  Tilde       leading term        ~ 10 N 2    10 N 2 + 22 N log N
                                                                       approximate model
                                               10 N 2 + 2 N +37


                                                       N2
               asymptotic                                                   classify
Big Theta                         Θ(N 2)             9000 N 2
               growth rate                                                algorithms
                                             5 N 2 + 22 N log N + 3N


                                                      N2
                                                                            develop
 Big Oh     Θ(N 2) and smaller    O(N 2)             100 N
                                                                         upper bounds
                                                22 N log N + 3 N


                                                    9000 N 2
                                                                            develop
Big Omega   Θ(N 2)   and larger   Ω(N 2)               N5
                                                                         lower bounds
                                             N 3 + 22 N log N + 3 N
Commonly-used notations


Ex 1. Our brute-force 3-sum algorithm takes Θ(N 3) time.


Ex 2. Conjecture: worst-case running time for any 3-sum algorithm is Ω(N 2).


Ex 3. Insertion sort uses O(N 2) compares to sort any array of N elements;
it uses ~ N compares in best case (already sorted) and ~ ½N 2 compares in the
worst case (reverse sorted).


Ex 4. The worst-case height of a tree created with union find with path
compression is Θ(N).


Ex 5. The height of a tree created with weighted quick union is O(log N).


                                                    base of logarithm absorbed by big-Oh

                                                                       1
                                                          loga N =          logb N
                                                                     logb a


                                                                                           38
Predictions and guarantees


Theory of algorithms. Worst-case running time of an algorithm is O(f(N)).


Advantages
•   describes guaranteed performance.      time/memory


•   O-notation absorbs input model.

                                                                            f(N)
Challenges                                           values represented

•
                                                         by O(f(N))
    Cannot use to predict performance.
•   Cannot use to compare algorithms.




                                                             input size



                                                                                   39
Predictions and guarantees


Experimental algorithmics. Given input model,average-case
running time is ~ c f(N).


Advantages.                                 time/memory


•   Can use to predict performance.
•   Can use to compare algorithms.
                                                                               c f(N)

                                                          values represented
Challenges.                                                   by ~ c f(N)

•   Need to develop accurate input model.
•   May not provide guarantees.




                                                              input size



                                                                                    40
‣   estimating running time
‣   mathematical analysis
‣   order-of-growth hypotheses
‣   input models
‣   measuring space




                                 41
Typical memory requirements for primitive types in Java


Bit. 0 or 1.
Byte. 8 bits.
Megabyte (MB). 210 bytes ~ 1 million bytes.
Gigabyte (GB). 220 bytes ~ 1 billion bytes.


                        type       bytes

                      boolean        1

                        byte         1

                        char         2

                        int          4

                       float         4

                        long         8

                       double        8




                                                          42
Typical memory requirements for arrays in Java


Array overhead. 16 bytes on a typical machine.




          type              bytes                       type              bytes

        char[]             2N + 16                   char[][]        2N2 + 20N + 16

         int[]             4N + 16                   int[][]         4N2 + 20N + 16

       double[]            8N + 16                 double[][]        8N2 + 20N + 16


            one-dimensional arrays                        two-dimensional arrays




Q. What's the biggest double[] array you can store on your computer?


                                     typical computer in 2008 has about 1GB memory



                                                                                      43
uble   instance vari-
       Typical memory requirements for objects in Java
 rograms create mil-
 nformation needed
       Object overhead. 8 bytes on a typical machine.
ansparency). A refer-
       Reference. 4 bytes on a typical machine.
data type contains a
ytes for theEach Complex object consumes 24 bytes of memory.
       Ex 1.
             reference
eded for the object’s
am 3.2.1)     public class Complex
                   32 bytes                        8 bytes overhead for object

 Charge       {
                       object
                 private double re;                8 bytes
                     overhead
 uble rx;        private double im;                8 bytes
 uble ry;               rx
                 ...           double
 uble q;                ry
              }                 values             24 bytes
                         q


gram 3.2.6)        24 bytes
 Complex
                      object
                     overhead
ouble re;
ouble im;               re      double
                        im      values


 rary)             12 bytes                                                      44
object (Program 3.2.6)         24 bytes
 c class Typical
         Complex     memory requirements for objects in Java
                                  object
                                 overhead
 ivate double re;
 ivate double im;                  re         double
           Object        overhead. im
                                    8     bytes on a typical machine.
                                              values

           Reference. 4 bytes on a typical machine.
ct (Java library)              12 bytes
ic class Color
           Ex 2. A String of length N consumes 2N + 40 bytes.
                           object
                          overhead
rivate   byte   r;
rivate   byte   g;              r g b a
rivate   byte   b;
rivate   byte   a;
                public class String
                              byte                                      8 bytes overhead for object
                              values
                {
                     private int offset;                                4 bytes
 object (Program 3.3.4)    16 bytes + string + vector
                     private int count;                                 4 bytes
 c class Document
                     private int hash;
                               object                                   4 bytes
                             overhead
 ivate String id;private char[] value;                                  4 bytes for reference
 ivate Vector profile;          id
                     ...                   references                   (plus 2N + 16 bytes for array)
                             profile
                }
                                                                        2N + 40 bytes
ect (Java library)             24 bytes + char array
c class String
                                  object
                                 overhead
ivate    char[] val;
ivate    int offset;              value       reference
ivate    int count;              offset
ivate    int hash;                              int
                                  count        values
                                   hash
                                                                                                         45
Example 1


Q. How much memory does this program use as a function of N ?



        public class RandomWalk {
           public static void main(String[] args) {
              int N = Integer.parseInt(args[0]);
              int[][] count = new int[N][N];
              int x = N/2;
              int y = N/2;

                for (int i = 0; i < N; i++) {
                   // no new variable declared in loop
                   ...
                   count[x][y]++;
                }
            }
        }




                                                                46
Example 2


Q. How much memory does this code fragment use as a function of N ?




              ...
              int N = Integer.parseInt(args[0]);
              for (int i = 0; i < N; i++) {
                   int[] a = new int[N];
                  ...
              }




Remark. Java automatically reclaims memory when it is no longer in use.


                                                                          47
Out of memory


Q. What if I run out of memory?



 % java RandomWalk 10000
 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

 % java -Xmx 500m RandomWalk 10000
 ...

 % java RandomWalk 30000
 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

 % java -Xmx 4500m RandomWalk 30000
 Invalid maximum heap size: -Xmx4500m
 The specified size exceeds the maximum representable size.
 Could not create the Java virtual machine.




                                                                          48
Turning the crank: summary


In principle, accurate mathematical models are available.
In practice, approximate mathematical models are easily achieved.


Timing may be flawed?
•   Limits on experiments insignificant compared to
    other sciences.


•   Mathematics might be difficult?
•   Only a few functions seem to turn up.
•   Doubling hypothesis cancels complicated constants.


Actual data might not match input model?
•   Need to understand input to effectively process it.
•   Approach 1: design for the worst case.
•   Approach 2: randomize, depend on probabilistic guarantee.


                                                                    49

Más contenido relacionado

La actualidad más candente

QX Simulator and quantum programming - 2020-04-28
QX Simulator and quantum programming - 2020-04-28QX Simulator and quantum programming - 2020-04-28
QX Simulator and quantum programming - 2020-04-28Aritra Sarkar
 
HiPEAC'19 Tutorial on Quantum algorithms using QX - 2019-01-23
HiPEAC'19 Tutorial on Quantum algorithms using QX - 2019-01-23HiPEAC'19 Tutorial on Quantum algorithms using QX - 2019-01-23
HiPEAC'19 Tutorial on Quantum algorithms using QX - 2019-01-23Aritra Sarkar
 
How to Generate Personalized Tasks and Sample Solutions for Anonymous Peer Re...
How to Generate Personalized Tasks and Sample Solutions for Anonymous Peer Re...How to Generate Personalized Tasks and Sample Solutions for Anonymous Peer Re...
How to Generate Personalized Tasks and Sample Solutions for Anonymous Peer Re...Mathias Magdowski
 
MSc Thesis Defense Presentation
MSc Thesis Defense PresentationMSc Thesis Defense Presentation
MSc Thesis Defense PresentationMostafa Elhoushi
 
Practical Spherical Harmonics Based PRT Methods
Practical Spherical Harmonics Based PRT MethodsPractical Spherical Harmonics Based PRT Methods
Practical Spherical Harmonics Based PRT MethodsNaughty Dog
 
SPU Optimizations - Part 2
SPU Optimizations - Part 2SPU Optimizations - Part 2
SPU Optimizations - Part 2Naughty Dog
 
REDUCING TIMED AUTOMATA: A NEW APPROACH
REDUCING TIMED AUTOMATA: A NEW APPROACHREDUCING TIMED AUTOMATA: A NEW APPROACH
REDUCING TIMED AUTOMATA: A NEW APPROACHijistjournal
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexityAnkit Katiyar
 
SPU Optimizations-part 1
SPU Optimizations-part 1SPU Optimizations-part 1
SPU Optimizations-part 1Naughty Dog
 
Design limitations and its effect in the performance of ZC1-DPLL
Design limitations and its effect in the performance of ZC1-DPLLDesign limitations and its effect in the performance of ZC1-DPLL
Design limitations and its effect in the performance of ZC1-DPLLIDES Editor
 
Chapter 2 dynamic characteristics of instruments
Chapter 2 dynamic characteristics of instrumentsChapter 2 dynamic characteristics of instruments
Chapter 2 dynamic characteristics of instrumentstalbachew tadesse nadew
 
Digital Signal Processing[ECEG-3171]-Ch1_L03
Digital Signal Processing[ECEG-3171]-Ch1_L03Digital Signal Processing[ECEG-3171]-Ch1_L03
Digital Signal Processing[ECEG-3171]-Ch1_L03Rediet Moges
 
Digital Signal Processing[ECEG-3171]-Ch1_L04
Digital Signal Processing[ECEG-3171]-Ch1_L04Digital Signal Processing[ECEG-3171]-Ch1_L04
Digital Signal Processing[ECEG-3171]-Ch1_L04Rediet Moges
 
Computational Method to Solve the Partial Differential Equations (PDEs)
Computational Method to Solve the Partial Differential  Equations (PDEs)Computational Method to Solve the Partial Differential  Equations (PDEs)
Computational Method to Solve the Partial Differential Equations (PDEs)Dr. Khurram Mehboob
 
2015 12-10 chabert
2015 12-10 chabert2015 12-10 chabert
2015 12-10 chabertSCEE Team
 
new optimization algorithm for topology optimization
new optimization algorithm for topology optimizationnew optimization algorithm for topology optimization
new optimization algorithm for topology optimizationSeonho Park
 

La actualidad más candente (20)

QX Simulator and quantum programming - 2020-04-28
QX Simulator and quantum programming - 2020-04-28QX Simulator and quantum programming - 2020-04-28
QX Simulator and quantum programming - 2020-04-28
 
HiPEAC'19 Tutorial on Quantum algorithms using QX - 2019-01-23
HiPEAC'19 Tutorial on Quantum algorithms using QX - 2019-01-23HiPEAC'19 Tutorial on Quantum algorithms using QX - 2019-01-23
HiPEAC'19 Tutorial on Quantum algorithms using QX - 2019-01-23
 
How to Generate Personalized Tasks and Sample Solutions for Anonymous Peer Re...
How to Generate Personalized Tasks and Sample Solutions for Anonymous Peer Re...How to Generate Personalized Tasks and Sample Solutions for Anonymous Peer Re...
How to Generate Personalized Tasks and Sample Solutions for Anonymous Peer Re...
 
MSc Thesis Defense Presentation
MSc Thesis Defense PresentationMSc Thesis Defense Presentation
MSc Thesis Defense Presentation
 
Practical Spherical Harmonics Based PRT Methods
Practical Spherical Harmonics Based PRT MethodsPractical Spherical Harmonics Based PRT Methods
Practical Spherical Harmonics Based PRT Methods
 
SPU Optimizations - Part 2
SPU Optimizations - Part 2SPU Optimizations - Part 2
SPU Optimizations - Part 2
 
REDUCING TIMED AUTOMATA: A NEW APPROACH
REDUCING TIMED AUTOMATA: A NEW APPROACHREDUCING TIMED AUTOMATA: A NEW APPROACH
REDUCING TIMED AUTOMATA: A NEW APPROACH
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Bin packing
Bin packingBin packing
Bin packing
 
SPU Optimizations-part 1
SPU Optimizations-part 1SPU Optimizations-part 1
SPU Optimizations-part 1
 
CLIM Program: Remote Sensing Workshop, Optimization for Distributed Data Syst...
CLIM Program: Remote Sensing Workshop, Optimization for Distributed Data Syst...CLIM Program: Remote Sensing Workshop, Optimization for Distributed Data Syst...
CLIM Program: Remote Sensing Workshop, Optimization for Distributed Data Syst...
 
Design limitations and its effect in the performance of ZC1-DPLL
Design limitations and its effect in the performance of ZC1-DPLLDesign limitations and its effect in the performance of ZC1-DPLL
Design limitations and its effect in the performance of ZC1-DPLL
 
Chapter 2 dynamic characteristics of instruments
Chapter 2 dynamic characteristics of instrumentsChapter 2 dynamic characteristics of instruments
Chapter 2 dynamic characteristics of instruments
 
Digital Signal Processing[ECEG-3171]-Ch1_L03
Digital Signal Processing[ECEG-3171]-Ch1_L03Digital Signal Processing[ECEG-3171]-Ch1_L03
Digital Signal Processing[ECEG-3171]-Ch1_L03
 
Digital Signal Processing[ECEG-3171]-Ch1_L04
Digital Signal Processing[ECEG-3171]-Ch1_L04Digital Signal Processing[ECEG-3171]-Ch1_L04
Digital Signal Processing[ECEG-3171]-Ch1_L04
 
Computational Method to Solve the Partial Differential Equations (PDEs)
Computational Method to Solve the Partial Differential  Equations (PDEs)Computational Method to Solve the Partial Differential  Equations (PDEs)
Computational Method to Solve the Partial Differential Equations (PDEs)
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Lec7
Lec7Lec7
Lec7
 
2015 12-10 chabert
2015 12-10 chabert2015 12-10 chabert
2015 12-10 chabert
 
new optimization algorithm for topology optimization
new optimization algorithm for topology optimizationnew optimization algorithm for topology optimization
new optimization algorithm for topology optimization
 

Destacado

Ebook En Sams Data Structures & Algorithms In Java
Ebook   En  Sams   Data Structures & Algorithms In JavaEbook   En  Sams   Data Structures & Algorithms In Java
Ebook En Sams Data Structures & Algorithms In JavaAldo Quelopana
 
31350052 introductory-mathematical-analysis-textbook-solution-manual
31350052 introductory-mathematical-analysis-textbook-solution-manual31350052 introductory-mathematical-analysis-textbook-solution-manual
31350052 introductory-mathematical-analysis-textbook-solution-manualMahrukh Khalid
 
Chapter 3 Feasibility analysis(lecture 4 & 5)
Chapter 3 Feasibility analysis(lecture 4 & 5)Chapter 3 Feasibility analysis(lecture 4 & 5)
Chapter 3 Feasibility analysis(lecture 4 & 5)Afzaal Ali
 
1. what is a feasibility study
1. what is a feasibility study1. what is a feasibility study
1. what is a feasibility studyRudy Flores
 

Destacado (7)

Algorithms with-java-1.0
Algorithms with-java-1.0Algorithms with-java-1.0
Algorithms with-java-1.0
 
Mathematical analysis
Mathematical analysisMathematical analysis
Mathematical analysis
 
Ebook En Sams Data Structures & Algorithms In Java
Ebook   En  Sams   Data Structures & Algorithms In JavaEbook   En  Sams   Data Structures & Algorithms In Java
Ebook En Sams Data Structures & Algorithms In Java
 
31350052 introductory-mathematical-analysis-textbook-solution-manual
31350052 introductory-mathematical-analysis-textbook-solution-manual31350052 introductory-mathematical-analysis-textbook-solution-manual
31350052 introductory-mathematical-analysis-textbook-solution-manual
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Chapter 3 Feasibility analysis(lecture 4 & 5)
Chapter 3 Feasibility analysis(lecture 4 & 5)Chapter 3 Feasibility analysis(lecture 4 & 5)
Chapter 3 Feasibility analysis(lecture 4 & 5)
 
1. what is a feasibility study
1. what is a feasibility study1. what is a feasibility study
1. what is a feasibility study
 

Similar a Analysis

Introduction to computing Processing and performance.pdf
Introduction to computing Processing and performance.pdfIntroduction to computing Processing and performance.pdf
Introduction to computing Processing and performance.pdfTulasiramKandula1
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsiqbalphy1
 
Complexity Analysis
Complexity Analysis Complexity Analysis
Complexity Analysis Shaista Qadir
 
VCE Unit 01 (1).pptx
VCE Unit 01 (1).pptxVCE Unit 01 (1).pptx
VCE Unit 01 (1).pptxskilljiolms
 
Design & Analysis of Algorithm course .pptx
Design & Analysis of Algorithm course .pptxDesign & Analysis of Algorithm course .pptx
Design & Analysis of Algorithm course .pptxJeevaMCSEKIOT
 
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...TechVision8
 
2.03.Asymptotic_analysis.pptx
2.03.Asymptotic_analysis.pptx2.03.Asymptotic_analysis.pptx
2.03.Asymptotic_analysis.pptxssuser1fb3df
 
Data Structures - Lecture 1 [introduction]
Data Structures - Lecture 1 [introduction]Data Structures - Lecture 1 [introduction]
Data Structures - Lecture 1 [introduction]Muhammad Hammad Waseem
 
IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...ijceronline
 
Algorithm analysis.pptx
Algorithm analysis.pptxAlgorithm analysis.pptx
Algorithm analysis.pptxDrBashirMSaad
 
A calculus of mobile Real-Time processes
A calculus of mobile Real-Time processesA calculus of mobile Real-Time processes
A calculus of mobile Real-Time processesPolytechnique Montréal
 
Multicore programmingandtpl(.net day)
Multicore programmingandtpl(.net day)Multicore programmingandtpl(.net day)
Multicore programmingandtpl(.net day)Yan Drugalya
 
Artificial Neural Networks for Storm Surge Prediction in North Carolina
Artificial Neural Networks for Storm Surge Prediction in North CarolinaArtificial Neural Networks for Storm Surge Prediction in North Carolina
Artificial Neural Networks for Storm Surge Prediction in North CarolinaAnton Bezuglov
 

Similar a Analysis (20)

Introduction to computing Processing and performance.pdf
Introduction to computing Processing and performance.pdfIntroduction to computing Processing and performance.pdf
Introduction to computing Processing and performance.pdf
 
2017 07 04_cmmse_quantum_programming_v1
2017 07 04_cmmse_quantum_programming_v12017 07 04_cmmse_quantum_programming_v1
2017 07 04_cmmse_quantum_programming_v1
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Complexity Analysis
Complexity Analysis Complexity Analysis
Complexity Analysis
 
VCE Unit 01 (1).pptx
VCE Unit 01 (1).pptxVCE Unit 01 (1).pptx
VCE Unit 01 (1).pptx
 
Design & Analysis of Algorithm course .pptx
Design & Analysis of Algorithm course .pptxDesign & Analysis of Algorithm course .pptx
Design & Analysis of Algorithm course .pptx
 
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
Data Structures and Algorithms Lecture 2: Analysis of Algorithms, Asymptotic ...
 
Lecture_2_v2_qc.pptx
Lecture_2_v2_qc.pptxLecture_2_v2_qc.pptx
Lecture_2_v2_qc.pptx
 
S1140183 Presentation
S1140183 PresentationS1140183 Presentation
S1140183 Presentation
 
2.03.Asymptotic_analysis.pptx
2.03.Asymptotic_analysis.pptx2.03.Asymptotic_analysis.pptx
2.03.Asymptotic_analysis.pptx
 
Cs2251 daa
Cs2251 daaCs2251 daa
Cs2251 daa
 
Quantum programming
Quantum programmingQuantum programming
Quantum programming
 
Analysis.ppt
Analysis.pptAnalysis.ppt
Analysis.ppt
 
Data Structures - Lecture 1 [introduction]
Data Structures - Lecture 1 [introduction]Data Structures - Lecture 1 [introduction]
Data Structures - Lecture 1 [introduction]
 
IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...IJCER (www.ijceronline.com) International Journal of computational Engineerin...
IJCER (www.ijceronline.com) International Journal of computational Engineerin...
 
chapter 1
chapter 1chapter 1
chapter 1
 
Algorithm analysis.pptx
Algorithm analysis.pptxAlgorithm analysis.pptx
Algorithm analysis.pptx
 
A calculus of mobile Real-Time processes
A calculus of mobile Real-Time processesA calculus of mobile Real-Time processes
A calculus of mobile Real-Time processes
 
Multicore programmingandtpl(.net day)
Multicore programmingandtpl(.net day)Multicore programmingandtpl(.net day)
Multicore programmingandtpl(.net day)
 
Artificial Neural Networks for Storm Surge Prediction in North Carolina
Artificial Neural Networks for Storm Surge Prediction in North CarolinaArtificial Neural Networks for Storm Surge Prediction in North Carolina
Artificial Neural Networks for Storm Surge Prediction in North Carolina
 

Más de Sri Prasanna

Más de Sri Prasanna (20)

Qr codes para tech radar
Qr codes para tech radarQr codes para tech radar
Qr codes para tech radar
 
Qr codes para tech radar 2
Qr codes para tech radar 2Qr codes para tech radar 2
Qr codes para tech radar 2
 
Test
TestTest
Test
 
Test
TestTest
Test
 
assds
assdsassds
assds
 
assds
assdsassds
assds
 
asdsa
asdsaasdsa
asdsa
 
dsd
dsddsd
dsd
 
About stacks
About stacksAbout stacks
About stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About  StacksAbout  Stacks
About Stacks
 
About Stacks
About StacksAbout Stacks
About Stacks
 
About Stacks
About StacksAbout Stacks
About Stacks
 
Network and distributed systems
Network and distributed systemsNetwork and distributed systems
Network and distributed systems
 
Introduction & Parellelization on large scale clusters
Introduction & Parellelization on large scale clustersIntroduction & Parellelization on large scale clusters
Introduction & Parellelization on large scale clusters
 
Mapreduce: Theory and implementation
Mapreduce: Theory and implementationMapreduce: Theory and implementation
Mapreduce: Theory and implementation
 
Other distributed systems
Other distributed systemsOther distributed systems
Other distributed systems
 

Último

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptxPoojaSen20
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 

Último (20)

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
PSYCHIATRIC History collection FORMAT.pptx
PSYCHIATRIC   History collection FORMAT.pptxPSYCHIATRIC   History collection FORMAT.pptx
PSYCHIATRIC History collection FORMAT.pptx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 

Analysis

  • 1. Analysis of Algorithms ‣ estimating running time ‣ mathematical analysis ‣ order-of-growth hypotheses ‣ input models ‣ measuring space Reference: Algorithms in Java, Chapter 2 Intro to Programming in Java, Section 4.1 Except as otherwise noted, the content of this presentation http://www.cs.princeton.edu/algs4 is licensed under the Creative Commons Attribution 2.5 License. Algorithms in Java, 4th Edition · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · September 2, 2009 2:46:54 AM
  • 2. Running time “ As soon as an Analytic Engine exists, it will necessarily guide the future course of the science. Whenever any result is sought by its aid, the question how many times do you have to turn the crank? Charles Babbage (1864) Analytic Engine 2
  • 3. Reasons to analyze algorithms Predict performance. this course (COS 226) Compare algorithms. Provide guarantees. Understand theoretical basis. theory of algorithms (COS 423) client gets poor performance because programmer did not understand performance characteristics Primary practical reason: avoid performance bugs. 3
  • 4. Some algorithmic successes Discrete Fourier transform. • Break down waveform of N samples into periodic components. • Applications: DVD, JPEG, MRI, astrophysics, …. • Brute force: N2 steps. Freidrich Gauss • FFT algorithm: N log N steps, enables new technology. 1805 time quadratic 64T 32T 16T linearithmic 8T linear size 1K 2K 4K 8K Linear, linearithmic, and quadratic 4
  • 5. Some algorithmic successes N-body Simulation. • Simulate gravitational interactions among N bodies. • Brute force: N2 steps. • Barnes-Hut: N log N steps, enables new research. Andrew Appel PU '81 time quadratic 64T 32T 16T linearithmic 8T linear size 1K 2K 4K 8K Linear, linearithmic, and quadratic 5
  • 6. estimating running time ‣ mathematical analysis ‣ order-of-growth hypotheses ‣ input models ‣ measuring space 6
  • 7. Scientific analysis of algorithms A framework for predicting performance and comparing algorithms. Scientific method. • Observe some feature of the universe. • Hypothesize a model that is consistent with observation. • Predict events using the hypothesis. • Verify the predictions by making further observations. • Validate by repeating until the hypothesis and observations agree. Principles. • Experiments must be reproducible. • Hypotheses must be falsifiable. Universe = computer itself. 7
  • 8. Experimental algorithmics Every time you run a program you are doing an experiment! Why is my program so slow ? First step. Debug your program! Second step. Choose input model for experiments. Third step. Run and time the program for problems of increasing size. 8
  • 9. Example: 3-sum 3-sum. Given N integers, find all triples that sum to exactly zero. Application. Deeply related to problems in computational geometry. % more 8ints.txt 30 -30 -20 -10 40 0 10 5 % java ThreeSum < 8ints.txt 4 30 -30 0 30 -20 -10 -30 -10 40 -10 0 10 9
  • 10. 3-sum: brute-force algorithm public class ThreeSum { public static int count(long[] a) { int N = a.length; int cnt = 0; for (int i = 0; i < N; i++) for (int j = i+1; j < N; j++) for (int k = j+1; k < N; k++) check each triple if (a[i] + a[j] + a[k] == 0) cnt++; return cnt; } public static void main(String[] args) { int[] a = StdArrayIO.readLong1D(); StdOut.println(count(a)); } } 10
  • 11. Measuring the running time Q. How to time a program? A. Manual. 11
  • 12. Measuring the running time Q. How to time a program? A. Automatic. Stopwatch stopwatch = new Stopwatch(); ThreeSum.count(a); double time = stopwatch.elapsedTime(); StdOut.println("Running time: " + time + " seconds"); client code public class Stopwatch { private final long start = System.currentTimeMillis(); public double elapsedTime() { long now = System.currentTimeMillis(); return (now - start) / 1000.0; } } implementation 12
  • 13. 3-sum: initial observations Data analysis. Observe and plot running time as a function of input size N. N time (seconds) † 1024 0.26 2048 2.16 4096 17.18 8192 137.76 † Running Linux on Sun-Fire-X4100 13
  • 14. Empirical analysis Log-log plot. Plot running time vs. input size N on log-log scale. slope = 3 power law Regression. Fit straight line through data points: c N a. slope Hypothesis. Running time grows cubically with input size: c N 3. 14
  • 15. Prediction and verification Hypothesis. 2.5 × 10 –10 × N 3 seconds for input of size N. Prediction. 17.18 seconds for N = 4,096. Observations. N time (seconds) 4096 17.18 4096 17.15 agrees 4096 17.17 Prediction. 1100 seconds for N = 16,384. Observation. N time (seconds) agrees 16384 1118.86 15
  • 16. Doubling hypothesis Q. What is effect on the running time of doubling the size of the input? N time (seconds) † ratio 512 0.03 - 1024 0.26 7.88 2048 2.16 8.43 4096 17.18 7.96 8192 137.76 7.96 numbers increases running time increases by a factor of 2 by a factor of 8 lg of ratio is exponent in power law (lg 7.96 ≈ 3) Bottom line. Quick way to formulate a power law hypothesis. 16
  • 17. Experimental algorithmics Many obvious factors affect running time: • Machine. • Compiler. • Algorithm. • Input data. More factors (not so obvious): • Caching. • Garbage collection. • Just-in-time compilation. • CPU use by other applications. Bad news. It is often difficult to get precise measurements. Good news. Easier than other sciences. e.g., can run huge number of experiments 17
  • 18. estimating running time ‣ mathematical analysis ‣ order-of-growth hypotheses ‣ input models ‣ measuring space 18
  • 19. Mathematical models for running time Total running time: sum of cost × frequency for all operations. • Need to analyze program to determine set of operations. • Cost depends on machine, compiler. • Frequency depends on algorithm, input data. Donald Knuth 1974 Turing Award In principle, accurate mathematical models are available. 19
  • 20. Cost of basic operations operation example nanoseconds † integer add a + b 2.1 integer multiply a * b 2.4 integer divide a / b 5.4 floating point add a + b 4.6 floating point multiply a * b 4.2 floating point divide a / b 13.5 sine Math.sin(theta) 91.3 arctangent Math.atan2(y, x) 129.0 ... ... ... † Running OS X on Macbook Pro 2.2GHz with 2GB RAM 20
  • 21. Cost of basic operations operation example nanoseconds † variable declaration int a c1 assignment statement a = b c2 integer compare a < b c3 array element access a[i] c4 array length a.length c5 1D array allocation new int[N] c6 N 2D array allocation new int[N][N] c7 N 2 string length s.length() c8 substring extraction s.substring(N/2, N) c9 string concatenation s + t c10 N Novice mistake. Abusive string concatenation. 21
  • 22. Example: 1-sum Q. How many instructions as a function of N? int count = 0; for (int i = 0; i < N; i++) if (a[i] == 0) count++; operation frequency variable declaration 2 assignment statement 2 less than comparison N+1 equal to comparison N between N (no zeros) and 2N (all zeros) array access N increment ≤ 2N 22
  • 23. Example: 2-sum Q. How many instructions as a function of N? int count = 0; for (int i = 0; i < N; i++) for (int j = i+1; j < N; j++) if (a[i] + a[j] == 0) count++; 1 0 + 1 + 2 + . . . + (N − 1) = N (N − 1) operation frequency 2 N = 2 variable declaration N+2 assignment statement N+2 less than comparison 1/2 (N + 1) (N + 2) equal to comparison 1/2 N (N − 1) tedious to count exactly array access N (N − 1) increment ≤ N2 23
  • 24. Tilde notation • Estimate running time (or memory) as a function of input size N. • Ignore lower order terms. - when N is large, terms are negligible - when N is small, we don't care Ex 1. 6 N 3 + 20 N + 16 ~ 6N3 Ex 2. 6 N 3 + 100 N 4/3 + 56 ~ 6N3 Ex 3. 6 N 3 + 17 N 2 lg N + 7 N ~ 6N3 discard lower-order terms (e.g., N = 1000 6 trillion vs. 169 million) f (N) Technical definition. f(N) ~ g(N) means lim = 1 N→ ∞ g(N) € 24
  • 25. Example: 2-sum Q. How long will it take as a function of N? int count = 0; for (int i = 0; i < N; i++) for (int j = i+1; j < N; j++) if (a[i] + a[j] == 0) count++; "inner loop" operation frequency cost total cost variable declaration ~ N c1 ~ c1 N assignment statement ~ N c2 ~ c2 N less than comparison ~ 1/2 N 2 c3 ~ c3 N 2 equal to comparison ~ 1/2 N 2 array access ~ N2 c4 ~ c4 N 2 increment ≤ N2 c5 ≤ c5 N 2 total ~ cN2 25
  • 26. Example: 3-sum 478 Algorithms and Data Q. How many instructions as a function of N? public class ThreeSum { the leading term of math public static int count(int[] a) { pressions by using a m int N = a.length; device known as the til int cnt = 0; 1 We write f (N) to re for (int i = 0; i < N; i++) quantity that, when divid for (int j = i+1; j < N; j++) N inner for (int k = j+1; k < N; k++) ~N / 2 2 approaches 1 as N grow loop if (a[i] + a[j] + a[k] == 0) ~N / 6 3 write N g (N)N (Nf − 1)(N −to in = (N) 2) 3 3! cnt++; g (N) f (N)1approaches 1 ∼ N 3 6 return cnt; depends on input data With this notation, we } complicated parts of an public static void main(String[] args) that represent small val { int[] a = StdArrayIO.readInt1D(); ample, the if statement int cnt = count(a); is executed N 3/6 tim StdOut.println(cnt); } N (N 1)(N 2)/6 N 3/ } Remark. Focus on instructions in inner loop; ignore everything else!N/3, which certainly, whe Anatomy of a program’s statement execution frequencies N 3/6, approaches 1 as N notation is useful when t ter the leading term are r 26
  • 27. Mathematical models for running time In principle, accurate mathematical models are available. In practice, • Formulas can be complicated. • Advanced mathematics might be required. • Exact models best left for experts. costs (depend on machine, compiler) TN = c1 A + c2 B + c3 C + c4 D + c5 E A = variable declarations B = assignment statements frequencies C = compare (depend on algorithm, input) D = array access E = increment Bottom line. We use approximate models in this course: TN ~ c N3. 27
  • 28. estimating running time ‣ mathematical analysis ‣ order-of-growth hypotheses ‣ input models ‣ measuring space 28
  • 29. Common order-of-growth hypotheses To determine order-of-growth: • Assume a power law TN ~ c N a. • Estimate exponent a with doubling hypothesis. • Validate with mathematical analysis. N time (seconds) † Ex. ThreeSumDeluxe.java 1,000 0.43 Food for thought. How is it implemented? 2,000 0.53 4,000 1.01 8,000 2.87 16,000 11.00 32,000 44.64 64,000 177.48 observations Caveat. Can't identify logarithmic factors with doubling hypothesis. 29
  • 30. Common order-of-growth hypotheses Linearithmic. We use the term linearithmic to describe programs whose running time for a problem of size N has order of growth N log N. Again, the base of the Good news. the smallrelevant. For example, CouponCollector (PROGRAM 1.4.2) is lin- logarithm is not set of functions earithmic.N, prototypical example 2, mergesort (see NROGRAM 4.2.6). Several im- 1, log The N, N log N, N is N3, and 2P portant problems have natural solutions that are quadratic but clever algorithms suffices to describe order-of-growth of typical algorithms. impor- that are linearithmic. Such algorithms (including mergesort) are critically tant in practice because they enable us to address problem sizes far larger than could be addressed with quadratic solutions. In the next section, we consider a general design technique for developing time growth rate linearithmic algorithms. name T(2N) / T(N) 1024T Quadratic. A 1 constant 1 exponential typical program whose c tic cubi ea c i hm dra 512T running time has order of growth N 2 r rit qua log N logarithmic ~1 ea lin lin has two nested for loops, used for some calculation involving all pairs linear ele- N of N 2 64T ments. The force update double loop in NBody (PROGRAM 3.4.2) is a linearithmicof N log N prototype ~2 the programs in this classification, as is the elementaryN2 quadratic sorting algorithm Inser- 4 8T tion (PROGRAM 4.2.4). 4T N 3 cubic 8 2T Cubic. Our example for this section, logarithmic N 2 exponential T(N) ThreeSum, is cubic (its running time has T constant order of growth N 3) because it has three size 1K 2K 4K 8K 1024K nested for loops, to process all triples of N elements. The running time of matrix factor for Orders of growth (log-log plot) multiplication, as implemented in SEC- doubling hypothesis TION 1.4 has order of growth M 3 to mul- tiply two M-by-M matrices, so the basic matrix multiplication algorithm is often considered to be cubic. However, the size of the input (the number of entries in the 30
  • 31. Common order-of-growth hypotheses growth name typical code framework description example rate 1 constant a = b + c; statement add two numbers while (N > 1) log N logarithmic { N = N / 2; ... } divide in half binary search for (int i = 0; i < N; i++) N linear { ... } loop find the maximum divide N log N linearithmic [see lecture 5] mergesort and conquer for (int i = 0; i < N; i++) N2 quadratic for (int j = 0; j < N; j++) double loop check all pairs { ... } for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) N3 cubic for (int k = 0; k < N; k++) triple loop check all triples { ... } exhaustive check all 2N exponential [see lecture 24] search possibilities 31
  • 32. Practical implications of order-of-growth Q. How long to process millions of inputs? Ex. Population of NYC was "millions" in 1970s; still is. seconds equivalent 1 1 second Q. How many inputs can be processed in minutes? 10 10 seconds Ex. Customers lost patience waiting "minutes" in 1970s; 102 1.7 minutes they still do. 103 17 minutes 104 2.8 hours 105 1.1 days For back-of-envelope calculations, assume: 106 1.6 weeks 107 3.8 months processor instructions decade speed per second 108 3.1 years 1970s 1 MHz 106 109 3.1 decades 1980s 10 MHz 107 1010 3.1 centuries 1990s 100 MHz 108 … forever 2000s 1 GHz 109 1017 age of universe 32
  • 33. Practical implications of order-of-growth problem size solvable in minutes time to process millions of inputs growth rate 1970s 1980s 1990s 2000s 1970s 1980s 1990s 2000s 1 any any any any instant instant instant instant log N any any any any instant instant instant instant tens of hundreds of N millions billions minutes seconds second instant millions millions hundreds of hundreds of tens of N log N millions millions hour minutes seconds thousands millions seconds tens of N2 hundreds thousand thousands decades years months weeks thousands N3 hundred hundreds thousand thousands never never never millennia 33
  • 34. Practical implications of order-of-growth effect on a program that runs for a few seconds growth name description rate time for 100x size for 100x more data faster computer 1 constant independent of input size - - log N logarithmic nearly independent of input size - - N linear optimal for N inputs a few minutes 100x N log N linearithmic nearly optimal for N inputs a few minutes 100x N2 quadratic not practical for large problems several hours 10x N3 cubic not practical for medium problems several weeks 4-5x 2N exponential useful only for tiny problems forever 1x 34
  • 35. estimating running time ‣ mathematical analysis ‣ order-of-growth hypotheses ‣ input models ‣ measuring space 35
  • 36. Types of analyses Best case. Running time determined by easiest inputs. Ex. N-1 compares to insertion sort N elements in ascending order. Worst case. Running time guarantee for all inputs. Ex. No more than ½N2 compares to insertion sort any N elements. Average case. Expected running time for "random" input. Ex. ~ ¼ N2 compares on average to insertion sort N random elements. 36
  • 37. Commonly-used notations notation provides example shorthand for used to 10 N 2 provide Tilde leading term ~ 10 N 2 10 N 2 + 22 N log N approximate model 10 N 2 + 2 N +37 N2 asymptotic classify Big Theta Θ(N 2) 9000 N 2 growth rate algorithms 5 N 2 + 22 N log N + 3N N2 develop Big Oh Θ(N 2) and smaller O(N 2) 100 N upper bounds 22 N log N + 3 N 9000 N 2 develop Big Omega Θ(N 2) and larger Ω(N 2) N5 lower bounds N 3 + 22 N log N + 3 N
  • 38. Commonly-used notations Ex 1. Our brute-force 3-sum algorithm takes Θ(N 3) time. Ex 2. Conjecture: worst-case running time for any 3-sum algorithm is Ω(N 2). Ex 3. Insertion sort uses O(N 2) compares to sort any array of N elements; it uses ~ N compares in best case (already sorted) and ~ ½N 2 compares in the worst case (reverse sorted). Ex 4. The worst-case height of a tree created with union find with path compression is Θ(N). Ex 5. The height of a tree created with weighted quick union is O(log N). base of logarithm absorbed by big-Oh 1 loga N = logb N logb a 38
  • 39. Predictions and guarantees Theory of algorithms. Worst-case running time of an algorithm is O(f(N)). Advantages • describes guaranteed performance. time/memory • O-notation absorbs input model. f(N) Challenges values represented • by O(f(N)) Cannot use to predict performance. • Cannot use to compare algorithms. input size 39
  • 40. Predictions and guarantees Experimental algorithmics. Given input model,average-case running time is ~ c f(N). Advantages. time/memory • Can use to predict performance. • Can use to compare algorithms. c f(N) values represented Challenges. by ~ c f(N) • Need to develop accurate input model. • May not provide guarantees. input size 40
  • 41. estimating running time ‣ mathematical analysis ‣ order-of-growth hypotheses ‣ input models ‣ measuring space 41
  • 42. Typical memory requirements for primitive types in Java Bit. 0 or 1. Byte. 8 bits. Megabyte (MB). 210 bytes ~ 1 million bytes. Gigabyte (GB). 220 bytes ~ 1 billion bytes. type bytes boolean 1 byte 1 char 2 int 4 float 4 long 8 double 8 42
  • 43. Typical memory requirements for arrays in Java Array overhead. 16 bytes on a typical machine. type bytes type bytes char[] 2N + 16 char[][] 2N2 + 20N + 16 int[] 4N + 16 int[][] 4N2 + 20N + 16 double[] 8N + 16 double[][] 8N2 + 20N + 16 one-dimensional arrays two-dimensional arrays Q. What's the biggest double[] array you can store on your computer? typical computer in 2008 has about 1GB memory 43
  • 44. uble instance vari- Typical memory requirements for objects in Java rograms create mil- nformation needed Object overhead. 8 bytes on a typical machine. ansparency). A refer- Reference. 4 bytes on a typical machine. data type contains a ytes for theEach Complex object consumes 24 bytes of memory. Ex 1. reference eded for the object’s am 3.2.1) public class Complex 32 bytes 8 bytes overhead for object Charge { object private double re; 8 bytes overhead uble rx; private double im; 8 bytes uble ry; rx ... double uble q; ry } values 24 bytes q gram 3.2.6) 24 bytes Complex object overhead ouble re; ouble im; re double im values rary) 12 bytes 44
  • 45. object (Program 3.2.6) 24 bytes c class Typical Complex memory requirements for objects in Java object overhead ivate double re; ivate double im; re double Object overhead. im 8 bytes on a typical machine. values Reference. 4 bytes on a typical machine. ct (Java library) 12 bytes ic class Color Ex 2. A String of length N consumes 2N + 40 bytes. object overhead rivate byte r; rivate byte g; r g b a rivate byte b; rivate byte a; public class String byte 8 bytes overhead for object values { private int offset; 4 bytes object (Program 3.3.4) 16 bytes + string + vector private int count; 4 bytes c class Document private int hash; object 4 bytes overhead ivate String id;private char[] value; 4 bytes for reference ivate Vector profile; id ... references (plus 2N + 16 bytes for array) profile } 2N + 40 bytes ect (Java library) 24 bytes + char array c class String object overhead ivate char[] val; ivate int offset; value reference ivate int count; offset ivate int hash; int count values hash 45
  • 46. Example 1 Q. How much memory does this program use as a function of N ? public class RandomWalk { public static void main(String[] args) { int N = Integer.parseInt(args[0]); int[][] count = new int[N][N]; int x = N/2; int y = N/2; for (int i = 0; i < N; i++) { // no new variable declared in loop ... count[x][y]++; } } } 46
  • 47. Example 2 Q. How much memory does this code fragment use as a function of N ? ... int N = Integer.parseInt(args[0]); for (int i = 0; i < N; i++) { int[] a = new int[N]; ... } Remark. Java automatically reclaims memory when it is no longer in use. 47
  • 48. Out of memory Q. What if I run out of memory? % java RandomWalk 10000 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space % java -Xmx 500m RandomWalk 10000 ... % java RandomWalk 30000 Exception in thread "main" java.lang.OutOfMemoryError: Java heap space % java -Xmx 4500m RandomWalk 30000 Invalid maximum heap size: -Xmx4500m The specified size exceeds the maximum representable size. Could not create the Java virtual machine. 48
  • 49. Turning the crank: summary In principle, accurate mathematical models are available. In practice, approximate mathematical models are easily achieved. Timing may be flawed? • Limits on experiments insignificant compared to other sciences. • Mathematics might be difficult? • Only a few functions seem to turn up. • Doubling hypothesis cancels complicated constants. Actual data might not match input model? • Need to understand input to effectively process it. • Approach 1: design for the worst case. • Approach 2: randomize, depend on probabilistic guarantee. 49

Notas del editor

  1. DFT = fast way to multiply two univariate polynomials FFT reference: Runge-K&amp;#xF6;nig (1924), Cooley-Tukey (1965) rediscovered FFT and popularized it. Discovered by Gauss at age of 28, two years before Fourier&apos;s paper on Fourier transforms! medical imaging = CAT scan, MR astrophysicists want N = 64 gigapoints to search for pulsars; seismologists want N = 10 gigapoints to predict earthquakes
  2. Appel designed his fast multipole method for senior thesis in Physics at Princeton! (actually can be made linear) physicists want N = # atoms in the universe
  3. assumes no integer overflow
  4. Run ThreeSum.java in class from the command-line and wait for it to finish, perhaps up to N = 4096. Ask students to make predictions.
  5. log-log plot identifies power law relationships
  6. Very accurate predictions. 4x input size -&gt; 64x increase in running time for cubic algorithm
  7. put more emphasis on ratios with larger N since lots of noise contained in data for smaller N
  8. if connected to Internet, some hacker is using your computer to send spam, steal your money, etc. Good news: compared to other scientists, running computational experiments is easy. Don&apos;t need to kill cows, blow up islands.
  9. most famous modeling by Knuth - detailed results to accurate predict running time Knuth&apos;s idea seems obvious in hindsight, but in the 1960s, programmers said it would never be possible to analyze the running time of a computer program since they are too complicated. Knuth&apos;s TAOCP is now a classic treatise on the subject.
  10. consider putting in instruction counts for N = 10000
  11. N^3 / 6 arises because N choose 3 is the number of triples i, j, k
  12. this partially explains reason why small set of functions arises from straightforward composition of loops, conditionals, functions
  13. didn&apos;t put column for cost - in 1970s, machine cost millions of $$$ Ex. Population of NYC was &amp;#x201C;millions&amp;#x201D; in 1970s; still is Ex. Customers lost patience waiting &amp;#x201C;minutes&amp;#x201D; in 1970s; still do
  14. Note: not an issue for 3-sum since best, average, and worst are all almost the same.
  15. dependence on input implied ????
  16. table contains typical memory requirements for primitive types
  17. 2d array is represented as an array of arrays: e.g., for double[][], there is one array with N references to arrays (4N + 16) and N double[] arrays (N (8N + 16)) for a total of 8N^2 + 20N + 16 bytes.
  18. A. ~ 4N^2
  19. What to do in last case? Need better algorithm that uses less memory.
  20. Mathematics might be difficult. It is known that properties of singularities of functions in the complex plane play a role in analysis of many algorithms Leading term might not be good enough? Typical programs have bottleneck in one function. Debugging tools are available to identify bottlenecks. Timing may be flawed. Different results on different computers. Different results on same computer at different times.