creating and implementing algorithms tallinn 2015 vladimir viies, lembit jürimägi, margit aarna...

31
Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna [email protected]

Upload: aron-lewis

Post on 17-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

Creating and implementing algorithms

Tallinn 2015

Vladimir Viies, Lembit Jürimägi, Margit Aarna [email protected]

Page 2: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

Knowledge and skills

logical thinking sub-tasks algorithm writing the code

Page 3: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

POINTS

practice homework tests bonus

Page 4: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

ALGORITHM CREATING 1

What is an algorithm?

Why use it?

How to use it?

Page 5: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

but not just any instruction, instructions with certain properties.

WHAT IS AN ALGORITHM WHEN SOLVING TASKS WITH COMPUTERS?

Instructions for solving the task.

Page 6: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

Examples:1. Task: Find the largest number from {N} numbers

Instructions that aren’t an algorithm: Compare the numbers and choose the largest number

Instructions that can be an algorithm :

1. Read the numbers (remember, write down etc.) – enter numbers.

2. Take the first number and mark it.

3. Compare marked number with next number(s) until a larger number is found or it has been compared with every number.

4. If during the comparison a larger number is found then mark it and repeat step 3.

Page 7: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

Examples

2. Task: win 1 million in a lottery game

Instructions that aren’t an algorithm: buy a ticket until you win.

Instructions that can be an algorithm: We have to specify more details. We need to explain, what “into smaller” means, which banknotes and how many we can use. If the task is vague then we need to put restrictions into place ourselves and consider these restrictions when creating an algorithm.

Instructions that can be an algorithm: there is none.

3. Task : exchange 100 euro bank note into smaller bank notes

Page 8: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

THE ALGORITHM PROPERTIES

• Must specify inputs and outputs which are interrelated

• It should provide a solution in finite time• Must be unequivocal

......

ALGORITHM CREATING 2

Page 9: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

Algorithm graphical presentation tools 1

(flowchart)

ALGORITHM VISUALISATION

ALGORITHM CREATING 3

Page 10: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

Algorithm graphical presentation tools 1

There are two types of algorithms editors:

I permit only drawing the algorithm ( eg. MS Excel, UML activity…);

II additionally generate basic program code in the selected language (eg. SFC, JSP….).

Page 11: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

Algorithm with such a layout always has one start and one end

All activities will take place in sequence!

Each kind of activity has its own notation.

This diagram shows the I / O, conditional and operation

activities.

SFC elements of the schemes(1)

Page 12: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

Task: find the total count of even and odd numbers in a row of numbers.. Algorithm (for writing program

code):

1. Insert data

2. Assign initial values to the counters

3. Let’s start comparing the numbers in a loop and update the counters

4. Print out the results

Page 13: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

Algorithm realization, the language choice

Mis on algoritm?

Page 14: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

// Program: erinevad arvud

// Author: viies // Course: iag0581

void main ()

{

mitu arvu sisestan;

loen mitu arvu - N;

paaris ja paaritu=0;

for (i = 1; i <= N; i = i + 1)

{

loen arvu a(i);

if (kas a(i) on paarisarv(jagub 2-ga))

{

paaris=paaris+1;

}

else

paaritu=paaritu+1;

}

väljasta paaris, paaritu;

}

Editor SFC generated

immediately the code in C

language based on the

description of the algorithm

SFC runs!

Page 15: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

ALGORITHM CREATING 4

Algorithm graphical presentation tools 2

(Jackson editor)

Page 16: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

Jackson algorithm Elements Editor

ACTIVITIES

SELECTION (branched) O in right corner

REPEAT (iteration)* in right corner

An algorithm moves from top down and from left to right

Page 17: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

Structural algorithm

The algorithm can be created as a complete solution, or consisting of sub-tasks. In the latter case, the taskneeds to be divided.

Each task can be divided into at least three parts:

DATA

INPUT

DATA

PROCESSING

RESULTS

OUTPUT

Structural approach is well suited to Jackson editor

Page 18: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

ALGORITHM CREATING 5

Algorithm graphical presentation tools 4

(Using the UML activity diagram)

Page 19: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

Algorithm using the UML (ArgoUML) :

Task: find the sum of positive numbers .

Page 20: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

ALGORITHM CREATING 6

Finding extremes, and "bubblesort" method

Page 21: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

.

Extremes and sorting

One frequent task when processing data is sorting and finding the maximum and minimum values. Such

tasks always include two activities:

Comparing the two values

“Exchanging places” of two values

The first includes one activity, but the second three activities.

Page 22: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

A simple sorting algorithm, can also be used successfully to find extremes

< MAXIMUM

Analogously can be found MINIMUM

Page 23: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

ALGORITHM CREATING 7

Shell sort algorithm

Page 24: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

Shell sort algorithm (1)(has less exchanges, presumes that data is partially sorted)

Page 25: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

Maatriks1( a’la tabel,kus lahtrid nummerdatud)

A11 A12 A13 A14

A21 A22 A23 A24

A31 A32 A33 A34

A41 A42 A43 A44.

{AIJ }-igal elemendil kaks indeksit, millega on määratud tema asukoht.

Page 26: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

Maatriks2( a’la tabel,kus lahtrid nummerdatud)

A11 A12 A13 A14

A21 A22 A23 A24

A31 A32 A33 A34

A41 A42 A43 A44.

{AIJ }-igal elemendil kaks indeksit, millega on määratud tema asukoht.Indeksite väärused võivad muutuda kas 1-st või 0-st(olenevalt keelest, kus neid kasutatakse) veergude/ridade arvuni või 0-korral ühe võrra vähem.

Maatriksi elementide eristamine, teatud üldistuste alusel, on enamlevinud loogika ülesandeid programmeerimises kasutuskõlblike algoritmide loomise õppimisel.

Page 27: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

Maatriks 3 ( a’la tabel,kus lahtrid nummerdatud)

A11 A12 A13 A14

A21 A22 A23 A24

A31 A32 A33 A34

A41 A42 A43 A44.

{AIJ }-igal elemendil kaks indeksit, millega on määratud tema asukoht.

Ruutmaatriksil(ridade arv=veergudearv) eristatakse:*peadiagonaali ,sinna kuuluvad elemendid, kus i=j ;*kõrvaldiagonaali ,sinna kuuluvad elemendid, kus i+j=const ;On vaja osata üldistades leida need elemendid, mis asuvad diagonaalide peal ja all!

Peadiagonali kohal I<J; all J<I;

Page 28: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

Output a matrix row based on maximum element

Output matrix row, on the basis of max element

Matrix input A(i,j) i,j=1..N

Max A(i,J) finding, max(i)

Row separation on the base of

max(i)

We separate the task into sub-tasks

and solve them later!

Page 29: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

K o k k u v õ t t e k s :

1 . A l g o r i t m i k o o s t a m i n e v õ i m a l d a be r a l d a d a ü l e s a n d e s i s u l i s e l a h e n d a m i s e p r o g r a m m i k o o d i k i r j u t a m i s e s t j a s e e g a m u u d a b l a h e n d u s e s õ l t u m a t u k s k e e l e s t .

2 . V õ i m a l d a b e r i t a s a n d i t e l r ü h m a t ö ö d .

3 . M u u d a b l a h e n d u s e j ä l g i t a v u s t , l i h t s u s t a b

a l g o r i t m i k o n t r o l l i m i s t j a t õ s t a b t ö ö k i n d l u s t .

What is an algorithm?

Why use it?

How to use it?

Page 30: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

K o k k u v õ t t e k s :

1 . A l g o r i t m i k o o s t a m i n e v õ i m a l d a be r a l d a d a ü l e s a n d e s i s u l i s e l a h e n d a m i s e p r o g r a m m i k o o d i k i r j u t a m i s e s t j a s e e g a m u u d a b l a h e n d u s e s õ l t u m a t u k s k e e l e s t .

2 . V õ i m a l d a b e r i t a s a n d i t e l r ü h m a t ö ö d .

3 . M u u d a b l a h e n d u s e j ä l g i t a v u s t , l i h t s u s t a b

a l g o r i t m i k o n t r o l l i m i s t j a t õ s t a b t ö ö k i n d l u s t .

In conclusion:

1. Algorithm creating enables separating the contents from the program code, thereby making the solution independent from writing and language.

2. Allows for different levels of group work.

3. Changes traceability of the solution,simplifies verification and increases the

reliability.

Page 31: Creating and implementing algorithms Tallinn 2015 Vladimir Viies, Lembit Jürimägi, Margit Aarna viis@ati.ttu.ee

HOMEPAGE:

http://www.tud.ttu.ee/im/Vladimir.Viies/