Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic 1: Problem Solving

Similar presentations


Presentation on theme: "Topic 1: Problem Solving"— Presentation transcript:

1 Topic 1: Problem Solving
Evaluating Algorithms

2 Evaluating Algorithms
We’ve tested solutions for problems before Using trace tables, for example There is one more aspect of algorithms that we need to cover That is evaluating them This means looking at an algorithm and checking to see if we have all our bases covered Is the solution easy to understand and fully decomposed (we’ll see this later) Is the solution complete (i.e. it solves every part of the problem) Is the solution efficient (i.e. does it only use the needed resources, time and space, and not more) Problem Solving: Evaluating Algorithms

3 Evaluating Algorithms
Evaluation is an important step between designing the solution, and implementing it It lets us take a step back from the solution, and make sure it actually works If we don’t evaluate our solutions, we may let mistakes slip through to the final program Problem Solving: Evaluating Algorithms

4 Evaluating Algorithms
There are a few ways in which solutions could be wrong I.e. mistakes being added to the final problem We’ve not fully decomposed the problem More on that later The solution is incomplete The solution is inefficient The solution does not meet the requirements originally set out A correct solution should not match any of these Problem Solving: Evaluating Algorithms

5 Evaluating Algorithms: Meeting Requirements
The first thing we should check with a solution Does it meet the requirements in the problem In most problems we’ll come across, it’ll mention what it wants the solution to do For example, is a Graphical User Interface (GUI) needed at all? It could also mention what data needs to be returned If we’re making a function, we need to know what data it returns Problem Solving: Evaluating Algorithms

6 Problem Solving: Evaluating Algorithms
Take a look at this example problem Can you identify the requirements for the solution? A manager of a greenhouse wants someone to create a program for his wall-mounted computer. They want the program to display the current temperature of the greenhouse, and include three buttons: one for increasing the temperature, one for decreasing the temperature, and one for resetting the temperature to 18oC Problem Solving: Evaluating Algorithms

7 Evaluating Algorithms: Meeting Requirements
A manager of a greenhouse wants someone to create a program for his wall-mounted computer. They want the program to display the current temperature of the greenhouse, and include three buttons: one for increasing the temperature, one for decreasing the temperature, and one for resetting the temperature to 18oC Examining this problem brings up the following requirements of the solution It needs a GUI It needs to show the current temperature on the GUI It needs to show three buttons on the GUI One button raises the temperature One button lowers the temperature One button resets the temperature Problem Solving: Evaluating Algorithms

8 Evaluating Algorithms: Meeting Requirements
A manager of a greenhouse wants someone to create a program for his wall-mounted computer. They want the program to display the current temperature of the greenhouse, and include three buttons: one for increasing the temperature, one for decreasing the temperature, and one for resetting the temperature to 18oC This comes before designing the solution However, we can use what we found here to evaluate the solution we create And make sure it meets the requirements we found Problem Solving: Evaluating Algorithms

9 Evaluating Algorithms: Inefficiency
Inefficiency comes in at two places Inefficient design Inefficient space/time usage Design inefficiency rears its head when similar code is used in lots of places Just different data A function/procedure can solve these types Inefficient space/time usage comes from the algorithms we use to help create a solution Like sorting values in an array Problem Solving: Evaluating Algorithms

10 Problem Solving: Evaluating Algorithms
Take a look at this code Can you think of a way of making it more ‘efficient’? For this one, you can take those two switch-statements into a separate function. Then call that function, passing in the ‘userThrow’ and ‘computerThrow’ values. Problem Solving: Evaluating Algorithms

11 Evaluating Algorithms: Completeness
Incomplete solutions tend to forget parts of a problem For example, a problem asks for a Cat to be able to feed, sleep, and meow These would be functions in the ‘class’ Cat The solution would be incomplete if we didn’t include sleep Problem Solving: Evaluating Algorithms

12 Problem Solving: Evaluating Algorithms
Decomposing Problems If a problem is too big/complex to tackle in one chunk We break it down into smaller chunks Technically, this is breaking down its composition Known as decomposition This can help us tackle much larger problems First we break it into sub-problems Then we solve those sub-problems The solutions come together to solve the whole problem Problem Solving: Evaluating Algorithms

13 Problem Solving: Evaluating Algorithms
Decomposing Problems When decomposing a problem, it’s worth finding the key areas For example, the Binary Search algorithm has two distinct steps Order the collection (from least-to-greatest or greatest-to-least) Continuously divide the list in two until the item is found, or not found We can tackle these in two different functions sortItems searchForItem And can then run them from a single function binarySearch This is an example of Composition Abstraction Combining procedures/functions to form compound procedures/functions Problem Solving: Evaluating Algorithms

14 Problem Solving: Evaluating Algorithms
Take a look at the following problem How far can you decompose this problem? Martyn is working on a video game that involves collisions between objects. Every object is represent using a rectangle (that has four points, with each point having an X and Y value). Martyn needs to create a function that can state whether two rectangles have collided. However, he only wants to do this if the two rectangles are within a distance of each other. How could Martyn make this function? Problem Solving: Evaluating Algorithms

15 Problem Solving: Evaluating Algorithms
Decomposing Problems We can decompose this problem into the following steps Sub-steps are the sub- problems to solve See if the two rectangles are within a distance Calculate the distance between the two rectangles Find the centre of one rectangle Find the centre of the other rectangle Calculate distance between these points Check if it is smaller than the given distance See if the two rectangles collide Check if rectangles are touching on the X axis Check if rectangles are touching on the Y axis Problem Solving: Evaluating Algorithms

16 Problem Solving: Evaluating Algorithms
For some programming practice, why not have a go at the first sub-problem Calculate the distance between two rectangles In a program, create four variables The X and Y for the centre of rectangle 1 The X and Y for the centre of rectangle 2 Then make clever use of Pythagoras’ Theorem to calculate the distance between these points Problem Solving: Evaluating Algorithms

17


Download ppt "Topic 1: Problem Solving"

Similar presentations


Ads by Google