Example #1
0
 /**
  * Returns a new UDGraph with the same vertices as "this" UDGraph. The new graph has an edge (v,
  * w) if and only if there is a path of length 2 from v to w in "this" graph. *** DO NOT CHANGE
  * "this" GRAPH!!! ***
  *
  * @return the new UDGraph.
  */
 public UDGraph length2Paths() {
   UDGraph newGraph = new UDGraph(vertices);
   for (int k = 0; k < vertices; k++) {
     for (int i = 0; i < vertices; i++) {
       for (int j = 0; j < vertices; j++) {
         if (hasEdge(i, k) == true && hasEdge(k, j) == true) {
           newGraph.addEdge(i, j);
         }
       }
     }
   }
   return newGraph;
 }
Example #2
0
 /**
  * Returns a new UDGraph with the same vertices as "this" UDGraph. The new graph has an edge (v,
  * w) if and only if there is a path of length "length" from v to w in "this" graph.
  *
  * @param length the length of paths used to construct the new graph.
  * @return the new UDGraph.
  */
 public UDGraph paths(int length) {
   UDGraph newGraph = new UDGraph(vertices);
   UDGraph storageGraph = new UDGraph(vertices);
   int l = length;
   if (l == 2) {
     newGraph = this.length2Paths();
   } else {
     storageGraph = paths(l - 1);
     for (int k = 0; k < vertices; k++) {
       for (int i = 0; i < vertices; i++) {
         if (storageGraph.hasEdge(i, k) == true) {
           for (int j = 0; j < vertices; j++) {
             if (hasEdge(k, j) == true) {
               newGraph.addEdge(i, j);
             }
           }
         }
       }
     }
   }
   return newGraph;
 }
Example #3
0
  public static void main(String[] args) {

    System.out.println("\n *** Lab 12:  " + "Square the unweighted directed graph! *** \n");

    // Create an 11-vertex graph.
    System.out.println("Creating a graph with 11 vertices");
    UDGraph graph = new UDGraph(11);
    graph.addEdge(0, 8);
    graph.addEdge(1, 0);
    graph.addEdge(1, 3);
    graph.addEdge(2, 0);
    graph.addEdge(3, 2);
    graph.addEdge(3, 5);
    graph.addEdge(4, 2);
    graph.addEdge(4, 5);
    graph.addEdge(5, 7);
    graph.addEdge(5, 9);
    graph.addEdge(6, 4);
    graph.addEdge(6, 7);
    graph.addEdge(8, 4);
    graph.addEdge(8, 6);
    graph.addEdge(8, 10);
    graph.addEdge(9, 1);
    graph.addEdge(10, 6);

    boolean goodJob = true;

    String t1String =
        "11 vertices and 17 edges\n. . . . . . . . t . .\n"
            + "t . . t . . . . . . .\nt . . . . . . . . . .\n. . t . . t . . . . .\n"
            + ". . t . . t . . . . .\n. . . . . . . t . t .\n. . . . t . . t . . .\n"
            + ". . . . . . . . . . .\n. . . . t . t . . . t\n. t . . . . . . . . .\n"
            + ". . . . . . t . . . .\n";
    System.out.println("\nThe original graph is\n" + graph);
    if (!t1String.equals(graph.toString())) {
      System.out.println("Error:  the original graph should be\n" + t1String);
      goodJob = false;
    }

    // Do length-2 paths work?
    String t2String =
        "11 vertices and 25 edges\n. . . . t . t . . . t\n"
            + ". . t . . t . . t . .\n. . . . . . . . t . .\nt . . . . . . t . t .\n"
            + "t . . . . . . t . t .\n. t . . . . . . . . .\n. . t . . t . . . . .\n"
            + ". . . . . . . . . . .\n. . t . t t t t . . .\nt . . t . . . . . . .\n"
            + ". . . . t . . t . . .\n";
    System.out.println("Testing length-2 paths.");
    System.out.println("The graph of length-2 paths is\n" + graph.length2Paths());
    if (!t2String.equals(graph.length2Paths().toString())) {
      System.out.println("Error:  the length-2 path graph should be\n" + t2String);
      goodJob = false;
    }

    // Do length-3 paths work?
    String t3String =
        "11 vertices and 34 edges\n. . t . t t t t . . .\n"
            + "t . . . t . t t . t t\n. . . . t . t . . . t\n. t . . . . . . t . .\n"
            + ". t . . . . . . t . .\nt . . t . . . . . . .\nt . . . . . . t . t .\n"
            + ". . . . . . . . . . .\nt . t . t t . t . t .\n. . t . . t . . t . .\n"
            + ". . t . . t . . . . .\n";
    System.out.println("Testing length-3 paths.");
    System.out.println("The graph of length-3 paths is\n" + graph.paths(3));
    if (!t3String.equals(graph.paths(3).toString())) {
      System.out.println("Error:  the length-3 path graph should be\n" + t3String);
      goodJob = false;
    }

    // Do length-4 paths work?
    String t4String =
        "11 vertices and 49 edges\nt . t . t t . t . t .\n"
            + ". t t . t t t t t . .\n. . t . t t t t . . .\nt . . t t . t . . . t\n"
            + "t . . t t . t . . . t\n. . t . . t . . t . .\n. t . . . . . . t . .\n"
            + ". . . . . . . . . . .\nt t t . . t . t t t .\nt . . . t . t t . t t\n"
            + "t . . . . . . t . t .\n";
    System.out.println("Testing length-4 paths.");
    System.out.println("The graph of length-4 paths is\n" + graph.paths(4));
    if (!t4String.equals(graph.paths(4).toString())) {
      System.out.println("Error:  the length-4 path graph should be\n" + t4String);
      goodJob = false;
    }

    // Do length-5 paths work?
    String t5String =
        "11 vertices and 63 edges\nt t t . . t . t t t .\n"
            + "t . t t t t t t . t t\nt . t . t t . t . t .\n. . t . t t t t t . .\n"
            + ". . t . t t t t t . .\nt . . . t . t t . t t\nt . . t t . t . . . t\n"
            + ". . . . . . . . . . .\nt t . t t . t t t t t\n. t t . t t t t t . .\n"
            + ". t . . . . . . t . .\n";
    System.out.println("Testing length-5 paths.");
    System.out.println("The graph of length-5 paths is\n" + graph.paths(5));
    if (!t5String.equals(graph.paths(5).toString())) {
      System.out.println("Error:  the length-5 path graph should be\n" + t5String);
      goodJob = false;
    }

    if (goodJob) {
      System.out.println(" *** Good Job! *** \n");
    }
  }