Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 3: Algorithms Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Aims of the presentation Algorithms and their representations – Pages 15 to 27
Faculty of Sciences and Social Sciences HOPE What is Problem Solving?
Faculty of Sciences and Social Sciences HOPE What is Problem Solving? Creating a solution to a problem
Faculty of Sciences and Social Sciences HOPE What is Problem Solving? Creating a solution to a problem A solution to a problem is a set of instructions that, if carried out, will lead to a successful conclusion.
Faculty of Sciences and Social Sciences HOPE What is Problem Solving? A set of instructions that, if carried out, will lead to a successful conclusion is known as..
Faculty of Sciences and Social Sciences HOPE What is Problem Solving? A set of instructions that, if carried out, will lead to a successful conclusion is known as..... an algorithm
Faculty of Sciences and Social Sciences HOPE An algorithm is … A solution to a problem A set of instructions to carry out a task
Faculty of Sciences and Social Sciences HOPE An algorithm is … A solution to a problem A set of instructions to carry out a task
Faculty of Sciences and Social Sciences HOPE Here’s a Problem Crossing a Busy Main Road !!!! Write down a set of instructions, using short phrases, to solve the above problem. Work in pairs or groups of three
Faculty of Sciences and Social Sciences HOPE Solution 1 Walk across the road till you reach the other side.
Faculty of Sciences and Social Sciences HOPE Solution 1 Walk across the road till you reach the other side. Is this a good solution ?
Faculty of Sciences and Social Sciences HOPE Is the next one a better solution ?
Faculty of Sciences and Social Sciences HOPE Solution 2 Look right Look left Look right again Cross the road
Faculty of Sciences and Social Sciences HOPE Is the next one a better solution ?
Faculty of Sciences and Social Sciences HOPE Solution 3 while it is not safe to cross begin Look right Look left Look right end cross the road
Faculty of Sciences and Social Sciences HOPE Solution 3 while it is not safe to cross begin Look right Look left Look right end cross the road We are using a specialised form of English to show repetition. This form of English is called: Structured English
Faculty of Sciences and Social Sciences HOPE Solution 3 while it is not safe to cross begin Look right Look left Look right end cross the road Structured English is also known as: Program Design Language PDL Pseudocode
Faculty of Sciences and Social Sciences HOPE Nearly there ! Some fine tweaking needed !!
Faculty of Sciences and Social Sciences HOPE Solution 4 Look right Look left Look right while it is not safe to cross begin Look right Look left Look right end cross the road
Faculty of Sciences and Social Sciences HOPE Solution 4 Look right Look left Look right while it is not safe to cross begin Look right Look left Look right end cross the road We look first then ask ourselves the question. We repeat the question asking after repeated looks......until it is safe to cross
Faculty of Sciences and Social Sciences HOPE Solution 4 Look right Look left Look right while it is not safe to cross begin Look right Look left Look right end cross the road We look first then ask ourselves the question. We repeat the question asking after repeated looks......until it is safe to cross
Faculty of Sciences and Social Sciences HOPE Solution 4 Look right Look left Look right while it is not safe to cross begin Look right Look left Look right end cross the road We look first then ask ourselves the question. We repeat the question asking after repeated looks......until it is safe to cross
Faculty of Sciences and Social Sciences HOPE Problem Solving Constructs A famous computer scientist called Dijkstra showed that solutions to any problem can be described in term of the following 3 constructs Sequence Selection Repetition
Faculty of Sciences and Social Sciences HOPE Problem Solving Constructs A famous computer scientist called Dijkstra showed that solutions to any problem can be described in term of the following 3 constructs Sequence Selection Repetition We met these in Java Trainer
Faculty of Sciences and Social Sciences HOPE if inside a loop createpieeater(); randompies(20); while (clearahead) { if (pieinsight) { eatpie(); } else { walk(); }
Faculty of Sciences and Social Sciences HOPE Problem I want a hot pie for lunch
Faculty of Sciences and Social Sciences HOPE go and buy a hot pie bring the pie home if it is Sunday today then begin put it on a posh plate end else begin put it on an old plate end get a knife and fork cut the pie into 4 pieces while there are 1 or more pieces left begin eat a piece of pie end
Faculty of Sciences and Social Sciences HOPE go and buy a hot pie bring the pie home if it is Sunday today then begin put it on a posh plate end else begin put it on an old plate end get a knife and fork cut the pie into 4 pieces while there are 1 or more pieces left begin eat a piece of pie end Structured English can show: Sequence Selection Repetition
Faculty of Sciences and Social Sciences HOPE go and buy a hot pie bring the pie home if it is Sunday today then begin put it on a posh plate end else begin put it on an old plate end get a knife and fork cut the pie into 4 pieces while there are 1 or more pieces left begin eat a piece of pie end Structured English can show: Sequence Selection Repetition Sequence Selection Sequence Repetition
Faculty of Sciences and Social Sciences HOPE Algorithms in Structured English The solutions to problems written in the above style using keywords such as: – if …then …else – while – begin … end are algorithms written in Structured English
Faculty of Sciences and Social Sciences HOPE Algorithms & Flowcharts Here is an algorithm consisting of a sequence: do the first thing do the second thing do the third thing
Faculty of Sciences and Social Sciences HOPE Algorithms & Flowcharts do the first thing do the second thing do the third thing Start Finish Here is a pictorial representation of the algorithm. This kind of pictorial representation is called a flow chart. Here is an algorithm consisting of a sequence: do the first thing do the second thing do the third thing
Faculty of Sciences and Social Sciences HOPE Algorithms & Flowcharts do the first thing do the second thing do the third thing Start Finish Each step is inside a box. The algorithm starts at the bubble labelled Start and ends at the bubble labelled Finish. The action follows the trail of arrows. Each step is inside a box. The algorithm starts at the bubble labelled Start and ends at the bubble labelled Finish. The action follows the trail of arrows. Here is an algorithm consisting of a sequence: do the first thing do the second thing do the third thing
Faculty of Sciences and Social Sciences HOPE Algorithms & Flowcharts Here is an algorithm with selection: do first thing if today is Thursday then begin do this thing end do last thing
Faculty of Sciences and Social Sciences HOPE Algorithms & Flowcharts Here is an algorithm with selection: do first thing if today is Thursday then begin do this thing end do last thing Note the indentation. This is essential if the reader is to see easily what happens if today is Thursday. Without indentation, the algorithm's structure is less clear: do first thing if today is Thursday then begin do this thing end do last thing Note the indentation. This is essential if the reader is to see easily what happens if today is Thursday. Without indentation, the algorithm's structure is less clear: do first thing if today is Thursday then begin do this thing end do last thing
Faculty of Sciences and Social Sciences HOPE Algorithms & Flowcharts do the first thing do this thing do the last thing Start Finish today is Thursday ? True False Again, each step is inside a box. One of them is diamond shaped. In addition, between the if and then keywords in the algorithm is the if condition that can be true or false. In this case whether or not it is Thursday today. Again, each step is inside a box. One of them is diamond shaped. In addition, between the if and then keywords in the algorithm is the if condition that can be true or false. In this case whether or not it is Thursday today. Here is the corresponding flow chart
Faculty of Sciences and Social Sciences HOPE Algorithms & Flowcharts do the first thing do this thing do the last thing Start Finish today is Thursday ? True False In the flow chart, the if condition is represented by the diamond box with two alternative routes out. If the condition is true then the True route is followed, otherwise the False route is followed. In the flow chart, the if condition is represented by the diamond box with two alternative routes out. If the condition is true then the True route is followed, otherwise the False route is followed. Here is the corresponding flow chart
Faculty of Sciences and Social Sciences HOPE selection with an else part Structured English The Flowchart do this thing do the other thing Finish do the last thing False do the first thing Start today is Thursday ? True do first thing if today is Thursday then begin do this thing end else begin do the other thing end do last thing
Faculty of Sciences and Social Sciences HOPE selection with an else part Structured English The Flowchart do this thing do the other thing Finish do the last thing False do the first thing Start today is Thursday ? True do first thing if today is Thursday then begin do this thing end else begin do the other thing end do last thing Calendar Thursday 3 rd Sept
Faculty of Sciences and Social Sciences HOPE selection with an else part Structured English The Flowchart do this thing do the other thing Finish do the last thing False do the first thing Start today is Thursday ? True do first thing if today is Thursday then begin do this thing end else begin do the other thing end do last thing Calendar Thursday 3 rd Sept
Faculty of Sciences and Social Sciences HOPE selection with an else part Structured English The Flowchart do this thing do the other thing Finish do the last thing False do the first thing Start today is Thursday ? True do first thing if today is Thursday then begin do this thing end else begin do the other thing end do last thing Calendar Thursday 3 rd Sept
Faculty of Sciences and Social Sciences HOPE selection with an else part Structured English The Flowchart do this thing do the other thing Finish do the last thing False do the first thing Start today is Thursday ? True do first thing if today is Thursday then begin do this thing end else begin do the other thing end do last thing Calendar Thursday 3 rd Sept
Faculty of Sciences and Social Sciences HOPE selection with an else part Change of day
Faculty of Sciences and Social Sciences HOPE selection with an else part Structured English The Flowchart do this thing do the other thing Finish do the last thing False do the first thing Start today is Thursday ? True do first thing if today is Thursday then begin do this thing end else begin do the other thing end do last thing Calendar Friday 4th Sept
Faculty of Sciences and Social Sciences HOPE selection with an else part Structured English The Flowchart do this thing do the other thing Finish do the last thing False do the first thing Start today is Thursday ? True do first thing if today is Thursday then begin do this thing end else begin do the other thing end do last thing Calendar Friday 4th Sept
Faculty of Sciences and Social Sciences HOPE selection with an else part Structured English The Flowchart do this thing do the other thing Finish do the last thing False do the first thing Start today is Thursday ? True do first thing if today is Thursday then begin do this thing end else begin do the other thing end do last thing Calendar Friday 4th Sept
Faculty of Sciences and Social Sciences HOPE selection with an else part Structured English The Flowchart do this thing do the other thing Finish do the last thing False do the first thing Start today is Thursday ? True do first thing if today is Thursday then begin do this thing end else begin do the other thing end do last thing Calendar Friday 4th Sept
Faculty of Sciences and Social Sciences HOPE algorithm with repetition catch rabbit while rabbit is still alive begin hit with stick end cook the rabbit True catch the rabbit hit with stick cook the rabbit Start Finish rabbit is still alive ? False Structured English The Flowchart
Faculty of Sciences and Social Sciences HOPE algorithm with repetition catch rabbit while rabbit is still alive begin hit with stick end cook the rabbit True catch the rabbit hit with stick cook the rabbit Start Finish rabbit is still alive ? False Structured English The Flowchart
Faculty of Sciences and Social Sciences HOPE algorithm with repetition catch rabbit while rabbit is still alive begin hit with stick end cook the rabbit True catch the rabbit hit with stick cook the rabbit Start Finish rabbit is still alive ? False Structured English The Flowchart
Faculty of Sciences and Social Sciences HOPE algorithm with repetition catch rabbit while rabbit is still alive begin hit with stick end cook the rabbit True catch the rabbit hit with stick cook the rabbit Start Finish rabbit is still alive ? False Structured English The Flowchart
Faculty of Sciences and Social Sciences HOPE algorithm with repetition catch rabbit while rabbit is still alive begin hit with stick end cook the rabbit True catch the rabbit hit with stick cook the rabbit Start Finish rabbit is still alive ? False Structured English The Flowchart
Faculty of Sciences and Social Sciences HOPE algorithm with repetition catch rabbit while rabbit is still alive begin hit with stick end cook the rabbit True catch the rabbit hit with stick cook the rabbit Start Finish rabbit is still alive ? False Structured English The Flowchart
Faculty of Sciences and Social Sciences HOPE algorithm with repetition catch rabbit while rabbit is still alive begin hit with stick end cook the rabbit True catch the rabbit hit with stick cook the rabbit Start Finish rabbit is still alive ? False Structured English The Flowchart
Faculty of Sciences and Social Sciences HOPE algorithm with repetition catch rabbit while rabbit is still alive begin hit with stick end cook the rabbit True catch the rabbit hit with stick cook the rabbit Start Finish rabbit is still alive ? False Structured English The Flowchart
Faculty of Sciences and Social Sciences HOPE algorithm with repetition catch rabbit while rabbit is still alive begin hit with stick end cook the rabbit True catch the rabbit hit with stick cook the rabbit Start Finish rabbit is still alive ? False Structured English The Flowchart
Faculty of Sciences and Social Sciences HOPE repetition that has selection inside the loop open the box of eggs open the fridge door while there are eggs in the box begin take out an egg if egg cracked then begin put egg in mother-in-law's shopping bag end else begin put egg in fridge end close the fridge door throw away the box Spot the 3 constructs !!
Faculty of Sciences and Social Sciences HOPE repetition that has selection inside the loop open the box of eggs open the fridge door while there are eggs in the box begin take out an egg if egg cracked then begin put egg in mother-in-law's shopping bag end else begin put egg in fridge end close the fridge door throw away the box Spot the 3 constructs !! Structured English
Faculty of Sciences and Social Sciences HOPE repetition that has selection inside the loop open the box of eggs open the fridge door while there are eggs in the box begin take out an egg if egg cracked then begin put egg in mother-in-law's shopping bag end else begin put egg in fridge end close the fridge door throw away the box Draw the flow chart
Faculty of Sciences and Social Sciences HOPE put egg in fridge True open the box of eggs take out an egg throw away the box StartFinish there are eggs in box ? False open the fridge door close the fridge door True egg is cracked ? False put egg in mother-in-law's shopping bag
Faculty of Sciences and Social Sciences HOPE Design tools for algorithms We have introduced two problem solving design tools: – Structured English – Flow charts
Faculty of Sciences and Social Sciences HOPE Next? Your first assessment is set in the seminar Eddie Izzard’s printing problem (web)web (flv)flv