Пример #1
0
 private void computeDependencies(List<GraphNode<T>> nodes) {
   List<GraphNode<T>> nextNodesToDisplay = null;
   for (GraphNode<T> node : nodes) {
     if (!isAlreadyEvaluated(node)) {
       List<GraphNode<T>> comingInNodes = node.getComingInNodes();
       if (areAlreadyEvaluated(comingInNodes)) {
         listener.evaluating(node.value);
         evaluatedNodes.add(node);
         List<GraphNode<T>> goingOutNodes = node.getGoingOutNodes();
         if (goingOutNodes != null) {
           if (nextNodesToDisplay == null) nextNodesToDisplay = new ArrayList<GraphNode<T>>();
           // add these too, so they get a chance to be displayed
           // as well
           nextNodesToDisplay.addAll(goingOutNodes);
         }
       } else {
         if (nextNodesToDisplay == null) nextNodesToDisplay = new ArrayList<GraphNode<T>>();
         // the checked node should be carried
         nextNodesToDisplay.add(node);
       }
     }
   }
   if (nextNodesToDisplay != null) {
     computeDependencies(nextNodesToDisplay);
   }
   // here the recursive call ends
 }
Пример #2
0
  /**
   * Given a set of SCC nodes make this the lead member of the SCC and reroute all incoming and
   * outgoing links accordingly. This eager rewrite is based on the assumption that there are few
   * cycles so it is better to rewrite once and keep the graph easy to traverse.
   */
  public void makeLeadNodeFor(Set<GraphNode> members) {
    // Accumulate all successors
    Set<GraphNode> newSucc = new HashSet<>();
    Set<GraphNode> newSuccClosed = new HashSet<>();
    for (GraphNode n : members) {
      newSucc.addAll(n.succ);
      newSuccClosed.addAll(n.succClosed);
    }
    newSucc.removeAll(members);
    newSuccClosed.removeAll(members);
    succ = newSucc;
    succClosed = newSuccClosed;

    // Rewrite all direct successors to have us as predecessor
    for (GraphNode n : succ) {
      n.pred.removeAll(members);
      n.pred.add(this);
    }

    // Find all predecessor nodes and relink link them to point to us
    Set<GraphNode> done = new HashSet<>();
    Set<GraphNode> newAliases = new HashSet<>();
    for (GraphNode member : members) {
      addSiblings(newAliases, member);
    }
    becomeLeaderOf(newAliases);
    for (GraphNode n : members) {
      if (n != this) {
        pred.addAll(n.pred);
        n.relocateAllRefTo(this, done);
        n.becomeSubordinateOf(this);
      }
    }
    pred.removeAll(members);
  }
Пример #3
0
  /**
   * Recursive DFS algorthm
   *
   * @param matrix Map of lists representing adj matrix
   * @param info Map of nodes representing node info
   * @param v Node source
   * @param n int number of nodes in graph
   * @param: print boolean if to print node during traversal
   * @return List of nodes in order visited
   */
  public ArrayList<String> dfs_rec(
      Map<Integer, List<Integer>> matrix,
      Map<Integer, GraphNode> info,
      GraphNode v,
      Integer n,
      boolean print,
      ArrayList<String> ops) {
    v.setVisited(true);
    if (print) System.out.println(v);
    ops.add(v.toString());

    //        if(checkStoppingCondition(v, getN())) {
    //            return;
    //        }

    List<Integer> edgeTarget = matrix.get(v.getID());

    for (int i = 0; i < edgeTarget.size(); ++i) {
      if (edgeTarget.get(i) == 1) {
        GraphNode node = info.get(i + 1);
        if (!node.getVisited()) {
          return dfs_rec(matrix, info, node, n, print, ops);
        }
      }
    }
    return ops;
  }
  private double getPMulticluster(List<List<Integer>> clusters, int numRestarts) {
    if (false) {
      Graph g = new EdgeListGraph();
      List<Node> latents = new ArrayList<Node>();
      for (int i = 0; i < clusters.size(); i++) {
        GraphNode latent = new GraphNode("L" + i);
        latent.setNodeType(NodeType.LATENT);
        latents.add(latent);
        g.addNode(latent);

        List<Node> cluster = variablesForIndices(clusters.get(i));

        for (int j = 0; j < cluster.size(); j++) {
          g.addNode(cluster.get(j));
          g.addDirectedEdge(latent, cluster.get(j));
        }
      }
      SemPm pm = new SemPm(g);

      //            pm.fixOneLoadingPerLatent();

      SemOptimizerPowell semOptimizer = new SemOptimizerPowell();
      semOptimizer.setNumRestarts(numRestarts);

      SemEstimator est = new SemEstimator(cov, pm, semOptimizer);
      est.setScoreType(SemIm.ScoreType.Fgls);
      est.estimate();
      return est.getEstimatedSem().getPValue();
    } else {
      double max = Double.NEGATIVE_INFINITY;

      for (int i = 0; i < numRestarts; i++) {
        Mimbuild2 mimbuild = new Mimbuild2();

        List<List<Node>> _clusters = new ArrayList<List<Node>>();

        for (List<Integer> _cluster : clusters) {
          _clusters.add(variablesForIndices(_cluster));
        }

        List<String> names = new ArrayList<String>();

        for (int j = 0; j < clusters.size(); j++) {
          names.add("L" + j);
        }

        mimbuild.search(_clusters, names, cov);

        double c = mimbuild.getpValue();
        if (c > max) max = c;
      }

      return max;
    }
  }
Пример #5
0
 private List<GraphNode<T>> getOrphanNodes() {
   List<GraphNode<T>> orphanNodes = null;
   Set<T> keys = nodes.keySet();
   for (T key : keys) {
     GraphNode<T> node = nodes.get(key);
     if (node.getComingInNodes() == null) {
       if (orphanNodes == null) orphanNodes = new ArrayList<GraphNode<T>>();
       orphanNodes.add(node);
     }
   }
   return orphanNodes;
 }
Пример #6
0
 public void computeDependencies() {
   List<GraphNode<T>> orphanNodes = getOrphanNodes();
   List<GraphNode<T>> nextNodesToDisplay = new ArrayList<GraphNode<T>>();
   if (orphanNodes != null) {
     for (GraphNode<T> node : orphanNodes) {
       listener.evaluating(node.value);
       evaluatedNodes.add(node);
       nextNodesToDisplay.addAll(node.getGoingOutNodes());
     }
     computeDependencies(nextNodesToDisplay);
   }
 }
Пример #7
0
 /** Dump a set to a string for debug. */
 private static String dumpSet(Set<GraphNode> s) {
   StringBuffer sb = new StringBuffer();
   sb.append("{");
   boolean started = false;
   for (GraphNode value : s) {
     if (started) {
       sb.append(", ");
     } else {
       started = true;
     }
     sb.append(value.toString());
   }
   sb.append("}");
   return sb.toString();
 }
Пример #8
0
  /**
   * BFS algorith
   *
   * @param: print boolean if to print node during traversal
   * @return List of nodes in order visited
   */
  public ArrayList<String> bfs(boolean print) {

    ArrayList<String> ops = new ArrayList<>();

    GraphNode v = source;
    ArrayList<GraphNode> Q = new ArrayList<>();
    Q.add(v);
    v.setVisited(true);
    while (Q.size() != 0) {
      v = Q.remove(0);
      if (print) System.out.println(v);
      ops.add(v.toString());
      //      if(checkStoppingCondition(v, getN())) {
      //        return;
      //      }
      List<Integer> edgeTarget = matrix.get(v.getID());
      for (int i = 0; i < edgeTarget.size(); ++i) {
        if (edgeTarget.get(i) == 1) {
          GraphNode w = info.get(i + 1);
          if (!w.getVisited()) {
            Q.add(w);
            w.setVisited(true);
          }
        }
      }
    }
    return ops;
  }
Пример #9
0
 public void add(T evalFirstValue, T evalAfterValue) {
   GraphNode<T> firstNode = null;
   GraphNode<T> afterNode = null;
   if (nodes.containsKey(evalFirstValue)) {
     firstNode = nodes.get(evalFirstValue);
   } else {
     firstNode = createNode(evalFirstValue);
     nodes.put(evalFirstValue, firstNode);
   }
   if (nodes.containsKey(evalAfterValue)) {
     afterNode = nodes.get(evalAfterValue);
   } else {
     afterNode = createNode(evalAfterValue);
     nodes.put(evalAfterValue, afterNode);
   }
   firstNode.addGoingOutNode(afterNode);
   afterNode.addComingInNode(firstNode);
 }
Пример #10
0
 /** Visit each predecessor of this node applying the given visitor. Breadth first. */
 private <Alpha, Beta> void doVisitPredecessors(
     Visitor<Alpha, Beta> visitor, Alpha arg1, Beta arg2, Set<GraphNode> seen) {
   if (seen.add(this)) {
     Collection<GraphNode> allKill = null;
     for (Iterator<GraphNode> i = pred.iterator(); i.hasNext(); ) {
       GraphNode pred = i.next();
       List<GraphNode> kill = visitor.visit(pred, this, arg1, arg2);
       if (kill != null) {
         if (allKill == null) allKill = new ArrayList<GraphNode>();
         allKill.addAll(kill);
       }
     }
     if (allKill != null) pred.removeAll(allKill);
     for (Iterator<GraphNode> i = pred.iterator(); i.hasNext(); ) {
       GraphNode pred = i.next();
       pred.doVisitPredecessors(visitor, arg1, arg2, seen);
     }
   }
 }
Пример #11
0
 /**
  * @param fname String file name to read info from
  * @return Map indexed by node id of Node classes
  */
 public Map<Integer, GraphNode> getNodeInfo(String fname) {
   try {
     BufferedReader br = new BufferedReader(new FileReader(fname));
     String line;
     boolean firstLine = true;
     Map<Integer, GraphNode> nodes = new HashMap<>();
     while ((line = br.readLine()) != null) {
       if (firstLine) {
         firstLine = false;
         continue;
       }
       List<String> words = Arrays.asList(line.split("\\s"));
       ArrayList<String> realWords = new ArrayList<>();
       for (String w : words) {
         if (w.equals("")) continue;
         else {
           realWords.add(w);
         }
       }
       if (realWords.size() != 4) {
         continue;
       }
       GraphNode node =
           new GraphNode(
               Integer.parseInt(realWords.get(0)),
               realWords.get(1),
               realWords.get(2),
               realWords.get(3));
       nodes.put(node.getID(), node);
     }
     br.close();
     return nodes;
   } catch (FileNotFoundException e) {
     e.printStackTrace();
     return null;
   } catch (IOException e) {
     e.printStackTrace();
     return null;
   }
 }
Пример #12
0
  /**
   * Iterative DFS algorithm
   *
   * @return List of nodes in order visited
   */
  public ArrayList<String> dfs_iter(boolean print) {
    ArrayList<String> ops = new ArrayList<>();
    GraphNode v = source;
    List<GraphNode> stack = new ArrayList<>();
    stack.add(v);
    while (stack.size() != 0) {
      v = stack.remove(stack.size() - 1);
      if (v.getVisited() == false) {
        if (print) System.out.println(v);
        ops.add(v.toString());
        v.setVisited(true);

        List<Integer> edgeTarget = matrix.get(v.getID());

        for (int i = 0; i < edgeTarget.size(); ++i) {
          if (edgeTarget.get(i) == 1) {
            GraphNode w = info.get(i + 1);
            stack.add(w);
          }
        }
      }
    }
    return ops;
  }
Пример #13
0
 @Override
 GraphNode leadNode(GraphNode unlessSpecified) {
   return leader.leadNode();
 }
Пример #14
0
 public void resetNodes() {
   for (GraphNode node : info.values()) {
     node.setVisited(false);
   }
 }
Пример #15
0
  /**
   * Check if we have found our node
   *
   * @param v Node we are analyzing
   * @param n int size of graph
   * @return True if v is our searched node
   */
  public boolean checkStoppingCondition(GraphNode v, Integer n) {
    if (n == 6) {
      if (Integer.parseInt(v.getPopulation()) > 500000
          && 150 <= Float.parseFloat(v.getElevation())
          && Float.parseFloat(v.getElevation()) <= 200) {
        return true;
      }
    } else if (n == 100) {
      if (v.getCity().equals("SouthAva")
          && Integer.parseInt(v.getPopulation()) == 2042542
          && Float.parseFloat(v.getElevation()) <= 1074.0) {
        return true;
      }

    } else if (n == 1000) {
      if (v.getCity().equals("NewMarlene")
          && Integer.parseInt(v.getPopulation()) == 47182047
          && Float.parseFloat(v.getElevation()) >= 8.0) {
        return true;
      }

    } else if (n == 10000) {
      if (v.getCity().equals("Careyland")
          && Integer.parseInt(v.getPopulation()) <= 5086547
          && Float.parseFloat(v.getElevation()) >= 1645.0) {
        return true;
      }
    }
    return false;
  }
Пример #16
0
 private GraphNode<T> createNode(T value) {
   GraphNode<T> node = new GraphNode<T>();
   node.value = value;
   return node;
 }