1 / 190

Introduction to Algorithms and Data Structures

Introduction to Algorithms and Data Structures . A.E. Csallner Department of Applied Informatics University of Szeged Hungary. Algorithms. Algorithm: Finite sequence of finite steps Provides the solution to a given problem Properties: Finiteness Definiteness Executability.

vita
Download Presentation

Introduction to 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. Introduction to Algorithms and Data Structures A.E. Csallner DepartmentofAppliedInformatics University of Szeged Hungary

  2. Algorithms • Algorithm: • Finite sequence of finite steps • Provides the solution to a given problem • Properties: • Finiteness • Definiteness • Executability • Communication: • Input • Output Algorithms and Data Structures I

  3. Structuredprogramming • Design strategies: • Bottom-up: synthesize smaller algorithmic parts into bigger ones • Top-down: formulate the problem and repeatedly break it up into smaller and smaller parts Algorithms and Data Structures I

  4. shoe a horse Example : Shoe a horse a horse has four hooves shoe a hoof need a horseshoe need to fasten the horseshoe to the hoof hammer a horseshoe drive a cog into a hoof need cogs hammer a cog Algorithms and Data Structures I

  5. Basic elements of structured programming • Sequence: series of actions • Selection: branching on a decision • Iteration: conditional repetition All structured algorithms can be defined using only these three elements (E.W. Dijkstra1960s) Algorithms and Data Structures I

  6. Algorithm description methods An algorithm description method defines an algorithm so that the description code should • be unambiguous; • programming language independent; • still easy to implement; • state-of-the-art Algorithms and Data Structures I

  7. Some possible types of classification: • Age (when the description method was invented) • Purpose (e.g. structural or object-oriented) • Formulation (graphical or text code, etc.) • ... Algorithms and Data Structures I

  8. Most popular and useful description methods • Flow diagram • old • not definitely structured(!) • graphical • very intuitive and easy to use Algorithms and Data Structures I

  9. A possible notation of flow diagrams START • Circle: STOP STOP Algorithms and Data Structures I

  10. A possible notation of flow diagrams • Rectangle: Any action execution can be given here Algorithms and Data Structures I

  11. A possible notation of flow diagrams • Diamond: Any yes/no question no yes yes Algorithms and Data Structures I

  12. A possible notation of flow diagrams START An example: Iteration Selection Sequence Need more horseshoes? yes Hammer a horseshoe no Shoe a hoof STOP Algorithms and Data Structures I

  13. Most popular and useful description methods • Pseudocode • old • definitely structured • text based • very easy to implement Algorithms and Data Structures I

  14. Properties of a possible pseudocode • Assignmentinstruction:  • Loopingconstructsasin Pascal: • for-doinstruction (countingloop) forvariable initialvalueto/downtofinalvalue dobody of theloop Algorithms and Data Structures I

  15. Properties of a possible pseudocode • while-do instruction (pre-test loop) while stay-in test do body of the loop • repeat-until instruction (post-test loop) repeat body of the loop until exit test Algorithms and Data Structures I

  16. Properties of a possible pseudocode • Conditional constructs as in Pascal: • if-then-else instruction (else clause is optional) if test thentest passed clause else test failed clause • Blocks are denoted by indentation Algorithms and Data Structures I

  17. Properties of a possible pseudocode • Object identifiers are references • Field of an object separator is a dot: object.field object.method object.method(formal parameter list) • Empty reference is NIL Algorithms and Data Structures I

  18. Properties of a possible pseudocode • Arraysareobjects • Parametersarepassedbyvalue Algorithms and Data Structures I

  19. Properties of a possible pseudocode An example: ShoeAHorse(Hooves) hoof  1 whilehoof ≤ Hooves.Count dohorseshoe  HammerAHorseshoe Hooves[hoof]  horseshoe hoof  hoof + 1 Iteration Sequence Algorithms and Data Structures I

  20. Typealgorithms Algorithm classification on the I/O structure • Sequence → Value • Sequence → Sequence • More sequences → Sequence • Sequence → More sequences Algorithms and Data Structures I

  21. Sequence → Value • sequence calculations (e.g. summation, product of a series, linking elements together, etc.), • decision (e.g. checking whether a sequence contains any element with a given property), • selection (e.g. determining the first element in a sequence with a given property provided we know that there exists at least one), Algorithms and Data Structures I

  22. Sequence → Value (continued) • search (e.g. finding a given element), • counting (e.g. counting the elements having a given property), • minimum or maximum search (e.g. finding the least or the largest element). Algorithms and Data Structures I

  23. Sequence → Sequence • selection (e.g. collect the elements with a given property of a sequence), • copying (e.g. copy the elements of a sequence to create a second sequence), • sorting (e.g. arrange elements into an increasing order). Algorithms and Data Structures I

  24. More sequences → Sequence • union (e.g. set union of sequences), • intersection (e.g. set intersection of sequences), • difference (e.g. set difference of sequences), • uniting sorted sequences (merging / combing two ordered sequences). Algorithms and Data Structures I

  25. Sequence → More sequences • filtering (e.g. filtering out elements of a sequence having given properties). Algorithms and Data Structures I

  26. Specialalgorithms • Iterative algorithm Consists of two parts: • Initialization (usually initializing data) • Iteration (repeated part) Algorithms and Data Structures I

  27. Recursive algorithms Basic types: • direct (self-reference) • indirect (mutual references) Two alternative parts depending on the base criterion: • Base case (if the problem is small enough) • Recurrences (direct or indirect self-reference) Algorithms and Data Structures I

  28. An example of recursive algorithms: Towers of Hanoi • Aim: • Move n disks from a rod to another, using a third one • Rules: • One disk moved at a time • No disk on top of a smaller one Algorithms and Data Structures I

  29. Recursive solution of the problem 1st step: move n–1 disks 2nd step:move 1 disk 3rd step: move n–1 disks Algorithms and Data Structures I

  30. Pseudocode of the recursive solution TowersOfHanoi(n,FirstRod,SecondRod,ThirdRod) 1 ifn > 0 2 then TowersOfHanoi(n – 1,FirstRod,ThirdRod,SecondRod) 3 write “Move a disk from ” FirstRod “ to ” SecondRod 4 TowersOfHanoi(n – 1,ThirdRod,SecondRod,FirstRod) line 2 line 4 line 3 Algorithms and Data Structures I

  31. Backtracking algorithms Backtracking algorithm: • Sequence of systematic trials • Builds a tree of decision branches • Steps back (backtracking) in the tree if no branch at a point is effective Algorithms and Data Structures I

  32. An example of the backtracking algorithms Eight Queens Puzzle: eight chess queens to be placed on a chessboard so that no two queens attack each other Algorithms and Data Structures I

  33. Pseudocode of the iterative solution EightQueens 1 column 1 2 RowInColumn[column]  0 3 repeat 4 repeat inc(RowInColumn[column]) 5 untilIsSafe(column,RowInColumn) 6 ifRowInColumn[column] > 8 7 thencolumncolumn – 1 8 elseifcolumn < 8 9 thencolumncolumn + 1 10 RowInColumn[column]  0 11 else draw chessboard 12 untilcolumn = 0 Algorithms and Data Structures I

  34. Complexity of algorithms Questions regarding an algorithm: • Does it solve the problem? • How fast does it solve the problem? • How much storage place does it occupy to solve the problem? Complexity issues of the algorithm Algorithms and Data Structures I

  35. Elementary storage or time: independent from the size of the input. Example 1 If an algorithm needs 500 kilobytes to store some internal data, this can be considered as elementary. Example 2 If an algorithm contains a loop whose body is executed 1000 times, it counts as an elementary algorithmic step. Algorithms and Data Structures I

  36. Hence a block of instructions count as a single elementary step if none of the particular instructions depends on the size of the input. A looping construct counts as a single elementary step if the number of iterations it executes does not depend on the size of the input and its body is an elementary step. ⇒ to shoe a horse can be considered as an elementary step ⇔ it takes constant time (one step) to shoe a horse Algorithms and Data Structures I

  37. The time complexity of an algorithm is a function depending on the size of the input. Notation: T(n) where n is the size of the input Function T can depend on more than one variable, e.g. T(n,m) if the input of the algorithm is an n⨯m matrix. Algorithms and Data Structures I

  38. Example: Find the minimum of an array. Minimum(A) 1 minA[1] 2 i 1 3 repeat 4 ii + 1 5 ifA[i] < min 6 thenminA[i] 7 until iA.Length 8 returnmin 1 1 n − 1 Algorithms and Data Structures I

  39. Hence T(n) = n (where n = A.Length) Does this change if line 8 (returnmin) is considered as an extra step? In other words: n ≈ n + 1 It does not change! Proof: n + 1 = (n − 1) + 2 ? this counts as a single elementary step ≈ (n − 1) + 1 = n Algorithms and Data Structures I

  40. This so-called asymptotic behavior can be formulated rigorously in the following way: We say that f (x) = O(g(x)) (big O notation) if (∃C, x0 > 0) (∀x ≥ x0) 0 ≤ f (x) ≤ C∙g(x) means that g is an asymptotic upper bound of f Algorithms and Data Structures I

  41. C∙g(x) f (x) g(x) x0 Algorithms and Data Structures I

  42. The O notation denotes an upper bound. If g is also a lower bound of f then we say that f (x) = θ (g(x)) if (∃c, C, x0 > 0) (∀x ≥ x0) 0 ≤ c∙g(x) ≤ f (x) ≤ C∙g(x) means that fasymptotically equalsg Algorithms and Data Structures I

  43. f (x) C∙g(x) g(x) c∙g(x) x0C x0c =x0 Algorithms and Data Structures I

  44. What does the asymptotic notation show us? We have seen: T(n) = θ (n) for the procedure Minimum(A) where n = A.Length However, due to the definition of the θ function T(n) = θ (n), T(2n) = θ (n), T(3n) = θ (n) ... Minimum does not run slower on more data? ? Algorithms and Data Structures I

  45. What does the asymptotic notation show us? Asymtotic notation shows us the tendency: • T(n) = θ (n)lineartendency • ndata → a certainamount of timet • 2ndata → time ≈ 2t • 3ndata → time ≈ 3t • T(n) = θ (n2) quadratic tendency • n data → a certain amount of time t • 2n data → time ≈ 22t = 4t • 3n data → time ≈ 32t = 9t Algorithms and Data Structures I

  46. Analyzing recursive algorithms Recursive algorithm – recursive function T Example: Towers of Hanoi TowersOfHanoi(n,FirstRod,SecondRod,ThirdRod) 1 ifn > 0 2 then TowersOfHanoi(n – 1,FirstRod,ThirdRod,SecondRod) 3 write “Move a disk from ” FirstRod “ to ” SecondRod 4 TowersOfHanoi(n – 1,ThirdRod,SecondRod,FirstRod) T(n)= T(n−1) +T(n−1) +1 =2T(n−1)+1 Algorithms and Data Structures I

  47. T(n) = 2T(n−1) + 1 is a recursive function In general it is very difficult (sometimes insoluble) to determine the explicit form of an implicit (recursive) formula If the algorithm is recursive, the solution can be achieved using recursion trees. T(n)= =2T(n−1)+1 Algorithms and Data Structures I

  48. n Recursion tree of TowersOfHanoi: n−1 1 n−1 1 n−2 • 1 n−2 n−2 • 1 n−2 • 2 4 • 1 • 1 • 1 • 1 2n−1 • 1 • 1 • 1 • 1 2n−1 Algorithms and Data Structures I

  49. Time complexity: T(n) = 2n− 1 = θ (2n) − exponential time (very slow) Example: n = 64 (from the original legend) T(n) = 2n− 1 = 264− 1 ≈ 1.8∙1019 seconds = ≈ 3∙1017 minutes = ≈ 5.1∙1015 hours = ≈ 2.1∙1014 days = ≈ 5.8∙1011 years >half a trillion years = (assuming one disk move per second) Algorithms and Data Structures I

  50. Differentcases Problem (example): search a given element in a sequence (array). LinearSearch(A,w) 1 i 0 2 repeatii + 1 3 untilA[i] = wori = A.Length 4 ifA[i] = wthenreturni 5 else return NIL Algorithms and Data Structures I

More Related