PowerPoint プレゼンテーション

Download Report

Transcript PowerPoint プレゼンテーション

Chapter 2 The Fundamentals: Algorithms, the Integers, and Matrices
• Algorithm: specifying a sequence of steps used to solve a problem.
• Computational complexity of an algorithm: what are the
computer resources needed to use this algorithm to solve a problem
of a specified size?
• Integers: properties of integers, division of integers, algorithms
involving integers.
• Matrices: basic material about matrices, matrix arithmetic.
2.1 Algorithms
Definition 1. An algorithm is a finite sequence of precise instructions
for performing a computation or for solving a problem.
Example 1 Describe an algorithm for finding the maximum (largest) value
in a finite sequence of integers.
Solution
1.
Set the temporary maximum equal to the first integer in the sequence.
2.
Compare the next integer in the sequence to the temporary maximum,
and set the larger one to be temporary maximum.
3.
Repeat the previous step if there are more integers in the sequence.
4.
Stop when there are no integers left in the sequence. The temporary
maximum at this point is the maximum in the sequence.
Algorit hm1. Finding theMaximumElementin a FiniteSequence
Instead of using a
P rocuduremax(a1,a2 ,...,an : integers)
particular computer
language, we use a
max : a1
form of pseudocode.
for i:  2 to n
if max  ai thenmax : ai
The properties of algorithms:
• Input
• Output
• Definiteness
• Correctness
• Finiteness
• Effectiveness
• Generality
Example 2 Describe an algorithm for finding an element x in a
list of distinct elements a1 , a2 ,...,an .
Algorithm2 T he linearSearch Algorithm.
P rocedurelinear search( x : integer,a1,a2 ,...,an : distinct integers)
i:  1
while (i  n and x  ai )
i:  i  1
if i  n thenlocation: i
else locatiton: 0
{locationis thesubscript of
term thatequals x, or is 0 if x is not found}
Algorit hm3 T he BinarySearch Algorit hm.
P rocedurebinary search( x : int eger,a1,a2 ,...,an : increasingint egers)
i:  1
j:  n
while (i  j )
begin
m :  (i  j)/ 2
if x  am t heni:  m  1
else j:  m
end
if x  ai t henlocation: i
else location: 0
{locationis t hesubscript of
t erm t hatequals x, or is 0 if x is not found}
2.2 Complexity of Algorithms
Assume that both algorithms A and B solve the problem P.
Which one is better?
• Time complexity: the time required to solve a problem of
a specified size.
• Space complexity: the computer memory required to
solve a problem of a specified size.
The time complexity is expressed in terms of the
number of operations used by the algorithm.
• Worst case analysis: the largest number of operations
needed to solve the given problem using this algorithm.
• Average case analysis: the average number of
operations used to solve the problem over all inputs.
Example 3 Analyze the time complexities of Algorithm 1 and 2
Algorit hm1. Finding theMaximumElementin a FiniteSequence
P rocuduremax(a1,a2 ,...,an : integers)
max : a1
for i:  2 to n
if max  ai thenmax : ai
Number of
operations
1
n
2(n-1)
3n-1
Algorithm2 T he linearSearch Algorithm.
P rocedurelinear search( x : integer,a1,a2 ,...,an : distinct integers)
i:  1
while (i  n and x  ai )
i:  i  1
if i  n thenlocation: i
else locatiton: 0
{locationis thesubscript of
term thatequals x, or is 0 if x is not found}
1
2(n+1)
n
2
3n+5
Algorit hm3 T he BinarySearch Algorit hm.
P rocedurebinary search( x : int eger,a1,a2 ,...,an : increasingint egers)
i:  1
j:  n
1
1
while (i  j )
begin
1
1
m :  (i  j)/ 2
if x  am t heni:  m  1
else j:  m
end
if x  ai t henlocation: i
2
Let n  2k.
It repeats at most k (k  log2 n) times.
2
else location: 0
{locationis t hesubscript of
t erm t hatequals x, or is 0 if x is not found}
Number of operations = 4 log n+4
2
For finding x in a list of n elements, we have two algorithms
Which use 3n  5 and 4log2 n  4 time(operations), respectively.
Which one is better?
• A constant factor does not influence the running time so
much.
Therefore, 3n+5 grows faster than 4 log2 n  4 when n
becomes larger. Usually, we say that 3n+5 is O(n) and say
that 4 log2 n  4 is O( log2 n ).
Orders of Growth
Running time for a problem with size n  106
Running
Time
Operation
Per second
necessary
lg n
n
operations
n
2
2n
6
instant
1 second
11.5 days
Never end
2 59350 days
12
instant
Instant
1 second
Never end
2 59340 days
10
10
Using silicon computer, no matter how fast CPU will be you can
never solve the problem whose running time is exponential !!!
Asymptotic Notations: O-notation
Definition 2.1 A function f(n) is said to be O(g(n)) if there exist
some constantc0  0 and n0  0 such that f (n) c0 g (n) for all n  n0 .
c0 f ( N )
g (N )
N0
If limn
N
f ( n)
 c (c  0 is a constant) , then f(n)  O(g(n)).
g ( n)
Example 3 Prove 2n+1=O(n)
Example4 Prove10n2  12n  5  O(n2 )
Example 5
List the following function in O-notation in increasing order:
lg n, n, n2 , n lg n, n3 , n!,2n.
Example 6 What is thebig - oh of the following functions?
5n5  100n 2 1000,
n lg n 2  100n lg n  1000n,
0.0001 2 n  n
2.2 The Integers and Division
• We discuss the properties of integers which belongs to
the branch of Mathematics called number theory.
Definition1. If a and b are integerswith a  0, we say thata divides b
if thereis an integerc such thatb  ac. Whena divides b we say thata is
a factorof b and b is a multipleof a. T henotationa|b denotesthata
divides b.We write a|b when a does not divide b.
a is a factor of b
b is a multiple of a
Example 1 Determine whether 3|7 and whether 3|12.
Solution: It follows that 3|7 and 3|12.
• Basic properties of divisibility of integers
Theorem 1. Let a, b, and c be integers. Then
1. If a|b and a|c, then a|(b+c).
2. If a|b, then a|bc for all integers c.
3. If a|b and b|c, then a|c.
Proof
1. There are s and t such that b=as and c=at. Therefore, b+c=a(s+t).
2. There is s such that b=as. Therefore, bc=a(sc)
3. There are s and t such that b=as and c=bt, therefore, c=a(st)
Definition 2. A positive integer p greater than 1 is called prime if
the only positive factors of p are 1 and p. A positive integer that is
greater than 1 and is not prime is called composite.
Example 2. Is 7 a prime?
• The primes less than 100 are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,
37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89 and 97.
Theorem 2. Every positive integer can be written uniquely as
the uniquely as the product of primes in order of increasing size.
Example3
100  2  2  5  5
641 641
999  3  3  3  37
1024 2  2  2  2  2  2  2  2  2  2
T heorem3. Let a be an integerand d a positiveinteger.
T hen thereare unique integerq and r, with 0  r  d , such
that a  dq  r.
quotient remainder
dividend divisor
Example 4 What are the quotient and remainder
when 101 is divided by 11?
Solution: We have 101  11  9  2.
quotient remainder
Definition 3. Let a and b be integers. The largest integer d such
that d|a and d|b is called the greatest common divisor of a and b,
denoted by gcd(a,b).
Example 5 What is gcd(24,36) and gcd(17,22)?
Definition 4. The integers a and b are relatively prime if gcd(a,b)=1.
Definition 5. The least common multiple of the positive integers
a and b is the smallest positive integer that is divisible by both a
and b, denoted by lcm(a,b).
Example6 Find lcm(95256,
432).
Solution : 95256 23357 2 ,432  2 433
lcm(23357 2 ,2 433 )  2 4357 2
Definition 6. Let a be an integer and m be a positive integer.
We denoted by a mod m the remainder when a is divided by m.
Example 7
17 mod 5 = 2, -133 mod 9=2, and 2001 mod 101 = 82.
Definition7. If a and b are integersand m is a positiveinteger,
thena is congruentto b modulo m if m divides a-b. We use a  b (modm)
to indicatethata is congruentto b modulo m.
Example8 Is 17  5 (mod6) and 24  14 (mod6)?
2.4 Integers and Algorithms
• The Euclidean Algorithm: Find the greatest common
divisor of two positive integers
Lemma 1 Let a=bq+r, where a,b,q, and r are integers.
Then gcd(a,b)=gcd(b,r).
Given positiveintegersa and b with a  b,
Let r0  a and r1  b. We have
r0  r1q1  r2
0  r2  r1,
r1  r2 q2  r3
0  r3  r2 ,

rn  2  rn 1qn 1  rn
0  rn  rn 1,
rn-1  rn qn
From Lemma1 we have
gcd( a,b)  gcd( r0 ,r1 )  gcd( r1,r2 )
   gcd( rn-1,rn )  gcd( rn ,0)  rn
Algorithm1 T heEuclidean Algorithm
P rocedure gcd( a,b : positiveintegers)
x:  a
y:  b
while y  0
begin
r:  x mod y
x:  y
y:  r
end{gcd(a,b) is x}
Algorithm1 T heEuclidean Algorithm
Example 1
P rocedure gcd( a,b : positiveintegers)
Find the greatest common divisor of 414
and 662 using the Euclidean Algorithm.
x:  a
y:  b
while y  0
begin
r:  x mod y
x:  y
y:  r
end{gcd(a,b) is x}
Solution:
662  4141  248
414  2481  166
248  1661  82
166  82  2  2
82  2  41
Hence, gcd(414,662)=2, since 2 is the
last nonzero remainder.
• Representation of integers
T heorem1 Let b a positiveintegergreater than 1. Any positiveintegern
can be expresseduniquely in theform
n  ak b k  ak 1b k 1   a1b  a0 .
where k is a nonnegative integer, a0 ,a1 ,  ak are nonnegative integersless than
b, and ak  0.
Example2
Whatis thedecimalexpansionof theinteger that has
(101011111
) 2 as its binary expansion?
Solution
(101011111
) 2  28  26  24  23  22  2  1  351
Example3
(2AE0B)16  2 164  10163  14162  0 16  11  (175627)10
Solution
Example4
Find thebase 8 expansionof (12345)10 .
12345 8 1543 1
1543 8 192 7
192  8  24  0
24  8  3  0
3  80  3
T herefore,(12345)10  (30071)8 .
Algorithm Constructing Base b expansion(n: positiveinteger)
P rocedurebase b expansion(n : positiveinteger)
q : n
k:  0
while q  0
begin
ak :  q modb
q: q/b
k:  k  1
end{thebase b expansionof n is (ak-1  a1a0 )b }
• Algorithm for integer operations
Addition of a and b
cn-1 cn  2 cn 3 c1c0
a  (an 1 an  2  a1 a0 ) 2
b  ( bn 1 bn  2  b1 b0 ) 2
a  b  ( sn sn 1sn  2  s1 s0 ) 2
Example 5
1110
1110
1011
11001
Algorithm3 Addition of integers
P rocedure add(a,b : positiveintegers)
{t he binary expansionsof a and b are (an-1an  2  a1a0 ) 2
and (bn-1bn  2  b1b0 ) 2 , respectively}
c : 0
for j:  0 to n-1
begin
d:  (a j  b j  c)/ 2
sj :  a j  b j  c  2d
c: d
end
sn:  c
{the binary expansionof thesum is (sn sn-1  s0 )2 }
Multiplication of a and b
Algorithm4 Multiplying Integers.
110
1 0 1 1
a
b
P rocedure Multiply(a,b : positiveintegers)
{t he binary expansionsof a and b are (an-1an  2  a1a0 ) 2
and (bn-1bn  2 b1b0 ) 2 , respectively}
1 1 0 a shifts 0 place for j:  0 to n-1
1 1 0 a shifts 1 place
000
110
1000010
begin
if b j  1 thenc j :  a shift j places
else c j :  0
a shifts 3 places end
{c0 , c1 ,...,cn 1 are thepart ialproducts}
p:  0
for j:  0 to n-1
p: p  c j
{ p is the value of ab}
2.6 Matrices
Definition1. A matrixis a rectangular arrayof numbers.
A matrix with m rows and n columnsis called as an m  n matrix.
A matrix with thesame number of rows and columnsis called square.
T wo matricesare equal if thayhave thesame entries.
a11

a21

A
ai1


an1

a12  a1 j  a1n 

a22  a2 j  a2 n 




ai 2  aij  ain  i row




an 2  anj  ann  i row j column
j column
A shorthandnotationis to write A  [aij ]
Definition2.
Let A  [aij ] and B  [bij ] be m  n matrices.
A  B  [aij  bij ].
Example1
 1 0 - 1   3 4 - 1   4 4 - 2
 2 2 - 3   1 - 3 0    3 - 1 - 3 

 
 

 3 4 0 - 1 1 2  2 5 2
Definition3.
Let A  [aij ] be an m  k matrixand B  [bij ] be k  n matrix.
k
AB  [cij ], where cij  ai1b1 j  ai 2b2 j    aik bkj   aitbtj
t 1
Example2
 1 0 -1   3 4 -1   4 3 - 3 
 2 2 - 3  1 - 3 0    11 - 1 - 8 

 
 

 3 4 0 - 1 1 2  13 0 - 3
Example3
1 1 
2
Let A  
and B  

 2 1
1
We have AB  BA.
1
.

1
• Algorithms for matrix multiplication
Algorithm1 MatrixMultiplication
procedurematrixmultiplication(A,B : matrices)
for i:  1 to m
begin
for j:  1 to n
begin
cij:  0
for q:  1 to k
cij:  cij  aiqbqj
end
end{C  [cij ] is theproduct of A and B}
• Transposes and powers of matrices
Definition4 T heidentitymatrixof order n is
then  n matrixI n  [ ij ], where δij  1 if i  j and δij  0 if i  j.
1 0  0
0 1  0 
.
Hence, I n  




0
0

1


Let A be an m  n matrix.Then AIn  I m A  A.
Let A be an n  n matrix.Wedefine therth power of A to be
A0  I n and Ar  
AA

A.



r times
Definition5 Let A  [aij ] be an m  n matrix.
T he transposeof A, denotedby At , is a matrixobtained
by interchanging therows and columnsof A.
In other words, At  [bij ], where bij  a ji .
• Zero-One Matrices
A matrix with entries that are either 0 or 1 is called a zero-one matrix.
Definition8 Let A  [aij ] and B  [bij ] be m  n zero- one matrices.
A  B  [aij  bij ] and A  B  [aij  bij ].
Example6
1 0 1
0 1 0 
Let A  
and B  
.


 0 1 0
1 1 0 
1  0 0  1 1  0  1 1 1 
We have A  B  

and


0  1 1  1 0  0 1 1 0
1  0 0  1 1  0  0 0 0 
A B  



0

1
1

1
0

0
0
1
0

 

Example4
1 0 
1


t
Let A   2 2  . T henA  
0

 3 4 
2 3
.

2 4
Definition6 A square matrix A  [aij ] is called symmetric
if A  At . T hus, A is symmetricif aij  a ji for all i and j wit h
1  i  n and1  j  n.
Example5
1 2 3 
Matrix 2 2 4  is symmetric.
 3 4 5 
Definit ion9.
Let A  [aij ] be an m  k zero - one matrixand B  [bij ] be k  n
zero - one matrix.A . B  [cij ] is them  n matrix,
where cij  (ai1  b1 j )  (ai 2  b2 j )    (aik  bkj ).
Example7
1 0 
1 1 0 


.
Let A   0 1  and B  

0 1 1 
 1 0 
(1  1)  (0  0) (1  1)  (0  1) (1 0)  (0  1)  1 1 0 
A . B  (0  1)  (1  0) (0  1)  (1 1) (0  0)  (1 1)  0 0 0 .
(1  1)  (0  0) (1  1)  (0  1) (1 0)  (0  1)  1 1 0 
Algorithm2 T heBooleanP roduct
procedureBooleanproduct(A,B : zero - one matrices)
for i:  1 to m
begin
for j:  1 to n
begin
cij:  0
for q:  1 to k
cij:  cij  (aiq  bqj )
end
end{C  [cij ] is theBooleanproduct of A and B}
Let A be a square zero - one mat rix.We define
t he rt h Booleanpower of A
t o be A[ 0 ]  I n and A[ r ]  
AA

A.



r times