1

This DepthFirstTraversal method works. However, it is only passing 9 out of the 13 tests. I have included the test class below with the test which isn't working. Please, could someone help me to understand why it's passing all the tests except this test included.

public class DepthFirstTraversal<T> extends AdjacencyGraph<T> implements Traversal<T> {

    private Queue<T> backTrack = new ArrayDeque<>();
    private List<T> traversal = new ArrayList<T>();

    @Override
    public List<T> traverse() throws GraphError {
        T node = getUnvisitedNode();
        while (node !=null){
            traversal.add(node);
            backTrack.add(node);
            traverseBackTrackList();
            node=getUnvisitedNode();
        }
        return traversal;
    }

    private boolean visited(T node) {
        return
                traversal.contains(node) || backTrack.contains(node);  // check if the node is 
                // in the traversal list
    }

    private T getUnvisitedNode() {
        for (T node: getNodes()) {
            if (!visited(node)) { // if this node has not been "visited"
                return node; // then this is an unvisited node
            }
        }
        return null;
    }

    private void traverseBackTrackList() throws GraphError {
        while (backTrack.size() > 0) { // as long as there are still nodes in the back track 
                                       // list
            T node = backTrack.remove(); // get the next node from the backtrack list
            visitNode(node); // and visit that node
        }
    }

    private void visitNode(T node) throws GraphError {
        if (visited(node)) return; // if the node is already visited do nothing
        traversal.add(node); // add the node to the traversal
        for (T neighbour : getNeighbours(node)) { // for all this node's neighbours
            if (!visited(neighbour))
                backTrack.add(neighbour);
            traversal.add(neighbour);
        }
    }
}
abstract class TraversalTest<T extends Traversal<Integer>> {
    Random random = new Random(System.currentTimeMillis());

    private void testPairedGraph(T graph,List<Integer> traversal) throws GraphError {
        if (graph.getNoOfNodes() == 0) return;
        if (graph.getNoOfNodes()%2 != 0) fail("Graph does not contain an even number of nodes");
        for (int index = 0; index < traversal.size(); index += 2) {
            
        assertEquals(graph.getNoOfNodes(),traversal.get(index)+traversal.get(index+1),"Incorrect 
        neighbouring values in paired graph");
        }

    private void testPaired(int noOfPairs) throws GraphError {
        T graph = generatePairedGraph(noOfPairs);
        List<Integer> traversal = graph.traverse();
        testContents(graph,traversal);
        testPairedGraph(graph,traversal);
    }

    @Test
    void testPaired3() throws GraphError {
        testPaired(3);
    }

    @Test
    void testPaired10() throws GraphError {
        testPaired(10);
    }

    @Test
    void testPaired38() throws GraphError {
        testPaired(38);
    }

    @Test
    void testPairedRandom() throws GraphError {
        int size = random.nextInt(40)+10;
        testPaired(size);
    }
}
mnestorov
  • 4,116
  • 2
  • 14
  • 24
ollie
  • 11
  • 1

0 Answers0