Last active
August 29, 2015 14:04
-
-
Save lssimoes/3c38ea40d7145dad7e03 to your computer and use it in GitHub Desktop.
Function that creates a N-sized square graph grid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Graphs | |
# This implementation takes too long to create the graphs, so I'm almost sure it's not the best way (certainly is not the only way) | |
function nsquaregraph(n::Int) | |
# Creating Vertices Array | |
newvertices = Array(ExVertex, 0) | |
for i in 1:n^2 | |
vertice = ExVertex(i,"eletron") | |
vertice.attributes["spin"] = randspin() # randspin() generates randomically -1 or +1 | |
newvertices = [newvertices, vertice] | |
end | |
# Creating Edges Array | |
n_edge = 4(n-2)^2 + 3*4(n-2) + 4*2 | |
n_edge = int(n_edge/2) | |
newedges = Array(Edge{typeof(newvertices[1])}, 0) | |
index = 1 | |
for i in 1:n-1 | |
for j in 1:n-1 | |
whoami = n*(i-1)+j | |
newedges = [newedges, Edge(index, newvertices[whoami], newvertices[whoami+n]) # down neighbors | |
, Edge(index+1, newvertices[whoami], newvertices[whoami+1])] # right | |
index += 2 | |
end | |
newedges = [newedges, Edge(index, newvertices[n*i], newvertices[n*i+n])] # down neighbor of the elemnt (i,n) | |
index += 1 | |
# exception to treat last line, i represents the column instead of the line | |
newedges = [newedges, Edge(index, newvertices[n*(n-1)+i], newvertices[n*(n-1)+i+1])] # neighbor to the right | |
index += 1 | |
end | |
return graph(newvertices, newedges, is_directed=false) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment