0

我想执行一些图形聚类,并且由于我非常依赖 Java,因此决定尝试使用 java 包 Jung。作为一个简单的图形,我创建了两个相互连接的每个 5vertices 的集群。我使用一个边缘连接两个集群。我希望在图形聚类之后检索两个大小均为 5 的集群,但我得到不同的结果。这是代码:

import edu.uci.ics.jung.algorithms.cluster.VoltageClusterer;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.SparseGraph;
import java.io.IOException;
import java.util.Collection;
import java.util.Set;

public class CreateGraph {

public static void main(String[] args) throws IOException {

    // Graph<V, E> where V is the type of the vertices
    // and E is the type of the edges
    Graph<Integer, String> g = new SparseGraph<Integer, String>();

    for (int i = 0; i < 5; i++) {
        g.addVertex((Integer) i);
    }

    for (int i = 0; i < 5; i++) {
        for (int ii = 0; ii < 5; ii++) {
            if (i != ii) {
                g.addEdge("EdgeA-" + i + ii, i, ii);
            }
        }
    }
    // cluster 2
    for (int i = 5; i < 10; i++) {
        g.addVertex((Integer) i);
    }

    for (int i = 5; i < 10; i++) {
        for (int ii = 5; ii < 10; ii++) {
            if (i != ii) {
                g.addEdge("EdgeB-" + i + ii, i, ii);
            }
        }
    }
    System.out.println(g.toString());

    g.addEdge("Edge-connector", 1, 5);

    System.out.println("Creating voltageclusterer");

    VoltageClusterer<Integer, String> vc = new VoltageClusterer<Integer, String>(g, 2);

    Collection<Set<Integer>> clusters = vc.cluster(2);

    for (Set<Integer> s : clusters) {
        System.out.println("set is " + s.size());
        for (Integer ss : s) {
            System.out.println("Element " + ss);
        }
    }
}
 }

和输出:+

  1. 设置为 1

    • 元素 8
  2. 设置为 9

    • 元素 0
    • 元素 1
    • 元素 2
    • 元素 3
    • 元素 4
    • 要素 5
    • 元素 6
    • 元素 7
    • 元素 9

有人知道吗?(也欢迎有关其他方法的建议,只要它们是在 Java 中的)。

4

1 回答 1

1

VoltageClusterer 有一个随机元素:取决于掷骰子的方式(以及你掷骰子的次数——见下文),有时你得到的答案会很奇怪,就像在这种情况下一样。您可以使用 指定随机种子setRandomSeed()

您遇到此问题的原因是VoltageClusterer构造函数的 Javadoc 错误:您传入的数字参数不是集群的数量;它是正在生成的随机样本的数量。对于那个很抱歉; 我们会修复它。

您几乎肯定希望使用比 2 更多的随机样本。

JUNG 实现的其他聚类算法是确定性的。 EdgeBetweennessClusterer如果您告诉它删除一条边,特别是会按照您的预期对图形进行分区。

于 2018-11-04T05:06:51.360 回答