AM exam Flashcards
What is an algorithm
It is a well defined procedure that given an input it produces an output
Knuth rules of the algorithm
Fitness: It needs to finish at a certain point
Definitness: Each step needs to be clearly defined
Input: It has
0 or more inputs
Output: It has one or more outputs
Effectiveness: It should be composed by basic operation that reach the hoped results (people should reproduce it)
Computational Problem
Is the mathmetical relation between a set of possible instances and a set of possible solutions. So that for each i there is a possible s
Algorithm analyisis
Study the preformance of the algorithm, given two algorithms that do the same operation, which one is faster? which one takes the least amount of operation to get to the same output. We care about its efficiency, that is typically in relation with the input size
Computation Problem - Sorting
The sorting computation problem is defined as: given an unsorted list of values (our instance) output the list ordered. There are multiple algorithm to do so:
- Insertion sort
- Merge sort
- Selection sort
Insertion sort
Insertion sort is one of the simplest algorithm to sort an unordered list, it runs with complexity O(n^2). The base concept is:
Start at index 2 (or 1 in python) and check if the element is lower than the previous one, if it is swapped continue moving backward to check if the element is lower than the selected one.
Stop if the element is not lower
example 1,4,3,2 1,4,3,2 1,3,4,2 1,2,3,4
Best case scenario:
The input is ordered so it needs to check only that the element are ordered
T(n) = bn+c (b and c are constants depending on the operation that needs to be taken)
Worst case scenario:
The input is in the inverse order
t(n) = an^2 + bn + c O(n^2)
Loop invariant
Loop invariant is the condition that algorithms needs to meet, it is used to check if the algorithm is correct:
- It is true prior to the first iteration
- The hipothesis needs to be correct at each loop (for instance for insertion sort the list on the left needs to be ordered)
- It is true at termination
Selection Sort
This is also a sorting algorithm but it takes a bit of a different path
It start and index 0 or 1 (depening on programming languages) and it search in the list the lowest value and move it at the beginning. So at every step it checks if the first element is lowest than the second, lowest than the third etc etc if it is not lower select the lowest element and keep checking. As soon that you found the smallest you exchange the two values
Example
1,4,3,2
1,2,3,4 Done
Worst case
a*n^2 + bn + c (quadratic)
Best case
Since we always have to loop over the entire list to find the minmum, for every n we have to loop over the entire list.
a*n^2 + bn + c (quadratic) O(n^2)
Complexity measures
Complexity is usually measured as asymptotic complexity, meaning that we care about the algo complexity for a given n number of instances
Big Oh= It defines an upper limit to the algorithm, it defines that the algorithm cannot be slower than this after a certain number of instances
f(x) = O(n)
It is used for worst case analyisis
Big Theta = It defines an upper and a lower boundry after a specific number of instances (Worst case analyisis). For a given set of constant you can find a function that multiplied to these constants sanwitch the function (Tight bound)
Big Omega= It defines the lower boundry after a specific number of instances (Best case analysis). It is not really useful
Divide and conquer paradigm
Divide the big task in smaller tasks that are easier to be solved. There are a lot of algorithm that follows this paradigm:
- Merge sort
- Matrix multiplication
- All the graph algorithm
Three steps:
- Divide
- Conquer
- Combine (most difficult part)
Recursive algorithm
A recursive algorithm is an algorithm that calls itself multiple times, it is the way we can implement divide and conquer algo
Merge Sort algorithm
Always it tries to solve the sorting problem:
It start by dividing the list in two, if the list has more elements than 1 keep dividing until it reaches 1 value at the end.
When it reaches the end we have one value per list (that is ordered of course)
at the combine step we need to order thw two sublist to get an ordered list. So when you combine it you need to add for every index the lowest between the two elements in the sublist. However you can exploit the fact that they are already ordered the two list, so you know that the left most element is the smallest of all of them
example
list 1 = 1,4
list 2 = 3, 4
step 1 = compares 1 and 3 and select 1
step 2 = compares 4 and 3 select 3
step 3 = compares 4 and 4 select the first 4
1,3,4,4
The combine step has O(n) complexity because = O(r-p +1) it scales linearly with the size of n, where r and p are the size of selector to select the subvectors
This means that if you want to measure the running time you need to solve
T(n) =
- O(1) in case n = 1
- 2*T(n/2) + BigTheta(n)
Meaning that for every step you need to devide the subproblem in two and then combine it that takes the BigTheta(n) complexity
Worst case BigTheta(n*lg(n))
How to solve recurrencies
We have:
Recurrence trees = You draw the trees of how the problem is split and depending on the number of leafs and levels you define the algorithm running time
Substituion = You guess the form of the solution and you use induction to prove it (The hardest)
Master theorem = Apply rules in case you are able to apply them to easily solve the problem
Matrix multiplication
Given two matrix multiply them, it is solved using the divide and conquer paradigm and therefore we apply recursion
Divide step = Let’s consider square matrices, we are able to divide it in 4 submatrices etc etc until we end up haveing a one value matrix
Combine step = We have a new matrix C that is the shape of all the submatrix combined and we fill it with a combination of the submatrices
Execution time
T(n) =
- BigTheta(1) in case of one element
- 8 * T(n/2) + BigTheta(n^2)
The combining step is quite expensive, but is not that the part that rules the whole running time (it could also be done by BigTheta(1)
Complexity = BigTheta(n^3)
Strassen algo
Smart implementation of matrix multiplication that allows to have this recursive formula
T(n)=
- BigTheta(1)
- 7*T(n/2) + BigTheta(n^2)
This allows to have a complexity of
BigTheta(n^(lg(7))), quite better than before
What is a list
It is a sequencial collection of values. It has an index.It can be heterogeneous (values of different type)
What is a stack
A stack is a collection of object that follows the Lifo principle (Last in first out)
What is a Queue
A queue is similar to the stack but it follows the principle of Fifo (First in First out)
What is a Deque
The Deque is a combination of the queue and the stack, you can access both the first element and the last one
What is a linked list
A linked list is a list where items are arranged in linear order. Is different from an array (or list) where the order is determined by the list, here the order is mantained by a pointer.
You have different types:
Single linked list = Every node has a pointer to the next node. The head is teh first element and the tail the last one. You say you ate Traversing the list when you move from the head to the tail. Important: You need to know where the head is because it does not have any pointer to it
Circulary Linked list: Linked list where the tail has a pointer to the head
Dubly linked list: Similar to normal linked list but every node has both a pointer for the next node but also to the previous. It brings more flexibility
What is the difference between arrays and list
A part of the fact that arrays cannot be of mixed types the majo differences are:
Arrays are faster to acess an element using the index O(1) while lists are O(n)
Arrays have resize issues
Memory consumptions,
Arrays take 2n space in memory due to the dynamic resizing (you can add and remove), also the memory allocation is contiguous (making it hard for deleting elements)
Single linked list they store n values and n reference while the double 2n references, you do not need to store the values contigously
Positional list
Is a list where you are able to refer elements using also non numerical index. So it is a list that tries to overcome the O(n) time to insert an element
What is a tree
A tree is a simple graphical representation of the connection between vertices through edges.
What is a free tree
A free tree is a tree that is connected, acyclical and undirected. If the graph is disconnected is a forest.
You can represent a tree as a linked list (double) where you point to the parent and to the child