Algorithms Analysis
1. Any function programmed is purely consisted of multiple lines of code. Each line of code represent a step of operation like variable initialization (x = 3) and logical choice (if x == 3). Running a function is actually to walk through the steps by executing the lines of code. As a result, the Algorithm in computer science is the step-by-step set of the operations. Note an algorithm is independent as it can be implemented in various programming languages like Java, C++ and Python.
2. In order to measure different algorithms, we use the running time of an algorithm as an indicator (called time complexity). The running time solely relies on the operation steps (i.e., lines of codes) but ignores the hardware environment. Obviously an algorithm is a function of the amount of data (called input size): more data, longer running time. So, when measuring an algorithm, we assume that every step of the algorithm is executed with the given input size (infinite or large enough). This is called worst-case running-time or worst-case time complexity.
3. An algorithm must include one or more loop to process multiple data. As a result, some steps in the algorithm are repeated when the data is looped over. For a loop, the total number of repeated steps is iterations * steps in this loop. So, worst-case running time = total number of non-repeated steps + total number of repeated steps within loop(s) = total number of non-repeated steps + loop iterations * steps in the loop(s).
Below are some simple but typical loop-over and subsequent iterations. (Note we only focus on the while/for loop but ignore the recurrence because the it is covered in CSC263). All the data are assumed to be stored in a list and then the input size (n) is equal to the length of this list (len(list)). We initialize pointer (s) to start the loop-over and then to update the pointer(s) to keep moving. When the pointer moves from one item to another, one iteration is finished or multiple iterations are finished. So the pointer(s) can be used as an indicator of the iterations.
a.
i = 0
while i < len(list):
...
i += 1
i = 0, 1, 2, 3,..., n-1.
iterations = number of items scanned * 1 iteration per item = ((n-1)-0 + 1) * 1 = n
b.
i = 0
while i < len(list)//2:
...
i += 1
i = 0, 1, 2, 3, ..., (n//2)-1
iterations = number of items scanned * 1 iteration per item = ((n/2 - 1) -0 + 1) * 1 = n/2
c.
i = 1
while i < len(list):
...
i = i*2
i = 1, 2, 4, 8,..., n-1 = 2^0, 2^1, 2^2, 2^3, ..., 2^k where 2^k <= n ---> k = log_2(n)
iterations = number of items scanned * 1 iteration per item = log_2(n) * 1 = log_2(n)
d.
i = len(list)
while i >= 0:
...
i = i//2
i = n//(2^0), n//(2^1), n//(2^2), ..., n//(2^k) where n/(2^k) = 1 ---> k = log_2(n)
iterations = number of items scanned * 1 iteration per item = (log_2(n)) * 1 = log_2(n)
e.
i = 0
while i < len(list)*len(lst):
...
i += 1
i = 0, 1, 2, 3, ..., n^2
iterations = number of items scanned * 1 iteration per item = ((n^2 - 1) -0 + 1) * 1 = n^2
f.
for i in range(len(list)):
for j in range(len(list)):
...
i = 0, 1, 2, 3, ..., n
j = 0, 1, 2, 3, ..., n
iterations of j = number of items scanned * 1 iterations per item = (n-1) - 0 + 1 = n
iterations of i = number of items scanned * j iterations per i iteration = ((n-1) - 0 + 1) * n = n*n = n^2
g.
for i in range(1, len(list)):
for j in range(1, i+1):
...
i = 1, 2, 3, 4, ..., n-1
j = 1, 2, 3, 4, ..., i
iterations of j = number of items scanned * 1 iterations per item = (i -1 + 1) * 1 = i
# different i includes different j iterations.
iterations of i = sum of j iterations at each i iteration = 1 + 2 + 3 + ... + (n-1) = ((n-1)(n))/2
4. When the input size is assumed infinite (or large enough), how can we evaluate the performance of different algorithms? Actually at this time what we're concerned is not how long an algorithm handle the infinite data (the time is infinite as well) but how efficient the algorithm can handle the infinite data because the efficiency can be measured: how the running time might be changed when the input size is changed. This is why the big_O is introduced: different algorithms with the same efficiency may be represented by the same O notation. For example, 3n^2 + 2n + 1 in O(n^2) because, when n is assumed large enough, the running time of 3n^2 + 2n + 1 grows no faster than that of n^2. So n^2 is the upper-bound of 3n^2 + 2n + 1 regarding the time complexity.
End of this post