示例#1
0
  public static Graph erdosRenyiGraph(int n, int e) {
    List<Node> nodes = new ArrayList<Node>();
    for (int i = 0; i < n; i++) nodes.add(new GraphNode("X" + i));

    Graph graph = new EdgeListGraph(nodes);

    for (int e0 = 0; e0 < e; e0++) {
      int i1 = RandomUtil.getInstance().nextInt(n);
      int i2 = RandomUtil.getInstance().nextInt(n);

      if (i1 == i2) {
        e0--;
        continue;
      }

      Edge edge = Edges.undirectedEdge(nodes.get(i1), nodes.get(i2));

      if (graph.containsEdge(edge)) {
        e0--;
        continue;
      }

      graph.addEdge(edge);
    }

    return graph;
  }
示例#2
0
文件: Lofs.java 项目: jdramsey/tetrad
  private void ruleR1(Graph skeleton, Graph graph, List<Node> nodes) {
    for (Node node : nodes) {
      SortedMap<Double, String> scoreReports = new TreeMap<Double, String>();

      List<Node> adj = skeleton.getAdjacentNodes(node);

      DepthChoiceGenerator gen = new DepthChoiceGenerator(adj.size(), adj.size());
      int[] choice;
      double maxScore = Double.NEGATIVE_INFINITY;
      List<Node> parents = null;

      while ((choice = gen.next()) != null) {
        List<Node> _parents = GraphUtils.asList(choice, adj);

        double score = score(node, _parents);
        scoreReports.put(-score, _parents.toString());

        if (score > maxScore) {
          maxScore = score;
          parents = _parents;
        }
      }

      for (double score : scoreReports.keySet()) {
        TetradLogger.getInstance()
            .log(
                "score",
                "For " + node + " parents = " + scoreReports.get(score) + " score = " + -score);
      }

      TetradLogger.getInstance().log("score", "");

      if (parents == null) {
        continue;
      }

      if (normal(node, parents)) continue;

      for (Node _node : adj) {
        if (parents.contains(_node)) {
          Edge parentEdge = Edges.directedEdge(_node, node);

          if (!graph.containsEdge(parentEdge)) {
            graph.addEdge(parentEdge);
          }
        }
      }
    }
  }
示例#3
0
  public static Graph weightedRandomGraph(int n, int e) {
    List<Node> nodes = new ArrayList<Node>();
    for (int i = 0; i < n; i++) nodes.add(new GraphNode("X" + i));

    Graph graph = new EdgeListGraph(nodes);

    for (int e0 = 0; e0 < e; e0++) {
      int i1 = weightedRandom(nodes, graph);
      //            int i2 = RandomUtil.getInstance().nextInt(n);
      int i2 = weightedRandom(nodes, graph);

      if (!(shortestPath(nodes.get(i1), nodes.get(i2), graph) < 9)) {
        e0--;
        continue;
      }

      if (i1 == i2) {
        e0--;
        continue;
      }

      Edge edge = Edges.undirectedEdge(nodes.get(i1), nodes.get(i2));

      if (graph.containsEdge(edge)) {
        e0--;
        continue;
      }

      graph.addEdge(edge);
    }

    for (Edge edge : graph.getEdges()) {
      Node n1 = edge.getNode1();
      Node n2 = edge.getNode2();

      if (!graph.isAncestorOf(n2, n1)) {
        graph.removeEdge(edge);
        graph.addDirectedEdge(n1, n2);
      } else {
        graph.removeEdge(edge);
        graph.addDirectedEdge(n2, n1);
      }
    }

    return graph;
  }
示例#4
0
  /**
   * @param v1Number the number of the first vertex v1
   * @param v2Number the number of the second vertex v2
   * @return exists the edge from v1 to v2
   */
  public boolean hasEdge(int v1Number, int v2Number) {
    V v1, v2;
    Boolean containsEdge = null;

    if (cacheEdges) {
      containsEdge = adjMatrix[v1Number][v2Number];
    }

    if (!cacheEdges || (containsEdge == null)) {
      v1 = getVertex(v1Number);
      v2 = getVertex(v2Number);
      containsEdge = graph.containsEdge(v1, v2);
    }

    if (cacheEdges && (adjMatrix[v1Number][v2Number] == null)) {
      adjMatrix[v1Number][v2Number] = containsEdge;
    }

    return containsEdge;
  }
示例#5
0
  public static Graph randomGraph(Iterable<? extends Body> bodies, int edges) {
    Graph g = new Graph();
    ArrayList<Body> bodylist = new ArrayList<Body>();
    for (Body b : bodies) {
      bodylist.add(b);
    }
    int bcount = bodylist.size();

    for (int i = 0; i < edges; i++) {
      String e1, e2;
      do {
        e1 = bodylist.get(rand.nextInt(bcount)).name();
        e2 = bodylist.get(rand.nextInt(bcount)).name();
      } while (e1.equals(e2) || g.containsEdge(e1, e2));

      g.addEdge(e1, e2);
      System.out.println(String.format("%s -> %s", e1, e2));
    }
    return g;
  }