1 / 12

Algorithm Design

Algorithm Design. CS105. Problem Solving. Algorithm: set of unambiguous instructions to solve a problem Breaking down a problem into a set of sub-problems Example: Clean the house Without instructions – computers cannot do anything at all!. Algorithm design. Analysis and specification

higginsm
Download Presentation

Algorithm Design

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. Algorithm Design CS105

  2. Problem Solving • Algorithm: set of unambiguous instructions to solve a problem • Breaking down a problem into a set of sub-problems • Example: Clean the house • Without instructions – computers cannot do anything at all!

  3. Algorithm design • Analysis and specification • Analyze: Understand/define the problem • Specify: Specify particulars • Algorithm development phase • Develop: Logical sequence of steps • Test: Follow outline, test cases • Implementation phase • Code: The steps into a programming language • Test: Debug • Maintenance phase • Use the program • Maintain: Correct errors, meet changing requirements

  4. An example: Making a perfect piece of toast What do we need: a loaf of bread, knife, toaster, plate, butter, cutting board Version 1 • Move a loaf of breadon a cutting board • Cut a slice of bread with a knife • Move the slice of bread to the toaster • Turn toaster on • Wait for the toaster to finish • Move the toasted bread on a plate • Spread butter on the toast with knife Variables Output

  5. An example: Making a perfect piece of toast Variables: A= loaf of bread K= knife T= toaster P= plate B= butter CB= cutting board Version 2 1. moveA to CB 2. cut S with K 3. move S to T 4. turn onT 5. wait forT 6. moveStoP 7. spreadBonS with K move move S= slice of bread move

  6. An example: Making toast for fussy friends: plain, butter, margarine Variables: A= loaf of bread K= knife T= toaster P= plate B= butter M= margarine F= friend CB= cutting board Version 3 Move A to CB FOR every F eating toast { Cut S with K Move S to T Turn on T Wait for T Move S to P IFF likes B { X=B } ELSE IFF likes M { X=M } ELSE { X= NOTHING } Spread X on S with K } S= slice of bread X

  7. Basic concepts • Example contains concepts used in most algorithms • Instructions– simple and unambiguous • Variables– input and temporary • Subprocedures – smaller tasks • Looping: FOR each variable, WHILE • Act of repeating tasks • Conditional statements: IF ELSE • Selectively execute instructions

  8. Pseudocode: Flowchart • Flowchart: diagram that represents an algorithm • Symbols: START , END LOOPS, FLOW OF CONTROL INSTRUCTIONS CONDITIONALS

  9. Pseudocode: Flowchart Fixing non functioning lamp algorithm

  10. Flowchart: Making toast for friends Done? Start End Move A to CB Yes Variables: A= loaf of bread K= knife T= toaster P= plate B= butter M= margarine F= friend CB= cutting board No Cut S with K Move S to T Turn on T Wait for T Spread X on S with K Move S to P Want B? Want M? S= slice of bread X No No Yes Set X to B Set X to NONE Yes Set X to M

  11. Start Add 30 to X Is X > 100? Subtract 15 from X Yes No Multiply X by 3 End Is X < 80? Yes Print X Yes No Is X an integer? No Round it to the nearest integer Divide X by 2

  12. Add 30 to X • IF X >100, subtract 15 • ELSE multiply X by 3 • IF X<80, PRINT X END • ELSE Divide by 2 • IF X is an integer, PRINT X END • ELSE round X, then PRINT X END Add 30 to X IF ( x>100) { Subtract 15 from X } ELSE { Multiply X by 3 } IF ( x<80) { PRINT X } ELSE { Divide X by 2 IF ( X is an integer) { PRINT X } ELSE { Round X to the nearest integer PRINT X } }

More Related