Algorithm: the Basics

Budi Hartono, ST, MPM Simulation Laboratory, UGM

Process, Instruction & Action

Algorithm = logical sequences

The instruction t0 = state - before the action action t1 = state - after

  

© Budi Hartono, ST, MPM

Example

A

B

A ACTION

B

t0

t1

t0 : A contains yellow, B contains blue 1. Pour the blue water to C 2. Pour the yellow water to B 3. Pour the blue water to A t1 : A contains blue, B contains yellow

Algorithm Exchange_the_contents

© Budi Hartono, ST, MPM

Steps of problem solving using computers
 Problem

Identification  Problem Analysis  Algorithm Design  Computer programming (coding)  Testing (debuging)  Documentation

problem solving phase

implementation phase

© Budi Hartono, ST, MPM

Steps of problem solving using computers (2)
 Problem
 find

Identification

the real needs of customers

 Problem
 Elaborate

Analysis

further problems.  Generate alternative solutions.  Compare and find the optimum strategy.

© Budi Hartono, ST, MPM

Steps of problem solving using computers (3)
 Algorithm

design

Define the solution idea
> example: optimization  linear programming, non-linear programming, forward induction, backward induction, etc > global
 Develop

Algorithm  Validate Algorithm
> represent the solution?
 Analysis

Algorithm
© Budi Hartono, ST, MPM

> efficiency

Good Algorithms …
1. 2. 3. 4. 5.

The procedure is logic and easy to understand The validity is traceable Not ambiguous Easily transferable to computer program Independent to any programming language
© Budi Hartono, ST, MPM

Steps of problem solving using computers (4)
 Coding
 converting  it

/ translating

should:

 integrated  logics  efficiency  flexibility  simplicity

in the usage of time & memory

© Budi Hartono, ST, MPM

Steps of problem solving using computers (5)
 Debugging
 does

it run well ?  syntax error?  logical error?  memory usage error?  etc …
 Documentation
 user

manual  future development © Budi Hartono, ST, MPM

Presenting Algoritms
1.
-

Pseudocode
description

2.

Flowchart
- graphical representation

© Budi Hartono, ST, MPM

Pseudocode
 descriptive  using

similar language  programming

will be discussed later …

© Budi Hartono, ST, MPM

Flowchart
 Graphical

representation of the algorithms  Easy to use; easy to check  Independent to any programming language

© Budi Hartono, ST, MPM

Top-down design
 General
1 10

First, then go details
2 15 3 5 4 17 5 2 Order from the smallest value N=5

- Iterations  N-1 times - Find the smallest - Position exchange

© Budi Hartono, ST, MPM

Pass k = 1
1 10 Pass k = 1 Find the smallest value, from N = 1 to N = 5 1 10 2 15 3 5 4 17 5 2 Result, min = 2 2 15 3 5 4 17 5 2 N=5

Exchange the location 1 2 2 15 3 5 4 17 5 10

© Budi Hartono, ST, MPM

Pass k = 2
1 2 Pass k = 2 Find the smallest value, from N = 2 to N = 5 1 2 2 15 3 5 4 17 5 10 Result, min = 5 2 15 3 5 4 17 5 10 N=5

Exchange the location 1 2 2 5 3 15 4 17 5 10 And so on
© Budi Hartono, ST, MPM

The initial & final states:
 

T0 : random sequence of values

Algorithm Data_Order

T1 : ordered sequence of values

Where:
Algorithm Data_Order

Given N random data. Order them so that data#1 < data #2 < … < data #N Description: For pass k = 1,2, …, N-1 1. Find the smallest value (min) from k = 1 to k = N 2. Exchange min with data#k
© Budi Hartono, ST, MPM

START

k=1

k = k+1

1. Find min

2. Position min @ k

K=4 ? YES
© Budi Hartono, ST, MPM STOP

1. Find min
• • Not detail enough Must be refined

Refining Step-1 1.1 Assume data #k is the min (temporary) 1.2 compare min with data #j; where j =(k+1), (k+2), .. , N if data #j < min, then data #j becomes the new min Refining Step-1.2 1.2.1 For j = (k+1), (k+2), …, N do: if data #j < min then data #j becomes the new min

© Budi Hartono, ST, MPM

2. Position min @ k Refining: 2.1 put data #k to a temporary place (temp) 2.2 put the min on the place of the data #k (formerly) 2.3 put the temp on the min (formerly)

© Budi Hartono, ST, MPM

The final algorithm
Algorithm Data_Order
Given N numbers of random data. Make an order of data so that: data#1 < data #2 < … < data #N  Description:

For pass k = 1,2, …, N-1, do the following: {1. Find the smallest value (min) from k = 1 to k = N} 1.1 assume that data #k is the min 1.2 For j = (k+1), (k+2), …, N do: if data #j < min then min  data#j {data #j becomes the new min} {2. Exchange min with data#k } 2.1 put data #k to a temporary place (temp)

2.2 put the min on the place of the data #k (formerly) 2.3 put the temp on the min (formerly)

© Budi Hartono, ST, MPM

I leave the flowchart for the next week’s homework

© Budi Hartono, ST, MPM

Basic Structures of Algorithms
 Sequence  Selection  Repetition

© Budi Hartono, ST, MPM

Basic Structures of Algorithms

Sequence
THE RESULT ?

A1

A1

A2

A3

A3

A2

A

B

A4

A4

© Budi Hartono, ST, MPM

C

Basic Structures of Algorithms

Selection

 Conditions  Recall:

if data #j < min then data #j = new min  The general structure
If condition then action

© Budi Hartono, ST, MPM

Basic Structures of Algorithms

Selection

 Example  If

Amir wins the competition then daddy will give him a new car  If Malioboro gets a traffic jam then go to Jl. Ahmad Dahlan
 Your

example …
© Budi Hartono, ST, MPM

Basic Structures of Algorithms

Selection

 Another

structure If condition then
action 1

else action 2  indenting
 Your

example …
© Budi Hartono, ST, MPM

Basic Structures of Algorithms

Repetition

 An

advantage of using compie:

 Do

repetitive jobs without getting bored  Precision
I

promise that I will never forget to do my homework 100 times !!!

© Budi Hartono, ST, MPM

Basic Structures of Algorithms

Repetition

Algorithm Writing_500_sentences Description
4. 5. 6. 7. 8.

Write “I promise that I will never forget to do my homework” Write “I promise that I will never forget to do my homework” Write “I promise that I will never forget to do my homework” Write “I promise that I will never forget to do my homework” Write “I promise that I will never forget to do my homework” Write “I promise that I will never forget to do my homework”

… 500

???
© Budi Hartono, ST, MPM

Basic Structures of Algorithms

Repetition

Algorithm Writing_500_sentences Description: sentenceNumber = 0 repeat write “I promise that I will never forget to do my homework”
sentenceNumber = sentenceNumber +1

until sentenceNumber = 500
© Budi Hartono, ST, MPM

Basic Structures of Algorithms

Repetition

 General

Structure

Repeat action Until condition

© Budi Hartono, ST, MPM

A combination
 Find

a NIM of a student from 500 entries (random) .

© Budi Hartono, ST, MPM

Sign up to vote on this title
UsefulNot useful