This is a Jupyter Notebook that contains Julia code I will run in the first lecture of Spectral Graph Theory. I find experiments to be incredibly useful when working on spectral graph theory. They help me figure out what is true, and they help me find counterexamples to my conjectures.
If you want to try using this, you will need to install Jupyter (via Python), Julia and IJulia. You also need my package, called Laplacians.jl
. It may be added in Julia via
Using Pkg
Pkg.add("Laplacians")
using Laplacians
using LinearAlgebra
using Statistics
using Plots
using SparseArrays
using FileIO
using JLD2
using Random
M = path_graph(4)
Matrix(M)
Matrix(lap(M))
n = 20;
L = lap(path_graph(n));
E = eigen(Matrix(L))
println(E.values)
v2 = E.vectors[:,2]
plot(v2,marker=5,legend=false)
xlabel!("vertex number")
ylabel!("value in eigenvector")
Plots.plot(E.vectors[:,2],label="v2",marker = 5)
Plots.plot!(E.vectors[:,3],label="v3",marker = 5)
Plots.plot!(E.vectors[:,4],label="v4",marker = 5)
xlabel!("Vertex Number")
ylabel!("Value in Eigenvector")
scaledE = E.vectors ./ maximum(abs.(E.vectors); dims=1);
maximum(abs.(scaledE); dims=1)
Plots.plot(scaledE[:,2],label="v2",marker = 5)
Plots.plot!(scaledE[:,3],label="v3",marker = 5)
Plots.plot!(scaledE[:,4],label="v4",marker = 5)
xlabel!("Vertex Number")
ylabel!("Value in Eigenvector")
Plots.plot(scaledE[:,n],label="v_n",marker=5)
xlabel!("Vertex Number")
ylabel!("Value in Eigenvector")
M = grid2(5,5)
L = lap(M)
E = eigen(Matrix(L))
V = E.vectors[:,2:3]
plot_graph(M,V[:,1],V[:,2])
Random.seed!(1)
p = randperm(size(M,1))
M = M[p,p]
E = eigen(Matrix(lap(M)))
V = E.vectors[:,2:3]
plot_graph(M,V[:,1],V[:,2], dots=false)
M = latin_square_graph(5);
println(eigvals(Matrix(lap(M))))
M = readIJV("dodec.txt")
spectral_drawing(M)
E = eigen(Matrix(lap(M)))
println(E.values)
using PyPlot: pygui
x = E.vectors[:,2]
y = E.vectors[:,3]
z = E.vectors[:,4]
pygui(false)
plot_graph(M, x, y, z; setaxis=false)
pygui(true)
plot_graph(M, x, y, z; setaxis=false)
x = E.vectors[:,11]
y = E.vectors[:,12]
pygui(false)
plot_graph(M, x, y; setaxis=false)
x = E.vectors[:,11]
y = E.vectors[:,12]
z = E.vectors[:,10]
pygui(true)
plot_graph(M, x, y, z; setaxis=false)
n = 5;
g = grid3(4,5,6);
E = eigen(Matrix(lap(g)));
x = E.vectors[:,2]
y = E.vectors[:,3]
z = E.vectors[:,4]
pygui(true)
plot_graph(g, x, y, z; setaxis=false)
n = 10;
p = 0.7;
q = 0.2;
B = convert.(Int64, rand(n,n) .< q)
A = convert.(Int64, rand(n,n) .< p);
triu!(A,1); # Take the upper trianguar part, above the first diagonal
A += A'; # Copy the upper triangular part to lower triangular
C = convert.(Int64, rand(n,n) .< p);
triu!(C,1);
C += C';
G = sparse([A B; B' C])
pygui(false)
plot_graph(G, rand(2*n), rand(2*n))
L = lap(G);
E = eigen(Matrix(L));
plot_graph(G, E.vectors[:,2], E.vectors[:,3]; number=true)
d = diag(L);
D_inv_half = spdiagm(0 => Vector(d.^(-0.5)))
N = D_inv_half * L * D_inv_half;
E = eigen(Matrix(N))
println(E.values)
plot_graph(G, D_inv_half * E.vectors[:,2], rand(2*n), number=true)
plot_graph(G, D_inv_half * E.vectors[:,2], D_inv_half * E.vectors[:,3], number = true)