




























































































Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!





























































































Goal This lecture covers the main strategies for algorithm design. You will learn to: ◦ Handle repetitive tasks through iteration , ◦ Iterate elegantly using recursion , ◦ Use brute force when you’re lazy but powerful, ◦ Test bad options and then backtrack , ◦ Save time with heuristics for a reasonable way out, ◦ Divide and conquer your toughest opponents, Identify old issues dynamically not to waste energy again, ◦ Bound your problem so the solution doesn’t escape.
Outline Introduction Backtracking Greedy algorithms Divide and Conquer Dynamic Programming Branch and Bound Local Search
Outline Introduction Backtracking Greedy algorithms Divide and Conquer Dynamic Programming Branch and Bound Local Search
Iteration The iterative strategy consists in using loops (e.g. for, while) to repeat a process until a condition is met. Each step in a loop is called an iteration. It’s great for running through an input and applying the same operations on every part of it.
Iteration Examples Fish reunion Exploring scents
Fish reunion Solution It is possible to iteratively compare the top items of the two lists as follows: Cod Herring Marlin Asp Carp Ide Trout Asp Cod Herring Marlin Carp Ide Trout Asp Carp Cod Herring Marlin Ide Trout Asp Carp Cod Herring Marlin Ide Trout Asp Carp Cod Herring
Asp Carp Cod Herring Ide Marlin Trout Marlin Trout Asp Carp Cod Herring Ide Marlin Marlin Ide Trout Asp Carp Cod Herring Ide Fish reunion Solution Trout Asp Carp Cod Herring Ide Marlin Trout
Exploring scents Problem : Floral fragrances are made combining scents from flowers. Given a set of flowers F, how do you list all fragrances that can be made?
Exploring scents Any fragrance is made from a subset of F, so its power set contains all possible fragrances. We can compute this power set iteratively. Given a collection of objects S , the power set of S is the set containing all subsets of S 2 . For considering an additional flower, we duplicate the fragrances we already have, adding the new flower to the duplicated fragrances.
Exploring scents Solution A single extra flower causes fragrances to double in size, indicating exponential growth ( 2 k+ = 2 × 2 k ). Algorithms that require double the operations if the input size increases by a single item are exponential, with O( n ) time complexity.
Recursion The recursion can be defined as a function that delegate the work to clones of itself. A recursive algorithm will naturally come to mind for solving a problem defined in terms of itself.
Fibonacci sequence The Fibonacci sequence starts with two ones, and each subsequent number is the sum of the two previous numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... function fib(n) if n ≤ 2 return 1 return fib(n - 1) + fib(n – 2)
Fibonacci sequence