Data Structures and Algorithms Algorithm Design Greedy Methods

Total Page:16

File Type:pdf, Size:1020Kb

Data Structures and Algorithms Algorithm Design Greedy Methods Programs = Algorithms + Data Structures Niklaus Wirth Algorithms are often designed using common Data Structures and Algorithms techniques, including: Algorithm Design • Greedy Methods Greedy Methods and Divide & • Divide & Conquer Conquer • Dynamic Programming See references in Goodrich & Tamassia to • Backtracking Greedy Methods & Divide & Conquer • Brute Force • Optimisation Problems • Branch & Bound • Greedy Methods • Linear Programming • Proving Greedy Methods Optimal • Integer Programming • Divide & Conquer • Neural Networks • Genetic Algorithms • Simulated Annealing • ... 1 2 Change Making Intuition: attempt to construct a sum using the minimum number of coins. Optimisation Problems A set of coin denominations, e.g. Comprise {1, 2, 5, 10, 20, 50} • A set of constraints A target, e.g. 67p • An optimisation function A solution is a sequence of coins ci ∈ denominations, e.g. [20, 20, 20, 2, 2, 2, 1] or A solution that satisfies the constraints is [20, 20, 20, 5, 1, 1] feasible. A feasible solution with the best n possible value for the optimisation function is The constraint is Σi ci = target optimal. The optimisation function is the length of the sequence, e.g. 7 or 6 The optimal solution has the minimum sequence length, i.e. 6 3 4 Greedy Algorithm Design Technique Many real world problems can be formulated as Key idea: Attempt to construct the optimal optimisation problems. Operations Research solution by repeatedly taking the ’best’ feasible studies such problems, amongst others, for solution. large organisations like Airlines or Strategic Greedy Alg. 1: Change Making commands etc. Chose the largest possible denomination coin at Example problems: each stage: • Find the shortest journey, visiting Target Coin Selected Edinburgh, Newcastle, Carlisle, Inverness, 67 Glasgow. • Find the maximum volume of arbitrary-sized crates that can be packed into a container. Optimal solution = Exercise: Trace the algorithm for target 48p, giving the optimal solution. 5 6 Greedy Alg. 2: Simple Shortest Path Chose the cheapest edge to traverse from the end of the current path. 40 1 ----> 2 Is the Greedy Algorithm Optimal? |\ ^\ 20| \30 |10\50 It is usually easy to produce a greedy algorithm | \ | >5 for a problem. | \|/ However the algorithm may not always find v v | /30 an optimal solution. 3 ----> 4 20 Hence it is essential to prove that a proposed algorithm does find an optimal soluion. simpleShortestPath(1,5) = length = ShortestPath(1,5) = length = Dijkstra’s Shortest Path: Choose the cheapest edge to traverse from any reached vertex. 7 8 Change Making Correctness Proof 2. For the optimal change, we can establish t10 < (by contradiction) 3, t < 2, f < 2, tw < 3, p < 2. To see this, Fifties, twenties, tens, fives, twos and pennies are note that if t10 >= 3, we can replace three currency denominations. Twenties are smaller twenties by a fifty and a ten and provide the denominations than fifties; and tens are smaller change using one less coin. This is not possible denominations twenties; etc. as t10+t+f+tw+p is the fewest number of coins with which the change can be provided. Let F10, T10, T, F, TW and P respectively, be the Similarly for the other denominations. Hence, number of fifties, twenties, tens, fives, twos and the total amount of change given in lower pennies in the change generated by the greedy denominations is less than the value of the algorithm. Let f10, t10, t, f, tw and p, respectively, next higher denomination. be the number of fifties, twenties, tens, fives, twos and pennies in the change generated by an optimal Now if F10 != f10, then either the greedy or the algorithm. optimal solution must provide 50 pence or more in We make the following observations: lower denominations. This violates the above 1. From the way the greedy algorithm works, it observations. So, F10 = f10. Similarly, if T10 != follows that the total amount of change given t10, then either the greedy or the optimal solution in lower denominations is less than the value of must provide 20p or more in lower denominations the next higher denomination. That is, the which violates the above observations, so, T10 = change given in pennies is less than 2p; the t10. We can show T = t, F = f, TW = tw and P = change given in pennies and twos is less than p in a similar way. Therefore, the greedy and < < < 5p; etc. Therefore, T10 3, T 2, F 2, optimal solutions are the same. TW < 3, P < 2. 9 10 D&C1: Detecting a Counterfeit Problem: you are given a balance and a bag of 16 coins, one of which may be counterfeit. Counterfeight coins are lighter than genuine coins. Algorithm Design Technique 2: 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 Divide & Conquer Algorithms: 1. Naive: compare coin 1 with coins Key idea: 2,3, ... 16 1. Divide the problem into smaller Max. No. comparisons = independent subproblems 2. Better: Weigh pairs Max. No. comparisons = 2. Solve the subproblems 3. Best: 3. Combine the solutions 1. Divide the coins into two sets of eight: A and Good for parallel programming B 2. Solve: weigh A and B 3. Combine: Counterfeit present iff (if and only if) A != B Max. No. comparisons = 11 12 D&C2: Identifying the Counterfeit Problem: locate the counterfeit coin in the scenario above. 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 Algorithms 1, and 2 from previous slide work, Exercise: Does this algorithm work if there are requiring at most 15 and 8 comparisons • at most two counterfeit coins, i.e. 0, 1 or 2 respectively. counterfeits? Exercise: How many comparisons to identify 14 • 0 or 3 counterfeit coins? as the counterfeit using Algorithm 1? And using Algorithm 2? • some natural number 0, 1, 2, .. 16 of counterfeit coins? 1. Divide the coins into two equal-size sets : A and B 2. Solve: weigh A and B, lighter set contains counterfeit, so apply D&C to it 3. Combine: return counterfeit coin Max. No. comparisons = 4 13 14 Backtracking D&C3: Quicksort Key idea: First organise the candidate Quicksort was devised by C.A.R. Hoare and solutions to make searching easy, e.g. a graph sorts a sequence (list/array/file/etc) into or tree. Then search the solution space ascending order as follows. depth-first, and if the node at the end of the 1. Divide: Pick an element of the sequence current search proves infeasible, mark it as (typically the first element) as the pivot. Split dead and backtrack to the previous node. the sequence into two: his: all elements Live nodes are part of the current solution greater than the pivot, and los all elements under investigation (often on a path). smaller than the pivot. Dead nodes are part of solutions that have 2. Solve: Apply D&C to his giving hiSort and proved to be infeasible. apply D&C to los giving loSort 3. Combine: Append loSort, pivot and hiSort More example backtracking algorithms in Weiss Section 10.5 15 16 Solution space is an undirected graph: 1,1 ---- 2,1 ---- 3,1 ---- 4,1 Maze Search Problem | | | | | | | | Find a path through a maze from top left (1,1) 1,2 ---- 2,2 ---- 3,2 ---- 4,2 to bottom right (4,4) avoiding obstacles | | | | Maze is represented as a matrix: | | | | 1,3 ---- 2,3 ---- 3,3 ---- 4,3 1 2 3 4 | | | | | | | | 1 0000 1,4 ---- 2,4 ---- 3,4 ---- 4,4 2 0111 3 0010 Every path from (1,1) to (4,4) is a solution, but 4 0000 some cross obstacles, and are not feasible. Search Objective: to find a feasible path from top-left to bottom-right. 17 18 A Backtracking Alg: Maze seach Depth First Search DFS tries to move Right, Down, Left, Up 0 0 0 0 Depth First Search 0 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 1 1 Path: 0 0 1 0 Note: Backtracking is easily implemented in 0 0 0 0 Logic languages like Prolog, and in lazy functional languages like Haskell. Backtrack! Exercise: Trace the algorithm for the following 0 0 0 0 maze. 0 1 1 1 0 0 0 0 0 0 1 0 0 1 0 1 0 0 0 0 0 1 1 1 0 0 0 0 19 20 Branch&Bound Alg: Maze Search Breadth First Search Branch&Bound 0 0 0 0 Key idea: Use a breadth first search of the 0 1 1 1 solution space, but prune suboptimal solutions 0 0 0 0 0 0 1 0 2 paths Good for parallelism: there are many solutions to consider simultaneously But ... multiple solutions consume resources, 0 0 0 0 including memory 0 1 1 1 0 0 0 0 0 0 1 0 2 paths 21 22 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 1 0 3 paths 0 0 1 0 1 paths Top path is infeasible Prune: chose shorter, or arbitrarily 0 0 0 0 between equal-length 0 1 1 1 0 0 0 0 0 0 0 0 0 1 1 1 0 0 1 0 1 path 0 0 0 0 0 0 1 0 2 paths Path: 23 24 Fibionacci Numbers Dynamic Programming The fibionacci numbers are a sequence of numbers defined as follows (in SML) Key idea: Store optimal solutions to subproblems, and use them to solve bigger fun fib 0 = 0 problems | fib 1 = 1 | fib n = fib(n-1) + fib(n-2) Often used to produce an efficient version of a recursively specified problem i.e.
Recommended publications
  • An Iterated Greedy Metaheuristic for the Blocking Job Shop Scheduling Problem
    Dipartimento di Ingegneria Via della Vasca Navale, 79 00146 Roma, Italy An Iterated Greedy Metaheuristic for the Blocking Job Shop Scheduling Problem Marco Pranzo1, Dario Pacciarelli2 RT-DIA-208-2013 Novembre 2013 (1) Universit`adegli Studi Roma Tre, Via della Vasca Navale, 79 00146 Roma, Italy (2) Universit`adegli Studi di Siena Via Roma 56 53100 Siena, Italy. ABSTRACT In this paper we consider a job shop scheduling problem with blocking (zero buffer) con- straints. Blocking constraints model the absence of buffers, whereas in the traditional job shop scheduling model buffers have infinite capacity. There are two known variants of this problem, namely the blocking job shop scheduling (BJSS) with swap allowed and the BJSS with no-swap. A swap is needed when there is a cycle of two or more jobs each waiting for the machine occupied by the next job in the cycle. Such a cycle represents a deadlock in no-swap BJSS, while with a swap all the jobs in the cycle move simulta- neously to their subsequent machine. This scheduling problem is receiving an increasing interest in the recent literature, and we propose an Iterated Greedy (IG) algorithm to solve both variants of the problem. The IG is a metaheuristic based on the repetition of a destruction phase, which removes part of the solution, and a construction phase, in which a new solution is obtained by applying an underlying greedy algorithm starting from the partial solution. Comparison with recent published results shows that the iterated greedy outperforms other state-of-the-art algorithms on benchmark instances.
    [Show full text]
  • GREEDY ALGORITHMS Greedy Algorithms Solve Problems by Making the Choice That Seems Best at the Particular Moment
    GREEDY ALGORITHMS Greedy algorithms solve problems by making the choice that seems best at the particular moment. Many optimization problems can be solved using a greedy algorithm. Some problems have no efficient solution, but a greedy algorithm may provide a solution that is close to optimal. A greedy algorithm works if a problem exhibits the following two properties: 1. Greedy choice property: A globally optimal solution can be arrived at by making a locally optimal solution. In other words, an optimal solution can be obtained by making "greedy" choices. 2. optimal substructure. Optimal solutions contain optimal sub solutions. In other Words, solutions to sub problems of an optimal solution are optimal. Difference Between Greedy and Dynamic Programming The most difference between greedy algorithms and dynamic programming is that we don’t solve every optimal sub-problem with greedy algorithms. In some cases, greedy algorithms can be used to produce sub-optimal solutions. That is, solutions which aren't necessarily optimal, but are perhaps very dose. In dynamic programming, we make a choice at each step, but the choice may depend on the solutions to subproblems. In a greedy algorithm, we make whatever choice seems best at 'the moment and then solve the sub-problems arising after the choice is made. The choice made by a greedy algorithm may depend on choices so far, but it cannot depend on any future choices or on the solutions to sub- problems. Thus, unlike dynamic programming, which solves the sub-problems bottom up, a greedy strategy usually progresses in a top-down fashion, making one greedy choice after another, interactively reducing each given problem instance to a smaller one.
    [Show full text]
  • Greedy Estimation of Distributed Algorithm to Solve Bounded Knapsack Problem
    Shweta Gupta et al, / (IJCSIT) International Journal of Computer Science and Information Technologies, Vol. 5 (3) , 2014, 4313-4316 Greedy Estimation of Distributed Algorithm to Solve Bounded knapsack Problem Shweta Gupta#1, Devesh Batra#2, Pragya Verma#3 #1Indian Institute of Technology (IIT) Roorkee, India #2Stanford University CA-94305, USA #3IEEE, USA Abstract— This paper develops a new approach to find called probabilistic model-building genetic algorithms, solution to the Bounded Knapsack problem (BKP).The are stochastic optimization methods that guide the search Knapsack problem is a combinatorial optimization problem for the optimum by building probabilistic models of where the aim is to maximize the profits of objects in a favourable candidate solutions. The main difference knapsack without exceeding its capacity. BKP is a between EDAs and evolutionary algorithms is that generalization of 0/1 knapsack problem in which multiple instances of distinct items but a single knapsack is taken. Real evolutionary algorithms produces new candidate solutions life applications of this problem include cryptography, using an implicit distribution defined by variation finance, etc. This paper proposes an estimation of distribution operators like crossover, mutation, whereas EDAs use algorithm (EDA) using greedy operator approach to find an explicit probability distribution model such as Bayesian solution to the BKP. EDA is stochastic optimization technique network, a multivariate normal distribution etc. EDAs can that explores the space of potential solutions by modelling be used to solve optimization problems like other probabilistic models of promising candidate solutions. conventional evolutionary algorithms. The rest of the paper is organized as follows: next section Index Terms— Bounded Knapsack, Greedy Algorithm, deals with the related work followed by the section III Estimation of Distribution Algorithm, Combinatorial Problem, which contains theoretical procedure of EDA.
    [Show full text]
  • Greedy Algorithms Greedy Strategy: Make a Locally Optimal Choice, Or Simply, What Appears Best at the Moment R
    Overview Kruskal Dijkstra Human Compression Overview Kruskal Dijkstra Human Compression Overview One of the strategies used to solve optimization problems CSE 548: (Design and) Analysis of Algorithms Multiple solutions exist; pick one of low (or least) cost Greedy Algorithms Greedy strategy: make a locally optimal choice, or simply, what appears best at the moment R. Sekar Often, locally optimality 6) global optimality So, use with a great deal of care Always need to prove optimality If it is unpredictable, why use it? It simplifies the task! 1 / 35 2 / 35 Overview Kruskal Dijkstra Human Compression Overview Kruskal Dijkstra Human Compression Making change When does a Greedy algorithm work? Given coins of denominations 25cj, 10cj, 5cj and 1cj, make change for x cents (0 < x < 100) using minimum number of coins. Greedy choice property Greedy solution The greedy (i.e., locally optimal) choice is always consistent with makeChange(x) some (globally) optimal solution if (x = 0) return What does this mean for the coin change problem? Let y be the largest denomination that satisfies y ≤ x Optimal substructure Issue bx=yc coins of denomination y The optimal solution contains optimal solutions to subproblems. makeChange(x mod y) Implies that a greedy algorithm can invoke itself recursively after Show that it is optimal making a greedy choice. Is it optimal for arbitrary denominations? 3 / 35 4 / 35 Chapter 5 Overview Kruskal Dijkstra Human Compression Overview Kruskal Dijkstra Human Compression Knapsack Problem Greedy algorithmsFractional Knapsack A sack that can hold a maximum of x lbs Greedy choice property Proof by contradiction: Start with the assumption that there is an You have a choice of items you can pack in the sack optimal solution that does not include the greedy choice, and show a A game like chess can be won only by thinking ahead: a player who is focused entirely on Maximize the combined “value” of items in the sack contradiction.
    [Show full text]
  • Types of Algorithms
    Types of Algorithms Material adapted – courtesy of Prof. Dave Matuszek at UPENN Algorithm classification Algorithms that use a similar problem-solving approach can be grouped together This classification scheme is neither exhaustive nor disjoint The purpose is not to be able to classify an algorithm as one type or another, but to highlight the various ways in which a problem can be attacked 2 1 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Divide and conquer algorithms Dynamic programming algorithms Greedy algorithms Brute force algorithms Randomized algorithms Note: we may even classify algorithms based on the type of problem they are trying to solve – example sorting algorithms, searching algorithms etc. 3 Simple recursive algorithms I A simple recursive algorithm: Solves the base cases directly Recurs with a simpler subproblem Does some extra work to convert the solution to the simpler subproblem into a solution to the given problem We call these “simple” because several of the other algorithm types are inherently recursive 4 2 Example recursive algorithms To count the number of elements in a list: If the list is empty, return zero; otherwise, Step past the first element, and count the remaining elements in the list Add one to the result To test if a value occurs in a list: If the list is empty, return false; otherwise, If the first thing in the list is the given value, return true; otherwise Step past the first element, and test whether the value occurs in the remainder of the list 5 Backtracking algorithms Backtracking algorithms are based on a depth-first* recursive search A backtracking algorithm: Tests to see if a solution has been found, and if so, returns it; otherwise For each choice that can be made at this point, Make that choice Recur If the recursion returns a solution, return it If no choices remain, return failure *We will cover depth-first search very soon (once we get to tree-traversal and trees/graphs).
    [Show full text]
  • Hybrid Metaheuristics Using Reinforcement Learning Applied to Salesman Traveling Problem
    13 Hybrid Metaheuristics Using Reinforcement Learning Applied to Salesman Traveling Problem Francisco C. de Lima Junior1, Adrião D. Doria Neto2 and Jorge Dantas de Melo3 University of State of Rio Grande do Norte, Federal University of Rio Grande do Norte Natal – RN, Brazil 1. Introduction Techniques of optimization known as metaheuristics have achieved success in the resolution of many problems classified as NP-Hard. These methods use non deterministic approaches that reach very good solutions which, however, don’t guarantee the determination of the global optimum. Beyond the inherent difficulties related to the complexity that characterizes the optimization problems, the metaheuristics still face the dilemma of exploration/exploitation, which consists of choosing between a greedy search and a wider exploration of the solution space. A way to guide such algorithms during the searching of better solutions is supplying them with more knowledge of the problem through the use of a intelligent agent, able to recognize promising regions and also identify when they should diversify the direction of the search. This way, this work proposes the use of Reinforcement Learning technique – Q-learning Algorithm (Sutton & Barto, 1998) - as exploration/exploitation strategy for the metaheuristics GRASP (Greedy Randomized Adaptive Search Procedure) (Feo & Resende, 1995) and Genetic Algorithm (R. Haupt & S. E. Haupt, 2004). The GRASP metaheuristic uses Q-learning instead of the traditional greedy- random algorithm in the construction phase. This replacement has the purpose of improving the quality of the initial solutions that are used in the local search phase of the GRASP, and also provides for the metaheuristic an adaptive memory mechanism that allows the reuse of good previous decisions and also avoids the repetition of bad decisions.
    [Show full text]
  • L09-Algorithm Design. Brute Force Algorithms. Greedy Algorithms
    Algorithm Design Techniques (II) Brute Force Algorithms. Greedy Algorithms. Backtracking DSA - lecture 9 - T.U.Cluj-Napoca - M. Joldos 1 Brute Force Algorithms Distinguished not by their structure or form, but by the way in which the problem to be solved is approached They solve a problem in the most simple, direct or obvious way • Can end up doing far more work to solve a given problem than a more clever or sophisticated algorithm might do • Often easier to implement than a more sophisticated one and, because of this simplicity, sometimes it can be more efficient. DSA - lecture 9 - T.U.Cluj-Napoca - M. Joldos 2 Example: Counting Change Problem: a cashier has at his/her disposal a collection of notes and coins of various denominations and is required to count out a specified sum using the smallest possible number of pieces Mathematical formulation: • Let there be n pieces of money (notes or coins), P={p1, p2, …, pn} • let di be the denomination of pi • To count out a given sum of money A we find the smallest ⊆ = subset of P, say S P, such that ∑di A ∈ pi S DSA - lecture 9 - T.U.Cluj-Napoca - M. Joldos 3 Example: Counting Change (2) Can represent S with n variables X={x1, x2, …, xn} such that = ∈ xi 1 pi S = ∉ xi 0 pi S Given {d1, d2, …, dn} our objective is: n ∑ xi i=1 Provided that: = ∑ di A ∈ pi S DSA - lecture 9 - T.U.Cluj-Napoca - M. Joldos 4 Counting Change: Brute-Force Algorithm ∀ ∈ n xi X is either a 0 or a 1 ⇒ 2 possible values for X.
    [Show full text]
  • The Classification of Greedy Algorithms
    View metadata, citation and similar papers at core.ac.uk brought to you by CORE provided by Elsevier - Publisher Connector Science of Computer Programming 49 (2003) 125–157 www.elsevier.com/locate/scico The classiÿcation of greedy algorithms S.A. Curtis Department of Computing, Oxford Brookes University, Oxford OX33 1HX, UK Received 22 April 2003; received in revised form 5 September 2003; accepted 22 September 2003 Abstract This paper presents principles for the classiÿcation of greedy algorithms for optimization problems. These principles are made precise by their expression in the relational calculus, and illustrated by various examples. A discussion compares this work to other greedy algorithms theory. c 2003 Elsevier B.V. All rights reserved. MSC: 68R05 Keywords: Greedy algorithms 1. Introduction What makes an algorithm greedy? Deÿnitions in the literature vary slightly, but most describe a greedy algorithm as one that makes a sequence of choices, each choice being in some way the best available at that time (the term greedy refers to choosing the best). When making the sequence of choices, a greedy algorithm never goes back on earlier decisions. Because of their simplicity, greedy algorithms are frequently straightforward and e9cient. They are also very versatile, being useful for di:erent problems in many varied areas of combinatorics and beyond. Some examples of applications include data compression and DNA sequencing [16,23,25], ÿnding minimum-cost spanning trees of weighted undirected graphs [24,30,34], computational geometry [17], and routing through networks [20], to name but a few. Some problems are impractical to solve exactly, but may have a greedy algorithm that can be used as a heuristic, to ÿnd solutions which are close to optimal.
    [Show full text]
  • Dynamic Programming Vs. Divide-&-Conquer
    Dynamic Programming vs. Divide-&-conquer • Divide-&-conquer works best when all subproblems are independent. So, pick partition that makes algorithm most efficient & simply combine solutions to solve entire problem. • Dynamic programming is needed when subproblems are dependent; we don’t know where to partition the problem. For example, let S1= {ALPHABET}, and S2 = {HABITAT}. Consider the subproblem with S1 = {ALPH}, S2 = {HABI}. Then, LCS (S1, S2) + LCS (S1-S1, S2-S2) LCS(S1, S2) • Divide-&-conquer is best suited for the case when no “overlapping subproblems” are encountered. • In dynamic programming algorithms, we typically solve each subproblem only once and store their solutions. But this is at the cost of space. 11/1/07 COT 5407 1 Dynamic programming vs Greedy 1. Dynamic Programming solves the sub-problems bottom up. The problem can’t be solved until we find all solutions of sub-problems. The solution comes up when the whole problem appears. Greedy solves the sub-problems from top down. We first need to find the greedy choice for a problem, then reduce the problem to a smaller one. The solution is obtained when the whole problem disappears. 2. Dynamic Programming has to try every possibility before solving the problem. It is much more expensive than greedy. However, there are some problems that greedy cannot solve while dynamic programming can. Therefore, we first try greedy algorithm. If it fails then try dynamic programming. 11/1/07 COT 5407 2 Fractional Knapsack Problem • Burglar’s choices: Items: x1, x2, …, xn Value: v1, v2, …, vn Max Quantity: q1, q2, …, qn Weight per unit quantity: w1, w2, …, wn Getaway Truck has a weight limit of B.
    [Show full text]
  • Greedy Algorithms Part One
    Greedy Algorithms Part One Announcements ● Problem Set Three due right now if using a late period. ● Solutions will be released at end of lecture. Outline for Today ● Greedy Algorithms ● Can myopic, shortsighted decisions lead to an optimal solution? ● Lilypad Jumping ● Helping our amphibious friends home! ● Activity Selection ● Planning your weekend! Frog Jumping ● The frog begins at position 0 in the river. Its goal is to get to position n. ● There are lilypads at various positions. There is always a lilypad at position 0 and position n. ● The frog can jump at most r units at a time. ● Goal: Find the path the frog should take to minimize jumps, assuming a solution exists. Formalizing the Algorithm ● Let J be an empty series of jumps. ● Let our current position x = 0. ● While x < n: ● Find the furthest lilypad l reachable from x that is not after position n. ● Add a jump to J from x to l's location. ● Set x to l's location. ● Return J. Greedy Algorithms ● A greedy algorithm is an algorithm that constructs an object X one step at a time, at each step choosing the locally best option. ● In some cases, greedy algorithms construct the globally best object by repeatedly choosing the locally best option. Greedy Advantages ● Greedy algorithms have several advantages over other algorithmic approaches: ● Simplicity: Greedy algorithms are often easier to describe and code up than other algorithms. ● Efficiency: Greedy algorithms can often be implemented more efficiently than other algorithms. Greedy Challenges ● Greedy algorithms have several drawbacks: ● Hard to design: Once you have found the right greedy approach, designing greedy algorithms can be easy.
    [Show full text]
  • Greedy Algorithms
    Greedy Algorithms Introduction Analysis of algorithms In this lecture we begin the actual \analysis of algorithms" by examining greedy algorithms, which are considered among the easiest algorithms to describe and implement. Before doing so, we describe the different aspects of an algorithm that require analysis. Correctness It must be established that the algorithm performs as advertised. In other words, for each legal input, called a problem instance, the algorithm produces the desired output. Complexity Bounds must be provided on either the amount of time required to execute the algo- rithm, or the amount of space (e.g., memory) that is consumed by the algorithm as a function of the size of an input instance. Implementation Appropriate data structures must be identified that allow for the algorithm to achieve a desired time or space complexity. The correctness of an algorithm is sometimes immediate from its description, while the correctness of others may require clever mathematical proofs. As for complexity, in this course we are primarily concerned with the big-O growth of the worst-case running time of an algorithm. Of course, the running time T will be a function of the size of an input to the algorithm, and will have a big-O growth that is directly proportional to the number of algorithm steps that are required to process the given input. Here we define the size of a problem instance as the minimum number of bits needed to represent the instance. The convention is to represent the size of an instance using one or more size parameters that increase in proportion to the problem size.
    [Show full text]
  • The Reconstruction and Optimization of Trie Hashing Functions
    THE RECONSTRUCTIONAND OPTIMIZATION OF TRIE HASHING FUNCTIONS Leen Torenvliet and P. van Emde Boas University of Amsterdam, Depts of Mathematics and Computer Science Roetersstraat 15, 1018 WB Amsterdam, the Netherlands. Abstract: We propose an adaptation to the trie running program but may be used by subsequent hashing algorithm published by W. Litwin in programs,which are updating this information. Even 1980. This adaptation extends the algorithm if there is only one program which builds this so that it will save necessary information on information and runs forever, a system crash might secondary storage to reconstruct the hashing spoil the created hashing function and we would function after loss of information (E.G., be forced to start all over again from scratch. system crash or termination of a find/insert Each program that wants to update the information program). An algorithm is given to reconstruct in the file would have to reconstruct the trie the trie from the information saved, and an- giving access to this file. There are essential- other for optimizing the reconstructed trie. ly two ways in which we can enable programs to A mathematical analysis of the trie data perform this reconstruction: structure is given, making visible the essen- First the original construction method can tial structural properties of these tries; be used by reinserting all records on the disk. based on this analysis correctness of the This is a rather slow and tedious job which usual- algorithms presented can be established. ly will require many disk accesses. Alternative- ly we can, during construction of the original 0.
    [Show full text]