-
Notifications
You must be signed in to change notification settings - Fork 0
/
algConnectivity.jl
60 lines (57 loc) · 1.57 KB
/
algConnectivity.jl
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
using Graphs
using SimpleGraphs
function AlgConnectivity(L::Symmetric;vecs=false)
# Calculates the algebraic connectivity of graph with laplacian L
# Input: L a symmetric laplacian matrix
# Output: value of algebraic connectivity λ_2
e_vals = eigfact(L,2:2)
λ = e_vals.values[1]
ν = e_vals.vectors
if vecs
return λ, ν
else
return λ
end
end
function AlgConnectivity(L::Array{Float64,2},vecs=false)
Ls = Symmetric(L)
results = AlgConnectivity(Ls,vecs=vecs)
return results
end
function AlgConnectivity(L::Array{Int64,2},vecs=false)
Ls = Symmetric(float(L))
results = AlgConnectivity(Ls,vecs=vecs)
return results
end
function AlgConnectivity(g::SimpleGraphs.SimpleGraph,sparse::Bool;vecs=false)
L = laplace(g)
results = AlgConnectivity(L,vecs)
return results
end
function AlgConnectivity(g::GenericGraph,sparse::Bool;vecs=false)
if sparse
L = laplacian_matrix_sparse(g)
else
L = laplacian_matrix(g)
end
results = AlgConnectivity(L,vecs)
return results
end
function AlgConnectivity(L::SparseMatrixCSC;vecs=false)
# Calculates the algebraic connectivity of graph with laplacian L
# Input: L a symmetric laplacian matrix
# Output: value of algebraic connectivity λ_2
s = svds(L,nsv=size(L)[1]-1)
if length(s[2]) < size(L)[1]
# there are some zero eigen values that weren't calculated
λ = s[2][end]
else
λ = s[2][end-1]
end
if vecs
ν = s[1][:,end-1]
return λ, ν
else
return λ
end
end