Algorithms Complexity and Sorting academy zariba com 1
Algorithms, Complexity and Sorting academy. zariba. com 1
Lecture Content 1. 2. 3. 4. Algorithms Overview Complexity Sorting Algorithms Homework 2
Algorithms Overview 3
Algorithms Overview Algorithms are normally described by a pseudo-code For example, here is a pseudo-code for Kruskal’s Algorithm for finding a minimum spanning tree in a Graph. 1. T (the final spanning tree) is defined to be the empty set; 2. For each vertex v of G, make the empty set out of v; 3. Sort the edges of G in ascending (non-decreasing) order; 4. For each edge (u, v) from the sored list of step 3. If u and v belong to different sets Add (u, v) to T; Get together u and v in one single set; 5. Return T 4
Algorithms Overview There are many types of algorithms in Programming: • • • … Sorting Searching Dynamic Programming Graph Theory Algorithms Combinatorial Algorithms 2 D and 3 D geometry algorithms Greedy Algorithms Generating Algorithms Randomized Algorithms 5
Algorithms Overview Examples How would you generate a “balanced” board for Sudoku? 6
Algorithms Overview Examples How would you generate a “balanced” board for Minesweeper? 7
Algorithms Overview Examples How would you draw an edge between two vertices in a graph? 8
Algorithms Overview Examples How would you generate a “balanced” board for the following game? 9
Algorithms Complexity Our goal is to examine how efficient a given algorithm is. We may not always need the fastest algorithm for a given problem. It depends on the amount of information we have to process. If, however, we need to optimize a given calculation to increase general performance, a “good” algorithm will be needed. We need to be able to recognize inefficient algorithms, assess them, and rewrite them into more efficient ones, where possible. 10
Algorithms Complexity The running time of an algorithm is defined as the number of primitive steps required for its execution. This is also known as algorithm complexity. We measure algorithm complexity with respect to the size of the input data. (e. g. how long would it take to sort a List of size n) In other words the complexity is a function, whose parameters are the size of the input data (e. g. f(n) = 3 n 2 + 3) The complexity of an algorithm can measure the CPU time, Memory, Number of Steps, among others. We always measure the worst-case scenario. 11
Algorithms Complexity We denote the complexity of an algorithm with the Big Oh notation, for example O(log n), O(n) or in general O(g(n)). For the rest of the lecture you can assume that for an algorithm which takes data with size n, O(g(n)) shows the “rounded”/”approximate” number of operations that an algorithm needs to execute. (a proper definition will follow later). 12
Algorithms Complexity – Example 1 Let us have an unsorted list of size n. We need to create an algorithm that finds a specific value “a” inside the array. What are the best, worst and average case number of steps required. What would be the complexity of such algorithm? 13
Algorithms Complexity Example - 2 Now let us assume that the list is sorted. Can we come up with a better algorithm? What is the complexity? 14
Algorithms Complexity Example - 2 Now let us assume that the list is sorted. Can we come up with a better algorithm? What is the complexity? 15
Algorithms Complexity Example - 3 Let us calculate the expression a^b with the following algorithm, where a and b are positive integers. What is the precise number of steps required. What is the complexity of the algorithm? public static long Calculate. Power(a, b) { long result = 1; while(b>0) { result*=a; b--; } return result } 16
Algorithms Complexity - Definition 17
Algorithms Complexity - Classes Complexity Notation Description Constant O(1) Constant time, independent of the input data Logarithmic O(log n) Very performance efficient. E. g. input data of 1 billion will take around 22 steps Linear O(n) Number of steps grows similarly to the input. Quadratic O(n 2) Complexity grows quadratically Cubic O(n 3) Cubically Exponential O(kn) Exponentially, e. g. input with size 10 would incur 1024 steps Factorial O(n!) Grows very quickly VEEEERY SLOW O(nn) Grows even quicker 18
Algorithms Complexity - Classes Compl. 10 20 50 100 1 000 100 000 O(1) <1 s <1 s O(log 2 n) <1 s <1 s <1 s <1 s O(nlog 2 n) <1 s <1 s O(n 2) <1 s <1 s <1 s 2 s 3 -4 min O(n 3) <1 s <1 s 20 s 5 hours 231 days O(2 n) <1 s 260 days Indef. O(n!) <1 s Indef. O(nn) 3 -4 min Indef. 19
Algorithms Complexity - Examples E 1. Find Max in a given array E 2. Traversing a matrix int max = arr[0]; for(int i=0; i<arr. length; i++) { if(arr[i]>max) { max = arr[i] } } return max; for(int i=0; i<len; i++) { for(int j=0; i<len; j++) { cw(a[i, j]); } } 20
Algorithms Complexity - Examples E 3. Finding all pairs of elements for(int i=0; i<len; i++) { for(int j=i+1; i<len; j++) { cw(“a[i]”+”a[j]”); } } E 4. Finding all from 2 arrays for(int i=0; i<n; i++) { for(int j=0; i<m; j++) { cw(“a[i]”+”b[j]”); } } 21
Sorting Algorithms 22
Bubble Sort 23
Insertion Sort 24
Selection Sort 25
Quick Sort 26
Merge Sort 27
Homework 1. ) Watch the following video on P vs NP problems https: //www. youtube. com/watch? v=YX 40 hb. AHx 3 s 2. ) Implement the following 5 sorting algorithms in C# - bubble, insertion, selection, quick, merge 28
References 29
Zariba Academy Questions 30
- Slides: 30