コード例 #1
0
 private static List<String> shortestCycle(DirectedGraph<String, DefaultEdge> graph) {
   FloydWarshallShortestPaths<String, DefaultEdge> floyd = new FloydWarshallShortestPaths<>(graph);
   int minDistance = Integer.MAX_VALUE;
   String minSource = null;
   String minDestination = null;
   for (DefaultEdge edge : graph.edgeSet()) {
     String src = graph.getEdgeSource(edge);
     String dst = graph.getEdgeTarget(edge);
     int dist = (int) Math.round(floyd.shortestDistance(dst, src)); // from dst to src
     if (dist < 0) {
       continue;
     }
     if (dist < minDistance) {
       minDistance = dist;
       minSource = src;
       minDestination = dst;
     }
   }
   if (minSource == null) {
     return null;
   }
   GraphPath<String, DefaultEdge> shortestPath = floyd.getShortestPath(minDestination, minSource);
   List<String> pathVertexList = Graphs.getPathVertexList(shortestPath);
   // note: pathVertexList will be [a, a] instead of [a] when the shortest path is a loop edge
   if (!Objects.equals(shortestPath.getStartVertex(), shortestPath.getEndVertex())) {
     pathVertexList.add(pathVertexList.get(0));
   }
   return pathVertexList;
 }
コード例 #2
0
  /*
   * The subroutine of DFS. NOTE: the set is used to distinguish between 1st
   * and 2nd round of DFS. set == null: finished vertices are stored (1st
   * round). set != null: all vertices found will be saved in the set (2nd
   * round)
   */
  private void dfsVisit(
      DirectedGraph<V, E> visitedGraph, VertexData<V> vertexData, Set<V> vertices) {
    Deque<VertexData<V>> stack = new ArrayDeque<VertexData<V>>();
    stack.add(vertexData);

    while (!stack.isEmpty()) {
      VertexData<V> data = stack.removeLast();

      if (!data.isDiscovered()) {
        data.setDiscovered(true);

        if (vertices != null) {
          vertices.add(data.getVertex());
        }

        stack.add(new VertexData1<V>(data, true, true));

        // follow all edges
        for (E edge : visitedGraph.outgoingEdgesOf(data.getVertex())) {
          VertexData<V> targetData = vertexToVertexData.get(visitedGraph.getEdgeTarget(edge));

          if (!targetData.isDiscovered()) {
            // the "recursion"
            stack.add(targetData);
          }
        }
      } else if (data.isFinished()) {
        if (vertices == null) {
          orderedVertices.addFirst(data.getFinishedData());
        }
      }
    }
  }
コード例 #3
0
 /** @see GraphListener#edgeRemoved(GraphEdgeChangeEvent) */
 public void edgeRemoved(GraphEdgeChangeEvent<V, E> e) {
   E edge = e.getEdge();
   V source = graph.getEdgeSource(edge);
   V target = graph.getEdgeTarget(edge);
   if (successorMap.containsKey(source)) {
     successorMap.get(source).removeNeighbor(target);
   }
   if (predecessorMap.containsKey(target)) {
     predecessorMap.get(target).removeNeighbor(source);
   }
 }
コード例 #4
0
ファイル: Main.java プロジェクト: damorim/pbicc
  /** Dot is a popular graph visualization format. See/Install Graphviz */
  private static String toDot(DirectedGraph<String, DefaultEdge> g) {

    StringBuffer sb = new StringBuffer();
    sb.append("digraph mygraph {\n ");
    Set<DefaultEdge> edges = g.edgeSet();
    for (DefaultEdge e : edges) {
      sb.append(g.getEdgeSource(e)./*remove file extension*/ split("\\.")[0]);
      sb.append("->");
      sb.append(g.getEdgeTarget(e)./*remove file extension*/ split("\\.")[0]);
      sb.append(";\n");
    }
    sb.append("}\n");
    return sb.toString();
  }
コード例 #5
0
  private boolean isPrimaryKey(
      DirectedGraph<IStructureElement, DefaultEdge> schema, ISqlElement column) {
    Set<DefaultEdge> outgoingEdges = schema.outgoingEdgesOf(column);

    for (DefaultEdge outgoingEdge : outgoingEdges) {
      IStructureElement element = schema.getEdgeTarget(outgoingEdge);

      if (element instanceof ColumnConstraintVertex
          && ((ColumnConstraintVertex) element)
              .getConstraintType()
              .equals(ConstraintType.PRIMARY_KEY)) {
        return true;
      }
    }

    return false;
  }
コード例 #6
0
ファイル: WriteGraphToDot.java プロジェクト: serialcake/joana
  public static <V, E> void write(DirectedGraph<V, E> g, String fileName, EdgeFilter<E> filter)
      throws FileNotFoundException {
    PrintWriter out = new PrintWriter(fileName);

    // System.out.println("Writing '" + fileName + "'");

    out.print("digraph \"DirectedGraph\" { \n graph [label=\"");
    out.print(g.toString());
    out.print("\", labelloc=t, concentrate=true]; ");
    out.print("center=true;fontsize=12;node [fontsize=12];edge [fontsize=12]; \n");

    for (V node : g.vertexSet()) {
      out.print("   \"");
      out.print(getId(node));
      out.print("\" ");
      out.print("[label=\"");
      out.print(node.toString());
      out.print("\" shape=\"box\" color=\"blue\" ] \n");
    }

    for (V src : g.vertexSet()) {
      for (E e : g.outgoingEdgesOf(src)) {
        if (!filter.accept(e)) {
          continue;
        }

        V tgt = g.getEdgeTarget(e);

        out.print(" \"");
        out.print(getId(src));
        out.print("\" -> \"");
        out.print(getId(tgt));
        out.print("\" ");
        out.print("[label=\"");
        out.print(e.toString());
        out.print("\"]\n");
      }
    }

    out.print("\n}");

    out.flush();
    out.close();
  }
コード例 #7
0
  private void buildStandardGraph() {
    ArrayList<OrderedPair> orderedpairList = orderedPairOrder.getListOfPairs();
    for (int i = 0; i < orderedpairList.size(); i++) {

      // if the nodes do not exists --> add them
      if (orderedpairList.get(i).getChild() != null
          && !(graph.containsVertex(orderedpairList.get(i).getChild()))) {

        graph.addVertex(orderedpairList.get(i).getChild());
      }

      if (orderedpairList.get(i).getChild() != null) {
        if (orderedpairList.get(i).getParent() == null) {
        } else if (!children.contains(orderedpairList.get(i).getChild())) {
          children.add(orderedpairList.get(i).getChild());
        }
      }
      if (orderedpairList.get(i).getParent() != null
          && !(graph.containsVertex(orderedpairList.get(i).getParent()))) {

        graph.addVertex(orderedpairList.get(i).getParent());
      }
      if (orderedpairList.get(i).getParent() != null
          && !parents.contains(orderedpairList.get(i).getParent())) {
        parents.add(orderedpairList.get(i).getParent());
      }

      // fuegt Kante hinzu, falls beide Knoten nicht NULL sind
      if (orderedpairList.get(i).getParent() != null && orderedpairList.get(i).getChild() != null) {
        graph.addEdge(orderedpairList.get(i).getParent(), orderedpairList.get(i).getChild());
        System.out.println(
            orderedpairList.get(i).getParent() + " -> " + orderedpairList.get(i).getChild());
      }
    }
    DirectedGraph<Object, DefaultEdge> temp_gr = new DefaultDirectedGraph<>(DefaultEdge.class);
    for (Object o : graph.vertexSet()) {
      temp_gr.addVertex(o);
    }
    for (DefaultEdge e : graph.edgeSet()) {
      temp_gr.addEdge(graph.getEdgeSource(e), graph.getEdgeTarget(e));
    }
    originalGraph.add(temp_gr);
  }
コード例 #8
0
  /** @see GraphListener#edgeAdded(GraphEdgeChangeEvent) */
  public void edgeAdded(GraphEdgeChangeEvent<V, E> e) {
    E edge = e.getEdge();
    V source = graph.getEdgeSource(edge);
    V target = graph.getEdgeTarget(edge);

    // if a map does not already contain an entry,
    // then skip addNeighbor, since instantiating the map
    // will take care of processing the edge (which has already
    // been added)

    if (successorMap.containsKey(source)) {
      getSuccessors(source).addNeighbor(target);
    } else {
      getSuccessors(source);
    }
    if (predecessorMap.containsKey(target)) {
      getPredecessors(target).addNeighbor(source);
    } else {
      getPredecessors(target);
    }
  }