Presentation is loading. Please wait.

Presentation is loading. Please wait.

25 November 2014Birkbeck College1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems

Similar presentations


Presentation on theme: "25 November 2014Birkbeck College1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems"— Presentation transcript:

1 25 November 2014Birkbeck College1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Autumn 2014 Week 9a: Algorithm Design

2 Review: Pseudo Code Structures Assignment if … then … else while … do … end while procedure … end procedure 25 November 2014Birkbeck College2

3 Review: Exercise procedure descending(A) j=0; flag=0; while flag==0 && j<Length[A]-1 do if A[j]<A[j+1] then flag=1; print(j); j=j+1; end while if flag==0 then print(-1); end procedure 25 November 2014Birkbeck College3

4 25 November 2014Brookshear, Section 5.34 Bottom Up Strategy Solve many small parts of the problem and gradually accumulate the information needed to solve the entire problem. Examples: evaluation of Boolean expressions, searching an unsorted list.

5 Example: Evaluation of a Boolean expression Suppose that A, B, C are Boolean variables such that A=0, B=1, C=0. Evaluate the Boolean expression (A AND B AND C) OR (A AND NOT(B)) 25 November 2014Birkbeck College5

6 Example: Searching an Unsorted List input: list L and element a output: 1 if a is in L otherwise 0 procedure search (L, a) if Length(L)==0 then print 0 and return; e=L[0]; while ((a<>e) && (e is not last entry of L)) do e=next entry of L; end while; if e==a then print 1 else print 0; return; end procedure 25 November 2014Compare Brookshear Section 5.46

7 25 November 2014Brookshear Section 5.37 Top Down Strategy Divide the problem into two or more simpler problems. Solve the simpler problems and then combine the answers. Examples: route finding, binary search of a sorted list.

8 Binary Search of a Sorted List input: sorted list L, element a output: 1 if a is in L, otherwise 0 procedure searchSortedList(L, a) if Length(L)==0 then print 0 and return; i1=0; i2=Length(L)-1; while i2>i1+1 do j=largest integer ≤ (i1+i2)/2; if L[j]==a then print 1 and return; if a<L[j] then i2=j else i1=j; end while; if a==L[i1] or a==L[i2] then print 1 else print 0; return; end procedure 25 November 2014Compare Brookshear, Section 5.58

9 25 November 2014Birkbeck College9 Other Strategies Find out if somebody else has solved the problem. Solve a simpler but related problem. Iteration: repetition of a standard calculation Find a data structure for the problem suited to a computer. Enumeration.

10 25 November 2014Birkbeck College10 Solve a Simpler but Related Problem Program to draw a polygon in any part of the display screen Program to draw a polygon at the centre of the display screen Program to draw a square in any part of the display screen

11 25 November 2014Birkbeck College11 Example: iteration input: none output: estimate of the square root of 2 procedure sqrt2 x=1; while (x 2 > 2.001) or (x 2 < 1.999 ) do x=(1/x)+(x/2); end while; print x; end procedure;

12 25 November 2014Birkbeck College12 Enumeration List all the possible states of the problem and search for the solution. Example 1: find all numbers which divide 10 by checking 1,2,…10. Advantages: thorough, simple, good for small cases. Disadvantages: very long run times, inefficient.

13 25 November 2014Birkbeck College13 Ferry Problem A man wishes to ferry a wolf, a sheep and a bale of hay across a river. His boat will only hold one item at a time. The wolf cannot be left with the sheep and the sheep cannot be left with the hay. How can he ferry the three items?

14 25 November 2014Birkbeck College14 Data Structure State: WSHb Red: start side of river Blue and underlined italic : other side of river Example: WSHb There are 16 states of which 10 are allowed

15 25 November 2014Birkbeck College15 Solution HSWb

16 25 November 2014Birkbeck College16 Question 11 from 2003 Exam Write an algorithm for calculation of tax for a yearly salary. The tax is zero when the salary is £3000 or less. It is 10% for the next salary band from £3001 to £8000. It is 20% for the part from £8001 to £20000, and it is 40% for any part of the salary over £20000.

17 25 November 2014Birkbeck College17 Question Design an algorithm that lists all possible rearrangements of the symbols in a string of five distinct characters.


Download ppt "25 November 2014Birkbeck College1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems"

Similar presentations


Ads by Google