1 / 44

Algorithms and data structures

Algorithms and data structures. Protected by http://creativecommons.org/licenses/by-nc-sa/3.0/hr/. Creative Commons. You are free to: share — copy and redistribute the material in any medium or format adapt — remix, transform, and build upon the material Under the following terms:

may-buckner
Download Presentation

Algorithms and data structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Algorithms and data structures Protected by http://creativecommons.org/licenses/by-nc-sa/3.0/hr/

  2. Creative Commons • You are free to: • share — copy and redistribute the material in any medium or format • adapt — remix, transform, and build upon the material • Under the following terms: • Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. • NonCommercial — You may not use the material for commercial purposes. • ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits. Notices: You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation. No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material. Text copiedfrom http://creativecommons.org/licenses/by-nc-sa/3.0/ Algorithms and data structures, FER

  3. Recursion

  4. The main idea of recursion • A procedure calls another instance of itself • There must be a termination! • Why a debt on a credit card cannot be paid using the same credit card? • Recursive programsare shorter, but the execution is more time consuming • Some languages (e.g.old versions ofFORTRAN) do not allow recursion • For storing of results and for enabling the return from recursion, the stackdata structure is used recursion: if still not clear, look under: recursion recursion: see under: recursion Algorithms and data structures, FER

  5. Elementary recursion and the system stack main f f’ f’’ f’’’ ... f(1); ... void f(int i) { int v; f(i+1); return; } void f(int i) { int v; f(i+1); return; } void f(int i) { int v; f(i+1); return; } void f(int i) { int v; f(i+1); return; } ElementarnaRekurzija (ElementaryRecursion) v ret.addr. 4 v v ret.addr. ret.addr. 3 3 v v v ret.addr. ret.addr. ret.addr. 2 2 2 v v v v ret.addr. ret.addr. ret.addr. ret.addr. 1 1 1 1 Algorithms and data structures, FER

  6. Calculation of factorials • Among the simplest recursive algorithms is the calculation ofn!for n >= 0 • 0! = 1 • 1! = 1 • n! = n* (n-1)! • Example: 4! k = fact (4); = 4 * fact (3); = 3 * fact (2); = 2 * fact (1); = 1 • int fact(int n){ • if (n <= 1) { • return 1; • } else { • return n * fact(n-1); • } • } Algorithms and data structures, FER

  7. Calculation of factorials main fact fact’ fact’’ ... i=fact(3); ... int fact(int n){ if (n <= 1) { return 1; } else { return n * fact(n-1); } } int fact(int n){ if (n <= 1) { return 1; } else { return n * fact(n-1); } } int fact(int n){ if (n <= 1) { return 1; } else { return n * fact(n-1); } } 1 6 2 Faktorijeli(Factorials ) 1 2 2 3 3 3 Algorithms and data structures, FER

  8. Exercises • Write the function that inputs two integer arguments x and y and in its name returns the value for xy. PotencijaRekurzijom (PowerByRecursion) • Function call: k = pot(2,5); = 2*pot(2,4) = 2*pot(2,3) = 2*pot(2,2) = 2*pot(2,1) = 2*pot(2,0) = 1 Algorithms and data structures, FER

  9. Stack contents Algorithms and data structures, FER

  10. Exercises • What happens if the following line is omitted: if (y <= 0) return 1; • The function would call itself incessantly and it would never return any value to the main program • For the above example, it would result in the following: pot(2,5); = 2*pot(2,4) = 2*pot(2,3) = 2*pot(2,2) = 2*pot(2,1) = 2*pot(2,0) = 2*pot(2,-1) = 2*pot(2,-2) = 2*pot(2,-3) ... Algorithms and data structures, FER

  11. Exercises • Solution without recursion: • Write the recursive functions to print the numbers from 1 to n, or from n to 1 in different ways RekurzivniIspisRedom (RecursiveSequentialPrint) • Write the recursive function to calculate the nthmember of the arithmetic series AritmetickiNiz (ArithmeticSeries) • int pot(long x, long y) { • int retval = 1; • int i; • for (i = 0; i < y; i++) retval *= x; • returnretval; • } Algorithms and data structures, FER

  12. Leonardo Pisano Fibonacci • born: 1170 (probably) Pisa • died: 1250 (probably) Pisa • In year 1202Liber abaci : • Introduction of Hindu-Arabic numbers (in Europe) • Simultaneous linear equations • Commercial mathematical problems • Calculation of profit • Exchange rates for currencies Algorithms and data structures, FER

  13. Do not match exactly our “Arabic” numbers The main achievement: introduction of zero and of weighted positions of digits within numbers ٠ 0 Sifer ١ 1 Wahid ٢ 2 Ithinin ٣ 3 Thalatha ٤ 4 Arba'a ٥ 5 Kamisa ٦ 6 Sita ٧ 7 Saba'a ٨ 8 Thamania ٩ 9 Tisa'a ١٠ 10 Ashara Arabic numbers Algorithms and data structures, FER

  14. Fibonacci numbers • 1, 1, 2, 3, 5, 8, 13, 21, 34,... (next?) F0=F1=1 Fi=Fi-2+Fi-1; i>1 • The program is very short and completely corresponds to the mathematical definition • Low efficiency • int F(int n) { • if (n <= 1) • return 1; • else • return F(n-2) + F(n-1); • } Algorithms and data structures, FER

  15. Fibonacci numbers – program execution 25 F(6) F(0) is calculated 5 times F(1) is calculated 8 times F(2) is calculated 5 times F(3) is calculated 3 times F(4) is calculated 2 times F(5) is calculated 1 time F(6) is calculated 1 time Total : 25 13 24 9 F(5) F(4) 8 5 23 8 14 3 F(4) F(2) F(3) F(3) 5 2 3 3 1 2 4 7 10 13 22 17 F(0) F(1) F(1) F(2) F(1) F(2) F(2) F(3) 1 1 1 2 1 2 2 3 5 6 11 12 15 16 18 21 F(0) F(1) F(0) F(1) F(0) F(1) F(1) F(2) 1 1 1 1 1 1 1 2 19 20 F(0) F(1)  Fibonacci 1 1 Algorithms and data structures, FER

  16. Largest common divisor • One of the oldest known algorithms is the Euclid‘s method for finding of the largest common divisor (lcd) of two nonnegative integers: • ifb = 0 • lcd = a • else • lcd = largest common divisor ofb • and rest of divisiona withb • Euklid • (Euclid) Algorithms and data structures, FER

  17. Largest common divisor – example and the function • Example: lcd(22,8) = lcd(8,6) = lcd(6,2) = lcd(2,0) = 2 lcd(21,13) = lcd(13,8) = lcd(8,5) = lcd(5,3) = lcd(3,2) = lcd(2,1) = lcd(1,0) = 1 lcd (21,0) = 21 lcd (0,21) = lcd (21,0) = 21 • Recursive function: • intlcd (int a, int b) { • if(b == 0) return a; • return lcd (b, a % b); • } Algorithms and data structures, FER

  18. Searching for a member of an array • Recursive search for the index of the first member of one-dimensional array of nmembers with the value x. If nonexistent, the result is -1. • The search is initiated by the function call search(A, x, n, 0). Rekurzija (Recursion) • int search(type A[], type x, int n, inti) { • if(i >= n) return -1; • if(A[i] == x) return i; • return search(A, x, n, i+1); • } Algorithms and data structures, FER

  19. Search with sentinel • The search can become faster if beforehand the array is extended with a member called sentinelA[n] = x; • The call: type i; A[n] = x; if ((i = search1 (A, x, 0)) == n) ... • int search1 (type A[], type x, inti){ • if(A[i] == x) return i; • return search1 (A, x, i+1) • } Algorithms and data structures, FER

  20. Searching for the largest array member • Finding the index of the largest member in array ofnmembers • intmaxmem (int A[], inti, int n) { • intimax; • if (i >= n-1) return n-1; • imax = maxmem (A, i + 1, n); • if (A[i] > A[imax]) return i; • return imax; • } Rekurzija (Recursion) Algorithms and data structures, FER

  21. Searching for the largest array member • Finding the largest member in array ofnmembers • Why is this implementation inefficient? #define maxof(a,b) ((a) > (b) ? (a) : (b)) int maxmem2 (int A[], inti, int n) { if (i >= n-1) return A[i]; else return maxof(A[i], maxmem2 (A, i + 1, n)); } Rekurzija (Recursion) Algorithms and data structures, FER

  22. Recursion characteristics • Basic cases • Basic cases to be solved without recursion must always exist • Progression • For cases to be solved recursively, each consecutive recursive call must progress towards the basic cases • Design rule • It is understood without questioning that every recursive call is functioning • No repetition rule • It should not be allowed to solve the same problem in separate recursive calls • This results in multiplication of processing, e.g.see Fibonacci numbers Algorithms and data structures, FER

  23. Example for error • For the valuen = 1recursive call is again withwiththeargument1 • There is no progressing towards the basic case • The program does not work neither for other argument values: • E.g.for n = 4,therecursive call ofbadhas the argument4/3 +1 = 2, then 2/3 +1 = 1and further on constantly 1/3 +1 = 1 • int bad (int n) { • if (n == 0) return 0; • return bad (n / 3 + 1) + n - 1; • } Algorithms and data structures, FER

  24. Recursion Exercises

  25. Interest • A given amount of money is deposited in the bank for a given number of years with a given interest rate. Write a program to calculate the sum after the deposit is withdrawn. Kamate (Interest) Algorithms and data structures, FER

  26. Anagram • Anagram – a word constructed from the starting word by a permutation of letters • anagrams of DOG: DOG, DGO, ODG, OGD, GOD, GDO • Word consisting of n different letters - n! possible permutations • How to construct them: • Permutation of n-1 letters on the right hand side is always performed • All the n letters must rotate, whereby all the letters are shifted for one place to the left, except the first one, which goes to the furthest place on the right • These steps are repeated n times • By rotation of letters, it is achieved that each letter may become the first one • While the selected letter is the first one, all the other letters are permutated • Basic case: permutation of a single letter Premetaljka (Anagram) Algorithms and data structures, FER

  27. Palindrome • Write a program to check whether the given word or phrase is a palindrome. In the input string blanks and punctuation should be neglected. • Examples: • Amore, Roma • A man, a plan, a canal: Panama • A Toyota! Race fast, safe car! A Toyota! • Hint: if the first and the last letter are omitted, the remaining text is also a palindrome Obrtaljka (Palindrome) Algorithms and data structures, FER

  28. Towers of Hanoi • There are rods I (from CroatianIzvor= Source), O (from Croatian Odredište = Destination), P (from CroatianPomoćni= Auxiliary). On the first rod (I) there are n disks of different sizes which can slide onto any rod. Disks are always ordered so that a larger one never tops a smaller one. • To cite the Wikipedia (http://en.wikipedia.org/wiki/Tower_of_Hanoi): The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules: • Only one disk can be moved at a time. • Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack. • No disk may be placed on top of a smaller disk. Hanoi Algorithms and data structures, FER

  29. Towers of Hanoi • Solution algorithm: • Ignore the lowest (largest) disk, and solve the problem for n-1disks, moving them from the rod I to the rod P using O as an auxiliary rod • Now the largest disk is on I, while the remaining n-1 are on P • Move the largest disk from I to O • Move n-1 disks from P to O using I asauxiliary (the problem is already solved for n-1 disks) • With three disks, the puzzle can be solved in seven moves. The minimum number of moves required to solve a Tower of Hanoi puzzle is 2n - 1, where n is the number of disks. Hanoi Algorithms and data structures, FER

  30. Eight queens • Write the function to find positions of 8 queens on a chess board, so that that none of them attacks any other • Solution algorithm: • We inspect columns on the chess board from the first to the last • In each column we place a queen • We inspect the chessboard when iqueens have already been placed (inidifferent columns),but so that they do not attack each other • We try to place the (i+1)th queen so that she does not attack any of the already placed queens, and so that the rest of the queens can be placed, satisfying the non attacking requirement • Note: • There are 92 different solutions Kraljice (Queens) Algorithms and data structures, FER

  31. Knight’s Tour • Write the program to print the tour of knight on a chess board, so that he visits any field only once. The knight’s tour starts in the upper left corner (A8). • The knight moves in form of the letter L. In the figure, the fields the knight can visit in his next moveare markedred • There are few billons of solutions • 122 million are closed paths Konj (Knight) Algorithms and data structures, FER

  32. Recursion Complexity in recursion

  33. An example for different complexities in solving a same problem (M.A. Weiss) – 1 • An array of integers is given: A0, A2,…,An-1. • e.g. int A [] = {4, -3, 5, -2, -1, 2, 6, -2}; • Numbers can be also negative. The largest value of the sum of consecutive numbers is to be found. It is supposed that the maximum sum is 0 if all the numbers are negative. • Practical example: Find the maximum necessary capacity of the wallet if the owner comes to a market with an empty wallet and buys and sells after the consecutive prices. If the wallet is empty, payment is by a credit card. RazneSlozenosti (DifferentComplexities) Algorithms and data structures, FER

  34. An example for different complexities in solving a same problem (M.A. Weiss) – 2 • MaxSubSumArray3O(n3) • i = 1, n-1 • j = i, n-1 • k = i, j • All the possible subsequences are checked. In the outer loop the first member of the subarray is iterated through its all possible values. In the first inner loop, the index of the last member is iterated from the index of the outer loop to the end. The second inner loop runs from the index of the first loop to the index of the second loop. In this way all the possible subarrays are exhaustively checked. • All the 3 loops are repeated n times in the worst case. Therefore the complexity is O(n3) . Algorithms and data structures, FER

  35. An example for different complexities in solving a same problem (M.A. Weiss) – 3 • MaxSubSumArray2O(n2) • If we notice: • the complexity can be reduced by removingone loop. RazneSlozenosti (DifferentComplexities) Algorithms and data structures, FER

  36. An example for different complexities in solving a same problem (M.A. Weiss) – 4 • MaxSubSumArrayO(nlog2n) • Rather complex recursive algorithm • If there were not a better (linear) solution, this would be a good example for the power of recursion and the divide-and-conquer method. • The input array is divided approximately in two halves, the solution might be found in the left hand side part, or in the right hand side part, or it results from a contiguous sequence extending from left to right hand sides of the array. The first two cases can be solved recursively. The last case can be checked by finding the maximum sum in the left hand side part, including its last member and the maximum sum of the right hand side of the array, such that includes its first member. These two sums are then added up and compared to the previous two sums. Algorithms and data structures, FER

  37. An example for different complexities in solving a same problem (M.A. Weiss) – 5 • The largest left sum is formed from members 0 to 2 and amounts 6. The largest right sum results in 8 from the sequence of members 5 to 6. The maximum left sum including the last member is built from members 0 to 3 and equals 4, while on the right hand side it is the sum from members 4-6 and equals 7. Adding left and right gives 11 and that is the maximum sum. • The calling program defines the initial array edges as0andn-1 Left part Right part -3 5 -2 4 -1 2 6 -2 0 2 3 1 4 5 6 7 Algorithms and data structures, FER

  38. An example for different complexities in solving a same problem (M.A. Weiss) – 6 • The source code is rather complicated, but it performs for an order of magnitude better than the previous one. It demonstrates that a shorter code does not imply a better code! • If n were a power of 2 it can be intuitively seen that there would be log2 n successive halving. A more detailed proof will be given later while discussing binary trees. As n data are processed, the complexity is O(n log2n). • It can be generally stated that an algorithm complexity is O(log2n) if it divides the problem in time O(1) (usually that is halving). • If each step reduces the problem sizefor 1, the complexity is O(n).. Algorithms and data structures, FER

  39. An example for different complexities in solving a same problem (M.A. Weiss) – 7 • MaxSubSumArray1O(n) • All the array members are added sequentially, and the sum which was the largestduring the whole process is remembered. Algorithms and data structures, FER

  40. Aposteriorianalysis • Example: calculate the mode of a sorted integer array, i.e. find the most frequent value and its frequency. ModPolja (ModeArray) • mode0direct solving • rmode0 recursive solving • rmode1 recursive algorithm transformed into a direct one • All the three algorithms have the execution time of (n). Which one is the best? Algorithms and data structures, FER

  41. rmode0 • Suppose in the array of n members a[0:n-1] the mode and its frequency f have been calculated for the first n-1 array members. Under what circumstances can the last array member change the mode? If a[n-1] != a[n-2] neither the mode, nor the frequency change. If they are equal, how to distinguish among 3 possible cases: • a) new mode has been found • b) mode remains the same but the frequency f is incremented • c) neither mode nor frequency change • It depends whether a[n-1] == a[n-1 - f] • If yes, then there are n-1 - (n-1 - f) +1 = f + 1 appearances of the value contained in a[n-1]. This means this value is either the new mode or the old mode but with increased frequency f. Algorithms and data structures, FER

  42. Exercises • Examples for recursive function calls PrimjeriRekurzije (ExamplesForRecursion) Algorithms and data structures, FER

  43. Exercises • Write the program for calculation of binomial coefficient using the expression: a) BINOM(m, n) = m!/(n!(m-n)!) b) BINOM(m, n) = BINOM(m-1, n) + BINOM(m-1, n-1); BINOM(m, 0) = BINOM(m, m) = 1 BinomniKoeficijenti (BinomialCoefficient) • Write the function to print on the screen the first n rows of the Pascal’s triangle. What is the complexity - a priori? PascalovTrokutRekurzija (PascalTriangle) Algorithms and data structures, FER

  44. Travelling salesman problem • Travelling Salesman Problem (TSP): Let G be a set of n towns, and cij fees for travelling from the town i to the town j. Starting from a given city, it is required to visit all the cities exactly once, while the travelling cost should be at minimum. cij = cji TSP(Gi, G) = min (cij + TSP (Gj, G \ Gi)) j TSP(Gi, {Gk}) = cik Complexity: O(n!), ~(n!/2) • TSP Algorithms and data structures, FER

More Related