Topic 1: Problem Solving

Slides:



Advertisements
Similar presentations
Chapter 11 Sorting and Searching. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Examine the linear search and.
Advertisements

Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
1 MT258 Computer Programming and Problem Solving Unit 7.
Top-down approach / Stepwise Refinement & Procedures & Functions.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion (Continued) Prof. Katherine Gibson Prof. Jeremy Dixon Based on slides from UPenn’s.
Advanced Higher Computing Science
Copyright Prentice Hall Modified by Sana odeh, NYU
Error Analysis Logic Errors.
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
Searching and Sorting Algorithms
Recursion Topic 5.
CMSC201 Computer Science I for Majors Lecture 20 – Recursion (Continued) Prof. Katherine Gibson Based on slides from UPenn’s CIS 110, and from previous.
Repetition Structures
COMP 53 – Week Eleven Hashtables.
CMSC201 Computer Science I for Majors Lecture 22 – Searching
Chapter 9: Searching, Sorting, and Algorithm Analysis
IGCSE 6 Cambridge Effectiveness of algorithms Computer Science
Collision Detection Box-to-Box.
CS 1321.
IF statements.
Robo – Software Engineering
CMSC201 Computer Science I for Majors Lecture 11 – Program Design
The life cycle.
Subnetting IP4 ICND/CCNA Prep.
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Topic 3: Data Binary Arithmetic.
CS 3343: Analysis of Algorithms
Binary Search Back in the days when phone numbers weren’t stored in cell phones, you might have actually had to look them up in a phonebook. How did you.
Last Class We Covered Data representation Binary numbers ASCII values
Learning to Program in Python
2008/12/03: Lecture 20 CMSC 104, Section 0101 John Y. Park
OOP Paradigms There are four main aspects of Object-Orientated Programming Inheritance Polymorphism Abstraction Encapsulation We’ve seen Encapsulation.
Searching and Sorting Topics Sequential Search on an Unordered File
Starter 15//2 = 7 (Quotient or Floor) (Modulus) 22%3 =1
For example:
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
Computational Thinking
Objective of This Course
Unit-2 Divide and Conquer
Abstraction & Automation
Searching and Sorting Topics Sequential Search on an Unordered File
Lesson Objectives Aims You should be able to:
An introduction to decomposition: Tut, clap or Jive
Coding Concepts (Sub- Programs)
Theory of Computation Turing Machines.
We’re slowly working our way towards classes and objects
Last Class We Covered Dictionaries Hashing Dictionaries vs Lists
Coding Concepts (Basics)
Computer Science Testing.
Algorithm Discovery and Design
Repetition Structures
Topic 1: Problem Solving
Searching and Sorting Topics Sequential Search on an Unordered File
4. Computational Problem Solving
Algorithms.
Algorithms and Problem Solving
Software Development Process
Search,Sort,Recursion.
COMPUTATIONAL THINKING COMPUTATIONAL THINKING IN PROGRAMMING
Recursion Chapter 11.
Chapter 18 Recursion.
Computational Thinking
Last Class We Covered Recursion Stacks Parts of a recursive function:
Basic Concepts of Algorithm
Chapter 13 Recursion Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CSE 326: Data Structures Lecture #14
CMSC201 Computer Science I for Majors Lecture 12 – Program Design
Presentation transcript:

Topic 1: Problem Solving Evaluating Algorithms

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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