Читать книгу Algorithms For Dummies - John Paul Mueller, John Mueller Paul, Luca Massaron - Страница 29
Considering Algorithm Design
ОглавлениеIN THIS CHAPTER
Considering how to solve a problem
Using a divide-and-conquer approach to solving problems
Understanding the greedy approach to solving problems
Determining the costs of problem solutions
Performing algorithm measurements
An algorithm consists of a series of steps used to solve a problem, that could include input data to provide the basis of solving the problem and sometimes constraints that any solution must consider before anyone will regard the algorithm as effective. The first section of this chapter helps you consider the problem solution (the solution to the problem you’re trying to solve). It helps you understand the need to create algorithms that are both flexible (can handle a wide range of data inputs) and effective (yield the desired output).
The second section of this chapter considers how to derive a solution. Feeling overwhelmed by a problem is common and the most common way to solve the issue is to divide the problem into smaller, manageable pieces. This divide-and-conquer approach to problem solving originally referred to warfare (see a history of this approach at https://classroom.synonym.com/civilization-invented-divide-conquer-strategy-12746.html
). However, people use the same ideas to cut problems of all sorts down to size.
The third section of the chapter refers to the greedy approach to problem solving. Greed normally has a negative connotation (like stealing your friend’s fruit cup from their plate), but not in this case. A greedy algorithm is one that makes an optimal choice at each problem-solving stage. By doing so, it hopes to obtain an overall optimal solution to solve the problem. Unfortunately, this strategy doesn’t always work, but it’s always worth a try. It often yields a good enough solution, making it a good baseline.
No matter what problem-solving approach you choose, every algorithm comes with costs. Being good shoppers, people who rely heavily on algorithms want the best possible deal, which means performing a cost/benefit analysis. Of course, getting the best deal also assumes that a person using the algorithm has some idea of what sort of solution is good enough. Getting a solution that is too precise (one that offers too much detail) or one that offers too much in the way of output is often wasteful, so part of keeping costs under control is getting what you need as output and nothing more.
To know what you have with an algorithm, you need to know how to measure it in various ways. Measurements create a picture of usability, size, resource usage, and cost in your mind. More important, measurements offer the means of making comparisons. You can’t compare algorithms without measurements. Until you can compare the algorithms, you can’t choose the best one for a task.