Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Java
//THis is my main class
public class MainDriver{
  
    public static void main(String[] args) {
        
        Graph mygraph = new Graph(22);
        mygraph.addEdge(1,2);
        mygraph.addEdge(2,4);
        mygraph.addEdge(2,5);
        mygraph.addEdge(4,8);
        mygraph.addEdge(4,9);
        mygraph.addEdge(8,16);
        mygraph.addEdge(8,17);
        mygraph.addEdge(9,18);
        mygraph.addEdge(9,19);
        mygraph.addEdge(5,10);
        mygraph.addEdge(5,11);
        mygraph.addEdge(10,20);
        mygraph.addEdge(10,21);
        mygraph.addEdge(1,3);
        mygraph.addEdge(3,6);
        mygraph.addEdge(3,7);
        mygraph.addEdge(6,12);
        mygraph.addEdge(6,13);
        mygraph.addEdge(7,14);
        mygraph.addEdge(7,15);
        
    }
      
     DepthFirstSearch dfs = new DepthFirstSearch();
    
}

//This is my DFS class
public class DepthFirstSearch{

private boolean[] marked;
private int count;

public DepthFirstSearch(Graph G, int s) {
    marked = new boolean[G.getV()];
    dfs(G, s);

}

private void dfs(Graph G, int v) {
   
    marked[v] = true;
    System.out.print(v);
    count++;
    
    for (int w : G.adj(v)) {
        if (!marked[w]) {
            dfs(G, w);
        }
       
    }
    
}

public boolean marked(int w) {
    return marked[w];
    
}


public int count() {
    return count;
}

} 

//I can't show any output? how's that?
Posted
Updated 2-Oct-15 15:08pm
v2
Comments
Richard MacCutchan 2-Oct-15 11:39am    
What error, where? Please format your code properly and show where the error occurs. From what I can figure looking at the above your program does not do anything.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900