public static void main(String[] args) {
    String filename = args[0];
    String delimiter = args[1];
    String source = args[2];

    StdOut.println("Zrodlo: " + source);

    SymbolGraph sg = new SymbolGraph(filename, delimiter);
    Graph G = sg.G();
    if (!sg.contains(source)) {
      StdOut.println(source + " nie wystepuje w bazie danych.");
      return;
    }

    int s = sg.index(source);
    BreadthFirstPaths bfs = new BreadthFirstPaths(G, s);

    while (!StdIn.isEmpty()) {
      String sink = StdIn.readLine();
      if (sg.contains(sink)) {
        int t = sg.index(sink);
        if (bfs.hasPathTo(t)) {
          for (int v : bfs.pathTo(t)) {
            StdOut.println("   " + sg.name(v));
          }
        } else {
          StdOut.println("Brak polaczenia");
        }
      } else {
        StdOut.println("   Nie wystepuje w bazie danych.");
      }
    }
  }
Beispiel #2
0
  /**
   * Reads in a social network from a file, and then repeatedly reads in individuals from standard
   * input and prints out their degrees of separation. Takes three command-line arguments: the name
   * of a file, a delimiter, and the name of the distinguished individual. Each line in the file
   * contains the name of a vertex, followed by a list of the names of the vertices adjacent to that
   * vertex, separated by the delimiter.
   */
  public static void main(String[] args) {
    String filename = args[0];
    String delimiter = args[1];
    String source = args[2];

    // StdOut.println("Source: " + source);

    SymbolGraph sg = new SymbolGraph(filename, delimiter);
    Graph G = sg.G();
    if (!sg.contains(source)) {
      StdOut.println(source + " not in database.");
      return;
    }

    int s = sg.index(source);
    BreadthFirstPaths bfs = new BreadthFirstPaths(G, s);

    while (!StdIn.isEmpty()) {
      String sink = StdIn.readLine();
      if (sg.contains(sink)) {
        int t = sg.index(sink);
        if (bfs.hasPathTo(t)) {
          for (int v : bfs.pathTo(t)) {
            StdOut.println("   " + sg.name(v));
          }
        } else {
          StdOut.println("Not connected");
        }
      } else {
        StdOut.println("   Not in database.");
      }
    }
  }
  /** @param args */
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Graph g = new Graph(7);
    g.addEdge(0, 1);
    g.addEdge(0, 2);
    g.addEdge(1, 3);
    g.addEdge(2, 4);
    g.addEdge(4, 5);
    g.addEdge(0, 6);
    BreadthFirstPaths test = new BreadthFirstPaths(g, 0);

    for (int w = 0; w < g.V(); w++) {
      System.out.println(test.distTo(g, w));
    }
  }
  // Determines whether a digraph has an Eulerian cycle using necessary
  // and sufficient conditions (without computing the cycle itself):
  //    - at least one edge
  //    - indegree(v) = outdegree(v) for every vertex
  //    - the graph is connected, when viewed as an undirected graph
  //      (ignoring isolated vertices)
  private static boolean hasEulerianCycle(Digraph G) {

    // Condition 0: at least 1 edge
    if (G.E() == 0) return false;

    // Condition 1: indegree(v) == outdegree(v) for every vertex
    for (int v = 0; v < G.V(); v++) if (G.outdegree(v) != G.indegree(v)) return false;

    // Condition 2: graph is connected, ignoring isolated vertices
    Graph H = new Graph(G.V());
    for (int v = 0; v < G.V(); v++) for (int w : G.adj(v)) H.addEdge(v, w);

    // check that all non-isolated vertices are conneted
    int s = nonIsolatedVertex(G);
    BreadthFirstPaths bfs = new BreadthFirstPaths(H, s);
    for (int v = 0; v < G.V(); v++) if (H.degree(v) > 0 && !bfs.hasPathTo(v)) return false;

    return true;
  }