forked from hussien89aa/DataStructureAndAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFSGraph.java
More file actions
50 lines (47 loc) · 985 Bytes
/
DFSGraph.java
File metadata and controls
50 lines (47 loc) · 985 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.graph;
import java.util.Stack;
public class DFSGraph {
int size;
adjList[] array;
public DFSGraph(int size) {
this.size=size;
array= new adjList[ this.size];
for(int i=0;i<size;i++){
array[i]=new adjList();
array[i].head=null;
}
}
public void add(int src, int dest){
Node n= new Node(dest, null);
n.next= array[src].head;
array[src].head=n;
}
public void DFSExplore( int StartVertex){
Boolean[] visited= new Boolean[size];
for(int i=0;i<size;i++)
visited[i]=false;
Stack<Integer> st= new Stack<Integer>();
st.push(StartVertex);
while( !st.isEmpty()){
int n= st.pop();
st.push(n);
visited[n]=true;
Node head= array[n].head;
Boolean isDone=true;
while(head!=null){
if(visited[head.value]==false){
st.push(head.value);
visited[head.value]=true;
isDone=false;
break;
}else{
head=head.next;
}
}
if(isDone==true){
int out=st.pop();
System.out.println("Visted node: "+ out);
}
}
}
}