- University of Applied Sciences - Data Structures and Algorithm Design - CSCI 340 - Friedhelm Seutter Institut für Angewandte Informatik Contents 1 Analyzing Algorithms and Problems 2 Data Abstraction 3 Recursion and Induction 4 Sorting 6 Dynamic Sets and Searching 7 Graphs and Graph Traversals 8 Optimization and Greedy Algorithms 10 Dynamic Programming 13 NP-Complete Problems Institut für Angewandte Informatik 2 1
3 Recursion and Induction Recursive procedures Proving correctness Recurrence equations Recurrence trees Institut für Angewandte Informatik 3 Recursive procedures A recursive procedure is a procedure which calls itself, directly or indirectly Each individual procedure invocation at run time has its own storage space for the procedure s local variables This storage space is called activation frame Institut für Angewandte Informatik 4 2
Fibonacci function Institut für Angewandte Informatik 5 Fibonacci algorithm Institut für Angewandte Informatik 6 3
Dynamic nesting of blocks fib(3) fib(2) fib(1) fib(1) fib(0) f = 1 f = 0 f = 1 + 0 = 1 f = 1 f = 1 + 1 = 2 Institut für Angewandte Informatik 7 Tree of activation frames fib(3) fib(2) fib(1) fib(1) fib(0) Institut für Angewandte Informatik 8 4
Sequence of calls and returns Procedure calls: Preorder tree walk Return values: Postorder tree walk Institut für Angewandte Informatik 9 Proving correctness A proof of a program (procedure or function) is an argument along the block structure of the program A block is a section of program code with an entry point and an exit point It refers to local or nonlocal data Institut für Angewandte Informatik 10 5
Precondition, postcondition, and specification Institut für Angewandte Informatik 11 Control structures block 1 block 2 block sequence Institut für Angewandte Informatik 12 6
Control structures IF (condition) THEN ELSE WHILE (condition) DO trueblock falseblock block alternation loop Institut für Angewandte Informatik 13 Control structures function(x, y) function(x 1, x 2 ) block procedure call Institut für Angewandte Informatik 14 7
Correctness lemma forms Institut für Angewandte Informatik 15 Correctness lemma forms Institut für Angewandte Informatik 16 8
Correctness lemma forms Institut für Angewandte Informatik 17 Correctness lemma forms Institut für Angewandte Informatik 18 9
Binary Search Institut für Angewandte Informatik 19 Binary Search Institut für Angewandte Informatik 20 10
Recurrence equations A recurrence equation defines a function over ΙN, say T(n), in terms of its own value at one or more integers smaller than n A base case needs to be defined separately Institut für Angewandte Informatik 21 Fibonacci function Institut für Angewandte Informatik 22 11
Complexity of recursive Procedures Let n be the input size, and f(n) the nonrecursive complexity, ie to split up the problem into subproblems and to combine the solutions of the subproblems to a solution Let T(n) be the complexity of the recursive procedure Then T(n) is defined as a recurrence equation: T(n) = T(m) + f(n), and m < n Institut für Angewandte Informatik 23 Examples Fibonacci Algorithm: T(n) = T(n-1) + T(n-2) + 1 T(0) = T(1) = 1 Binary Search: T(n) = T(n/2) + 1 T(0) = 1 Institut für Angewandte Informatik 24 12
Fibonacci algorithm Institut für Angewandte Informatik 25 Binary Search Institut für Angewandte Informatik 26 13
Divide and Conquer Institut für Angewandte Informatik 27 Divide and Conquer The problem is divided recursively into subproblems of a fractional size of the problem T(n) = b T(n/c) + f(n) b 1 number of subproblems of size n/c, c > 1, branching factor f(n) some nonrecursive complexity Institut für Angewandte Informatik 28 14
Chip and Conquer The problem is chipped down recursively to a subproblem of a smaller size of the problem T(n) = T(n c) + f(n) c > 0, f(n) some nonrecursive complexity Institut für Angewandte Informatik 29 Chip and Be Conquered The problem is chipped down recursively to subproblems of a smaller size of the problem T(n) = b T(n c) + f(n) b 1 number of subproblems of size n c, c > 0, branching factor f(n) some nonrecursive complexity Institut für Angewandte Informatik 30 15
Recursion trees Recursion trees provide a tool for analyzing the complexity of recursive procedures for which a recurrence equation has been developed Node of a recursion tree: T(size) nonrec cost Institut für Angewandte Informatik 31 Divide and Conquer Recursion Tree Recurrence equation of Merge-Sort: T(n) = 2 T(n/2) + n Institut für Angewandte Informatik 32 16
Divide and Conquer Recursion Tree T(n) n T(n/2) n/2 T(n/2) n/2 T(n/4) n/4 T(n/4) n/4 T(n/4) n/4 T(n/4) n/4 Institut für Angewandte Informatik 33 Divide and Conquer Recursion Tree T(n) = n + 2 T(n/2) = n + 2(n/2) + 4 T(n/4) = 2n + 4 T(n/4) = =? Institut für Angewandte Informatik 34 17
Divide and Conquer Recursion Tree Summing up nonrecursive costs: depth 0: 1 n = n depth 1: 2 n/2 = n depth 2: 4 n/4 = n depth lg n: n 1 = n n ( lg n + 1) = θ(n lg n) Institut für Angewandte Informatik 35 Divide and Conquer (general case) Recurrence Equation: T(n) = b T(n/c) + f(n) Node depth of base-case nodes: n/c D = 1 D = lg n / lg c Number of nodes at depth D (ie leaves): L = b D lg L = D lg b = lg n (lg b / lg c) L = n E Critical exponent: E = lg b / lg c Institut für Angewandte Informatik 36 18
Master Theorem Institut für Angewandte Informatik 37 Divide and Conquer (special cases) Binary Search (b=1, c=2, f(n)=1): E = lg 1 / lg 2 = 0 und f(n) Θ(n 0 ) = Θ(n E ), thus T(n) Θ(log n) Merge-Sort (b=2, c=2, f(n)=n): E = lg 2 / lg 2 = 1 und f(n) Θ(n 1 ) = Θ(n E ), thus T(n) Θ(n log n) Institut für Angewandte Informatik 38 19
Chip and be conquered rec tree Recurrence equation of Fibonacci Algorithm: T(n) = T(n-1) + T(n-2) + 1 2 T(n-1) + 1 Institut für Angewandte Informatik 39 Chip and be conquered rec tree T(n) 1 T(n-1) 1 T(n-1) 1 T(n-2) 1 T(n-2) 1 T(n-2) 1 T(n-2) 1 Institut für Angewandte Informatik 40 20
Chip and be conquered rec tree Summing up nonrecursive costs: depth 0: 1 1 = 1 depth 1: 2 1 = 2 depth 2: 4 1 = 4 depth n: 2 n 1 = 2 n θ ( 2 n ) Institut für Angewandte Informatik 41 Chip and Be Conquered (general case) Recurrence Equation: T(n) = b T(n-c) + f(n) Node depth of base-case nodes: D = n / c Number of nodes at depth D: L = b D = b n/c Institut für Angewandte Informatik 42 21
Chip and Be Conquered (general case) Institut für Angewandte Informatik 43 Chip and Be Conquered (special cases) Fibonacci Algorithm (b=2, c=1, f(n)=1): T(n) = (0 d n/c) 2 d = 2 n+1 1 Θ(2 n ) b=1, f(n) Θ(n α ): T(n) Θ(n α +1 ) b=1, f(n) Θ(log n): T(n) Θ(n log n) Institut für Angewandte Informatik 44 22