Professional Documents
Culture Documents
linliang@ieee.org
Algorithm Specification
Algorithm Analysis
,
)
001
002
003
004
S01
L01
S01
S02
001003
001,
002,
002..
002,.
004
001,003,
004,.
..
..
..
...
...
...
...
100M
100M
2
100M
200M
200M
200M
200M
1
2
0,1,2,3,4,5,6,7,8,910
(1,2)(1.1,1.2)
1.
()
(Beijing)
2.
3.:
9,8,7,6,5,4,3,2,1,0
0,2,4,6,8,1,3,5,7,9
Terms
C9292
Terms
Terms
(Data It
em)
Terms
:
3N
6
""""""
""""
""
Terms
""
Terms
4
1.. R1 = {}
2. R1 = {(d1, d2), (d2, d3), , (d(n-1),
dn)}, d1,dn,
.
3.(D, {R1}) ,
, .
4. (D, {R1}),
.
M.
,():
S: D -> M
Dd,S(d) M,
R
, :
:
. .
, D = {d1, d2, d3, d4, d5}
R = {(d1,d2), (d2, d3), (d3, d4),(d4, d5)}
,
d1200.
200
d1
201
d2
202
d3
203
d4
204
d5
,
, :
, . ,
R = {(d1, d2), (d1, d3), (d2, d4), (d2, d5), (d3, d6)}
d1
d1
d3
d3
d2
d4
d2
d5
d6
d4
d5
d6
Algorithm Specification
Algorithm Analysis
ADT =D,S,P
DSD
PD
ADT <> is
Data:
<>
Operations:
<>
End <>
ADT
ADTADT ADT
ADTADT
ADTOO
C++ADTclass,
C++ADT
class
class Circle {
private:
float r, x, y;
public:
Circle(float v1,float v2, float v3);
float area ()= {return pi*r*r};
};
ADTADTtemplate
Algorithm Specification
Algorithm Analysis
Algorithm SpecificationDiscussion
DefinitionAn algorithm is a finite set of instructions that, if
followed, accomplishes a particular task. In addition, all
algorithms must satisfy the following criteria:
(1) Input: There are zero or more quantities that are externally
supplied.
(2) Output : At least one quantity is produced.
Algorithm Specification
(3) Finiteness: If we trace out the instructions of an algorithm,
then for all cases, the algorithm terminates after finite number
of instructions.
(4) Definiteness: Each instruction is clear and unambiguous.
(5) Effectiveness: Every instruction must be carried out by
enough basic operators, in principle, by a person using only
pencil and paper.
Algorithm Specification
.
, ,
(
). ,
.
, :
.
, ;
,
Algorithm Specification
:
(1) , ;
(2) ,
;
(3) ;
(4) , .
Algorithm Specification
:
(1) : ;
(2) : , ;
(3) :
;
(4) : ;
(5) : ,
.
:Discussion
1
2
Algorithm Design
Example 1 Selection Sort: Sort a set of n 1 integers
a1, a2, , an in increasing order.
From those integers that are currently unsorted, find
the smallest and place it next in the sorted list.
Where?
// a[0], , a[n-1]
for (int i = 0; i < n -1; i++) {
int j = i;
// a[i]a[n-1]a[j]
for (int k = i + 1; k < n, k++)
if (a[k] < a[j]) j = k;
// j
//a[i]a[j]
int temp t = a[i]; a[i]= a[j]; a[j] = t;
}
}
Template()
C++
//
int GetMax( int a, int b )
{
return( a > b ) ? a : b;
}
//
long GetMax( long a, long b )
{
return( a > b ) ? a : b;
}
//Typeintlongdoublechar
C++
Practice
T *element;
int arraySize;
void swap(const int m1, const int m2);
int minKey( const int low, const int high);
public:
dataList( int size = 10): arraySize(size),
element(new T[size]){}
~dataList(){delete [] element;}
void sort();
};
}
template <class T>
int dataList<T>::minKey( const int low, const int high){
Algorithm Specification
Algorithm Analysis
Algorithm Analyses
, ().
, ,
.
: .
: (Correctness)
(Robustness)(Readability);
: (Time Complexity)
(Space Complexity).
: .
Algorithm Analyses
()
(1) : ;
(2) : ,
,
;
(3) : ,
.
Algorithm Analyses
()
:
,
.
,
.
Algorithm Analyses
()
,
,
.
.
,
.
Algorithm Analyses
()
(1)
,
. =
+
, .
(2)
: n, T(n).
T(n).
T(n)n, : T(n) = c1nk + c2nk-1 + ,
.
Algorithm Analyses
cg(n)
f(n)
cg(n)
()
(3)
1: f(n)g(n), c, n0
Z+,
Z+,
n0
n n0, :
O(1) < O(log2n) < O(n) < O(nlog2n) < O(n2) < O(n3) < O(2n)
2: f(n)g(n), c, n0 Z+, n Z+, n n0, :
Time Complexity
f(n)
c, n
T(n) <= c f(n)
T(n) = O(f(n))
f(n) = 3n2 +12n, T(n) = O(n2)
T(n)=O(n3)n
n3
Time Complexity
T(n)= O(f(n))
f(n)n
n
f(n)O
f(n)T(n)T(n)
nT(n)
CA*B
define n
MATRIXMLT(float A[n][n],float B[n][n],float C[n][n])
{
int i,j,k;
for(i=0;i<n;i++)
//n+1
for(j=0;j<n;j++)
//n(n+1)
{
C[i][j]=0;
//n*n
for( k=0;k<n;k++)
//n*n*(n+1)
C[i][j]=A[i][k]*B[k][j] //n*n*n
}
}
T(n ) 2n 3 3n 2 2n 1
T(n ) O(n 3 )
Algorithm Analyses
()
(4)
: n, T(n).
1). = O(1)
2). 1 = O(1) + O(1)
3). 2(if /else) = O(1) + max(O(1),
O(2))
4). 12 =
Algorithm Analyses
()
(5)
1). O(1) + O(1) = O(1)
2). O(1) + O(1) + + O(1) = O(k)
3). O(f1(n)) + O(f2(n)) = max(O(f1(n)), O(f2(n)))
4). O(mf(n)) O(f1(n)) + O(f2(n)) + + O(fm(n)) O(mF(n))
Algorithm AnalysesPractice
()
(6)
. Ann, Bnn, A B.
#define N 20
void Multiply(int a[][N], int b[][N])
{
for (i = 0; i < n; i++)
for ( j = 0; j < n; j++) {
c[i][ j] = 0;
for (k = 0; k < n; k++) c[i][ j] += a[i][k]*b[k][ j];
}
}
, O(n3).
n (O(1)
+ O(n))
n O(n2) = O(n3)
= n O(n)
2) = O(n)
n
O(1)
=
O(n
x = 0; y = 0;
for (k=1;k<=n;k++)
x++;
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
y++; //n*n
T(n) = On2
T(n)= O(1)
temp = i;
i = j;
j = temp;
:n
n
void bubble-sort(int a[]int n)
for(I=n-1;change=TURE;I>1 && change;--I)
{// a
change=false;
for(j=0;j<I;++j)
if (a[j]>a[j+1]) {
a[j] a[j+1];
change=TURE}
}//bubble-sort
0;
1+2+3++n-1 =n(n-1)/2;
Practical Complexity
Time
1
log n
n
n log n
n2
n3
2n
n!
Name
constant
logarithmic
linear
log linear
quadratic
cubic
exponential
factorial
1
1
0
1
0
1
1
2
1
Instance characteristic n
2 4
8
16
1 1
1
1
1 2
3
4
2 4
8
16
2 8
24
64
4 16
64
256
8 64
512
4096
4 16
256
65536
2 24 40326 2092278988000
32
1
5
32
160
1024
32768
4294967296
26313 1033
2n
n2
60
50
40
n log n
f 30
20
n
10
Log n
10
T1(n)
T2(n)
100
0.06
0.01
1000
10000
13
100
100000
166
1000000
33
16666=11.5
10
T(n)
n/n
10n
1000
10000
n=10n
10
20n
500
5000
n=10n
10
5nlogn
250
1842
3n<n<10n
7.37
2n2
70
223
n=101/2n
3.16
2n
13
16
n=n+3
n:n:
{
action1(); // action1O(f1(n))
actions(); //action2O(f2(n))
}
O(max(f1(n), f2(n))).
O(f1(n))O(f2(n)),
O(max(f1(n), f2(n))).
(n) = T1(n) + T2(n) = O(f1(n)) + O(f2(n)) = O(max(f1(n), f2(n)))
action()
forT1(n)= O(n),action
O(f(n))
O(nf(n)).
O(f1(n)),
O(f2(n)),
O(f1(n)f2(n)).
(n) = T1(n) T2(n) = O(f1(n)) O(f2(n)) = O(f1(n) f2(n))
Space Complexity
S(n) = O(f(n)).
Write a program that will print the calendar of the current year.
Modify the program so that it will read a year number and print the
calendar for that year. A year is a leap year (that is, February has 29 days
instead of 28 days) if it is a multiple of 4, except that century years
(multiples of 100) and leap years only when the year is divisible by 400.
Hence the year 1900 is not a leap year, but the year 2000 is a leap year.
Modify the program so that it will accept any date(day, month, year) and
print the day of the week for that date.
Modify the program so that it will read two dates and print the number of
days from one to the other.
Using the rules on leap years, show that the sequence of calendars
repeats exactly every 400 years
Sicily
1134
1093