makeLap[A_]:=DiagonalMatrix[Total /@ A] - A

spectralDraw[A_]:=Module[{L,coords},
L=makeLap[A];
coords=Transpose[Eigenvectors[N[L],{-3,-2}]];
GraphPlot[AdjacencyGraph[A], VertexCoordinateRules -> Table[k->coords[[k]], {k,1,Length[A]}]]]

path[n_]:=Table[If[Abs[i-j]==1,1,0], {i,1,n},{j,1,n}]

p10=path[10]

spectralDraw[p10]

chessPos[n_]:={Floor[n/8], Mod[n,8]}

kingNeighbor[{a_,b_}, {x_,y_}]:= Abs[a-x] ≤1 && Abs[b-y] ≤ 1 && {a,b} ≠ {x,y}

kingGraph=Table[If[kingNeighbor[chessPos[m], chessPos[n]],1,0], {m,0,63},{n,0,63}];

spectralDraw[kingGraph]

knightNeighbor[{a_,b_}, {x_,y_}]:= Sort[{Abs[a-x], Abs[b-y]}]=={1,2};

knightGraph=Table[If[knightNeighbor[chessPos[m], chessPos[n]],1,0], {m,0,63},{n,0,63}];

spectralDraw[knightGraph]

GraphPlot[AdjacencyGraph[knightGraph]]

(* Here is a graph with two sets a2 and b2, edge probability 0.7 within ses and 0.3 between them. *)

foo2= Table[RandomInteger[1], {100}]

a2=Select[Range[100], (foo2[[#]]==0)&]; b2=Select[Range[100], (foo2[[#]]==1)&];

a2

b2

sg2=Table[
If[foo2[[i]]==foo2[[j]], If[RandomInteger[10]≤7,1,0], If[RandomInteger[10]≤ 3,1,0]],{i,100},{j,100}];

v2old=First[Eigenvectors[N[makeLap[sg2]], {-2}]]

ListPlot[Sort[v2old]]

a2points= {#,v2[[#]]}& /@ a2;

b2points= {#,v2[[#]]}& /@ b2;

ListPlot[{a2points,b2points}, PlotStyle -> {Red, Blue}]

ListPlot[v2]

(* Let's do three sets instead *)

foo3= Table[RandomInteger[2], {100}]

a3=Select[Range[100], (foo3[[#]]==0)&]; b3=Select[Range[100], (foo3[[#]]==1)&]; 
c3=Select[Range[100], (foo3[[#]]==2)&];

s3g = Table[
If[foo3[[i]]==foo3[[j]], If[RandomInteger[10]≤7,1,0], If[RandomInteger[10]≤ 3,1,0]], {i,1,100},{j,1,100}];

v2=First[Eigenvectors[N[makeLap[s3g]], {-2}]]

ListPlot[v2]

ListPlot[Sort[v2]]

a3points= {#,v2[[#]]}& /@ a3; b3points= {#,v2[[#]]}& /@ b3; c3points= {#,v2[[#]]}& /@ c3;

ListPlot[{a3points,b3points, c3points}, PlotStyle -> {Red, Blue, Green}]

blah=InversePermutation[Ordering[v2]];

a3sort={blah[[#]],v2[[#]]}& /@ a3;b3sort={blah[[#]],v2[[#]]}& /@ b3;c3sort={blah[[#]],v2[[#]]}& /@ c3;

ListPlot[{a3sort,b3sort, c3sort}, PlotStyle -> {Red, Blue, Green}]

(* Some actual data: Political book recommendations from Amazon. *)

books=LMG=Import["~/Desktop/565/polbooks/polbooks.gml"];

booksMatrix = AdjacencyMatrix[books];

booksMatrix == Transpose[booksMatrix]

v2=First[Eigenvectors[N[makeLap[booksMatrix]], {-2}]]

ListPlot[Sort[v2]]

bt=Import["~/Desktop/565/polbooks/booktitles", "Table"];
titleList = {First[#]+1,Last[#]}& /@ Select[bt,(Length[#]>1)&]

toTitle[k_]:=Last[Flatten[Select[titleList,(First[#]==k)&]]]

toTitle /@ Ordering[v2]

?Take

Take[Out[53], {40,60}]