Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes.

Similar presentations


Presentation on theme: "Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes."— Presentation transcript:

1 Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes also go by the name method, procedure, or technique. One of the simplest algorithms is finding the largest number in an unsorted list of numbers.

2 Expressing algorithms Algorithms can be expressed in many kinds of notation, including:  Natural languages  Pseudocode  Flowcharts  Box diagrams  Programming languages

3 Natural language expressions Natural language expressions of algorithms tend to be verbose and ambiguous, and rarely used for complex or technical algorithms.

4 Pseudocode Pseudocode is a compact and informal high- level description of a computer programming algorithm that uses the structural conventions of a programming language. Pseudocode typically omits details that are not essential for human understanding of the algorithm, such as variable declarations.

5 Pseudocode Textbooks and specific publications related to computer science and numerical computation often use pseudocode in description of algorithms, so that all programmers can understand them, even if they do not all know the same programming languages.

6 Syntax of pseudocode Pseudocode generally does not actually obey the syntax rules of any particular languages. There is no systematic standard form, although any particular writer will generally borrow style and syntax from some conventional programming language, such as BASIC. Variable declarations are typically omited.

7 Syntax of pseudocode = if do stuff else do other stuff while do stuff

8 Syntax of pseudocode for = to step do stuff with variable function ( ) do stuff with arguments return something ( ) ' function call

9 Flowcharts Flowchart is a common type of chart that represents an algorithm or process, showing the steps as boxes of various kinds, and their order by connecting these with arrows. The first structured method for documenting process flow, “flow process chart”, was introduced by Frank Gilbreth in 1921.

10 Flowcharts Herman Goldstine and John von Neumann developed the flow chart to plan computer programs in Flowcharts used to be a popular means for describing computer algorithms and are still used for this purpose.

11 Types of flowcharts Sterneckert (2003) divides four more general types of flowcharts:  Document flowcharts, showing a document flow through system.  Data flowcharts, showing data flows in a system.  System flowcharts, showing controls at a physical or resource level.  Program flowcharts, showing the controls in a program within a system.

12 Symbols of program flowcharts Flow: line with an arrow Terminal: round rectangle Start of subroutine STARTENDRETURN x FUNCTION sum(x, y)

13 Symbols of program flowcharts Preparation: hexagon Input/Output: parallelogram Process: rectangle INPUT xPRINT x x = x + 1 x = 1

14 Symbols of program flowcharts Decision: diamond Connector: circle Off-page connector x = 1 yes no 1 2

15 Box diagrams Box diagram is a graphical design representation for structured programming. Another name is Nassi-Shneiderman diagram, because it is developed by Isaac Nassi and Ben Shneiderman in Box diagram is also called structogram, as it shows a program structure.

16 Box diagrams vs flowcharts Everything you can represent with a box diagram you can also represent with a flowchart. For flowchart of programs, just about everything you can represent with a flowchart you can represent with a box diagram. The exceptions are things like goto, break, and continue statements.

17 Blocks in a box diagram Process blocks Branching blocks ACTION CONDITION truefalse

18 Blocks in a box diagram Top tested loops Bottom tested loops ACTION LOOP CONDITION ACTION

19 Examples of problems (1) Calculation of electricity costs  Known: number of meter digits = 5 metric: KWh for first 20 KWh = 385 rupiah / KWh for next 40 KWh = 445 rupiah / KWh for the rest = 495 rupiah / KWh  Input: old and new meter digits  Output: a bill that contains a number of KWh used and total cost

20 Examples of problems (2) Calculation of roots of a quadratic equation  Input: coefficients of a quadratic equation  Output: roots  Formula: D = b^2 – 4 a c if D > 0 then roots = two real numbers if D = 0 then roots = a real number if D < 0 then roots = two imaginary numbers roots = (- b +/- √ D) / (2 a)

21 Examples of problems (3) Calculation of factorial of a number  Input: n  Output: n!  Formula: n! = n x (n – 1) x (n – 2) x … for n > 0 0! = 1  Examples: 0! = 1, 1! = 1, 2! = 2, 3! = 6, 4! = 24

22 Examples of problems (4) Calculation of n-th fibonacci number  Input: n  Output: n-th fibonacci number  Formula: F(n) = F(n – 1) + F(n – 2) for n > 2 F(1) = F(2) = 1  Fibonacci series: 1, 1, 2, 3, 5, 8, 13, 21, 34,...

23 Examples of problems (5) Calculation of sine of a number using Maclaurin series  Input: x  Output: sin(x)  Formula: x^3 x^5 x^7 x^9 sin(x) = x … 3! 5! 7! 9! number of iterations used is 10

24 Examples of problems (6) Calculation of a real root of a cubic equation using Newton-Raphson method  Input: coefficients of a cubic equation  Output: a real root  Formula: x1 = x0 – f(x0) / f'(x0) iterates until a relative error < a threshold relative error = | (x1 – x0) / x1 |

25 Examples of problems (7) Finding minimum and maximum values of an unordered vector  Input: n and n-length of unordered vector  Output: minimum and maximum values  Example: (n = 10) Minimum value = 0 Maximum value = 7

26 Examples of problems (8) Displaying a Pascal's triangle  Input: n  Output: a Pascal's triangle of height-n Tip: use a 2D-array for n =

27 Examples of problems (9) Displaying a magic square using Siamese method (1688)  Input: n  Output: a magic square of order-n (n = odd) for n =


Download ppt "Algorithms An algorithm is a finite sequence of instructions, logic, an explicit step-by-step procedure for solving a problem. Specific algorithms sometimes."

Similar presentations


Ads by Google