1+ using SparseArrays: SparseMatrixCSC, sparse
2+ using ArnoldiMethod: LR
3+ using LinearAlgebra: eigen
4+
15"""
26Position nodes uniformly at random in the unit square.
37For every node, a position is generated by choosing each of dim
@@ -56,7 +60,7 @@ function circular_layout(G)
5660 return [0.0 ], [0.0 ]
5761 else
5862 # Discard the extra angle since it matches 0 radians.
59- θ = linspace (0 , 2pi , _nv (G) + 1 )[1 : end - 1 ]
63+ θ = range (0 , stop = 2pi , length = _nv (G)+ 1 )[1 : end - 1 ]
6064 return cos .(θ), sin .(θ)
6165 end
6266end
@@ -96,11 +100,11 @@ julia> locs_x, locs_y = spring_layout(g)
96100function spring_layout (G, locs_x= 2 * rand (_nv (G)).- 1.0 , locs_y= 2 * rand (_nv (G)).- 1.0 ; C= 2.0 , MAXITER= 100 , INITTEMP= 2.0 )
97101
98102 # size(adj_matrix, 1) != size(adj_matrix, 2) && error("Adj. matrix must be square.")
99- const N = _nv (G)
103+ N = _nv (G)
100104 adj_matrix = _adjacency_matrix (G)
101105
102106 # The optimal distance bewteen vertices
103- const K = C * sqrt (4.0 / N)
107+ K = C * sqrt (4.0 / N)
104108
105109 # Store forces and apply at end of iteration all at once
106110 force_x = zeros (N)
@@ -183,7 +187,7 @@ julia> nlist[2] = [6:num_vertiecs(g)]
183187julia> locs_x, locs_y = shell_layout(g, nlist)
184188```
185189"""
186- function shell_layout (G, nlist:: Union{Void , Vector{Vector{Int}}} = nothing )
190+ function shell_layout (G, nlist:: Union{Nothing , Vector{Vector{Int}}} = nothing )
187191 if _nv (G) == 1
188192 return [0.0 ], [0.0 ]
189193 end
@@ -199,7 +203,7 @@ function shell_layout(G, nlist::Union{Void, Vector{Vector{Int}}} = nothing)
199203 locs_y = Float64[]
200204 for nodes in nlist
201205 # Discard the extra angle since it matches 0 radians.
202- θ = linspace (0 , 2pi , length (nodes) + 1 )[1 : end - 1 ]
206+ θ = range (0 , stop = 2pi , length= length (nodes)+ 1 )[1 : end - 1 ]
203207 append! (locs_x, radius* cos .(θ))
204208 append! (locs_y, radius* sin .(θ))
205209 radius += 1.0
@@ -229,37 +233,41 @@ julia> weight = rand(num_edges(g))
229233julia> locs_x, locs_y = spectral_layout(g, weight)
230234```
231235"""
232- function spectral_layout (G)
236+ function spectral_layout (G, weight = nothing )
233237 if _nv (G) == 1
234238 return [0.0 ], [0.0 ]
239+ elseif _nv (G) == 2
240+ return [0.0 , 1.0 ], [0.0 , 0.0 ]
235241 end
236242
243+ if weight == nothing
244+ weight = ones (length (_edges (G)))
245+ end
237246 if _nv (G) > 500
238- A = sparse (Int[_src_index (e) for e in _edges (G)],
239- Int[_dst_index (e) for e in _edges (G)],
247+ A = sparse (Int[_src_index (e, G ) for e in _edges (G)],
248+ Int[_dst_index (e, G ) for e in _edges (G)],
240249 weight, _nv (G), _nv (G))
241250 if _is_directed (G)
242251 A = A + A'
243252 end
244253 return _spectral (A)
245254 else
246255 L = _laplacian_matrix (G)
247- return _spectral (full (L))
256+ return _spectral (Matrix (L))
248257 end
249258end
250259
251260function _spectral (L:: Matrix )
252- eigenvalues, eigenvectors = eig (L)
261+ eigenvalues, eigenvectors = eigen (L)
253262 index = sortperm (eigenvalues)[2 : 3 ]
254263 eigenvectors[:, index[1 ]], eigenvectors[:, index[2 ]]
255264end
256265
257266function _spectral (A:: SparseMatrixCSC )
258- data = collect (sum (A, 1 ))
259- D = sparse ([ 1 : length (data)], [ 1 : length (data)] , data)
267+ data = vec (sum (A, dims = 1 ))
268+ D = sparse (Base . OneTo ( length (data)), Base . OneTo ( length (data)) , data)
260269 L = D - A
261- ncv = max (7 , int (sqrt (length (data))))
262- eigenvalues, eigenvectors = eigs (L, nev= 3 , ncv= ncv)
270+ eigenvalues, eigenvectors = LightGraphs. LinAlg. eigs (L, nev= 3 , which= LR ())
263271 index = sortperm (real (eigenvalues))[2 : 3 ]
264272 real (eigenvectors[:, index[1 ]]), real (eigenvectors[:, index[2 ]])
265273end
0 commit comments