コード例 #1
0
  private WeightedMultigraph<Vertex, LabeledWeightedEdge> step1() {

    logger.debug("<enter");

    WeightedMultigraph<Vertex, LabeledWeightedEdge> g =
        new WeightedMultigraph<Vertex, LabeledWeightedEdge>(LabeledWeightedEdge.class);

    for (int i = 0; i < steinerNodes.size(); i++) {
      g.addVertex(steinerNodes.get(i));
    }

    BellmanFordShortestPath<Vertex, LabeledWeightedEdge> path;

    for (int i = 0; i < steinerNodes.size(); i++) {
      path =
          new BellmanFordShortestPath<Vertex, LabeledWeightedEdge>(this.graph, steinerNodes.get(i));

      for (int j = 0; j < steinerNodes.size(); j++) {

        if (i == j) continue;

        if (g.containsEdge(steinerNodes.get(i), steinerNodes.get(j))) continue;

        String id = "e" + String.valueOf(i) + String.valueOf(j);
        LabeledWeightedEdge e = new LabeledWeightedEdge(id, new URI(id, null, null), null);
        g.addEdge(steinerNodes.get(i), steinerNodes.get(j), e);
        g.setEdgeWeight(e, path.getCost(steinerNodes.get(j)));
      }
    }

    logger.debug("exit>");

    return g;
  }
コード例 #2
0
  public EdgeBetweennessGraph(WeightedMultigraph<String, DefaultWeightedEdge> originalGraph) {
    super(DefaultWeightedEdge.class); // Constructor inherented from parent class

    // 1. Initialize the edge betweenness digraph
    // 1.1. Add vertices
    for (String thisVertex : originalGraph.vertexSet()) this.addVertex(thisVertex);
    // 1.2. Add edges
    for (DefaultWeightedEdge thisEdge : originalGraph.edgeSet()) {
      DefaultWeightedEdge newEdge = new DefaultWeightedEdge();
      String sendingName = originalGraph.getEdgeSource(thisEdge);
      String receivingName = originalGraph.getEdgeTarget(thisEdge);
      this.addEdge(sendingName, receivingName, newEdge);
      this.setEdgeWeight(newEdge, 0.0);
    }
    // 1.3. Create corresponding unweighted graph
    // 1.3.1. Add vertices
    WeightedMultigraph<String, DefaultWeightedEdge> originalUnweightedGraph =
        new WeightedMultigraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class);
    for (String thisVertex : originalGraph.vertexSet())
      originalUnweightedGraph.addVertex(thisVertex);
    // 1.3.2. Add edges
    for (DefaultWeightedEdge thisEdge : originalGraph.edgeSet()) {
      DefaultWeightedEdge newEdge = new DefaultWeightedEdge();
      String sendingName = originalGraph.getEdgeSource(thisEdge);
      String receivingName = originalGraph.getEdgeTarget(thisEdge);
      originalUnweightedGraph.addEdge(sendingName, receivingName, newEdge);
      originalUnweightedGraph.setEdgeWeight(newEdge, 1.0);
    }

    for (String thisVertex : this.vertexSet()) {
      // 2. Create shortest-path graph for every vertex by Dijkstra algorithm
      Multigraph<String, DefaultEdge> shortestPathDigraph =
          DijkstraAlgorithm(originalUnweightedGraph, thisVertex);

      // 3. Calculate the contribution of current vertex's shortest-path graph to final edge
      // betweenness (Newman algorithm)
      SimpleWeightedGraph<String, DefaultWeightedEdge> edgeBetweennessDigraph =
          NewmanAlgorithm(shortestPathDigraph, thisVertex);

      // 4. Update final edge betweenness digraph with current vertex's shortest-path graph
      if (edgeBetweennessDigraph != null)
        for (DefaultWeightedEdge thisEdge : edgeBetweennessDigraph.edgeSet()) {
          String sourceVertex = edgeBetweennessDigraph.getEdgeSource(thisEdge);
          String targetVertex = edgeBetweennessDigraph.getEdgeTarget(thisEdge);
          double betweennessWeight = edgeBetweennessDigraph.getEdgeWeight(thisEdge);
          DefaultWeightedEdge edgeInWholeGraph = this.getEdge(sourceVertex, targetVertex);
          double newWeight = this.getEdgeWeight(edgeInWholeGraph) + betweennessWeight;
          this.setEdgeWeight(edgeInWholeGraph, newWeight);
        }
    }

    // 5. Revise the edge betweenness digraph with the original active power digraph
    for (DefaultWeightedEdge thisEdge : this.edgeSet()) {
      String sourceVertex = this.getEdgeSource(thisEdge);
      String targetVertex = this.getEdgeTarget(thisEdge);
      double betweennessWeight = this.getEdgeWeight(thisEdge);
      DefaultWeightedEdge edgeInWholeGraph = originalGraph.getEdge(sourceVertex, targetVertex);
      double newWeight = betweennessWeight / originalGraph.getEdgeWeight(edgeInWholeGraph);
      this.setEdgeWeight(thisEdge, newWeight);
      //			System.out.println("Final edge betweenness between " + sourceVertex + " and " +
      // targetVertex + ": " + newWeight);
    }
  }