Time and space analysis of algorithms

Algorithm

  • An essential aspect to data structures is algorithms.
  • Data structures are implemented using algorithms.
  • An algorithm is a procedure that you can write as a C function or program, or any other language.
  • An algorithm states explicitly how the data will be manipulated.

Algorithm Efficiency

  • Some algorithms are more efficient than others. We would prefer to choose an efficient algorithm, so it would be nice to have metrics for comparing algorithm efficiency.
  • The complexity of an algorithm is a function describing the efficiency of the algorithm in terms of the amount of data the algorithm must process.
  • Usually there are natural units for the domain and range of this function. There are two main complexity measures of the efficiency of an algorithm.

Time complexity

  • Time Complexity is a function describing the amount of time an algorithm takes in terms of the amount of input to the algorithm.
  • “Time” can mean the number of memory accesses performed, the number of comparisons between integers, the number of times some inner loop is executed, or some other natural unit related to the amount of real time the algorithm will take.

Space complexity

  • Space complexity is a function describing the amount of memory (space) an algorithm takes in terms of the amount of input to the algorithm.
  • We often speak of “extra” memory needed, not counting the memory needed to store the input itself. Again, we use natural (but fixed-length) units to measure this.
  • We can use bytes, but it’s easier to use, say, number of integers used, number of fixed-sized structures, etc. In the end, the function we come up with will be independent of the actual number of bytes needed to represent the unit.
  • Space complexity is sometimes ignored because the space used is minimal and/or obvious, but sometimes it becomes as important an issue as time.