-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuntington.py
153 lines (124 loc) · 4.33 KB
/
huntington.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# The following is a transcription of Dr. Michael Huntington's code from his doctoral dissertation
# As a result, the following code is Copyright Jake Billings 2017 All Rights Reserved.
# Unless Dr. Michael Huntington, the original author of the MAGMA implementation claims it. In this case,
# the code is copyright Dr. Michael Huntington 2017.
import numpy
import networkx as nx
import graph
# Throughout the code it becomes necessary to make sure that the graphs we are working with do not have 3, 4 or
# 6-cycles. The following three programs do this by looking at various powers of the adjacency matrix.
# From Huntington's dissertation
#
# x A networkx graph
def c3free(x):
m = nx.to_numpy_matrix(x)
squared = numpy.dot(m, m)
cubed = numpy.dot(squared, m)
diagonal = numpy.diagonal(cubed)
return numpy.sum(diagonal)
# Throughout the code it becomes necessary to make sure that the graphs we are working with do not have 3, 4 or
# 6-cycles. The following three programs do this by looking at various powers of the adjacency matrix.
# From Huntington's dissertation
#
# x A networkx graph
def c4free(x):
m = nx.to_numpy_matrix(x)
m1 = numpy.dot(m, m)
for i in range(1, len(m1) - 1):
for j in (i + 1, len(m1[i]) - 1):
if m1[i, j] > 1:
return m1[i, j]
return 0
# Throughout the code it becomes necessary to make sure that the graphs we are working with do not have 3, 4 or
# 6-cycles. The following three programs do this by looking at various powers of the adjacency matrix.
# From Huntington's dissertation
#
# x A networkx graph
def c6free(x):
m = nx.to_numpy_matrix(x)
squared = numpy.dot(m, m)
m1 = numpy.dot(squared, m)
for i in range(1, len(m1) - 1):
for j in (i + 1, len(m1[i]) - 1):
if m1[i, j] > 1 and m[i, j] == 0:
return m1[i, j]
return 0
# This function calls the three previous functions and tests to see if any 3, 4 or 6-cycles are present, it returns true
# if none of these cycles are present and false otherwise.
# From Huntington's dissertation
#
# G A networkx graph
def check(G):
s = c3free(G)
t = c4free(G)
u = c6free(G)
if s+t+u == 0:
return True
else:
return False
# Returns the size of the graph in the set of graphs L with the greatest number of edges
#
# "Given a list of graphs this next function returns the largest number of edges of the given
# graphs. Often in our search we would find suboptimal graphs and needed a way to discard these, this showed which
# graphs we should keep."
# From Huntington's dissertation
#
# L A list of networkx graphs
def maxedges(L):
H = []
for x in L:
s = x.number_of_edges()
exists_greater = False
for y in H:
if y > s:
exists_greater = True
if not exists_greater:
H.append(s)
return H[len(H)-1].number_of_edges()
# Returns a filtered version of the list of graphs L in which only a single version of each
# isomorphic graph remains.
#
# "Often given a list of graphs many would be isomorphic. This function shortens the list by keeping only one isomorphic
# copy of each graph."
# From Huntington's dissertation
#
# L A list of networkx graphs
def iso(L):
H = []
for x in L:
found_iso = False
for g in H:
if nx.is_isomorphic(x, g):
found_iso = True
if not found_iso:
H.append(x)
return H
# Returns a filtered version of a list of graphs including only graphs of the specified size e
#
# "This next function takes a list of graphs and returns a new list of graphs of the given size."
# From Huntington's dissertation
#
# T A list of networkx graphs
# e A whole number representing the desired number of edges in the returned list L
def extgraph(T, e):
L = []
for x in T:
if x.number_of_edges() == e:
L.append(x)
return L
if __name__ == "__main__":
# Create a tree.
G = graph.treex(3, 2)
# This edge creates a 3-cycle. Commenting it out should cause this program to print True
G.add_edge(2, 7)
# Create a tree.
G2 = graph.treex(4, 2)
G3 = graph.treex(1, 4)
L = [G, G2, G3]
print G.number_of_edges()
print G2.number_of_edges()
print G3.number_of_edges()
print maxedges(L)
print extgraph(L, 13)
# Should print False
print check(G)