Docsity

Complexity Analysis of Algorithms: Time and Space Complexity, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Complexity Analysis of Algorithms: Time and Space Complexity and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Data Structures and Algorithms

LECTURE 10 – COMPLEXITY ANALYSIS

Complexity

  • Same problem can be solved with different approaches:
  • What if the scale of this evaluation is extended to N operations?
  • The difference may be immaterial for processing a small number of data items
  • However, it grows proportionally with the number of data items int i = 23; //1 assignment statement OR int i = 0; for (j = 0; j < 23; j++) i++; //23 assignment statements

Space Complexity

  • Space complexity estimates depend on what we define to be a fundamental storage location.
  • Such storage must offer reading and writing functions as fundamental steps.
  • In general, space complexity is bounded by time complexity.
  • Many algorithms that require a large time can be implemented using small space.
  • The factor of time is more important than that of space; therefore, we will talk about CPU time.

Time Complexity

  • Time complexity estimates depend on what we define to be a fundamental step.
  • Running time of an algorithm and or time complexity is the actual time taken by an algorithm.
  • Time complexity depends on a number of factors:
    • Single-Processor-Machine or Multi-Processor Machine.
    • Read/Write speed to the memory or disk.
    • 32 bit architecture or 64 bit architecture.
    • Input.
  • Interest is in how the algorithm behaves for various inputs i.e. how the programs grows with the input to the program known as the rate of growth of time taken with respect to the input.

Calculating Time Complexity

  • Example: Subtraction of two elements
  • Tsum is the total time taken to execute the function.
  • 1 unit of time will be taken for subtraction of a and b (arithmetic function).
  • 1 unit of time is taken for returning the data.
  • So,
    • Tsum = 1 + 1
    • Tsum = 2
  • The time taken by the program is always ‘ 2’ irrespective of the input.
  • These algorithms are called Constant Time Algorithms. subtract(a,b) { return (a-b); }

Calculating Time Complexity

  • Example: Adding all element in an Array
    • Line 1 costs 1 unit and executes 1 time.
    • Line 2 costs 2 units and executes n+1 times (1 extra because of the false condition comparison).
    • Line 3 cost 2 units and executes n times.
    • Line 4 costs 1 unit and executes 1 times.
  • So,
    • Tsum = 1 + 2(n+1) + 2n + 1
    • Tsum = 4n + 4
  • The rate of growth is a linear function because it is proportional to n. sumOfArray(myArray, size) {
    1. total = 0;
    2. for ( int i=1; i<n; i++)
    3. total = total + myArray[i];
    4. return total; }

Constant and Linear Time

  • Assume that a data structure List (class) is implemented using an array.
  • For each of the following List methods:
  • Is the number of operations independent of the size of the list? (constant time)
  • Is the number of operations in proportional to the size of the list? (linear time)
  • What will be time for each operation below?
    • the constructor
    • add (to the end of the list)
    • isEmpty
    • contains

Constant and Linear Time

  • constructor:
    • is called only for initialization and is a constant-time.
  • add (to the end of the list):
    • If space is available then it is a constant time.
    • Else new memory has to be allocated and all the values are copied in new location and then the addition is performed at the end – linear time.
  • isEmpty :
    • constant time.
  • contains :
    • This method involves looking at each item in the list in turn to see if it is equal to the given item. In the worst case (the given item is at the end of the list or is not in the list at all), this takes time proportional to the size of the list – linear time.

Why not measure empirically?

  • Algorithms are platform-independent,
    • A given algorithm can be implemented in an arbitrary programming language on an arbitrary computer
    • This computer must be running an arbitrary operating system with unique access to the machine’s resources)
  • Consider the following run-times of 2 different implementations of the same function on two different machines
  • Based on these metrics, it would be easy to jump to the conclusion that Computer A is running an algorithm that is far superior in efficiency to what Computer B is running.

Why not measure empirically?

  • However, extra data now shows us that our original conclusions were false.
  • Computer A, running the linear algorithm, exhibits a linear growth rate.
  • Computer B, running the logarithmic algorithm, exhibits a logarithmic growth rate.
  • B is a much better solution for large input.

Big-O Notation

Big-O Notation

  • For a problem of size N:
    • A constant-time method is of "order 1": O(1)
    • A linear-time method is of "order N": O(N)
    • A quadratic-time method is "order N squared": O(N 2 )

Examples

  • Example 02:
  • Time for sequence of statement is constant, i.e., O(1)
  • Loop executes N times; therefore, total time is: N × O(1) = O(N) for (i = 0; i < N; i++) { sequence of statements }

Examples

  • Example 03:
  • Statements in the inner loop execute a total of N×M times. Therefore, the complexity is O(N×M)
  • If N = M, then the complexity would be O(N 2 ) for (i = 0; i < N; i++) { for (j = 0; j < M; j++) { sequence of statements } }