Graphs and Matrices

35
Graphs and Matrices Spring 2012 Mills College Dan Ryan Lecture Slides by Dan Ryan is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 3.0 Unported License.

description

Graphs and Matrices. Spring 2012 Mills College Dan Ryan. Lecture Slides by  Dan Ryan is licensed under a  Creative Commons Attribution- NonCommercial - ShareAlike 3.0 Unported License . Four Representations of a Graph. G={V,E}. EDGE List AB AD AE BA BC BD CA CB CD DA DB DC EA. - PowerPoint PPT Presentation

Transcript of Graphs and Matrices

Page 2: Graphs and Matrices

Four Representations of a Graph

G={V,E}

NODE ListA B D EB A C DC A B DD A B CE A

EDGE ListABADAEBABCBDCACBCDDADBDCEA

(− 1 0 1 11 − 1 1 0111

110

−10

1−0

00−)A

C

BD

E

MATRIX

Page 3: Graphs and Matrices

Matrix

1 0 0 0 1

1 1 2 2 0

1 2 0 0 0

3 0 1 0 0

0 1 1 1 1

ROWS COLUMNS

A matrix ELEMENT

The MAIN DIAGONAL

SQUARE MATRIX

RECTANGULAR MATRIX

Page 4: Graphs and Matrices

Nomenclature: nrows by mcols

1 0

1 1

1 2

3 0

0 1

1 0 0 0

1 1 2 2

1 0 0

1 1 2

1 2 0

1

3

05x2 matrix

2x4 matrix

3x1 matrix

3x3 matrix

Page 5: Graphs and Matrices

Matrix

• A table of values arranged in ordered rows and columns

• Elements of matrix M referred to by row and column number as subscripts

• An unspecified element is referred to generically as being in row i and column j

M=(𝑀11 𝑀 12 𝑀 13

𝑀21 𝑀 22 𝑀 23

𝑀31 𝑀 32 𝑀 33

 )

Page 6: Graphs and Matrices

Sometimes we say “Vector”

• Each row (or column) in a matrix can be thought of as an ordered set of numbers describing an object.

• For a graph matrix, a row or a column is a “vector” of a vertex’s connections

• The fourth vertex is connectedto the first, second, and third,but not the fifth

− 1 0 1 11 − 1 1 0111

110

−10

1−0

00−

Page 7: Graphs and Matrices

Transpose

• The transpose of a matrix is a swapping of its rows and columns

Page 8: Graphs and Matrices

Transposing a Matrix

1 1001

010

0 1100

001

0 0110

011

1 01 0

0 11 0

0 11 00010

0 00 11101

transpose

4×6𝑀𝑎𝑡𝑟𝑖𝑥 𝐼 6×4𝑀𝑎𝑡𝑟𝑖𝑥 𝐼𝑇

i

j

𝐼 𝑖 , 𝑗 𝐼❑𝑇𝑗 , 𝑖

Page 9: Graphs and Matrices

Matrix Arithmetic

• Multiplication• Row of first matrix times column of second• Element by element multiplication• Sum the products

Page 10: Graphs and Matrices

Example: Convert 2-Mode to 1-Mode(s)

1. Write the rectangular incidence matrix I

A

1 2 3

B DC

A B C D

1 1 0 1 1

2 0 1 1 0

3 1 1 1 1

GROUPS

PEOPLE

A “groups by people” matrix

Page 11: Graphs and Matrices

Example: Convert 2-Mode to 1-Mode(s)

2. Compute transpose of I, IT

A B C D

1 1 0 1 1

2 0 1 1 0

3 1 1 1 1

1 2 3

A 1 0 1

B 0 1 1

C 1 1 1

D 1 0 1

A “groups by people” matrix

A “people by groups” matrix

Page 12: Graphs and Matrices

When we multiply matrices…

• Columns of first factor = rows of second factor

• (People x Groups) X (Groups x People) = (People x People)

• (Groups x People) X (People x Groups) = (Groups x Groups)

IT101011111101

I101101101111

AGxG313122324

IT101011111101

I101101101111

APxP2122122122322122

=x =x

Page 13: Graphs and Matrices

Degree

• DEF: degree of vertex is number of edges connected to it

• Written as • For undirected graph, sum of row i of matrix

- 1 0 0 01 - 0 1 00 0 - 1 10 1 1 - 10 0 1 1 -

Page 14: Graphs and Matrices

Mean(average) Degree

• The mean degree, c, in a graph is the sum of all the vertex degrees divided by the number of vertices:

Page 15: Graphs and Matrices

Degree and Edges

• Each of m edges has two ends and so contributes 2 to the total degree in graph. Thus

• But

• So

• Which is just the sum of all the elements of the adjacency matrix (since we run through all the j’s for each i – adding up row by row as we go)

Page 16: Graphs and Matrices

Adjacency Matrix

• One row and one column for each vertex• Each matrix element describes link between

row vertex and column vertex

Page 17: Graphs and Matrices

Matrix Arithmetic

• Addition and Subtraction• Element by element

See also: http://www.jtaylor1142001.net/calcjat/Contents/CMatrix.htm#MatrixMult

Page 18: Graphs and Matrices

DirectedUnweighted

UndirectedWeighted

UndirectedUnweighted

DirectedWeighted

dichotomize

dichotomize

symmetrize

symmetrize

E

A B

CD2

4

5

1

1

- 4 0 0 00 - 0 1 00 0 - 0 00 1 5 - 10 0 2 0 -

E

A B

CD

E

A B

CD2

4

5

1

1

E

A B

CD

- 1 0 0 00 - 0 1 00 0 - 0 00 1 1 - 10 0 1 0 -

- 4 0 0 04 - 0 1 00 0 - 5 20 1 5 - 10 0 2 1 -

- 1 0 0 0 - 0 1 0 - 1 1 - 1 -

Page 19: Graphs and Matrices

Paths

• If there is a path from vertex A to vertex B……then there is a sequence of edges connecting them

• E.g., if there is a 2-path from A to C then there is some vertex B such that there is an edge AB and an edge BC

A CBPath A to C?

Page 20: Graphs and Matrices

Put another way• An edge from vertex j to vertex i means Aij=1

• If there is a 2-path from j to i then there is a k such that

Aik=1 and Akj=1.… i … j … k …

0 0 1 1 0 0 0

i 0 1 1 1 0 1 0

0 1 1 0 1 0 0j 0 1 1 0 1 0 0 0 0 0 0 0 0 0

k 0 0 1 1 0 0 1

0 1 0 0 0 0 0

j

i

k

Page 21: Graphs and Matrices

Aik=1 and Akj=1

• That means: if there is a path from vertex j to vertex k to vertex i THEN

there is a k such that AikAkj=1

… i … j … k …

- 0 0 0 0 0 0

i 0 - 1 0 0 1 0

0 0 - 1 0 1 0j 0 0 0 - 0 0 0 0 0 1 0 - 0 0

k 1 0 0 1 0 - 0

0 1 0 0 0 0 -

j

i

k

a

b c

There is an edge from k

to i

There is an edge from j

to k

Page 22: Graphs and Matrices

Another way to see it…

• If vertex j is connected to vertex k AND• Vertex I is connected to k THEN• Row I will have a 1 in position k AND• Column j will have a 1 in position k

Page 23: Graphs and Matrices

Elements of Product Matrix are Vector Products

2 1 1 0 1

=

1 3 1 1 0

1 1 2 0 1

0 1 0 1 0

1 0 1 0 2

0 1 1 0 0

1 0 1 0 1

1 1 0 0 0

0 0 0 0 1

0 1 0 1 0

0 1 1 0 0

X

1 0 1 0 1

1 1 0 0 0

0 0 0 0 1

0 1 0 1 0

i

j

A2ij is how many times

the row version and the column version have 1s

in the same place

Page 24: Graphs and Matrices

… i … j … k …

- 0 0 0 0 0 0

i 0 - 1 0 0 1 0

0 0 - 1 0 1 0

j 0 0 0 - 0 0 0

0 0 1 0 - 0 0

k 1 0 0 1 0 - 0

0 1 0 0 0 0 -

Are there other paths from j to i?

• Yes, if there are other k’s such that AikAkj=1• In fact, we can write

• But this is just the number of times that we get 1 and 1 as we look down column j and across row i

j

ik

Page 25: Graphs and Matrices

Look for vertices between j and i

… i … j … … …

0 0 1 1 0 0 0

i 0 1 1 1 0 1 0

0 1 1 0 1 0 0

j 0 1 1 0 1 0 0

0 0 0 0 0 0 0

0 0 1 1 0 0 1

0 1 0 0 0 0 0

j

ik

Page 26: Graphs and Matrices

Example: Convert 2-Mode to 1-Mode(s)

2. Compute transpose of I, IT

A

1 2 3

B DC

A B C D

1 1 0 1 1

2 0 1 1 0

3 1 1 1 1

Page 27: Graphs and Matrices

Degree Distribution

• “Distribution of X” = pattern of different values X takes on

• More specifically: the frequency of each value

42

3

3 5

1244

44 455

2

2 2

22

2

1

33

31 ||2 |||| |||3 ||||4 |||| |5 |||

3 5

1244

44 455

2

2 2

22

22

1

33 3

34

1 2 3 4 50123456789

Page 28: Graphs and Matrices

Diagonal Elements of A2 are Vertex Degree

0 1 1 0 0

A=

1 0 1 0 1

1 1 0 0 0

0 0 0 0 1

0 1 0 1 0

2 1 1 0 1

A2=1 3 1 1 0

1 1 2 0 1

0 1 0 1 0

1 0 1 0 2

Page 29: Graphs and Matrices

Why?

2 1 1 0 1

=

1 3 1 1 0

1 1 2 0 1

0 1 0 1 0

1 0 1 0 2

A2

0 1 1 0 0

1 0 1 0 1

1 1 0 0 0

0 0 0 0 1

0 1 0 1 0

A

0 1 1 0 0

X

1 0 1 0 1

1 1 0 0 0

0 0 0 0 1

0 1 0 1 0

A

Degree equals number of “out and back” paths of length 2

Page 30: Graphs and Matrices

How about A3?

2 4 3 1 1

=

4 2 4 0 4

3 4 2 1 1

1 0 1 0 2

1 4 1 2 0

A3

0 1 1 0 0

X

1 0 1 0 1

1 1 0 0 0

0 0 0 0 1

0 1 0 1 0

A

2 1 1 0 1

1 3 1 1 0

1 1 2 0 1

0 1 0 1 0

1 0 1 0 2

A2

Page 31: Graphs and Matrices

What do these represent?

2 4 3 1 1

=

4 2 4 0 4

3 4 2 1 1

1 0 1 0 2

1 4 1 2 0

A3

0 1 1 0 0

X

1 0 1 0 1

1 1 0 0 0

0 0 0 0 1

0 1 0 1 0

A

2 1 1 0 1

1 3 1 1 0

1 1 2 0 1

0 1 0 1 0

1 0 1 0 2

A2

Page 32: Graphs and Matrices

PRACTICE:COMPUTE A2

FROM

W X Y Z

W - 1 0 0

TOX 0 - 1 1

Y 1 0 - 0

Z 0 0 1 0

A

W

Z

X

Y

FROM

W X Y Z

W 0 0 1 1

TOX 1 0 1 0

Y 0 1 0 0

Z 1 0 0 0

A2

Page 33: Graphs and Matrices

PRACTICE:COMPUTE A3

FROM

W X Y Z

W - 1 0 0

TOX 0 - 1 1

Y 1 0 - 0

Z 0 0 1 0

A

W

Z

X

Y

FROM

W X Y Z

W 0 0 1 1

TOX 1 0 1 0

Y 0 1 0 0

Z 1 0 0 0

A2

FROM

W X Y Z

W 1 0 1 0

TOX 1 1 0 0

Y 0 0 1 1

Z 0 1 0 0

A3

Page 34: Graphs and Matrices

1 2 3 4 50

1

2

3

4

5

6 Degree Distribution

Vertex Degree

# of

Ver

tices

Page 35: Graphs and Matrices

… i … j … k …

- 0 0 0 0 0 0

i 0 - 1 0 0 1 0

0 0 - 1 0 1 0j 0 0 0 - 0 0 0 0 0 1 0 - 0 0

k 1 0 0 1 0 - 0

0 1 0 0 0 0 -

Are there other paths from j to i?

• Yes, if there are other k’s such that AikAkj=1• In fact, we can write

• But this is just the number of times that we get 1 and 1 as we look down column j and across row i

j

ik