Lecture 01 - Algorithm Analysis

1.0 - Course Overview

1.1 - Course Aims

1.2 - Assumed Background

1.3 - Motivation

2.0 - Recap of Algorithm Analysis

2.1 - What is an Algorithm

2.2 - Sorting Problem

💡 More broadly, how do we define a problem in a way so that we can define algorithms to solve them

Input A sequence of numbers a1,a2,a3,,an\langle a_1, a_2, a_3, \cdots, a_n\rangle Output A permutation a1,a2,a3,,an\langle a_1', a_2', a_3', \cdots, a_n'\rangle of the input sequence such that a1a2,,ana_1'\le a_2',\cdots, \le a_n'

2.2.1 - Sorting Algorithms: Insertion Sort

🌱 We have many sorting algorithms, and one of these are called Insertion Sort.

// Note that in this example, the algorithm assumes // that the starting index of an array is 1 for j = 2 to A.length key = A[j] // Insert A[j] into the sorted sequence A[1..j-1] i = j-1 while i > 0 and A[i] > key: A[i + 1] = A[i] i = i - 1 A[i + 1] = key

🌱 Run insertion sort on the array A = [5, 4, 2, 6, 3]

Initially, j=2 - that is, we begin the insertion sort on the second element.

                 i    jA=        5    4    3    2    1\def\t{\ \ \ \ } \t\t\t\t\ i\t j\\ A=\t\t5\t4\t3\t2\t1

Additionally at this point, key=4key=4.

Since i=5>key=4i=5 > \text{key}=4, we shuffle up the position of the comparison element.

We decrement the value of ii, and in this case, we have found where to insert 4.

         i        jA=    5    4    3    2    1\def\t{\ \ \ \ } \t\ \ \ \ \ i\t\t j\\ A=\t5\t4\t3\t2\t1

We now work on the insertion of the following elements (where we try and insert it into the sorted section).

How do we verify that this algorithm is correct?

🌱 We can use the idea of invariants to help us prove that algorithms are correct using an inductive argument. That is, we can use these invariants as the inductive argument to prove that these algorithms are correct.

3.0 - Execution Time

🌱 What does execution time depend on?

3.1 - Execution Time: Worst, Average and Best Case

3.2 - Running Time Analysis

🌱 The running time of an algorithm on a given input can be measured in terms of the number of primitive steps executed.

3.3 - Running Time Analysis - Recursion

3.4 - Asymptotic Analysis: The General Idea

4.0 - Asymptotic Notation

4.1 - Growth of Functions

🌱 The following table shows the largest instance that can be solved in a given time

T(n) 1 second 1 day 1 year
nn 1,000,000 86,400,000,000 31,536,000,000,000
nlognn \log n 62,746 2,755,147,514 798,160,978,500
n2n^2 1,000 293,938 5,615,629
n3n^3 100 4,421 31,593
2n2^n 19 36 44

4.2 - Limitations of Asymptotic Analysis

4.3 - Asymptotic Notation

For functions ff and gg: