40
CS100: DISCRETE STRUCTURES Lecture 3 – Matrices Ch 3 – Pages: 246-262

CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

CS100: DISCRETE STRUCTURES

Lecture 3 – MatricesCh 3 – Pages: 246-262

Page 2: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Matrices

Introduction

¨ Example: The matrix is a 3 x 2 matrix.

DEFINITION 1:

A matrix is a rectangular array of numbers.

A matrix with m rows and n columns is called an m x n matrix. The plural of matrix is matrices. A matrix with the same number of rows as columns is called square.

Two matrices are equals if they have the same number of rows and the same number of columns and the corresponding entries in every position are equal.

úúú

û

ù

êêê

ë

é

312011

2

Page 3: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

MatricesDEFINITION 2:3

Page 4: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Then A is ….with a12 = …. and a23 = …, B is …with b21 = …, C is …., D is ……, and E is …….

Example 14

ANS:Then A is 2*3 with a12 = 3 and a23 =2 , B is 2*2 with b21 =4, C is 1*4, D is 3*1, and E is 3*3

Page 5: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Exercise

Let

1) What size is A ?

2) What is the third column of A ?

3) What is the second row of A ?

4) What is the element of A in the (3,2)th position ?

5

3×4

A(3,2)=1

Page 6: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

A square matrix A = [aij ] for which every entry off the main diagonal is zero, that is,

aij = 0 for i ≠ j, is called a diagonal matrix

Example :

Diagonal Matrix6

Page 7: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Identity Matrix

¨ The n x n diagonal matrix

all of whose diagonal elements are 1, is called the identity matrix of order n.

¨ Multiplying a matrix by an appropriately sized identity matrix does not change

this matrix. In other words, when A is an m x n matrix, we have

AIn= ImA = A

¨ Powers of square matrices can be defined. When A is an n x n matrix, we have

A0 = In , Ar = AAAA…A (r times)

7

Page 8: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Example of Matrix applications

v Matrices are used in many applications in computer science,

and we shall see them in our study of relations and graphs.

v At this point, we present the following simple application

showing how matrices can be used to display data in a tabular

form

8

Page 9: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Cont’d

¨ The following matrix gives the airline distance between the cities indicated

9

Page 10: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Equal Matrices

DEFINITION 2:

• Two m x n matrices A = [aij ] and B = [bij] are said to be equal

• if aij = bij ,

1 ≤ i ≤ m , 1 ≤ j ≤ n; that is, if corresponding elements are the same.

• Notice how easy it is to state the definition using generic elements aij , bij

Two matrices are equal if :• they have the same dimension or order and• the corresponding elements are identical.

10

Page 11: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Cont’d

Then A = B

if and only if X=-3, y=0, and z=6

11

Page 12: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Matrices

v The sum of two matrices of the same size is obtained by adding

elements in the corresponding positions.

v Matrices of different sizes can’t be added.

DEFINITION 3:

Let A = [aij] and B = [bij] be m x n matrices.

The sum of A and B, denoted by A + B, is the m x n matrix that has aij + bij as its (i,j)th

element.

In other words, A + B = [aij + bij].

Matrix Arithmetic12

Page 13: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Example 1

=úúú

û

ù

êêê

ë

é

--

-+úúú

û

ù

êêê

ë

é--

211031143

043322101

Example 2

13

úúú

û

ù

êêê

ë

é---

252313244

Page 14: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Zero Matrix

v A matrix all of whose entries are zero is called a zero matrix and

is denoted by 0

v Each of the following is Zero matrix

14

Page 15: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Properties of Matrix Addition

v A + B = B + A

v (A + B) + C = A + (B + C)

v A + 0 = 0 + A = A

15

Page 16: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Matrices Production

DEFINITION 4:

Let A be an m x k matrix and B be a k x n matrix.

The product of A and B, denoted by AB, is the m x n matrix with its (i,j)th entry equal to

the sum of the products of the corresponding elements from the i th row of A and the i th column

of B. In other words, if AB = [cij], then

cij = ai1b1j + ai2b2j + … + aikbkj.

16

Page 17: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Matrices Production

¨ The product of the two matrices is not defined when the number of columns in the first matrix and the number of rows in the second matrix is not the same.

17

Page 18: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Matrices Production18

Page 19: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

AB=19

Page 20: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Matrices Production

¨ Example:

Let A 4X3= and B3X2 =

Find AB if it is defined.

AB =

úúúú

û

ù

êêêê

ë

é

220013112401

úúú

û

ù

êêê

ë

é

031142

20

úúúú

û

ù

êêêê

ë

é

2813798414

Page 21: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Exercise

Consider the matrices A= ,B=,C=

Find the following:2A= 4A + B = A+0=

21

Page 22: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Matrices Production

¨ Example: Let A 2x2 = and B 2x2=

Does AB = BA?Solution:

AB = and BA =

úû

ùêë

é1211

úû

ùêë

é1112

DEFINITION 5:

If A and B are two matrices, it is not necessarily true that AB and BA are the same.

E.g. if A is 2 x 3 and B is 3 x 4, then AB is defined and is 2 x 4, but BA is not defined.

Even when A and B are both n x n matrices, AB and BA are not necessarily equal.

22

úû

ùêë

é3523

úû

ùêë

é2334

Page 23: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Properties of Multiplication

If A = m x p matrix, and B is a p x n matrix, then AB can be computed and is an m x n matrix. As for BA, we have four different possibilities:1. BA may not be defined; we may have n ≠ m

For Example : A=4x5 , B=5x62. BA may be defined if n = m, and then BA is p x p, while AB is

m x m and p ≠ m. Thus AB and BA are not equalFor Example : A=4x5 , B=5x4

3. AB and BA may both the same size, but not equal as matrices AB ≠ BA

For Example : A=4x4 , B=4x44. AB = BA

23

Page 24: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Basic Properties of Multiplication

¨ The basic properties of matrix multiplication are given by the following

theorem:

a. A(BC) = (AB)C

b. A(B + C)= AB + AC

c. (A + B)C = AC + BC

24

Page 25: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Transpose Matrices

¨ Example: The transpose of the matrix

is the matrix

DEFINITION 6:

Let A = [aij] be an m x n matrix. The transpose of A, denoted by At, is the n x m matrix

obtained by interchanging the rows and columns of A.

In other words, if At = [bij], then bij = aji, for i = 1,2,…,n and j = 1,2,…,m.

úû

ùêë

é654321

úúú

û

ù

êêê

ë

é

635241

25

Page 26: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Properties for Transpose

If A and B are matrices, then1. 𝐴% % = 𝐴2. (𝐴 + 𝐵)%= 𝐴% +𝐵%

3. (𝐴𝐵)%= 𝐵%𝐴%

26

Page 27: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Exercises

Consider the matrices : A=

,B= ,C=

Find the following:𝐶% (𝐴 + 𝐵)%

= =

=

27

1 2 53 −4 −2 −1 1 4

0 3 22 −2 3

+6 4 38 −2 15 7 2

=5 5 78 1 37 5 5

Page 28: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Symmetric Matrices

¨ Example:

The matrix is symmetric.

¨ Example

DEFINITION 7:

A square matrix A is called symmetric if A = At.

Thus A = [aij] is symmetric if aij = aji for all i and j with 1 ≤ i≤ n and 1 ≤ j ≤ n.

úúú

û

ù

êêê

ë

é

010101011

28

Page 29: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

The Transpose of a Symmetric Matrix29

Page 30: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Boolean Matrix Operation

A Boolean matrix is an m x n matrix whose entries are either zero or one.

1 0 10 0 11 1 0

Example:

30

Page 31: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Boolean Matrix Operations (join)

v Let A = [aij] and B = [bij] be m x n Boolean matrices.

v We define A v B = C = [ Cij], the join of A and B, by

1 if aij =1 or bij = 1

Cij = 0 if aij and bij are both 0

31

Page 32: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Example

¨ Find the join of A and B:

A = B =

A v B =

1 0 10 1 0

0 1 01 1 0

1v0 0v1 1v00v1 1v1 0v0 = 1 1 1

1 1 0

32

Page 33: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Boolean Matrix Operations (Meet)

v We define A ^ B = C = [ Cij], the meet of A and B, by1 if aij and bij are both 1

Cij = 0 if aij = 0 or bij = 0

v Meet & Join are the same as the addition procedure

¤ each element with the corresponding element in the other

matrix

¤ Matrices have the same size

33

Page 34: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Example

¨ Find the meet of A and B:A = B =

A ^ B =

1 0 10 1 0

0 1 01 1 0

1^0 0^1 1^00^1 1^1 0^0

= 0 0 00 1 0

34

Page 35: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Boolean PRODUCT

The Boolean product of A and B, denoted , 𝑨 ⊙ 𝑩is the m x n Boolean matrix defined by

C𝑖𝑗 = <1, if aik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0, 𝑂𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

Procedure:

1. Select row i of A and column j of B, and arrange them side by side.

2. Compare corresponding entries. If even a single pair of corresponding entries consists of two 1’s, then Cij = 1, otherwise Cij = 0

35

Page 36: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Example

¨ Find the Boolean product of A and B:A 3x2= B 2x3 =

𝑨 ⊙ 𝑩3x3=

1 00 11 0

1 1 00 1 1

(1 ^ 1) v (0 ^ 0) (1 ^ 1) v (0 ^ 1) (1 ^ 0) v (0 ^ 1) (0 ^ 1) v (1 ^ 0) (0 ^ 1) v (1 ^ 1) (0 ^ 0) v (1 ^ 1) (1 ^ 1) v (0 ^ 0) (1 ^ 1) v (0 ^ 1) (1 ^ 0) v (0 ^ 1)

=1 1 00 1 11 1 0

36

Page 37: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Boolean Operations Properties

¨ If A, B, and C are Boolean Matrices with the same sizes, then

1. A v B = B v A

2. A ^ B = B ^ A

3. (A v B) v C = A v (B v C)

4. (A ^ B) ^ C = A ^ (B ^ C)

37

Page 38: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Exercises

¨ Find meet and join for A and B:

Solution :

Meet of A and B =

join of A and B =

=

=

38

Page 39: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Exercises

¨ Find 𝑨 ⊙𝑩

Solution:

𝑨 ⊙𝑩 =

=

=

•Find

39

Page 40: CS100: DISCRETE STRUCTURES · 2017-10-17 · C:; =< 1 , if a ik = 1 and bkj = 1 for some k,1 ≤ k ≤ p 0,>?ℎABC:DA Procedure: 1. Select row iof Aand column jof B, and arrange

Any Question

¨ Refer to chapter 3 of the book for further reading

40