programming fundamentals 9 th lecture szabolcs papp
TRANSCRIPT
ELTEELTE
22/31/3123.04.20.23.04.20.
More Patterns of Algorithms (PoA)
Copy – calculation with function Multiple item selection Partitioning Intersection Union
PoAs – summary
Content
ELTEELTE
33/31/3123.04.20.23.04.20.
Further PoAs
What is Pattern of Algorithm (PoA)?It is a general solution for a typical programming task.
Array single valueArray ArrayArray ArraysArrays Array
ELTEELTE
44/31/3123.04.20.23.04.20.
7. Copy calculation with
function
Example tasks: Let's define the absolute values of
all elements in an array! Let's convert all letters of a text
to lowercase! Let's calculate the sum of two
vectors! Let's calculate sin(x) values for a
given x series! We know the ordinal number of N
months, let's give their names!
ELTEELTE
55/31/3123.04.20.23.04.20.
7. Copy calculation with
function
What is in common? We have N something, and we have to assign N other things to these. The type of assigned values can differ from the type of original values, but the count (N) remains the same, as well as the order.
ELTEELTE
66/31/3123.04.20.23.04.20.
7. Copy calculation with
function
Specification: Input: N:Integer,
X:Array[1..N:Type1]
Output: Y:Array[1..N:Type2] Precondition: N0 Post condition: i (1≤i≤N):
Y[i]=f(X[i])
Algorithm:i=1..N
Y[i]:=f(X[i])
ELTEELTE
77/31/3123.04.20.23.04.20.
7. Copy calculation with
function
Specification (a frequent special case): Input: N:Integer,
X:Array[1..N:Type1]
Output: Y:Array[1..N:Type2] Precondition: N0 Post cond.: i (1≤i≤N):
Algorithm:i=1..N
T(X[i])
Y[i]:=f(X[i])
Y[i]:=X[i]
otherwise, iX
iX Tif, iXfY[i]
Y N
ELTEELTE
88/31/3123.04.20.23.04.20.
7. Copy calculation with
function
Specification (other special case): Input: N:Integer,
X,Z:Array[1..N:Real] Output: Y:Array[1..N:Real] Precondition: N0 Post condition: i(1≤i≤N): Y[i]=X[i]
+Z[i]Algorithm:
i=1..N
Y[i]:=X[i]+Z[i]
ELTEELTE
99/31/3123.04.20.23.04.20.
8. Multiple item selection
Example tasks: Let's define all excellent students
in a class! Let's calculate the divisors of an
integer! Let's list all the vowels from an
English word! Let's garble all the people who are
taller than 180cm, from a set of people!
Let's list those days of the year when it didn't freeze!
ELTEELTE
1010/31/3123.04.20.23.04.20.
8. Multiple item selection
What is in common?We have to list all elements from N "something", which have a common attribute T.
ELTEELTE
1111/31/3123.04.20.23.04.20.
8. Multiple item selection
Specification: Input: N:Integer,
X:Array[1..N:Type] Output: Cnt:Integer,
Y:Array[1..N:Integer] Precondition: N0 Post cond.: Cnt= and
i(1≤i≤Cnt): T(X[Y[i]])
and Y(1,2,…,N)
N
])i[X(T1i
1See PoA "Count"
In case of static In case of static array array
declarationdeclaration
ELTEELTE
1212/31/3123.04.20.23.04.20.
8. Multiple item selection
Algorithm:
Comment:Collecting ordinal numbers is more general than collecting elements. If we needed elements then we would have: Y[Cnt]:=X[i]. (And accordingly modified specification, too.)
Cnt:=0
i=1..N
T(X[i])
Cnt:=Cnt+1 Y[Cnt]:=i
Y N
See PoA "Count"
ELTEELTE
1313/31/3123.04.20.23.04.20.
8. Multiple item selection (not freezing days)
Specification: Input: N:Integer,
H:Array[1..N:Real] Output:
Cnt:Integer,NF:Array[1..N:Integer] Precondition: N0 Post cond.: Cnt= and
i(1≤i≤Cnt): H[NF[i]]>0 and
NF(1,2,…,N)
N
0H[i]1i
1
ELTEELTE
1414/31/3123.04.20.23.04.20.
8. Multiple item selection (not freezing days)
Algorithm:
Cnt:=0
i=1..N
H[i]>0
Cnt:=Cnt+1
NF[Cnt]:=i
Y N
ELTEELTE
1515/31/3123.04.20.23.04.20.
10. Partitioning
Example tasks: Let's define all excellent and non-
excellent students in a class! Let's garble all the people who are
taller than 180cm, and others who are not taller than 180cm, from a set of people!
Let's list all even and all odd numbers from a series!
Let's list those days of the year when it did freeze and didn't freeze!
Let's list all the vowels and consonants from an English word!
ELTEELTE
1616/31/3123.04.20.23.04.20.
10. Partitioning
What is in common? We have to list all elements from N "something", which have a common attribute T, and then also list those ones not having attribute T.
ELTEELTE
1717/31/3123.04.20.23.04.20.
10. Partitioning
Specification: Input: N:Integer,
X:Array[1..N:Type] Output: Cnt:Integer,
Y,Z:Array[1..N:Integer] Precondition: N0 Post cond.: Cnt= and
i(1≤i≤Cnt): T(X[Y[i]]) and i(1≤i≤N–Cnt): not T(X[Z[i]])
and Y(1,2,…,N) and Z(1,2,…,N)
N
])i[X(T1i
1
ELTEELTE
1818/31/3123.04.20.23.04.20.
10. Partitioning
Algorithm:
Comment:If we needed the partitioned elements, than here we could also have:=X[i] instead of :=i (And accordingly modified specification!)CntZ can be eliminated. How?
Cnt:=0
CntZ:=0
i=1..N
T(X[i])
Cnt:=Cnt+1
CntZ:=CntZ+1
Y[Cnt]:=I Z[CntZ]:=i
Y N
ELTEELTE
1919/31/3123.04.20.23.04.20.
10. Partitioning
Permutation(1,2,...,N):=set of all permutations of
1..N (set of sets)
Modification:There are exactly N number of elements in Y and Z together, so it's sufficient to have only one array. We will collect the T attributed elements at the beginning of the resulting array Y.
Solution: Input: N:Integer, X:Array[1..N:Type] Output: Cnt:Integer, Y:Array[1..N:Integer] Precondition: N0 Post cond.: Cnt= and
i(1≤i≤Cnt): T(X[Y[i]]) and i(Cnt+1≤i≤N): not T(X[Y[i]])
and YPermutation(1,2,…,N)
N
])i[X(T1i
1
ELTEELTE
2020/31/3123.04.20.23.04.20.
10. Partitioning
Algorithm:Cnt:=0
CntZ:=N+1
i=1..N
T(X[i])
Cnt:=Cnt+1
CntZ:=CntZ–1
Y[Cnt]:=I Y[CntZ]:=i
Y N
ELTEELTE
2121/31/3123.04.20.23.04.20.
11. Intersection
Example tasks: Let's list all common divisors of
two integers! We investigate birds in winter
and summer. Let's collect the birds which are not migratory!
Based on the free hours from the calendars of two persons, let's determine when they can meet.
ELTEELTE
2222/31/3123.04.20.23.04.20.
11. Intersection
What is in common?We have two sets as input (with same typed items), and we have to list all elements that are part of both sets!
ELTEELTE
2323/31/3123.04.20.23.04.20.
11. Intersection
One item included One item included only onceonly once
Specification: Input: N,M:Integer,
X:Array[1..N:Type] Y:Array[1..M:Type]
Output: Cnt:Integer, Z:Array[1..Cnt:Type]
Precondition: N0 and M0 andIsSet(X) and IsSet(Y)
Post cond.: Cnt= and
i(1≤i≤Cnt): (Z[i]X and Z[i]Y) and
IsSet(Z)
N
YX[i]1i
1
Could be alsoCould be also: : min(N,M)min(N,M)
ELTEELTE
2424/31/3123.04.20.23.04.20.
11. Intersection
Algorithm:
Comment:The solution is a multiple item selection and a decision.
Cnt:=0i=1..N
j:=1
j≤M and X[i]≠Y[j]
j:=j+1
j≤M
Cnt:=Cnt+1
Z[Cnt]:=X[i]
Y N
ELTEELTE
2525/31/3123.04.20.23.04.20.
11. Intersection
Variations for intersection:
We have two sets, we have to determine the number of common elements.
We have two sets, we have to determine if they have at least one common element.
We have two sets, we have to provide one common element.
ELTEELTE
2626/31/3123.04.20.23.04.20.
12. Union
Example tasks: We have two courses, let's list the
students visiting both courses! Let's collect all the birds we can
investigate in winter as well as in summer!
Based on the free hours from the calendars of two persons, let's determine when we can reach at least one of them!
ELTEELTE
2727/31/3123.04.20.23.04.20.
12. Union
What is in common?We have two sets as input (with same typed items), and we have to list all elements that are included at least in one of the sets.
ELTEELTE
2828/31/3123.04.20.23.04.20.
12. Union
Specification: Input: N,M:Integer,
X:Array[1..N:Type] Y:Array[1..M:Type]
Output: Cnt:Integer, Z:Array[1..Cnt:Type]
Precondition: N0 and M0 andIsSet(X) and IsSet(Y)
Post cond.: Cnt=N+ and
i(1≤i≤Cnt): (Z[i]X or Z[i]Y) and IsSet(Z)
M
XY[j]
1j1
Could be alsoCould be also: : N+MN+M
ELTEELTE
2929/31/3123.04.20.23.04.20.
12. Union
Algorithm:Z:=XCnt:=N
j=1..Mi:=1
i≤N and X[i]≠Y[j]
i:=i+1
i>N
Cnt:=Cnt+1
Z[Cnt]:=Y[j]
Y N
CopyCopy
DecisionDecision
Multiple item Multiple item selectionselection
ELTEELTE
3030/31/3123.04.20.23.04.20.
Patterns of Algorithms (PoAs)
Sequence Sequence7. Copy – calculation with function
8. More item selection9. Sort (will have later) Sequence Sequences10.Partitioning Sequences Sequence11.Intersection12.Union