Complexity

Document Sample
Complexity Powered By Docstoc
					                                          Complexity

More analyses

      Unequal times (but complete analysis possible!)
          o Searching in a complete binary tree
          o Searching in an ordered linked list

A puzzling algorithm

      See Code:

       /* Sort positive integers */
       void CountSort(int L[], int len) {
              int i,j,k,m, *counts;

                m=max(L, len);
                counts=(int *)calloc(sizeof(int)*(m+1));
                if (counts==(int *)0) { /* out of memory */
                        exit(1);
                }

                for (i=0; i<len; i++)
                        counts[L[i]]++;

                k=0;
                for (i=0; i<=m; i++) {
                        for (j=0; j<counts[i]; j++) {
                               L[k] = i;
                               k++;
                        }
                }
                free(counts);
       }

       int max(int L[], int len) {
              int i,m=0;
              for(i=0;i<len;i++)
                      if (L[i]>m) m=L[i];
              return m;
       }

       int const size=10;
       int testl[] = {5,6,23,1,1,2,2,12,11,15};

       int main(int argc, char **argv) {
              int i;
              CountSort(testl,size);

                for (i=0;i<size;i++)
                        printf("%d ",testl[i]);
                printf("\n");
                exit(0);
       }


      Can (sometimes!) sort in linear time
          o But can also take a huge amount of space and time.
A bit of Math

      Recurrence relations
          o A recurrence realtions for a sequence {an} is a formula that expresses a_n
              terms of one or more (fixed) of the previous terms am-1 , an-2, etc for all
              integers n with nn0.
          o A sequence is called a solution of a recurrence relation if its terms satisfy
              the recurrence.

      Application:
          o The sequence of run-times {Tn} of an algorithm on input of size n.

Examples of recurrences

      Example
          o Let an=an-1-an-2, n2, a0=3 and a1=5. Compute a2 and a3.
          o Let an=2an-1-an-2. Check if the sequences given by {3*n}, {2n} and {5}
             satisfy the reccurence.

      Initial condictions
           o The term(s) that precede the first term where the reccurence takes effect.

Practical example

      Find a recurrence relation (and intial condition) for the number of bit strings of
       length n that do not have 2 consecutive 0s. How many such bit strings are there of
       length 5?

Solving recurrence realtions

      Concentrate on linear recurrences with constant coefficients (of degree k)
          o an=c1an-1+c2an-2+…+ckan-k
          o where all the ci’s are constants, and with k initial conditions a0=b0,…,ak-
             1=bk-1.


      Basic idea: look for solutions that look like rn for some r.
          o Derive characteristics equation
          o See examples
          o Warning: multiple roots. . .

Applications of Recurrences

      Analysis of recursive algorithms
          o Searching in a linked list
          o Fibonacci
          o Dot product

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:3
posted:10/4/2011
language:English
pages:2