Theory of Computation Abstraction Automation Computer Science Problem
Theory of Computation Abstraction & Automation Computer Science
Problem Solving • A key part (arguably the key part) to being a computer scientist is in being able to solve problems – Not so much programming – More thinking about how a problem could be solved • We call this Problem Solving, and typically involves two steps (that have sub-steps) – Analysing a problem – Designing a solution 2 Theory of Computation: Abstraction & Automation
Problem Solving • We’ll start simple and work out way up • The first thing we need to be able to do is solve ‘simple’ logic problems – And check our solutions for them • This involves being given a small problem, then stating how we would solve them 3 Theory of Computation: Abstraction & Automation
• See if you can come up with a good solution for the following problem – Be sure to discuss amongst the whole class! A group of philosophers are out to lunch in a fancy restaurant. Each philosopher can do only one of two things at a time: think and eat. They are around a circular table, with bowls of spaghetti in front of them. To the left and right of each philosopher is a fork. A philosopher can only eat if they use both forks. Think of a system the philosophers can use so that they eat as often as possible (before they starve)! 4 Theory of Computation: Abstraction & Automation
Problem Solving • There’s no wrong answer to this problem – But some solutions are better than others • Here is one possible way of solving this problem Start with a random philosopher around the table, and let them pick up both forks to eat with. After an arbitrary amount of time, make them put down their forks and move over to the next philosopher clockwise! 5 Theory of Computation: Abstraction & Automation
Creating Algorithms • Any time we create a solution for a problem, we have different ways of representing that solution • Two such ways are – Pseudocode – Flowcharts • An important term to bring up before looking at these is algorithm “A sequence of steps that can be followed to complete a task, and that always terminates” 6 Theory of Computation: Abstraction & Automation
Creating Algorithms • The strict definition of an algorithm simply means listing the steps required to solve a problem – There is no requirements on the representation of these steps • We could write a paragraph of text, list the steps in bullet-point form, and more – Paragraphs of text are not the best formats to use – Makes the steps difficult to spot or understand 7 Theory of Computation: Abstraction & Automation
Creating Algorithms • This is where those two representation formats we mentioned earlier come in • We will focus on pseudocode for the A Level – Means “fake code” • Is syntactically similar to most programming languages – Though nowhere near as strict 8 Theory of Computation: Abstraction & Automation
Creating Algorithms • Let’s take the philosopher solution example from earlier and turn it into pseudocode BEGIN Feed. Philosophers(philosophers) starting. Philosopher RANDOM(0, SIZE(philosophers)) current. Philosopher starting. Philosopher BEGIN DO Feed. Philosopher(philosophers, current. Philosopher) current. Philosopher + 1 IF current. Philosopher > SIZE(philosophers) current. Philosopher 0 END IF END WHILE(current. Philosopher != starting. Philosopher) END Feed. Philosophers 9 Theory of Computation: Abstraction & Automation
Creating Algorithms • Some things to note here • We still use indentation – To show scope • We begin and end scope BEGIN Feed. Philosophers(philosophers) starting. Philosopher RANDOM(0, SIZE(philosophers)) current. Philosopher starting. Philosopher BEGIN DO Feed. Philosopher(philosophers, current. Philosopher) current. Philosopher + 1 IF current. Philosopher > SIZE(philosophers) current. Philosopher 0 END IF END WHILE(current. Philosopher != starting. Philosopher) END Feed. Philosophers • Keywords are capitalised 10 Theory of Computation: Abstraction & Automation
• Take the solution you created earlier and turn it into pseudocode • Don’t worry too much about the correct words to use for now – Just turn it into something that looks like code • We’ll look at pseudocode standards after this exercise 11 Theory of Computation: Abstraction & Automation
Pseudocode Standards • Every construct usable in a programming language can be included in pseudocode too • This includes – Sequence – Selection – Iteration • To make all pseudocode easy to read (especially when reading different algorithms), standards are used 12 Theory of Computation: Abstraction & Automation
Pseudocode Standards • First, we use capital letters for keywords – Starting/ending scope – Using constructs • Use the arrow ( ) symbol to represent assignment – Leaving equals (=) for equality checking 13 OUTPUT FOR EACH WHILE DO IF ELSE IF WHILE playing even … END WHILE IF number is … END IF pivot ← length /2 Theory of Computation: Abstraction & Automation
• Create a solution for the following problem (and represent it using pseudocode) “Given two numbers (which are assigned the words Fizz and Buzz respectively), how could we output the numbers from 1 to 100, replacing the number with Fizz and/or Buzz whenever it is a multiple of them? 14 Theory of Computation: Abstraction & Automation
Pseudocode to Programming Language • If we use the given standards for pseudocode, converting pseudocode to a programming language is relatively simple – Depending on the programming language we use • As we use the name of the construct in the pseudocode, we simply write code for that construct in the language • As the pseudocode works in a sequence, we also write the code in the same order 15 Theory of Computation: Abstraction & Automation
Pseudocode to Programming Language BEGIN Feed. Philosophers(philosophers) starting. Philosopher RANDOM(0, SIZE(philosophers)) current. Philosopher starting. Philosopher BEGIN DO Feed. Philosopher(philosophers, current. Philosopher) current. Philosopher + 1 IF current. Philosopher > SIZE(philosophers) current. Philosopher 0 END IF END WHILE(current. Philosopher != starting. Philosopher) END Feed. Philosophers 16 Python Theory of Computation: Abstraction & Automation Java C#
• Implement your algorithm in a programming language – Python, Java, or C# • Don’t worry about testing it – Only make sure there are no syntax errors 17 Theory of Computation: Abstraction & Automation
Abstraction • When it comes to creating a solution for a problem, there may be times when there is too much information – Data that isn’t needed for the solution • Stripping away (or hiding) this information is known as abstraction – Keeping only the key details/pieces of information needed to solve the problem • There are multiple types of abstraction, that deal with removing different details – Representational Abstraction – Abstraction by Generalisation/Categorisation 18 Theory of Computation: Abstraction & Automation
Representational Abstraction • This type of abstraction involves removing unnecessary information from the problem itself • For example, if the problem involves calculating the number of computers needed in a lab, we may not need to know – The tutor’s name – The types of computer – What programs the computers will be running • If this information is included in the problem, we can hide it and only focus on the bits we need 19 Theory of Computation: Abstraction & Automation
Representational Abstraction • For example, take this problem Casey is playing a game with her students in school. She has six boxes in front of her, each for a subject (Maths, Biology, Physics, Chemistry, Geography, and History). She asks one of her students to roll a die, and depending on the number picks a random question from one of the boxes (1 Maths, 2 Biology, 3 Physics, 4 Chemistry, 5 Geography, 6 History) and then asks the student that rolled the die that question. If they get it right, they earn a point. If they don’t, any student can answer the question (and earn a point if they get it right). In either case, the next student rolls the die and repeats the same steps. 20 Theory of Computation: Abstraction & Automation
Representational Abstraction • We can ‘cross out’ any unnecessary information (leaving only what we need to solve this problem) Casey is playing a game with her students in school. She has six boxes in front of her, each for a subject (Maths, Biology, Physics, Chemistry, Geography, and History). She asks one of her students to roll a die, and depending on the number picks a random question from one of the boxes (1 Maths, 2 Biology, 3 Physics, 4 Chemistry, 5 Geography, 6 History) and then asks the student that rolled the die that question. If they get it right, they earn a point. If they don’t, any student can answer the question (and earn a point if they get it right). In either case, the next student rolls the die and repeats the same steps. 21 Theory of Computation: Abstraction & Automation
Representational Abstraction • Can also apply to remove details from an object • For example, we have a cat – Certain fur colour – Tail length – Eye colours • We don’t need to know the details – Only the general structure • Also an example of information hiding – Though more obvious on practice – Used in public, private, protected 22 Theory of Computation: Abstraction & Automation
Abstraction by Generalisation • This type of abstraction impacts the design of the solution more than the problem itself • It’s main goal is to put pieces of information in a problem into categories – And the state how they relate to each other in some way • Generalisation/categorisation works hand-in-hand with objectorientated-programming 23 Theory of Computation: Abstraction & Automation
Abstraction by Generalisation • For example, we’re working on a solution that involves collecting cats – We have a Cat class for this • We could collect these cats in different ways Arrays, Lists, Linked. Lists, Stacks, Queues, etc. • Making a ‘groom’ function to apply to a series of cats would be tedious – groom(Cat[]), groom(List<Cat>), groom(Linked. List<Cat>), etc. • We can make a more general function that applies to a whole range of possibilities – groom(Collection<Cat>) 24 Theory of Computation: Abstraction & Automation
Abstraction by Generalisation • This also applies in inheritance • Imagine we have different animals that we want to groom – Dogs, Lizards, Birds • We can make each one an Animal • Then apply the function to that – groom(Collection<Animal>) 25 Theory of Computation: Abstraction & Automation
Abstraction • There are more types of abstraction techniques too – That involve different aspects of the problem/solution • These are – Procedural Abstraction – Functional Abstraction – Data Abstraction – Problem Abstraction/Reduction 26 Involving the solution Involving the problem Theory of Computation: Abstraction & Automation
Procedural Abstraction • Goes hand-in-hand with decomposition – We’ll see that later • Involves breaking parts of a solution off into their own procedure – Takes the ‘complexity’ away from the actual solution – Means we don’t need to know how that part is done – Only that it works as intended • A very important part to any complex solution • The procedure we make ends up not caring about the data used – Only how it works – We supply the data when we run this procedure 27 Theory of Computation: Abstraction & Automation Here register_user is the abstracted procedure
Functional Abstraction • This is a variant of Procedural Abstraction – Involves functions instead of procedures • That means a value is expected after running the function 28 Theory of Computation: Abstraction & Automation Here length_between_points is the abstracted function
Data Abstraction • This is all about hiding how specific data-types/data-structures work from the user – They can still use it – But do not need to know how it works • For example, we could create a My. List class – Acts like a List (dynamic storage of values) • It works by making an array and recreating arrays every time an item is added/deleted • The user doesn’t need to know this – Only that the My. List dynamically resizes 29 Theory of Computation: Abstraction & Automation
Problem Abstraction/Reduction • Occurs more in mathematical problems than others • Used when a problem may be difficult to solve • Involves transforming a problem into a different problem – Which is easily solvable • We then use the result of solving that problem in the solution for the original problem • For example, if we’re calculating the Lowest Common Multiple of two numbers – Can find the Greatest Common Divider instead – Then use it in a calculation to get the Lowest Common Multiple 30 Theory of Computation: Abstraction & Automation
(De)Composition • The final aspect of abstraction is the idea of a problem’s makeup – Its composition • 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 31 Theory of Computation: Abstraction & Automation
(De)Composition • 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 – sort. Items – search. For. Item • And can then run them from a single function – binary. Search • This is an example of Composition Abstraction – Combining procedures/functions to form compound procedures/functions 32 Theory of Computation: Abstraction & Automation
(De)Composition • We can also apply Composition Abstraction to create more complex data types – From simpler ones • That’s Object-Orientation! • For example, we could make a Dog data-type – From integers, strings, and other primitive types • The same applies to data-structures (like Graphs and Trees) 33 Theory of Computation: Abstraction & Automation
Automation • All of this problem/solution abstraction feeds into a very interesting topic in computer science – Automation • Generally put: involves making systems that can create/monitor other systems, or solve problems, without any human input • Quite a common example comes in the form of automated cars 34 Theory of Computation: Abstraction & Automation
Automation • Computer scientists have been looking into automating cars for a few years • Involves huge amounts of complexity, as systems need to think about – Cars around the system – Traffic rules in the area – Objects around the system • They need to be able to recognise other vehicles and objects – So they know how to act in different situations 35 Theory of Computation: Abstraction & Automation
Automation • We’re bringing this up now because automation relies on thorough abstraction • On the visual recognition side, a camera in an automated car needs to be able to recognise other cars – Cars can have different makes, models, sizes, colours, and more – Proper abstraction needs to be put in place so a camera can recognise any car • Can take lots of work, involving other aspects of computer science – Like Genetic Programming 36 Theory of Computation: Abstraction & Automation
- Slides: 37