1 / 17

Introduction to Computer Systems

Introduction to Computer Systems. Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Autumn 2013 Week 9a: Algorithm Design. Review: Pseudo Code Structures. Assignment if … then … else while … do … end while

chi
Download Presentation

Introduction to Computer Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Review: Pseudo Code Structures • Assignment • if … then … else • while … do … end while • procedure … end procedure Birkbeck College

  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 Birkbeck College

  4. 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 unordered list. Brookshear, Section 5.3

  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)) Birkbeck College

  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 goto end procedure; 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; end procedure Compare Brookshear Section 5.4

  7. 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. Brookshear Section 5.3

  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 goto end procedure; 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 goto end procedure; 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; end procedure Compare Brookshear, Section 5.5

  9. 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. Birkbeck College

  10. 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 Birkbeck College

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

  12. 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. Birkbeck College

  13. Ferry Problem • A man wishes to ferry a wolf, a sheep and a bale of hay across as 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? Birkbeck College

  14. 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 Birkbeck College

  15. Solution HSWb HS Wb HS Wb HSWb H S Wb H SWb HSW b H SW b H SWb HSWb Birkbeck College

  16. 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. Birkbeck College

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

More Related