Download presentation
Presentation is loading. Please wait.
1
Algorithms Prof. Dr. Péter Iványi
2
One of the oldest algorithms
bc. IV century, Alexandria, Euclides Largest common denominator of two integers Lets assume a and b positive integer number and the largest common denominator is ( a , b )
3
Largest common denominator
If a = b * q + r , then ( a , b ) = ( b , r ) , thus the problem is reduced to a problem with smaller numbers. If we continue the procedure, then the last remainder that is different from zero is the largest common denominator
4
Largest common denominator
( 360, 225 ) = ? According to the Eucledian algorithm: a b r 360 = 225* 225 = 135*1 + 90 135 = 90*1 + 45 90 = 45*2 + 0 Therefore ( 360, 225 ) = 45.
5
Programming Two basic concepts:
Description of relations between quantities and information x+y Evaluation of these relations, where values are substituted into the expression x = 2 y = 3 2+3 = 5
6
Operations We can specify simple operations on every data For example:
Numbers: addition, subtraction, multiplication, ... Characters: Concatenate, sort,... A programmer creates a program such simple operations
7
Information Information is describd as data
There are different types of data: Simple, (atomic, not divisible), like numbers Complex, like number series Although data represents the information, but the interpretation is up to us For example can represent temperature time distance
8
Programming, practical approach
To divide up the problem to simple operations (steps) that a computer can understand The program: It is a sequence of operations that can be understood by a computer and that solves a problem.
9
Elements of a program Input Output Operations, instructions
(variables) Input Operations Output
10
Input and output Input Output Keyboard Mouse File Serial port, etc
Screen
11
Variables We store data in variables Name Value Assigning
Denotes a storage place Value The stored data Assigning We put a data into a storage place
12
Data, simple What kind of data can we store? Numbers (bináris szám)
Integer Real Complex, etb Characters Binary number is converted to character The same data can be stored in different ways 1, ‘1’, “1”
13
Derived, complex data Character series, string, Vectors Matrices Lists
Etc. Depends on the programming language!
14
Operations Manipulating data Arithmetic: Relational: Logical
+, -, *, / Relational: >, <, =, <=, >= Logical NOT, AND, OR
15
Order of operations a=6+12*5 Evaluation order,
How to, in what order to execute the operations? Generally from left to right Precedence Priority of operations Brackets a=(6+12)*5 a=6+(12*5)
16
Program, summarised Program, consists of several programs
Properties of algorithms: Can be executed (consits of simple steps) Every step is well defined After a finite number of steps it stops It is valid for a defined set of input It produces the appropriate output It solves a specific problem
17
Algorithms, design of programs
18
Programming methodology
Until 1960 monolithic programming Properties: One programmer – one program There is no structure in the program
19
Programming methodology
The most important criteria of a good program It has a visibly good structure It is well documented It can be „proven” that it does what it should
20
Modular programming The basis of design: decomposition
The complicated problem is divided in pieces We create a solution for the pieces We assemble the pieces
21
Guidelines Divide and conquer Divide the problem to modules
Modules do not „get into” each other’s data Modules operate independently Easier to detec mistakes Easier to overview the task Easier to modify
22
Guidelines Hiding data Delaying decisions
Modules work only with their own data Delaying decisions Decision is only made when it is necessary If there is not enough information, delay the decision An early decision may have to be modified later Once a decision has been made, declare it Write it down, or record it So later no contradictory decision is made
23
Modular programming Top-Down decomposition Bottom-Up composition
The task is divided into subtasks, which are also divided into further subtasks, until their size is small enough that they can be handler. Bottom-Up composition We use already existing building blocks We do not know how they will connect together, which will connect to which other one These building blocks must already exist
24
Modular programming Advantages: Subprograms can be easily overviewed
Easy to create them Easy to test them Several modules can be developed at the same time (parallel development) Easy to correct mistakes Modules can be standardised It is possible to create module libraries Reusable modules
25
Structured programming
Go To Statement Considered Harmful. DOI: / Dijkstra: Based on the Top-Down dekomposition The original problem is divided into subproblems. This is an abstract program which works on an abstract computer. It can be proven that it works according to specification. Refinement, which reduces the abstraction, e.g. a subproblem is described in more detail, which runs on another abstract machine. Still possible to prove that is satisfies the specification. Further refinement, until we reach the level of a real computer, with its instructions. 2015 Szept 23
26
Structured programming
Outcome: The resulting is provably correct. Problem: The required amount of work is large. Solution: We miss certain steps, and therefore an approximately correct program is created, which can be checked by tests
27
Boehm, Jackopini 1964 Every algorithm can be described by three control structure Selection Iteration Sequence
28
Mills 1968 Proved the theory of Boehm and Jackopini with certain conditions: The basic structure of a program is a sequence Every sequence has one entry and one exit point. An exit point of a module is an entry point of another module. Every sequence can structured internally as required.
29
Graphic representation of algorithms
Pseudo code Almost like sentences Flowchart Structogram Nassi–Shneiderman diagram Starting point is one rectangle, which can be divided. ...
30
Flowchart, basic elements
Start: Input data Output data Action, statement
31
Flowchart, basic elements
Conditional action, branching Label End:
32
Structogram Module name, beginning or algorithm Input data Output data
33
Structogram Action, statement Conditional action, statement, branching
Iteration
34
Program execution The computer executes one step after another one.
Control structure: deviation from the sequential order
35
Control structure Jump Conditional execution
Multiple conditional execution Count controlled cycle, iteration, loop Pre-testing cycle, iteration, loop Post-testing cycle, iteration, loop (Subroutines)
36
Jump The program continues at a new given point, not at the next step.
Usually results in structure that is difficult to understand Not used in this semester!!!
37
Subprogram Repeated task Subtasks to separate Names: Subroutine
Procedure Function
38
Control structure, flowchart
Sequence Selection with else Selection
39
Control structure, flowchart
Multiple selection
40
Control structure, flowchart
Pretesting iteration Posttesting iteration
41
Control structure, flowchart
Count controlled iteration
42
Control structure, structogram
Sequence Selection, branching Selection with else
43
Control structure, structogram
Multi branching Pretesting iteration Posttesting iteration Count controlled iteration
44
Pseudo code Function Something BEGIN actions END IF cond THEN action1 ELSE action2 END IF
45
Pseudo code WHILE cond DO actions END WHILE DO WHILE cond
46
Pseudo code FOR var=a…b DO actions END FOR
47
Sequence
48
Selection, branching
49
Selection, branching
50
Selection, branching
51
Pre-testing iteration
We do pushup as long as we can.
52
Post-testing iteration
The previous example.
53
Count controlled iteration
Lets do 10 push ups.
54
Making tea
55
The problem A farmer (Joe) would like to buy cattle, goat, chicken, in total 100 pieces of animal for 100 dollar. The price of 1 cattle is 3,5 dollar, the price of 1 goat is 1,33 dollar and the price of 1 chicken is 0,5 dollar. How many animals can Joe buy?
56
Variables and conditions
s : number of cattles k : number of goats j : number of chicken Conditions: 1 <= s <= 100 1 <= k <= 100 1 <= j <= 100 Number: s + k + j = 100 Price: 7/2*s + 4/3*k + 1/2*j = 100
57
1. Algorithm
58
1. Algorithm, analysis Number of executions
Can we create a better algorithm? If Joe buys only one type of animal, then: cattle: 7/2 * s = 100 thus ‘s’ can be: 28 goat: 4/3 * k = 100 thus k = 75 chicken: 1/2 * j = 100 thus j = 200 But Joe can buy only 100 animals, so j = 100
59
2. Algorithm
60
2. Algorithm, analysis 28 * 75 * 100 = 210 000
Can we create a better algorithm? If we already know the number of animals for two types of animal, then the third can be calculated j = 100 – s – k Consequence: The internal iteration can be removed The value of j can be determined, however j cannot be negative, therefore the condition will change a little bit
61
3. Algorithm
62
3. Algorithm, analysis 28 * 75 = 2100 O(n2)
Can we create a better algorithm? If j = 100 – s – k then substitute this into 7/2*s + 4/3*k + 1/2*j = 100 we get a new result k = /5 * s Condition will also change: ‘k’ must be positive, integer
63
4. Algorithm
64
4. Algorithm, analysis 28 steps to be execute!!! O(n) Can we create a better algorithm? We know that ‘k’ must be positive and integer, which is only possible if ‘s’ is divisible by 5: s = 5, 10, 15, 20, 25 However if: s = 25 then k = -30 AND s = 20 then k = -12 Therefore these number are not good for our purpose
65
5. Algorithm The problem can be solved in three steps O(1)
66
Programs S T R U C T O R I Z E R: Flowgorithm:
Flowgorithm:
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.