Friday, July 11, 2014

Recurrences Solving




Recurrences Solving

Recurrence Relations

  • A recurrence relation is an equation which is defined in terms of itself.
  • Why are recurrences good things?
        Many natural functions are easily expressed as recurrences:
         an = a n-1 + 1, a1 = 1--> an = n (polynomial)
         an = 2a n-1  ,a1 = 1--> an = 2n (exponential)
         an = na n-1  ,a1 = 1--> an = n! (weird fun)

  • It is often easy to find a recurrence as the solution of a counting problem.
  • Recursion is Mathematical Induction!
  • In both, we have general and boundary conditions, with the general condition breaking the problem into smaller and smaller pieces.
  • The initial or boundary condition terminates the recursion.

Recurrence Solving

There are 3 general methods for solving recurrences

Substitution: Guess & Verify: Guess a solution and verify: It is correct with an inductive proof.

Recurrence Tree Method: Draw a recurrence tree and calculate the time taken by every level of tree.

Master Theorem: If the recurrence has the form T(n) =aT (n/b) +f(n), then there is a formula that can (often) be applied.

1) Substitution Method: We make a guess for the solution and then we use mathematical induction to prove the the guess is correct or incorrect.

For example consider the recurrence T(n) = 2T(n/2) + n

We guess the solution as T(n) = O(nLogn). Now we use induction
to prove our guess.

We need to prove that T(n) <= cnLogn. We can assume that it is true
for values smaller than n.

T(n) = 2T(n/2) + n
    <= cn/2Log(n/2) + n
    =  cnLogn - cnLog2 + n
    =  cnLogn - cn + n
    <= cnLogn

2) Recurrence Tree Method: In this method, we draw a recurrence tree and calculate the time taken by every level of tree. Finally, we sum the work done at all levels. To draw the recurrence tree, we start from the given recurrence and keep drawing till we find a pattern among levels. The pattern is typically a arithmetic or geometric series.

For example consider the recurrence relation 
T(n) = T(n/4) + T(n/2) + cn2
 
           cn2
         /      \
     T(n/4)     T(n/2)
 
If we further break down the expression T(n/4) and T(n/2), we get following recursion tree.
 
                cn2
           /           \      
       c(n2)/16      c(n2)/4
      /      \          /     \
  T(n/16)     T(n/8)  T(n/8)    T(n/4) 
Breaking down further gives us following
 
                 cn2
            /            \      
       c(n2)/16          c(n2)/4
       /      \            /      \
c(n2)/256   c(n2)/64  c(n2)/64    c(n2)/16
 /    \      /    \    /    \       /    \  
 
To know the value of T(n), we need to calculate sum of tree nodes level by level. If we sum the above tree level by level, we get the following series T(n)  = c(n^2 + 5(n^2)/16 + 25(n^2)/256) + ....
 
The above series is geometrical progression with ratio 5/16.
To get an upper bound, we can sum the infinite series. 
We get the sum as (n2)/(1 - 5/16) which is O(n2).

3) Master Method:
Master Method is a direct way to get the solution. The master method works only for following type of recurrences or for recurrences that can be transformed to following type.








Thursday, July 10, 2014

Algorithm: Efficiency and Analysis




Algorithm: Efficiency and Analysis

Algorithm Efficiency
Algorithmic efficiency is the properties of an algorithm which relate to the amount of resources used by the algorithm.
An algorithm must be analyzed to determine its resource usage. And so to get maximum efficiency we need to minimize resource usage. And these resources are Time and Space.
Algorithms is considered to be more efficient often depends on high speed, or for minimum memory usage. An algorithm is considered efficient if its resource consumption (or computational cost) is at or below some acceptable level. Roughly speaking, 'acceptable' means: will it run in a reasonable amount of time on an available computer [1].
Measures of resource usage
Ø  The two most common measures are:

Time: How long does the algorithm take to complete.

Space: How much working memory (typically RAM) is needed by the algorithm. This has two aspects: the amount of memory needed by the code, and the amount of memory needed for the data on which the code operates.

Ø  For computers whose power is supplied by a battery (e.g. laptops), or for very long/ large calculations (e.g. supercomputers), other measures of interest are:

F                   Direct power consumption: Power needed directly to operate the computer.

F                   Indirect power consumption: Power needed for cooling, lighting, etc.

Ø  In some cases other less common measure may also be relevant:

F              Transmission size: Bandwidth could be a limiting factor. Data compression can be used to reduce the   amount of data to be transmitted. Displaying a picture or image (e.g. Google logo) can result in transmitting tens of thousands of bytes (48K in this case) compared with transmitting six bytes for the text "Google" [1].

F           External space: space needed on a disk or other external memory device; this could be for temporary storage while the algorithm is being carried out, or it could be long-term storage needed to be carried forward for future reference [1].

F        Response time: this is particularly relevant in a real-time application when the computer system must respond quickly to some external event [1].

Ø  Implementation issues can also have an effect on actual efficiency, such as the choice of programming language, or the way in which the algorithm is actually coded, or the choice of a compiler for a particular language, or the compilation options used, or even the operating system being used.

  
Algorithm Complexity
“The complexity of an algorithm is the cost to use the algorithm to solve a problem.”
The cost can be measured in terms of
F                      Executed Instructions (the amount of work the Algorithm does)
F                      Running Time
F                      Memory Consumption etc..

Thus the Algorithm complexity is a measure of the algorithms resource demands, it’s not about how hard the algorithm is to understand!
The most interesting resource seems a running time, but is it the good measure?
The running time of an algorithm depends on many things:
·         The programmer’s skill and the programming language.
·         The computers machine language.
·         The code that the compiler generates.
·         The size of the problem (e.g. no. of inputs).
·         The nature of input (sorted/unsorted).
·         The method chosen.

Elementary Operations
The work algorithm does, can be referred as the Elementary Operations.
Elementary operations are assumed to require one time unit e.g. it’s an operation whose complexity can be bounded by a constant.
Complexity is a relative concept, only interesting together with the corresponding elementary operation.
Following are some examples of problems, its size and Elementary operations.




Analyzing Control Statements
Consider the following algorithm of Insertion Sort.
 
Here we start by presenting the INSERTION-S ORT procedure with the time “cost” of each statement and the number of times each statement is executed. For each j = 2, 3, ..., n, where n = A: length, we let tj denote the number of times the while loop test in line 5 is executed for that value of j. When for or while loop exits in the usual way (i.e., due to the test in the loop header), the test is executed one time more than the loop body. We assume that comments are not executable statements, and so they take no time.

Here in algorithm, cost (c1, c2, …, cn) shows the time taken by that particular operation and time shows the number of times the statement executes.

So the running time T(n) of Insertion Sort on n values, we sum the product of cost and times column.



From Series Math,
 


So,



Thus we can express this worst-case running time as an2 + bn + c for constants a, b, and c that again depends on the statement costs ci, it is thus a quadratic function of n.
 
Asymptotic Notation, Complexity Class Theory, Average and Worst Case Analysis and Algorithm Analysis are explained in very simple manner in PDF files provided here. Kindly download the PDF and go through it!