本文整理汇总了Golang中github.com/gonum/graph.Undirected.communities方法的典型用法代码示例。如果您正苦于以下问题:Golang Undirected.communities方法的具体用法?Golang Undirected.communities怎么用?Golang Undirected.communities使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/gonum/graph.Undirected
的用法示例。
在下文中一共展示了Undirected.communities方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: reduce
// reduce returns a reduced graph constructed from g divided
// into the given communities. The communities value is mutated
// by the call to reduce. If communities is nil and g is a
// ReducedUndirected, it is returned unaltered.
func reduce(g graph.Undirected, communities [][]graph.Node) *ReducedUndirected {
if communities == nil {
if r, ok := g.(*ReducedUndirected); ok {
return r
}
nodes := g.Nodes()
// TODO(kortschak) This sort is necessary really only
// for testing. In practice we would not be using the
// community provided by the user for a Q calculation.
// Probably we should use a function to map the
// communities in the test sets to the remapped order.
sort.Sort(ordered.ByID(nodes))
communities = make([][]graph.Node, len(nodes))
for i := range nodes {
communities[i] = []graph.Node{node(i)}
}
weight := weightFuncFor(g)
r := ReducedUndirected{
nodes: make([]community, len(nodes)),
edges: make([][]int, len(nodes)),
weights: make(map[[2]int]float64),
communities: communities,
}
communityOf := make(map[int]int, len(nodes))
for i, n := range nodes {
r.nodes[i] = community{id: i, nodes: []graph.Node{n}}
communityOf[n.ID()] = i
}
for _, u := range nodes {
var out []int
uid := communityOf[u.ID()]
for _, v := range g.From(u) {
vid := communityOf[v.ID()]
if vid != uid {
out = append(out, vid)
}
if uid < vid {
// Only store the weight once.
r.weights[[2]int{uid, vid}] = weight(u, v)
}
}
r.edges[uid] = out
}
return &r
}
// Remove zero length communities destructively.
var commNodes int
for i := 0; i < len(communities); {
comm := communities[i]
if len(comm) == 0 {
communities[i] = communities[len(communities)-1]
communities[len(communities)-1] = nil
communities = communities[:len(communities)-1]
} else {
commNodes += len(comm)
i++
}
}
r := ReducedUndirected{
nodes: make([]community, len(communities)),
edges: make([][]int, len(communities)),
weights: make(map[[2]int]float64),
}
r.communities = make([][]graph.Node, len(communities))
for i := range r.communities {
r.communities[i] = []graph.Node{node(i)}
}
if g, ok := g.(*ReducedUndirected); ok {
// Make sure we retain the truncated
// community structure.
g.communities = communities
r.parent = g
}
weight := weightFuncFor(g)
communityOf := make(map[int]int, commNodes)
for i, comm := range communities {
r.nodes[i] = community{id: i, nodes: comm}
for _, n := range comm {
communityOf[n.ID()] = i
}
}
for uid, comm := range communities {
var out []int
for i, u := range comm {
r.nodes[uid].weight += weight(u, u)
for _, v := range comm[i+1:] {
r.nodes[uid].weight += 2 * weight(u, v)
}
for _, v := range g.From(u) {
vid := communityOf[v.ID()]
found := false
for _, e := range out {
//.........这里部分代码省略.........