None graphs_casper

Exercises on Graphs

In [2]:
import matplotlib.pyplot as plt
import numpy as np
import networkx as nx


# Functions
def permute(M, P):
    return P@(P@M).transpose()

def make_perm_matrix(l):
    P = np.zeros((len(l),len(l)))
    for i,j in enumerate(l):
        P[i,j] = 1
        P[j,i] = 1
    return P

def drawmatrix(M):
    plt.figure(np.random.randint(20000))
    nx.draw(nx.Graph(M))

REMEMBER: DO NOT SOLVE PROBLEMS FOR THE ASSIGNMENT - AUTOMATICALLY CHECKING THE NUMBER OF AUTOMORPHISMS IS THE ASSIGNMENT!

Exercise 1

Let P1 and P2 be two permutation matrices. Is P1 × P2 also a permutation matrix? Argue for or against your answer.

In [18]:
# Example:

A = np.array([[0,0,0,1],[0,1,0,0],[0,0,1,0],[1,0,0,0]])
B = np.array([[1,0,0,0],[0,0,1,0],[0,1,0,0],[0,0,0,1]])

A@B
Out[18]:
array([[0, 0, 0, 1],
       [0, 0, 1, 0],
       [0, 1, 0, 0],
       [1, 0, 0, 0]])

In general, yes. Consider P1 as an adjacency matrix of a graph instead. This graph has the property that every vertex has in- and out- degree exactly 1 (Permutation matrices have one '1' in each row and column). P2 now permutes this graph, but it still satisfies the same condition.

Corresponds to composing the permutations.

Exercise 2

  1. Draw the graphs A and B
In [19]:
A = np.array([[0,1,1,1],[1,0,1,0],[1,1,0,1],[1,0,1,0]])
B = np.array([[0,1,1,0],[1,0,1,1],[1,1,0,1],[0,1,1,0]])

# Draw
plt.figure(1)

nx.draw(nx.Graph(A))

plt.figure(2)
nx.draw(nx.Graph(B))
  1. Are the two graphs isomorphic?

Yes, the graphs are clearly isomorphic

  1. How many different representations (in terms of adjacency matrices) of

GA are there?

All four nodes can be permuted, so 4! = 12

  1. How many different representations (in terms of adjacency matrices) of

GB are there?

Ditto, 4! = 12

  1. Is there a permutation matrix P s.t. A = P(PB)^T holds?

Yes, as they are clearly isomorphic

  1. If so, give all matrices P s.t. A = P(PB)^T Hold
In [20]:
# Brute-force check all permutations of the two deg2 nodes and deg3 nodes (only 4 total)
print(make_perm_matrix([0,1,2,3])) #Trivial
print(make_perm_matrix([2,1,0,3]))
print(make_perm_matrix([0,3,2,1]))
print(make_perm_matrix([2,3,0,1]))

print('Permutation test')

print(permute( np.array([[1,2],[3,4]]), np.array([[0,1],[1,0]]) ))

#etc
[[1. 0. 0. 0.]
 [0. 1. 0. 0.]
 [0. 0. 1. 0.]
 [0. 0. 0. 1.]]
[[0. 0. 1. 0.]
 [0. 1. 0. 0.]
 [1. 0. 0. 0.]
 [0. 0. 0. 1.]]
[[1. 0. 0. 0.]
 [0. 0. 0. 1.]
 [0. 0. 1. 0.]
 [0. 1. 0. 0.]]
[[0. 0. 1. 0.]
 [0. 0. 0. 1.]
 [1. 0. 0. 0.]
 [0. 1. 0. 0.]]
Permutation test
[[4 2]
 [3 1]]

Exercise 3

  1. Give an adjacency matrix A for the graph
In [21]:
A = np.array([[0,1,1],[1,0,1],[1,1,0]])
drawmatrix(A)

This is the only one!

  1. For your chosen adjacency matrix, how many permutation matrices P are there s.t. A = P(PA)^T holds? (The size of the automorphism group)

All possible permutation matrices hold, as the graph is completely symmetric, 3! = 6

Exercise 4

  1. Give adjacency matrix A for the graph
In [22]:
A = np.array([[0,1,1,0,0],[0,0,1,1,0],[0,1,0,1,0],[0,1,1,0,1],[0,0,0,1,0]])
drawmatrix(A)
  1. For your chosen adjacency matrix, how many permutation matrices P are there s.t. A = P(PA)^T holds?

Two different, vertices 1, 2 can be permuted

Exercise 5

  1. Given the following tow graphs G_a and G_b, give adjacency matrices
In [23]:
GA = np.array([[0,1,1,0],[1,0,1,0],[1,1,0,1],[0,0,1,0]])
drawmatrix(GA)

GB = np.array([[0,1,0],[1,0,1],[0,1,0]])
drawmatrix(GB)
  1. Is G_B a subgraph of G_A?

Yes, clearly (both induced and not)

  1. How many ways are there to find G_B as a subgraph of G_A?

Need to make n(A) x n(B) matrix as a 'permutation matrix'

At least 4

Exercise 6

The following is from the unit-testing of the graph theory assignment. Explain the expected result 10.

In [24]:
A = np.array([[ 0, 1, 0, 0, 1], \
[ 1, 0, 1, 0, 0], \
[ 0, 1, 0, 1, 0], \
[ 0, 0, 1, 0, 1], \
[ 1, 0, 0, 1, 0]])

drawmatrix(A)

Number of isomorphisms from/to itself.

10 automorphisms:

  • identity + 4 'rotations'
  • invert direction + 4 'rotations'

Exercise 7

Use sigma aldrich https://www.sigmaaldrich.com/DK/en/structure-search to look for chemical structures. How many structures can you find which have the following as a substructure?

Good example of application - subgraph of LSD

https://www.sigmaaldrich.com/DK/en/product/sigma/l7007

A = np.array([[0,0,]])

In [25]:
A = np.array([[0,0,1,1,1],[0,0,0,1,1],[1,0,0,0,0],[1,1,0,0,1],[1,1,0,1,0]])

P = make_perm_matrix([0,1,2,4,3])

print(P)

drawmatrix(A)

B = P@(P@A).T
print(A)
print(B)
drawmatrix(B)
[[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 1.]
 [0. 0. 0. 1. 0.]]
[[0 0 1 1 1]
 [0 0 0 1 1]
 [1 0 0 0 0]
 [1 1 0 0 1]
 [1 1 0 1 0]]
[[0. 0. 1. 1. 1.]
 [0. 0. 0. 1. 1.]
 [1. 0. 0. 0. 0.]
 [1. 1. 0. 0. 1.]
 [1. 1. 0. 1. 0.]]
In [26]:
A = np.array([[ 0, 1, 0, 0, 1], \
[ 1, 0, 1, 0, 0], \
[ 0, 1, 0, 1, 0], \
[ 0, 0, 1, 0, 1], \
[ 1, 0, 0, 1, 0]])

drawmatrix(A)