Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

java - Passing Depth first traversal test

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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...