Describing algorithms in a lingua franca
Algorithms do provide a basis for communication between people, even when those individuals have different perspectives and speak different languages. For example, Bayes’ Theorem (the probability of an event occurring given certain premises; see https://betterexplained.com/articles/an-intuitive-and-short-explanation-of-bayes-theorem/
for a quick explanation of this amazing theorem)
P(B|E) = P(E|B)*P(B)/P(E)
appears the same whether you speak English, Spanish, Chinese, German, French, or any other language. Regardless what language you speak, the algorithm looks the same and acts the same given the same data. Algorithms help cross all sorts of divides that serve to separate humans from each other by expressing ideas in a form that anyone can prove. As you go through this book, you discover the beauty and magic that algorithms can provide in communicating even subtle thoughts to others.
Apart from universal mathematical notations, algorithms take advantage of programming languages as a means for explaining and communicating the formulas they solve. You can find all the sorts of algorithms in C, C++, Java, Fortran, Python (as in this book), and other languages. Pseudocode is a way to describe computer operations by using common English words. Some writers rely on pseudocode to overcome the fact that an algorithm may be proposed in a programming language that you don't know. In addition, pseudocode can be more concise than a programming language because you can use intuitive ideas that the programming language may not capture well.
Facing problems that are like brick walls, only harder
An important consideration when working with algorithms is that you can use them to solve issues of any complexity. The algorithm doesn’t think, have emotion, or care how you use it (or even abuse it). You can use algorithms in any way required to solve an issue. For example, the same group of algorithms used to perform facial recognition to act as an alternative to computer passwords (for security purposes) can find terrorists lurking in an airport or recognize a lost child wandering the streets. The same algorithm has different uses; how to use it depends on the interests of the user. Part of the reason you want to read this book carefully is to help you solve those hard problems that may require only a simple algorithm to address.
Structuring Data to Obtain a Solution
Humans think about data in nonspecific ways and apply various rules to the same data to understand it in ways that computers never can. A computer’s view of data is structured, simple, uncompromising, and most definitely not creative. When humans prepare data for a computer to use, the data often interacts with the algorithms in unexpected ways and produces undesirable output. The problem is one in which the human fails to appreciate the limited view of data that a computer has. The following sections describe two aspects of data that you see illustrated in many of the chapters to follow.
Understanding a computer’s point of view
A computer has a simple view of data, but it’s also a view that humans typically don’t understand. For one thing, everything is a number to a computer because computers aren’t designed to work with any other kind of data. Humans see characters on the computer display and assume that the computer interacts with the data in that manner, but the computer doesn’t understand the data or its implications. The letter A is simply the number 65 to the computer. In fact, it’s not truly even the number 65. The computer sees a series of electrical impulses that equate to a binary value of 0100 0001.
Computers also don’t understand the whole concept of uppercase and lowercase. To a human, the lowercase a is simply another form of the uppercase A, but to a computer they’re two different values. A lowercase a appears as the number 97 (a binary value of 0110 0001).
If these simple sorts of single letter comparisons could cause such problems between humans and computers, it isn’t hard to imagine what happens when humans start assuming too much about other kinds of data. For example, a computer can’t hear or appreciate music. Yet, music comes out of the computer speakers. The same holds true for graphics. A computer sees a series of 0s and 1s, not a graphic containing a pretty scene of the countryside.
It’s important to consider data from the computer’s perspective when using algorithms. The computer sees only 0s and 1s, nothing else. Consequently, when you start working through the needs of the algorithm, you must view the data in that manner. You may actually find it beneficial to know that the computer’s view of data makes some solutions easier to find, not harder. You discover more about this oddity in viewing data as the book progresses.
Arranging data makes the difference
Computers also have a strict idea about the form and structure of data. When you begin working with algorithms, you find that a large part of the job involves making the data appear in a form that the computer can use when using the algorithm to find a solution to an issue. Although a human can mentally see patterns in data that isn’t arranged precisely right, computers really do need the precision to find the same pattern. The benefit of this precision is that computers can often make new patterns visible. In fact, that’s one of the main reasons to use algorithms with computers — to help locate new patterns and then use those patterns to perform other tasks. For example, a computer may recognize a customer’s spending pattern so that you can use the information to generate more sales automatically.
Chapter 2
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