Graphs and Matrices
description
Transcript of Graphs and Matrices
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
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
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
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
)
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−
Transpose
• The transpose of a matrix is a swapping of its rows and columns
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
𝐼 𝑖 , 𝑗 𝐼❑𝑇𝑗 , 𝑖
Matrix Arithmetic
• Multiplication• Row of first matrix times column of second• Element by element multiplication• Sum the products
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
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
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
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 -
Mean(average) Degree
• The mean degree, c, in a graph is the sum of all the vertex degrees divided by the number of vertices:
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)
Adjacency Matrix
• One row and one column for each vertex• Each matrix element describes link between
row vertex and column vertex
Matrix Arithmetic
• Addition and Subtraction• Element by element
See also: http://www.jtaylor1142001.net/calcjat/Contents/CMatrix.htm#MatrixMult
DirectedUnweighted
UndirectedWeighted
UndirectedUnweighted
DirectedWeighted
dichotom
ize
dichotom
ize
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 -
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?
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
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
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
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
… 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
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
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
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
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
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
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
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
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
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
1 2 3 4 50
1
2
3
4
5
6 Degree Distribution
Vertex Degree
# of
Ver
tices
… 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