メモ

library(igraph)
# 全域木にplusE本のエッジを加えたグラフを作る
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
# 辺の両端ノードの値の差(0以上)をその辺の値として、その総和を統計量としてみる
# その分布をとる
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