library(igraph)
N<-5
plusE<-3
s<-sample(1:(N-1),plusE)
pluses<-rep(0,N)
pluses[s]<-1
M<-matrix(0,N,N)
for(i in 1:(N-2)){
num<-1
if(pluses[i]==1){
num<-2
}
M[i,sample((i+1):N,num)]<-1
}
M[N-1,N]<-1
M
s<-sample(1:N)
M<-M[s,s]
M
M2<-(M+t(M))
M2[lower.tri(M2)]<-0
M2
g<-graph.adjacency(M2)
is.connected(g,"strong")
plot(g)
P<-runif(N)
library(gtools)
perms<-permutations(N,N)
perms
stats<-rep(0,length(perms[,1]))
this<-1
minStats<-0
for(i in 1:length(perms[,1])){
tmpP<-P[perms[i,]]
stats[i]<-sum(abs(outer(tmpP,tmpP,"-"))*M2)
if(i==1){
this<-i
minStats<-stats[i]
}else{
if(minStats>stats[i]){
this<-i
minStats<-stats[i]
}
}
}
plot(sort(stats))
plot(g)
tmpP<-P[perms[this,]]
tmpP
M2
abs(outer(tmpP,tmpP,"-"))*M2