Ma/CS 6bmath.caltech.edu/~2014-15/2term/ma006b/12 Graphs...Β Β· incidence matrix 𝑀 and adjacency...

16
2/1/2015 1 Ma/CS 6b Class 12: Graphs and Matrices By Adam Sheffer 1 2 3 4 5 0 1 0 1 0 1 0 0 1 1 0 3 0 1 0 0 0 1 0 1 1 3 0 1 0 Non-simple Graphs In this class we allow graphs to be non- simple. We allow parallel edges, but not loops.

Transcript of Ma/CS 6bmath.caltech.edu/~2014-15/2term/ma006b/12 Graphs...Β Β· incidence matrix 𝑀 and adjacency...

Page 1: Ma/CS 6bmath.caltech.edu/~2014-15/2term/ma006b/12 Graphs...Β Β· incidence matrix 𝑀 and adjacency matrix ࣡. Express 𝑀𝑀𝑇 using ࣡. Answer. This is a 𝑉×𝑉 matrix.

2/1/2015

1

Ma/CS 6b Class 12: Graphs and Matrices

By Adam Sheffer

𝑣1

𝑣2

𝑣3 𝑣4

𝑣5

010

101

0 0 11 0 30 1 0

0 0 1 0 11 3 0 1 0

Non-simple Graphs

In this class we allow graphs to be non-simple.

We allow parallel edges, but not loops.

Page 2: Ma/CS 6bmath.caltech.edu/~2014-15/2term/ma006b/12 Graphs...Β Β· incidence matrix 𝑀 and adjacency matrix ࣡. Express 𝑀𝑀𝑇 using ࣡. Answer. This is a 𝑉×𝑉 matrix.

2/1/2015

2

Incidence Matrix

Consider a graph 𝐺 = 𝑉, 𝐸 .

β—¦ We order the vertices as 𝑉 = 𝑣1, 𝑣2, … , 𝑣𝑛 and the edges as 𝐸 = 𝑒1, 𝑒2, … , π‘’π‘š

β—¦ The incidence matrix of 𝐺 is an 𝑛 Γ—π‘š matrix 𝑀. The cell 𝑀𝑖𝑗 contains 1 if 𝑣𝑖 is an endpoint

of 𝑒𝑗, and 0 otherwise.

𝑣1

𝑣2

𝑣3 𝑣4

𝑣5 1 0 00 1 10 0 0

0 0 00 0 10 1 1

0 0 01 1 1

1 1 01 0 0

𝑒1 𝑒2

𝑒3 𝑒4 𝑒5 𝑒6

Playing with an Incidence Matrix

Let 𝑀 be the incidence matrix of a graph 𝐺 = 𝑉, 𝐸 , and let 𝐡 = 𝑀𝑀𝑇 .

β—¦ What is the value of 𝐡𝑖𝑖? The degree of 𝑣𝑖 in 𝐺.

β—¦ What is the value of 𝐡𝑖𝑗 for 𝑖 β‰  𝑗? The number

of edges between 𝑣𝑖 and 𝑣𝑗.

𝑣1

𝑣2

𝑣3 𝑣4

𝑣5 𝑀 =

1 0 00 1 10 0 0

0 0 00 0 10 1 1

0 0 01 1 1

1 1 01 0 0

𝑒1 𝑒2

𝑒3 𝑒4 𝑒5 𝑒6

Page 3: Ma/CS 6bmath.caltech.edu/~2014-15/2term/ma006b/12 Graphs...Β Β· incidence matrix 𝑀 and adjacency matrix ࣡. Express 𝑀𝑀𝑇 using ࣡. Answer. This is a 𝑉×𝑉 matrix.

2/1/2015

3

Adjacency Matrix

Consider a graph 𝐺 = 𝑉, 𝐸 .

β—¦ We order the vertices as 𝑉 = 𝑣1, 𝑣2, … , 𝑣𝑛 .

β—¦ The adjacency matrix of 𝐺 is a symmetric 𝑛 Γ— 𝑛 matrix 𝐴. The cell 𝐴𝑖𝑗 contains the

number of edges between 𝑣𝑖 and 𝑣𝑗.

𝑣1

𝑣2

𝑣3 𝑣4

𝑣5 𝐴 =

010

101

0 0 11 0 30 1 0

0 0 1 0 11 3 0 1 0

A Connection

Problem. Let 𝐺 = 𝑉, 𝐸 be a graph with incidence matrix 𝑀 and adjacency matrix 𝐴. Express 𝑀𝑀𝑇 using 𝐴.

Answer. This is a 𝑉 Γ— 𝑉 matrix.

β—¦ 𝑀𝑀𝑇 𝑖𝑖 is the degree of 𝑣𝑖.

β—¦ 𝑀𝑀𝑇 𝑖𝑗 for 𝑖 β‰  𝑗 is number of edges

between 𝑣𝑖 and 𝑣𝑗.

β—¦ Let 𝐷 be a diagonal matrix with 𝐷𝑖𝑖 being the degree of 𝑣𝑖. We have

𝑀𝑀𝑇 = 𝐴 + 𝐷.

Page 4: Ma/CS 6bmath.caltech.edu/~2014-15/2term/ma006b/12 Graphs...Β Β· incidence matrix 𝑀 and adjacency matrix ࣡. Express 𝑀𝑀𝑇 using ࣡. Answer. This is a 𝑉×𝑉 matrix.

2/1/2015

4

Example

𝑣2 𝑣3

𝑣1 𝐴 =0 1 11 0 31 3 0

𝐷 =2 0 00 4 00 0 4

𝑀 =1 1 0 0 01 0 1 1 10 1 1 1 1

𝑀𝑀𝑇 =2 1 11 4 31 3 4

= 𝐴 + 𝐷

Multiplying by 1’s

Let 1𝑛 denote the 𝑛 Γ— 𝑛 matrix with 1 in each of its cells.

Problem. Let 𝐺 = 𝑉, 𝐸 be a graph with adjacency matrix 𝐴. Describe the values in the cells of 𝐡 = 𝐴1 𝑉 .

Answer. It is a 𝑉 Γ— 𝑉 matrix.

β—¦ The column vectors of 𝐡 are identical.

β—¦ The 𝑖’th element of each column is the degree of 𝑣𝑖.

Page 5: Ma/CS 6bmath.caltech.edu/~2014-15/2term/ma006b/12 Graphs...Β Β· incidence matrix 𝑀 and adjacency matrix ࣡. Express 𝑀𝑀𝑇 using ࣡. Answer. This is a 𝑉×𝑉 matrix.

2/1/2015

5

Playing with an Adjacency Matrix

Let 𝐴 be the adjacency matrix of a simple graph 𝐺 = 𝑉, 𝐸 , and let 𝑀 = 𝐴2.

β—¦ What is the value of 𝑀𝑖𝑖? The degree of 𝑣𝑖 in 𝐺.

β—¦ What is the value of 𝑀𝑖𝑗 for 𝑖 β‰  𝑗? The number

of vertices that are adjacent to both 𝑣𝑖 and 𝑣𝑗.

𝑣1

𝑣2

𝑣3 𝑣4

𝑣5 𝐴 =

010

101

0 0 11 0 10 1 0

0 0 1 0 11 1 0 1 0

What about 𝑀3?

Let 𝐴 be the adjacency matrix of a simple graph 𝐺 = 𝑉, 𝐸 . For 𝑖 β‰  𝑗:

β—¦ 𝐴𝑖𝑗 tells us if there is an edge 𝑣𝑖 , 𝑣𝑗 ∈ 𝐸.

β—¦ 𝐴2 𝑖𝑗 tells us how many vertices are adjacent

to both 𝑣𝑖 and 𝑣𝑗.

β—¦ What is 𝐴3 𝑖𝑗? It is the number of paths of

length three between 𝑣𝑖 and 𝑣𝑗.

β—¦ In fact, 𝐴2 𝑖𝑗 is the number of paths of

length two between 𝑣𝑖 and 𝑣𝑗.

Page 6: Ma/CS 6bmath.caltech.edu/~2014-15/2term/ma006b/12 Graphs...Β Β· incidence matrix 𝑀 and adjacency matrix ࣡. Express 𝑀𝑀𝑇 using ࣡. Answer. This is a 𝑉×𝑉 matrix.

2/1/2015

6

The Meaning of π‘€π‘˜

Theorem. Let 𝐴 be the adjacency matrix of a (not necessarily simple) graph

𝐺 = 𝑉, 𝐸 . Then π΄π‘˜π‘–π‘—

is the number of

(not necessarily simple) paths between 𝑣𝑖 and 𝑣𝑗.

An Example

𝑐 𝑑

𝑏 π‘Ž

𝐴 =

0 1 0 11 0 1 001100 11 0

𝐴2 = 𝐴𝐴 =

0 1 0 11 0 1 001100 11 0

0 1 0 11 0 1 001100 11 0

=

2 0 2 00 2 0 220022 00 2

𝐴3 = 𝐴2𝐴 =

2 0 2 00 2 0 220022 00 2

0 1 0 11 0 1 001100 11 0

=

0 4 0 44 0 4 004400 44 0

Page 7: Ma/CS 6bmath.caltech.edu/~2014-15/2term/ma006b/12 Graphs...Β Β· incidence matrix 𝑀 and adjacency matrix ࣡. Express 𝑀𝑀𝑇 using ࣡. Answer. This is a 𝑉×𝑉 matrix.

2/1/2015

7

The Meaning of π‘€π‘˜

Theorem. Let 𝑀 be the incidence matrix of a (not necessarily simple) graph

𝐺 = 𝑉, 𝐸 . Then π‘€π‘˜π‘–π‘—

is the number

of (not necessarily simple) paths between 𝑣𝑖 and 𝑣𝑗.

Proof. By induction on π‘˜.

β—¦ Induction basis. Easy to see for π‘˜ = 1 and π‘˜ = 2.

The Induction Step

We have π΄π‘˜ = π΄π‘˜βˆ’1𝐴. That is

π΄π‘˜π‘–π‘—= π΄π‘˜βˆ’1

π‘–π‘šπ΄π‘šπ‘—

|𝑉|

π‘š=1

β—¦ By the induction hypothesis, π΄π‘˜βˆ’1π‘–π‘š

is the

number of paths of length π‘˜ βˆ’ 1 between 𝑣𝑖 and π‘£π‘š.

β—¦ π΄π‘šπ‘— is the number of edges between π‘£π‘š, 𝑣𝑗.

Page 8: Ma/CS 6bmath.caltech.edu/~2014-15/2term/ma006b/12 Graphs...Β Β· incidence matrix 𝑀 and adjacency matrix ࣡. Express 𝑀𝑀𝑇 using ࣡. Answer. This is a 𝑉×𝑉 matrix.

2/1/2015

8

The Induction Step (cont.)

π΄π‘˜π‘–π‘—= π΄π‘˜βˆ’1

π‘–π‘šπ΄π‘šπ‘—

|𝑉|

π‘š=1

For a fixed π‘š, π΄π‘˜βˆ’1π‘–π‘šπ΄π‘šπ‘— is the

number of paths from 𝑣𝑖 to 𝑣𝑗 of length π‘˜

with π‘£π‘š as their penultimate vertex.

β—¦ Thus, summing over every 1 ≀ π‘š ≀ |𝑉| results in the number of paths from 𝑣𝑖 to 𝑣𝑗 of

length π‘˜.

Which Classic English Rock Band is More Sciency?

Page 9: Ma/CS 6bmath.caltech.edu/~2014-15/2term/ma006b/12 Graphs...Β Β· incidence matrix 𝑀 and adjacency matrix ࣡. Express 𝑀𝑀𝑇 using ࣡. Answer. This is a 𝑉×𝑉 matrix.

2/1/2015

9

Computing the Number of Paths of Length π‘˜ Problem. Consider a graph 𝐺 = 𝑉, 𝐸 ,

two vertices 𝑠, 𝑑 ∈ 𝑉, and an integer π‘˜ > 0. Describe an algorithm for finding the number of paths of length π‘˜ between 𝑠 and 𝑑.

β—¦ Let 𝐴 be the adjacency matrix of 𝐺.

β—¦ We need to compute π΄π‘˜, which involves π‘˜ βˆ’ 1 matrix multiplication.

Matrix Multiplication: A Brief History

We wish to multiply two 𝑛 Γ— 𝑛 matrices.

β—¦ Computing one cell requires about 𝑛 multiplications and additions. So computing an entire matrix takes 𝑐𝑛3 (for some constant 𝑐).

β—¦ In 1969 Strassen found an improved algorithm with a running time of 𝑐𝑛2.807.

β—¦ In 1987, Coppersmith and Winograd obtained an improved 𝑐𝑛2.376.

β—¦ After over 20 years, in 2010, Stothers obtained 𝑐𝑛2.374.

β—¦ Williams immediately improved to 𝑐𝑛2.3728642.

β—¦ In 2014, Le Gall improved to 𝑐𝑛2.3728639.

Page 10: Ma/CS 6bmath.caltech.edu/~2014-15/2term/ma006b/12 Graphs...Β Β· incidence matrix 𝑀 and adjacency matrix ࣡. Express 𝑀𝑀𝑇 using ࣡. Answer. This is a 𝑉×𝑉 matrix.

2/1/2015

10

A More Efficient Algorithm

To compute π΄π‘˜, we do not need π‘˜ βˆ’ 1 matrix multiplications.

If π‘˜ is a power of 2:

β—¦ 𝐴2 = 𝐴𝐴, 𝐴4 = 𝐴2𝐴2, …, π΄π‘˜ = π΄π‘˜/2π΄π‘˜/2.

β—¦ Only log2 π‘˜ multiplications!

If π‘˜ is not a power of 2:

β—¦ We again compute 𝐴, 𝐴2, 𝐴4, … , 𝐴 π‘˜ .

β—¦ We can then obtain π΄π‘˜ by multiplying a subset of those.

β—¦ For example, 𝐴57 = 𝐴32𝐴16𝐴8𝐴.

β—¦ At most 2log π‘˜ βˆ’ 1 multiplications!

Connectivity and Matrices

Problem. Let 𝐺 = 𝑉, 𝐸 be a graph with an adjacency matrix 𝐴. Use 𝑀 = 𝐼 + 𝐴

+ 𝐴2 + 𝐴3 +β‹―+ 𝐴 𝑉 βˆ’1 to tell whether 𝐺 is connected.

Answer.

β—¦ 𝐺 is connected iff every cell of 𝐴 is positive.

β—¦ The main diagonal of 𝐴 is positive due to 𝐼.

β—¦ A cell 𝐴𝑖𝑗 for 𝑖 β‰  𝑗 contains the number of

paths between 𝑣𝑖 and 𝑣𝑗 of length at most

π‘˜ βˆ’ 1. If the graph is connected, such paths exist between every two vertices.

Page 11: Ma/CS 6bmath.caltech.edu/~2014-15/2term/ma006b/12 Graphs...Β Β· incidence matrix 𝑀 and adjacency matrix ࣡. Express 𝑀𝑀𝑇 using ࣡. Answer. This is a 𝑉×𝑉 matrix.

2/1/2015

11

And Now with Colors

Problem. Consider a graph 𝐺 = 𝑉, 𝐸 , two vertices 𝑠, 𝑑 ∈ 𝑉, and an integer π‘˜ > 0. Moreover, every edge is colored either red or blue. Describe an algorithm for finding the number of paths of length π‘˜ between 𝑠 and 𝑑 that have an even number of blue edges.

Solution

We define two sets of matrices:

β—¦ Cell 𝑖𝑗 of the matrix 𝐸 π‘š contains the number of paths of length π‘š between 𝑣𝑖 an 𝑣𝑗 using an even number of blue edges.

β—¦ Cell 𝑖𝑗 of the matrix 𝑂 π‘š contains the number of paths of length π‘š between 𝑣𝑖 an 𝑣𝑗 using an odd number of blue edges.

What are 𝐸 1 and 𝑂(1)?

β—¦ 𝐸 1 is the adjacency matrix of 𝐺 after

removing the blue edges. Similarly for 𝑂 1 after removing the red edges.

Page 12: Ma/CS 6bmath.caltech.edu/~2014-15/2term/ma006b/12 Graphs...Β Β· incidence matrix 𝑀 and adjacency matrix ࣡. Express 𝑀𝑀𝑇 using ࣡. Answer. This is a 𝑉×𝑉 matrix.

2/1/2015

12

Solution (cont.)

We wish to compute 𝐸 π‘˜ .

β—¦ We know how to find 𝐸 1 and 𝑂 1 .

β—¦ How can we compute 𝐸 π‘š and 𝑂(π‘š) using

𝐸 π‘šβˆ’1 and 𝑂(π‘šβˆ’1)?

𝐸 π‘š = 𝐸 π‘šβˆ’1 𝐸 1 + 𝑂(π‘šβˆ’1)𝑂(1).

β—¦ 𝐸 π‘šβˆ’1 𝐸 1𝑖𝑗

is the number of paths of

length π‘š between 𝑣𝑖 and 𝑣𝑗 with an even

number of blue edges and whose last edge is red.

β—¦ Similarly for 𝑂 π‘šβˆ’1 𝑂 1𝑖𝑗

except that the

last edges of the paths is blue.

Solution (cont.)

How can we similarly compute 𝑂 π‘š using

𝐸 π‘šβˆ’1 and 𝑂 π‘šβˆ’1 ? 𝑂 π‘š = 𝐸 π‘šβˆ’1 𝑂 1 + 𝑂(π‘šβˆ’1)𝐸(1).

Concluding the solution.

β—¦ We compute about 2π‘˜ matrices.

β—¦ Each computation involves two matrix multiplications and one addition.

Page 13: Ma/CS 6bmath.caltech.edu/~2014-15/2term/ma006b/12 Graphs...Β Β· incidence matrix 𝑀 and adjacency matrix ࣡. Express 𝑀𝑀𝑇 using ࣡. Answer. This is a 𝑉×𝑉 matrix.

2/1/2015

13

Identical Graphs?

Are the following two graphs identical?

Possible answers:

β—¦ No, since in one 𝑣1 has degree 2 and in the other degree 1.

β—¦ Yes, because they have exactly the same structure.

𝑣1

𝑣2 𝑣3

𝑣2

𝑣1 𝑣3

Graph Isomorphism

An isomorphism from a graph 𝐺 = 𝑉, 𝐸 to a graph 𝐺′ = 𝑉′, 𝐸′ is a bijection 𝑓: 𝑉 β†’ 𝑉′ such that for every 𝑒, 𝑣 ∈ 𝑉, 𝐸 has the same number of edges between 𝑒 and 𝑣 as 𝐸′ has between 𝑓 𝑒 and 𝑓 𝑣 .

β—¦ We say that 𝐺 and 𝐺′ are isomorphic if there is an isomorphism from 𝐺 to 𝐺′.

β—¦ In our example, the graphs are isomorphic (𝑓 𝑣1 = 𝑒2, 𝑓 𝑣2 = 𝑒1, 𝑓 𝑣3 = 𝑒3).

𝑣1

𝑣2 𝑣3

𝑒2

𝑒1 𝑒3

Page 14: Ma/CS 6bmath.caltech.edu/~2014-15/2term/ma006b/12 Graphs...Β Β· incidence matrix 𝑀 and adjacency matrix ࣡. Express 𝑀𝑀𝑇 using ࣡. Answer. This is a 𝑉×𝑉 matrix.

2/1/2015

14

Uniqueness?

Question. If two graphs are isomorphic, can there be more than one isomorphism from one to the other?

β—¦ Yes!

β—¦ π‘Ž β†’ 𝑀, 𝑏 β†’ 𝑧, 𝑐 β†’ π‘₯, 𝑑 β†’ 𝑦.

β—¦ π‘Ž β†’ 𝑦, 𝑏 β†’ 𝑧, 𝑐 β†’ π‘₯, 𝑑 β†’ 𝑀.

π‘Ž 𝑏

𝑐 𝑑

𝑀 π‘₯

𝑦 𝑧

Isomorphisms and Adjacency Matrices What can we

say about adjacency matrices of isomorphic graphs?

π‘Ž 𝑏

𝑐 𝑑

𝑀 π‘₯

𝑦 𝑧

0 1 0 01 0 1 000100 11 0

0 0 0 10 0 1 101110 00 0

Page 15: Ma/CS 6bmath.caltech.edu/~2014-15/2term/ma006b/12 Graphs...Β Β· incidence matrix 𝑀 and adjacency matrix ࣡. Express 𝑀𝑀𝑇 using ࣡. Answer. This is a 𝑉×𝑉 matrix.

2/1/2015

15

Answer

Two graphs 𝐺, 𝐺′ are isomorphic if and only if the adjacency matrix of 𝐺 is obtained by permuting the rows and columns of the adjacency matrix of 𝐺′.

β—¦ (The same permutation should apply both to the rows and to the column).

β—¦ 1 β†’ 1, 2 β†’ 4, 4 β†’ 3, 3 β†’ 2:

0 1 0 01 0 1 000100 11 0

0 0 0 10 0 1 101110 00 0

Incidence Matrix of a Directed Graph Consider a directed graph 𝐺 = 𝑉, 𝐸 .

β—¦ We order the vertices as 𝑉 = 𝑣1, 𝑣2, … , 𝑣𝑛 and the edges as 𝐸 = 𝑒1, 𝑒2, … , π‘’π‘š

β—¦ The incidence matrix of 𝐺 is an 𝑛 Γ—π‘š matrix 𝑀. The cell 𝑀𝑖𝑗 contains -1 if 𝑒𝑗 is entering 𝑣𝑖,

and 1 if 𝑒𝑗 is leaving 𝑣𝑖.

𝑣1

𝑣2

𝑣3 𝑣4

𝑣5 βˆ’1 0 00 βˆ’1 βˆ’10 0 0

0 0 00 0 βˆ’10 βˆ’1 1

0 0 01 1 1

βˆ’1 1 01 0 0

𝑒1 𝑒2

𝑒3 𝑒4 𝑒5 𝑒6

Page 16: Ma/CS 6bmath.caltech.edu/~2014-15/2term/ma006b/12 Graphs...Β Β· incidence matrix 𝑀 and adjacency matrix ࣡. Express 𝑀𝑀𝑇 using ࣡. Answer. This is a 𝑉×𝑉 matrix.

2/1/2015

16

The End: Queen

Ph.D. in astrophysics

Freddie Mercury

Electronics engineer

Degree in Biology