static {
    TEST_ADJACENCY_LIST.add(V1, V2);
    TEST_ADJACENCY_LIST.add(V2, V3);
    TEST_ADJACENCY_LIST.add(V3, V1);
    TEST_ADJACENCY_LIST.add(V3, V4);

    TEST_REVERS_ADJACENCY_LIST.add(V2, V1);
    TEST_REVERS_ADJACENCY_LIST.add(V3, V2);
    TEST_REVERS_ADJACENCY_LIST.add(V1, V3);
    TEST_REVERS_ADJACENCY_LIST.add(V4, V3);

    TEST_FINISHING_TIME.add(V4);
    TEST_FINISHING_TIME.add(V3);
    TEST_FINISHING_TIME.add(V2);
    TEST_FINISHING_TIME.add(V1);

    TEST_LEADER_COTUNTERS.put(V3, new BigInteger("3"));
    TEST_LEADER_COTUNTERS.put(V4, new BigInteger("1"));

    TEST_RESULT.add(new BigInteger("3"));
    TEST_RESULT.add(new BigInteger("1"));
    TEST_RESULT.add(new BigInteger("0"));
    TEST_RESULT.add(new BigInteger("0"));
    TEST_RESULT.add(new BigInteger("0"));
  }
 @Test
 public void testContainsEdge() {
   AdjacencyList<String, Integer> myGraph = new AdjacencyList<String, Integer>();
   boolean added = myGraph.addEdge("A", "B", new Integer(5));
   assertTrue(added);
   assertEquals(new Integer(5), myGraph.getEdge("A", "B"));
 }
 @Test
 public void testContainsNode() {
   AdjacencyList<String, Integer> myGraph = new AdjacencyList<String, Integer>();
   boolean added = myGraph.addNode("B");
   assertTrue(added);
   assertTrue(myGraph.containsNode("B"));
 }
 @Test
 public void testRemoveConnectedNode() {
   AdjacencyList<String, Integer> myGraph = new AdjacencyList<String, Integer>();
   boolean added = myGraph.addEdge("A", "B", new Integer(42));
   assertTrue(added);
   assertTrue(myGraph.adjacent("A", "B"));
   myGraph.removeNode("A");
   assertFalse(myGraph.adjacent("A", "B"));
 }
 @Test
 public void testGetNodes1Node() {
   AdjacencyList<String, Integer> myGraph = new AdjacencyList<String, Integer>();
   boolean added = myGraph.addNode("A");
   assertTrue(added);
   Set<String> allNodes = myGraph.getNodes();
   assertEquals(1, allNodes.size());
   assertTrue(allNodes.contains("A"));
 }
 @Test
 public void testRemoveEdge() {
   AdjacencyList<String, Integer> myGraph = new AdjacencyList<String, Integer>();
   boolean added = myGraph.addEdge("A", "B", new Integer(42));
   assertTrue(added);
   assertTrue(myGraph.adjacent("A", "B"));
   Integer edgeRemoved = myGraph.removeEdge("A", "B");
   assertEquals(new Integer(42), edgeRemoved);
   assertFalse(myGraph.adjacent("A", "B"));
 }
 @Test
 public void testSelfNode() {
   AdjacencyList<String, Integer> myGraph = new AdjacencyList<String, Integer>();
   boolean added = myGraph.addEdge("A", "A", new Integer(42));
   assertFalse(added);
   Set<String> allNodes = myGraph.getNodes();
   assertEquals(0, allNodes.size());
   assertFalse(allNodes.contains("A"));
   assertFalse(allNodes.contains("B"));
 }
 @Test
 public void testContains2() {
   AdjacencyList<String, Integer> myGraph = new AdjacencyList<String, Integer>();
   // contains nodes A and B after adding edge A->B
   boolean added = myGraph.addEdge("A", "B", new Integer(42));
   assertTrue(added);
   assertTrue(myGraph.containsNode("A"));
   assertTrue(myGraph.containsNode("B"));
   // still contains nodes A and B after removing edge A->B
   Integer edgeRemoved = myGraph.removeEdge("A", "B");
   assertEquals(new Integer(42), edgeRemoved);
   assertTrue(myGraph.containsNode("A"));
   assertTrue(myGraph.containsNode("B"));
 }
예제 #9
0
  /**
   * compute the transitive closure of the input graph
   *
   * @param <NodeLabel>
   * @param <EdgeLabel>
   * @param original
   * @return
   */
  public static <NodeLabel, EdgeLabel> Graph<NodeLabel, EdgeLabel> transitiveClosure(
      Graph<NodeLabel, EdgeLabel> input) {
    Graph<NodeLabel, EdgeLabel> graph = AdjacencyList.copy(input);

    int numChange = 0;
    do {
      numChange = 0;
      for (NodeLabel from : graph.getNodeLabelSet()) {
        for (NodeLabel to : graph.getNodeLabelSet()) {
          if (graph.hasEdge(from, to)) continue;

          int destNodeID = graph.getNodeID(to);
          for (Edge startPoint : graph.getOutEdgeSet(from)) {
            int intermediateNodeID = startPoint.getDestNodeID();

            if (graph.hasEdge(new Edge(intermediateNodeID, destNodeID))) {
              graph.addEdge(from, to);
              ++numChange;
              break;
            }
          }
        }
      }
    } while (numChange > 0);

    return graph;
  }
예제 #10
0
 @Test
 public void testContains3() {
   AdjacencyList<String, Integer> myGraph = new AdjacencyList<String, Integer>();
   myGraph.addNode("A");
   assertTrue(myGraph.containsNode("A"));
   assertFalse(myGraph.containsNode("B"));
   myGraph.addNode("B");
   assertTrue(myGraph.containsNode("A"));
   assertTrue(myGraph.containsNode("B"));
   myGraph.removeNode("A");
   assertFalse(myGraph.containsNode("A"));
   assertTrue(myGraph.containsNode("B"));
 }
  public static void main(String[] args) {

    String inputFileName = args[0];
    String rootNode = args[1];

    AdjacencyList adjacencyList = new AdjacencyList(inputFileName);
    nodes.addAll(adjacencyList.getAdjacencyList().keySet());

    Map<Node, List<Edge>> list = adjacencyList.getAdjacencyList();
    for (Node node : list.keySet()) {
      List<Edge> edgeList = list.get(node);
      edges.addAll(edgeList);
    }

    // print header
    System.out.println("Node \t Cost \t Next-Hop");

    // perform dijkstras for each node as the destination from the root node
    Node source = null;
    for (Node node : adjacencyList.getAdjacencyList().keySet()) {
      if (node.getAddr() == Integer.valueOf(rootNode)) {
        source = node;
        break;
      }
    }

    if (source != null) {
      execute(source);
    }

    for (Node node : adjacencyList.getAdjacencyList().keySet()) {

      if (node.getAddr() != source.getAddr()) {
        LinkedList<Node> path = getPath(node);
        System.out.println(
            node.getAddr() + " \t " + distance.get(node) + " \t " + path.get(1).getAddr());
      }
    }
  }
예제 #12
0
  @Test
  public void testNeighbors() {
    AdjacencyList<String, Integer> myGraph = new AdjacencyList<String, Integer>();
    boolean added = myGraph.addEdge("A", "B", new Integer(42));
    assertTrue(added);
    added = myGraph.addEdge("B", "C", new Integer(18));
    assertTrue(added);
    added = myGraph.addEdge("C", "D", new Integer(52));
    assertTrue(added);
    added = myGraph.addEdge("C", "A", new Integer(18));
    assertTrue(added);

    Set<String> neighborsOfA = myGraph.neighbors("A");
    assertEquals(1, neighborsOfA.size());
    assertFalse(neighborsOfA.contains("A"));
    assertTrue(neighborsOfA.contains("B"));
    assertFalse(neighborsOfA.contains("C"));
    assertFalse(neighborsOfA.contains("D"));

    Set<String> neighborsOfB = myGraph.neighbors("B");
    assertEquals(1, neighborsOfB.size());
    assertFalse(neighborsOfB.contains("A"));
    assertFalse(neighborsOfB.contains("B"));
    assertTrue(neighborsOfB.contains("C"));
    assertFalse(neighborsOfB.contains("D"));

    Set<String> neighborsOfC = myGraph.neighbors("C");
    assertEquals(2, neighborsOfC.size());
    assertTrue(neighborsOfC.contains("A"));
    assertFalse(neighborsOfC.contains("B"));
    assertFalse(neighborsOfC.contains("C"));
    assertTrue(neighborsOfC.contains("D"));

    Set<String> neighborsOfD = myGraph.neighbors("D");
    assertEquals(0, neighborsOfD.size());
    assertFalse(neighborsOfD.contains("A"));
    assertFalse(neighborsOfD.contains("B"));
    assertFalse(neighborsOfD.contains("C"));
    assertFalse(neighborsOfD.contains("D"));
  }
예제 #13
0
 @Test
 public void testNeighborsUnconnected() {
   AdjacencyList<String, Integer> myGraph = new AdjacencyList<String, Integer>();
   boolean added = myGraph.addNode("A");
   assertTrue(added);
   added = myGraph.addNode("B");
   assertTrue(added);
   added = myGraph.addNode("C");
   assertTrue(added);
   Set<String> neighborsOfA = myGraph.neighbors("A");
   assertEquals(0, neighborsOfA.size());
   assertFalse(neighborsOfA.contains("A"));
   assertFalse(neighborsOfA.contains("B"));
   assertFalse(neighborsOfA.contains("C"));
   // Neighbors for a node not in the graph
   Set<String> neighborsOfD = myGraph.neighbors("D");
   assertEquals(0, neighborsOfD.size());
   assertFalse(neighborsOfD.contains("A"));
   assertFalse(neighborsOfD.contains("B"));
   assertFalse(neighborsOfD.contains("C"));
 }
예제 #14
0
 @Test
 public void testAdjacent() {
   AdjacencyList<String, Integer> myGraph = new AdjacencyList<String, Integer>();
   // A ; B
   myGraph.addNode("A");
   myGraph.addNode("B");
   assertFalse(myGraph.adjacent("A", "B"));
   assertFalse(myGraph.adjacent("B", "A"));
   // A -> B
   myGraph.addEdge("A", "B", new Integer(18));
   assertTrue(myGraph.adjacent("A", "B"));
   assertFalse(myGraph.adjacent("B", "A"));
   // A -> B ; C
   myGraph.addNode("C");
   assertTrue(myGraph.adjacent("A", "B"));
   assertFalse(myGraph.adjacent("A", "C"));
   assertFalse(myGraph.adjacent("B", "A"));
   assertFalse(myGraph.adjacent("B", "C"));
   assertFalse(myGraph.adjacent("C", "A"));
   assertFalse(myGraph.adjacent("C", "B"));
   // C -> A -> B
   myGraph.addEdge("C", "A", new Integer(42));
   assertTrue(myGraph.adjacent("A", "B"));
   assertFalse(myGraph.adjacent("A", "C"));
   assertFalse(myGraph.adjacent("B", "A"));
   assertFalse(myGraph.adjacent("B", "C"));
   assertTrue(myGraph.adjacent("C", "A"));
   assertFalse(myGraph.adjacent("C", "B"));
 }