forked from oscar-system/Oscar.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.jl
More file actions
204 lines (170 loc) · 6.02 KB
/
Graph.jl
File metadata and controls
204 lines (170 loc) · 6.02 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
@testset "Graphs" begin
@testset "core functionality" begin
g = Graph{Directed}(5)
@test n_vertices(g) == 5
@test n_edges(g) == 0
add_edge!(g, 1, 2)
@test n_edges(g) == 1
@test has_edge(g, 1, 2)
rem_edge!(g, 1, 2)
@test n_edges(g) == 0
@test !has_edge(g, 1, 2)
@test add_vertex!(g)
@test n_vertices(g) == 6
@test has_vertex(g, 6)
rem_vertex!(g, 1)
@test n_vertices(g) == 5
@test has_vertex(g, 1)
@test !has_vertex(g, 6)
@test add_vertices!(g, 5) == 5
@test n_vertices(g) == 10
@test rem_vertices!(g, [2, 4, 6, 11])
@test n_vertices(g) == 7
g = Graph{Directed}(4)
add_edge!(g, 1, 2)
add_edge!(g, 2, 3)
add_edge!(g, 3, 1)
@test signed_incidence_matrix(g) == Matrix([-1 0 1; 1 -1 0; 0 1 -1; 0 0 0])
e = Edge(1,2)
@test 1 in e
@test 2 in e
@test !(3 in e)
end
triangle = simplex(2)
c = cube(3)
cr = cross_polytope(3)
pos = convex_hull([0 0 0; 1 0 0], [0 1 0; 0 0 1])
pl = convex_hull([0 0 0; 1 0 0], nothing, [0 1 1])
egtriangle = vertex_edge_graph(triangle)
dgtriangle = dual_graph(triangle)
egcube = vertex_edge_graph(c)
dgcube = dual_graph(c)
egcr = vertex_edge_graph(cr)
egpos = vertex_edge_graph(pos)
egpl = vertex_edge_graph(pl)
egplc = vertex_edge_graph(pl, modulo_lineality=true)
@testset "graphs from polytopes" begin
@test n_vertices(egtriangle) == 3
@test n_edges(egtriangle) == 3
@test n_vertices(dgtriangle) == 3
@test n_edges(dgtriangle) == 3
@test n_vertices(egcube) == 8
@test n_edges(egcube) == 12
@test n_vertices(dgcube) == 6
@test n_edges(dgcube) == 12
@test is_isomorphic(dgtriangle, egtriangle)
@test is_isomorphic(egcr, dgcube)
@test !is_isomorphic(egcr, egcube)
# unbounded examples
@test n_vertices(egpos) == 2
@test n_edges(egpos) == 1
@test n_vertices(egpl) == 0
@test n_edges(egpl) == 0
@test n_vertices(egplc) == 2
@test n_edges(egplc) == 1
@test incidence_matrix(egtriangle) == incidence_matrix([[1,2],[1,3],[2,3]])
@test is_isomorphic(dual_graph(convex_hull([0 0 0; 1 0 0], nothing, [0 1 0])), Graph{Undirected}(2))
@test is_isomorphic(dual_graph(convex_hull([0 0 0], [0 0 1; 0 1 0; 1 0 0])), complete_graph(3))
g = dual_graph(convex_hull([0 0 0; 1 0 0], [0 0 1; 0 1 0]))
@test n_vertices(g) == 4
@test n_edges(g) == 5
end
@testset "isomorphic" begin
g = Graph{Directed}(5)
add_edge!(g, 1, 2)
add_edge!(g, 2, 3)
add_edge!(g, 3, 1)
gg = Graph{Directed}(5)
add_edge!(gg, 3, 5)
add_edge!(gg, 5, 4)
@test !is_isomorphic(g, gg)
add_edge!(gg, 3, 4)
@test !is_isomorphic(g, gg)
rem_edge!(gg, 3, 4)
add_edge!(gg, 4, 3)
@test is_isomorphic(g, gg)
G = matrix(ZZ, 3, 3, [0,1,0,1,0,1,0,1,0])
J = [2,3,1]
H = G[J,J]
b, I = Oscar._is_equal_up_to_permutation_with_permutation(G, H)
@assert G[I,I] == H
end
@testset "connectivity" begin
g = Graph{Directed}(5)
@test !is_weakly_connected(g)
@test !is_strongly_connected(g)
@test length(weakly_connected_components(g)) == 5
@test length(strongly_connected_components(g)) == 5
add_edge!(g, 1, 2)
add_edge!(g, 2, 3)
add_edge!(g, 3, 1)
add_edge!(g, 3, 4)
add_edge!(g, 5, 3)
@test is_weakly_connected(g)
@test !is_strongly_connected(g)
@test length(weakly_connected_components(g)) == 1
@test length(strongly_connected_components(g)) == 3
add_edge!(g, 4, 5)
@test is_weakly_connected(g)
@test is_strongly_connected(g)
@test length(weakly_connected_components(g)) == 1
@test length(strongly_connected_components(g)) == 1
@test diameter(g) == 4
g = Graph{Undirected}(5)
@test !is_connected(g)
@test connectivity(g) == 0
@test length(connected_components(g)) == 5
add_edge!(g, 1, 2)
add_edge!(g, 2, 3)
add_edge!(g, 1, 3)
add_edge!(g, 4, 5)
@test !is_connected(g)
@test connectivity(g) == 0
@test length(connected_components(g)) == 2
add_edge!(g, 3, 5)
@test is_connected(g)
@test connectivity(g) == 1
@test length(connected_components(g)) == 1
@test diameter(g) == 3
end
@testset "errors" begin
g = Graph{Undirected}(1)
@test !add_edge!(g,1,2)
end
@testset "grap_from_edges" begin
x1 = [[5,6],[7,8],[11,12]]
G1 = graph_from_edges(x1)
@test n_vertices(G1) == 12
@test n_edges(G1) == 3
x2 = [[11,3],[3,5],[4,5],[2,4],[2,3]]
G2 = graph_from_edges(Undirected, x2, 13)
@test n_vertices(G2) == 13
@test n_edges(G2) == 5
ei = edges(G2)
@test length(ei) == 5
ee = collect(ei)
@test length(ei) == 0
@test collect(ei) == Edge[]
GG2 = graph_from_edges(Undirected, ee, 13)
@test is_isomorphic(G2, GG2)
end
@testset "adjacency_matrix laplacian_matrix" begin
G0 = Graph{Directed}(3)
add_edge!(G0,1,2)
add_edge!(G0,1,3)
@test matrix(ZZ, adjacency_matrix(G0)) == matrix(ZZ, [0 1 1; 0 0 0; 0 0 0])
@test laplacian_matrix(G0) == matrix(ZZ, [2 -1 -1; 0 0 0; 0 0 0])
G1 = vertex_edge_graph(cube(2))
@test matrix(ZZ, adjacency_matrix(G1)) == matrix(ZZ, [0 1 1 0; 1 0 0 1; 1 0 0 1; 0 1 1 0])
@test laplacian_matrix(G1) == matrix(ZZ, [2 -1 -1 0; -1 2 0 -1; -1 0 2 -1; 0 -1 -1 2])
end
@testset "is_bipartite" begin
G0 = Graph{Undirected}(3)
add_edge!(G0,1,2)
add_edge!(G0,1,3)
add_edge!(G0,2,3)
@test is_bipartite(G0) == false
G1 = graph_from_edges([[1,2],[2,3],[3,4]])
@test is_bipartite(G1) == true
end
end