More Algorithms In this presentation, we're going to explore algorithms further. We're going to look at Algorithms applied to two separate problems. [Slide 1] As we already know, algorithms are set of instructions that can be used repeatedly to solve a problem or complete a task. As your problem gets bigger, the need for a good algorithm grows too because you need a systematic way to solve the bigger problem. We're going to examine two simple problems, counting and mazes. [Slide 2] Okay, counting, something we all know how to do. We're going to look at three counting problems. Counting students in your class. Counting students in your school. Counting all the high school students in your state. [Slide 3] Counting students in your class is really easy, I mean your teacher can go around and count the students or the students can count off one at a time. Now we can write a bit of pseudocode, that's code written in English not in a specific computer programming language. Here's how the pseudocode will look for counting students in a class. You would set the variable of ClassCount to be zero. While there's students still left in your class to count, you would set the variable ClassCount to ClassCount plus one, that's a really easy problem. [Slide 4] Now counting students in the school gets to be a little harder. You could use sort of a brute force method and gather all the students together and line them up and have someone go down the line counting them or have them count off one at a time, but that would probably take quite a long time. Another method is to use divide and conquer. Divide and conquer is an algorithmic principle which breaks the problem down into smaller pieces and then solves it. One way to use divide and conquer to count the students in your school would be to leave the students into classrooms, have each classroom count their students, creating a variable for that classroom called ClassCount then you would add all the ClassCounts together and get a number called SchoolCount. [Slide 5] The pseudocode for counting the students in your school looks something like this. The first part of the pseudocode would be the same as for the classroom count, so in each classroom you'd set the ClassCount to zero and while there's students left in the class to count, you'd set the variable ClassCount to ClassCount plus one. Then for the school, you would set the variable school count to zero and while there's still classrooms left to count, you would go to each classroom ask that class how many students are in it, ask them for their variable ClassCount and then set the SchoolCount to equal the SchoolCount plus that classroom's ClassCount. You would repeat this for all the classrooms in the school. At the end your SchoolCount would be equal to the number of students in your school. More Algorithms Page 1 of 3 [Slide 6] Okay, now how many high school students are there in the state. This is an even harder problem. Well, obviously, you can't really line them up. You would have to use the divide and conquer approach. You'd have to have each school count their students and at each school, you'd leave the students in their classroom and have each class count off their students and then add the students from each of those classroom to get the SchoolCount then you'd have the schools report their SchoolCount numbers and for each school, you would add that SchoolCount to the StateCount. [Slide 7] Let's look at the pseudocode. Now, you'd notice the first two parts of the pseudocode are exactly the same as before, so for each classroom and each school, you set the ClassCount to zero. Then while there's still students left in that class to count, you would set the ClassCount variable to ClassCount plus one. After you counted all the students in each classroom, then for that school, you'd set the SchoolCount to zero and while there's still classrooms left to count, you would go to each classroom, ask that classroom how many students are in it, asking for its ClassCount and then you would set the SchoolCount to be SchoolCount plus ClassCount. Then for the state, you would set the StateCount to be zero and while there's still schools left to count, you would go to each school and ask them with how many students are in that school or ask them for their SchoolCount then you would set the StateCount to be the StateCount plus the SchoolCount and you repeat that for all the schools in the state. [Slide 8] Let's look at mazes. Here's a really simple maze. You don't actually need an algorithm to solve this maze. You'd start here and wend your way around like this and come out over here. No algorithm required. [Slide 9] On the other hand, perhaps you might need an algorithm for this one. [Slide 10] What kind of algorithms exists for solving mazes? Well, actually a number of algorithms exists for solving mazes and we're going to talk about two here. The first one we're going to talk about is wall following. Now this is good if you can't see the whole maze and it works on simply connected mazes. With wall following, you pick a wall either the right wall or the left wall and you keep following it. Never changing which wall you're following. In the diagram below, the diagram on the left is using the right wall follow and as you can see it's following the right wall all the way around to the end. The diagram on the right is using left wall follow and it's following the left wall all the way around to the end. [Slide 11] Here's the pseudocode for wall following. This pseudocode is for a left wall following. First you find the nearest wall on the left, then if you can turn left do so, and move forward. If you can't turn left move forward if you can, but if More Algorithms Page 2 of 3 you can't move forward, turn right and then move forward and you would repeat that for every step that you take. Now that's a little hard to understand verbally. I found a video on YouTube for you to watch. [Slide 12] Here's the reference for that video, and here's the video. [Slide 13] [Slide 14] This second approach we're going to look about for mazes is dead end filling. This algorithm is good if you can see the whole maze or if your computer can see the whole maze. What you do if find all the dead ends and then fill them in, what you're left with is the solution to the maze. Now this works only if there are dead end. Some mazes don't have any dead ends and hence the dead end filling method won't work. [Slide 15] Here's the pseudocode for the dead end filling algorithm. First find all the dead ends, you'd scan the maze, identify the dead ends which would have walls on three sides. Then you would remember where the dead ends are. Mark them in some way. Then you would go back and fill in the dead ends. You'd go to each dead end and while there's not a junction, which has walls only on two sides, you'd fill in the maze. What you'd have is the solution to your maze problem. [Slide 16] Again I found another video on YouTube and here's the reference for that video, and here's the video. [Slide 17] [Slide 18] In summary, algorithms are sets of instructions that can be used to repeatedly solve a problem or completed task. The bigger the problem, the more you need a systematic way to solve it. In this video, we examine two types of problems ... counting problems where we learned about the algorithmic principle of divide and conquer and mazes where we looked at two algorithms, wall following and dead end filling. Thank you. More Algorithms Page 3 of 3