-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDepthFirstSearch.java
More file actions
95 lines (77 loc) · 1.88 KB
/
Copy pathDepthFirstSearch.java
File metadata and controls
95 lines (77 loc) · 1.88 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import java.util.ArrayList;
import java.util.List;
public class DepthFirstSearch {
static boolean bipartite=true;
public static void main(String[] args){
//Preparing Graph
DFSNode d1=new DFSNode(1);
DFSNode d2=new DFSNode(2);
DFSNode d3=new DFSNode(3);
DFSNode d4=new DFSNode(4);
//DFSNode d5=new DFSNode(5);
d1.addNeighbour(d2);
d1.addNeighbour(d4);
//d2.addNeighbour(d1);
d2.addNeighbour(d3);
d2.addNeighbour(d4);
//d2.addNeighbour(d5);
//d3.addNeighbour(d1);
d3.addNeighbour(d2);
d3.addNeighbour(d4);
d4.addNeighbour(d3);
d4.addNeighbour(d1);
// d4.addNeighbour(d5);
// d5.addNeighbour(d2);
// d5.addNeighbour(d4);
// doDFS(d1);
checkBipartite(d1,0);
//System.out.println(bipartite);
if (bipartite){
System.out.println("Graph is Bipartite!!!");
}
else
System.out.println("Not Bipartite!!");
}
static void doDFS(DFSNode root){
root.visited=true;
System.out.println(root.data);
for(int i=0;i<root.noOfNeighbours;i++){
if(root.neighbours.get(i).visited==false)
doDFS(root.neighbours.get(i));
}
}
static void checkBipartite(DFSNode root,int color){
if (root.visited==false){
root.visited=true;
root.color=color;
//System.out.println(color);
}
for(int i=0;i<root.noOfNeighbours;i++){
if (root.neighbours.get(i).visited==false){
checkBipartite(root.neighbours.get(i),1-color);
}
else{
if (root.neighbours.get(i).color==color){
bipartite= false;
//System.out.println(root.data+"->"+root.neighbours.get(i).data);
}
}
}
}
}
class DFSNode{
boolean visited;
List<DFSNode> neighbours;
int noOfNeighbours;
int data;
int color=-1; //0 for red and 1 for green
public DFSNode(int data) {
this.visited=false;
neighbours=new ArrayList<DFSNode>();
this.data=data;
}
void addNeighbour(DFSNode node){
neighbours.add(node);
noOfNeighbours++;
}
}