Skip to content

Commit

Permalink
Swap row and column labels in triangular and hexagonal lattice constr…
Browse files Browse the repository at this point in the history
…uctors (#38)
  • Loading branch information
mtfishman authored Aug 8, 2023
2 parents c092854 + 193100e commit c4170a3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
34 changes: 17 additions & 17 deletions src/Graphs/generators/namedgraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@ function hexagonal_lattice_graph(m::Int64, n::Int64; periodic=false)
error("Periodic Hexagonal Lattice needs m > 1, n > 1 and n even")
end

G = NamedGraph([(i, j) for i in cols for j in rows])
G = NamedGraph([(j, i) for i in cols for j in rows])

col_edges = [(i, j) => (i, j + 1) for i in cols for j in rows[1:(M + 1)]]
row_edges = [(i, j) => (i + 1, j) for i in cols[1:n] for j in rows if i % 2 == j % 2]
col_edges = [(j, i) => (j + 1, i) for i in cols for j in rows[1:(M + 1)]]
row_edges = [(j, i) => (j, i + 1) for i in cols[1:n] for j in rows if i % 2 == j % 2]
add_edges!(G, col_edges)
add_edges!(G, row_edges)
rem_vertex!(G, (1, M + 2))
rem_vertex!(G, (n + 1, (M + 1) * (n % 2) + 1))
rem_vertex!(G, (M + 2, 1))
rem_vertex!(G, ((M + 1) * (n % 2) + 1, n + 1))

if periodic == true
for i in cols[1:n]
G = merge_vertices(G, [(i, 1), (i, M + 1)])
G = merge_vertices(G, [(1, i), (M + 1, i)])
end

for i in cols[2:(n + 1)]
G = merge_vertices(G, [(i, 2), (i, M + 2)])
G = merge_vertices(G, [(2, i), (M + 2, i)])
end

for j in rows[2:M]
G = merge_vertices(G, [(1, j), (n + 1, j)])
G = merge_vertices(G, [(j, 1), (j, n + 1)])
end

rem_vertex!(G, (n + 1, M + 1))
rem_vertex!(G, (M + 1, n + 1))
end

return G
Expand All @@ -48,27 +48,27 @@ function triangular_lattice_graph(m::Int64, n::Int64; periodic=false)
error("Periodic Triangular Lattice needs m > 2, n > 4")
end

G = NamedGraph([(i, j) for i in cols for j in rows])
G = NamedGraph([(j, i) for i in cols for j in rows])

grid_edges1 = [(i, j) => (i + 1, j) for j in rows for i in cols[1:N]]
grid_edges2 = [(i, j) => (i, j + 1) for j in rows[1:m] for i in cols]
grid_edges1 = [(j, i) => (j, i + 1) for j in rows for i in cols[1:N]]
grid_edges2 = [(j, i) => (j + 1, i) for j in rows[1:m] for i in cols]
add_edges!(G, vcat(grid_edges1, grid_edges2))

diagonal_edges1 = [(i, j) => (i + 1, j + 1) for j in rows[2:2:m] for i in cols[1:N]]
diagonal_edges2 = [(i + 1, j) => (i, j + 1) for j in rows[1:2:m] for i in cols[1:N]]
diagonal_edges1 = [(j, i) => (j + 1, i + 1) for j in rows[2:2:m] for i in cols[1:N]]
diagonal_edges2 = [(j, i + 1) => (j + 1, i) for j in rows[1:2:m] for i in cols[1:N]]
add_edges!(G, vcat(diagonal_edges1, diagonal_edges2))

if periodic == true
for i in cols
G = merge_vertices(G, [(i, 1), (i, m + 1)])
G = merge_vertices(G, [(1, i), (m + 1, i)])
end

for j in rows[1:m]
G = merge_vertices(G, [(1, j), (N + 1, j)])
G = merge_vertices(G, [(j, 1), (j, N + 1)])
end

elseif n % 2 == 1
rem_vertices!(G, [(N + 1, j) for j in rows[2:2:(m + 1)]])
rem_vertices!(G, [(j, N + 1) for j in rows[2:2:(m + 1)]])
end

return G
Expand Down
9 changes: 9 additions & 0 deletions test/test_namedgraphgenerators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ using Random
#Should just be 1 triangle
@test is_path_graph(g)

g = hexagonal_lattice_graph(2, 1)
dims = maximum(vertices(g))
@test dims[1] > dims[2]

g = triangular_lattice_graph(2, 1)
@show g
dims = maximum(vertices(g))
@test dims[1] > dims[2]

#Check consistency with the output of triangular_lattice_graph(7,7) in networkx
g = triangular_lattice_graph(7, 7)
@test length(vertices(g)) == 36
Expand Down

0 comments on commit c4170a3

Please sign in to comment.