David Park
David Park

Reputation: 1

How to stop my iterator from skipping the first inputted value?

I am trying to print out the Depth First Traversal of my "graph" program. The DepthFirstTraversal(int v) method is supposed to start from the first index, which is supposed to be A. However, it seems like it is skipping this. Any suggestions?

I have tried changing the value v from 1 to 0, but this just prints an extra 0 on top of the same code.

    import java.util.Arrays;
    import java.util.Iterator;

    public class Graph {
        private boolean[][] edges;
        private int[] labels;
        private int N; //number of vertices;

        //constructor. This constructor will create a matrix of size n by n.
        public Graph(int n) {
            N=n;
            edges = new boolean[n][n];
            labels = new int[n];
        }

        //this method will allow user to add an edge
        public void addEdge(int source, int target) {
            edges[source][target] = true;
        }

        //this method will return the label of the vertex
        public int getLabel(int vertex) {
            return labels[vertex];
        }

        //this method will allow user to remove an edge
        public void removeEdge(int source, int target) {
            edges[source][target] = false;
        }

        //this method will allow user to set a label
        public void setLabels(int vertex, int newLabel) {
            labels[vertex] = newLabel;
        }

        //this method will return the size of the labels array
        public int size() {
            return labels.length;
        }

        //this method will grab the neighbors of the desired vertex
        public int[] neighbors(int vertex) {
            int i;
            int counter = 0;
            int[] result;

            for (i = 0; i < labels.length; i++) {
                if (edges[vertex][i])
                    counter++;
            }
            result = new int[counter];
            counter = 0;
            for (i = 0; i < labels.length; i++) {
                result[counter++] = i;
            }
            return result;
        }

        //this method will print out the vertices starting from the first value
        I tried fixing my code a little, but I ran into another problem.
I do have to use neighbors method and also I cannot use recursion.

public void DepthFirstTraversal(int v) {
        boolean[] visited = new boolean[N];
        Stack<Integer> stack = new Stack<>();
        stack.add(v);

        visited[v]=true;
        while(!stack.isEmpty()){
            int i = stack.pop();
            if (i == 1) {
                System.out.print("A" + "-");
            } else if (i == 2) {
                System.out.print("B" + "-");
            } else if (i == 3) {
                System.out.print("C" + "-");
            } else if (i == 4) {
                System.out.print("D" + "-");
            } else if (i == 5) {
                System.out.print("E" + "-");
            } else if (i == 6) {
                System.out.print("F" + "-");
            } else if (i == 7) {
                System.out.print("G" + "-");
            } else if (i == 8) {
                System.out.print("H" + "-");
            } else if (i == 9) {
                System.out.print("I" + "-");
            }
            System.out.print(labels[i] + " \n");

            int[] neighborsOfI = neighbors(i);
            for(int k=0;k<neighborsOfI.length;k++){
                int neighborTest = neighborsOfI[k];
                if(!visited[neighborTest]){
                    stack.add(neighborTest);
                    visited[neighborTest]=true;
                }
            }
        }
    }
}

public class graphDemo {
    public static void main(String args[]){
        Graph graph = new Graph(10);
        graph.addEdge(1,1);
        graph.addEdge(2,3);
        graph.addEdge(3,5);
        graph.addEdge(4,7);
        graph.addEdge(5,9);
        graph.addEdge(6,2);
        graph.addEdge(7,3);
        graph.addEdge(8,5);
        graph.addEdge(9,8);

        graph.setLabels(1,9);
        graph.setLabels(2,3);
        graph.setLabels(3,5);
        graph.setLabels(4,7);
        graph.setLabels(5,4);
        graph.setLabels(6,8);
        graph.setLabels(7,6);
        graph.setLabels(8,2);
        graph.setLabels(9,1);

        System.out.println("Depth First Traversal from first value: \n");
        graph.DepthFirstTraversal(1);

    }
}

I am expecting the Depth First Traversal to start from A and follow the Depth First Traversal until the last element in the graph, but instead I am outputting this:

Depth First Traversal from first value:

A-1 I-9

Upvotes: 0

Views: 49

Answers (1)

Breina
Breina

Reputation: 537

Java is 0-indexed, meaning that arrays start at index 0. You were creating an array of size 10, but using the latter 9 entries. I changed your input data to match this, but it wasn't the issue.

public class GraphDemo {
    public static void main(String args[]) {
        Graph graph = new Graph(9);
        graph.addEdge(0, 0);
        graph.addEdge(1, 2);
        graph.addEdge(2, 4);
        graph.addEdge(3, 6);
        graph.addEdge(4, 8);
        graph.addEdge(5, 1);
        graph.addEdge(6, 2);
        graph.addEdge(7, 4);
        graph.addEdge(8, 7);

        graph.setLabels(0,9);
        graph.setLabels(1,3);
        graph.setLabels(2,5);
        graph.setLabels(3,7);
        graph.setLabels(4,4);
        graph.setLabels(5,8);
        graph.setLabels(6,6);
        graph.setLabels(7,2);
        graph.setLabels(8,1);

        System.out.println("Depth First Traversal from first value: \n");
        graph.depthFirstTraversal(1);

    }
}

I'm not sure what labels are or mean, so I've ignored them completely along with the neighbors which iterates through these labels. Here's the depth first traversal either way:

    private void depthFirstTraversal(int v, boolean[] visitedIndexes) {
        if (visitedIndexes[v]) {
            System.out.println("Arrived at index " + v +
                    " with letter " + (char) ('A' + v) +
                    " but we've already been here, so skipping this.");
            return;
        }

        System.out.println("Traversing index " + v +
                " which has label " + labels[v] +
                " and here's some letter " + (char) ('A' + v)
        );
        visitedIndexes[v] = true;

        for (int i = 0; i < n; i++) {
            if (edges[v][i]) {
                depthFirstTraversal(i, visitedIndexes);
            }
        }
    }

Upvotes: 1

Related Questions