Sunday, 21 August 2016
Thinking in Algorithms
An algorithm is a step-by-step process. This lends itself to being written in an imperative programming language like C#, which executes operations one at a time and in order.
Algorithms are written with statements starting at a beginning statement and finishing that statement before moving on to the next. Just because we know what an algorithm is doesn’t mean it’s any easier to create one.
Therefore, how do we go about writing the all-important algorithm? The first thing should be how you might accomplish any given task by hand. As an example, say we’re a computer surrounded by a mob of zombies.
Thankfully, computers are very fast so we can do a lot before the zombies can get to us.
With this in mind we’re going to want to focus on the zombie closest to us.
Unfortunately, computers are also very dumb, so there’s nothing which allows us to just guess at which one is the closest. First, we’re going to need to know how far away every zombie is from our point of view.
Therefore, the computer would probably start with measuring the distance from itself to every zombie available to make a measurement to. After that we’re going to have to compare zombies and distances, so we’ll start with the first zombie we found and compare its distance to the next zombie we found.
If the next zombie is closer we’ll have to remember that one as being the closest one we’ve come across. Otherwise, if the next one is farther away, then we can forget about him and move on to the next zombie and compare numbers again.
Once we’ve gone through all of the zombies, we should have identified the zombie who is the closest one to us. The process which we just thought through is an algorithm for finding the zombie closest to us. It’s not complex, but we had to do a few steps to get to it. First, we needed to get a list of all the zombies we needed to sort through.
After that it was an iterative process, by which we needed to remember one zombie as the closest one, then compare others against him to decide whether or not he would retain the status of being the zombie nearest to us. We’ll be going over many of these problem-solving thought processes as we learn how to program. This algorithm would look something like the following code block:
Programming is a lot more about a thought process than it is about processing. In other words, programming is less about how to do multiplication as it is about what to do with the result of a multiplication process. In your everyday life we take for granted the everyday things we do on a regular basis. However, take a moment and really consider the complexity involved with something as simple as breathing.
Your diaphragm is receiving signals from your autonomic nervous system, causing several chemical reactions in the diaphgragm’s muscle tissue, which then pull proteins toward one another causing it to contract. This in turn creates a lowered air pressure in your lungs, which pulls in air.
There’s a whole number of chemical processes which then allow the oxygen in the atmosphere to enter your blood stream. Thankfully, all of this happens without our thinking of it because of chemistry and physics. In programming, nothing is so automatic.
Algorithms are written with statements starting at a beginning statement and finishing that statement before moving on to the next. Just because we know what an algorithm is doesn’t mean it’s any easier to create one.
Therefore, how do we go about writing the all-important algorithm? The first thing should be how you might accomplish any given task by hand. As an example, say we’re a computer surrounded by a mob of zombies.
Thankfully, computers are very fast so we can do a lot before the zombies can get to us.
With this in mind we’re going to want to focus on the zombie closest to us.
Unfortunately, computers are also very dumb, so there’s nothing which allows us to just guess at which one is the closest. First, we’re going to need to know how far away every zombie is from our point of view.
Therefore, the computer would probably start with measuring the distance from itself to every zombie available to make a measurement to. After that we’re going to have to compare zombies and distances, so we’ll start with the first zombie we found and compare its distance to the next zombie we found.
If the next zombie is closer we’ll have to remember that one as being the closest one we’ve come across. Otherwise, if the next one is farther away, then we can forget about him and move on to the next zombie and compare numbers again.
Once we’ve gone through all of the zombies, we should have identified the zombie who is the closest one to us. The process which we just thought through is an algorithm for finding the zombie closest to us. It’s not complex, but we had to do a few steps to get to it. First, we needed to get a list of all the zombies we needed to sort through.
After that it was an iterative process, by which we needed to remember one zombie as the closest one, then compare others against him to decide whether or not he would retain the status of being the zombie nearest to us. We’ll be going over many of these problem-solving thought processes as we learn how to program. This algorithm would look something like the following code block:
void Update ()
{
float closestDistance = Mathf.Infinity;
GameObject closestGameObject = null;
GameObject[] allObjects = (GameObject[]) GameObject.FindSceneObjectsOfType( typeof(GameObject));
foreach(GameObject g in allObjects)
{
if (g.name != this.name)
{
float distance = (g.transform.position - this. transform. position).sqrMagnitude;
if(distance < closestDistance)
{
closestDistance = distance;
closestGameObject = g;
}
}
}
Debug.DrawLine(this.transform.position, closestGameObject.transform.position);
}We won’t cover the entirety of how this code works, as we are yet to discuss the bulk of what’s going on here, but in short we are indeed creating a list of objects called allObjects. Then we enter a loop where we compare the distance of each object against the one we’ve decided was the closest. If we find a closer object then we change the closestGameObject to the compared object. Just for a proof of concept we draw a line from the object. Because the script is attached to the object, we draw a line from the object the script is attached to toward the object selected as the closest.
Programming is a lot more about a thought process than it is about processing. In other words, programming is less about how to do multiplication as it is about what to do with the result of a multiplication process. In your everyday life we take for granted the everyday things we do on a regular basis. However, take a moment and really consider the complexity involved with something as simple as breathing.
Your diaphragm is receiving signals from your autonomic nervous system, causing several chemical reactions in the diaphgragm’s muscle tissue, which then pull proteins toward one another causing it to contract. This in turn creates a lowered air pressure in your lungs, which pulls in air.
There’s a whole number of chemical processes which then allow the oxygen in the atmosphere to enter your blood stream. Thankfully, all of this happens without our thinking of it because of chemistry and physics. In programming, nothing is so automatic.
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment