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.

Ma/CS 6b2014-15/2term/ma006b/12 Graphs and... · 2015-02-01 · 2/1/2015 1 Ma/CS 6b Class 12: Graphs and Matrices By Adam Sheffer तഇ तഈ तഊ तഉ तഋ Մ Յ Մ Յ Մ

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Ma/CS 6b2014-15/2term/ma006b/12 Graphs and... · 2015-02-01 · 2/1/2015 1 Ma/CS 6b Class 12: Graphs and Matrices By Adam Sheffer तഇ तഈ तഊ तഉ तഋ Մ Յ Մ Յ Մ

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 6b2014-15/2term/ma006b/12 Graphs and... · 2015-02-01 · 2/1/2015 1 Ma/CS 6b Class 12: Graphs and Matrices By Adam Sheffer तഇ तഈ तഊ तഉ तഋ Մ Յ Մ Յ Մ

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 6b2014-15/2term/ma006b/12 Graphs and... · 2015-02-01 · 2/1/2015 1 Ma/CS 6b Class 12: Graphs and Matrices By Adam Sheffer तഇ तഈ तഊ तഉ तഋ Մ Յ Մ Յ Մ

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 6b2014-15/2term/ma006b/12 Graphs and... · 2015-02-01 · 2/1/2015 1 Ma/CS 6b Class 12: Graphs and Matrices By Adam Sheffer तഇ तഈ तഊ तഉ तഋ Մ Յ Մ Յ Մ

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 6b2014-15/2term/ma006b/12 Graphs and... · 2015-02-01 · 2/1/2015 1 Ma/CS 6b Class 12: Graphs and Matrices By Adam Sheffer तഇ तഈ तഊ तഉ तഋ Մ Յ Մ Յ Մ

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 6b2014-15/2term/ma006b/12 Graphs and... · 2015-02-01 · 2/1/2015 1 Ma/CS 6b Class 12: Graphs and Matrices By Adam Sheffer तഇ तഈ तഊ तഉ तഋ Մ Յ Մ Յ Մ

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 6b2014-15/2term/ma006b/12 Graphs and... · 2015-02-01 · 2/1/2015 1 Ma/CS 6b Class 12: Graphs and Matrices By Adam Sheffer तഇ तഈ तഊ तഉ तഋ Մ Յ Մ Յ Մ

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 6b2014-15/2term/ma006b/12 Graphs and... · 2015-02-01 · 2/1/2015 1 Ma/CS 6b Class 12: Graphs and Matrices By Adam Sheffer तഇ तഈ तഊ तഉ तഋ Մ Յ Մ Յ Մ

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 6b2014-15/2term/ma006b/12 Graphs and... · 2015-02-01 · 2/1/2015 1 Ma/CS 6b Class 12: Graphs and Matrices By Adam Sheffer तഇ तഈ तഊ तഉ तഋ Մ Յ Մ Յ Մ

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 6b2014-15/2term/ma006b/12 Graphs and... · 2015-02-01 · 2/1/2015 1 Ma/CS 6b Class 12: Graphs and Matrices By Adam Sheffer तഇ तഈ तഊ तഉ तഋ Մ Յ Մ Յ Մ

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 6b2014-15/2term/ma006b/12 Graphs and... · 2015-02-01 · 2/1/2015 1 Ma/CS 6b Class 12: Graphs and Matrices By Adam Sheffer तഇ तഈ तഊ तഉ तഋ Մ Յ Մ Յ Մ

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 6b2014-15/2term/ma006b/12 Graphs and... · 2015-02-01 · 2/1/2015 1 Ma/CS 6b Class 12: Graphs and Matrices By Adam Sheffer तഇ तഈ तഊ तഉ तഋ Մ Յ Մ Յ Մ

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 6b2014-15/2term/ma006b/12 Graphs and... · 2015-02-01 · 2/1/2015 1 Ma/CS 6b Class 12: Graphs and Matrices By Adam Sheffer तഇ तഈ तഊ तഉ तഋ Մ Յ Մ Յ Մ

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 6b2014-15/2term/ma006b/12 Graphs and... · 2015-02-01 · 2/1/2015 1 Ma/CS 6b Class 12: Graphs and Matrices By Adam Sheffer तഇ तഈ तഊ तഉ तഋ Մ Յ Մ Յ Մ

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 6b2014-15/2term/ma006b/12 Graphs and... · 2015-02-01 · 2/1/2015 1 Ma/CS 6b Class 12: Graphs and Matrices By Adam Sheffer तഇ तഈ तഊ तഉ तഋ Մ Յ Մ Յ Մ

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 6b2014-15/2term/ma006b/12 Graphs and... · 2015-02-01 · 2/1/2015 1 Ma/CS 6b Class 12: Graphs and Matrices By Adam Sheffer तഇ तഈ तഊ तഉ तഋ Մ Յ Մ Յ Մ

2/1/2015

16

The End: Queen

Ph.D. in astrophysics

Freddie Mercury

Electronics engineer

Degree in Biology