-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbipartite.js
More file actions
111 lines (95 loc) · 2.84 KB
/
bipartite.js
File metadata and controls
111 lines (95 loc) · 2.84 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const GraphBase = require('./graph-base');
const Queue = require('./ds/queue');
class Bipartite extends GraphBase {
constructor(numVertices = 0, numEdges = 0, startingPoint = 0) {
super(numVertices, numEdges);
}
bipartiteCheck() {
let queue = new Queue();
let Adj = this.getSimpleAdj();
// defining the color array and initializing all the vertices initially to -1
var color = [];
color = new Array(this.numVertices);
for (var i = 0; i < this.numVertices; i++) {
color[i] = -1;
}
// initializing a 2D Array for adjacency matrix representation
var graph = [];
for (var i = 0; i < this.numVertices; i++) {
graph[i] = new Array(this.numVertices);
}
//initialinzing all elements of Adjacency matrix =0
for (var i = 0; i < this.numVertices; i++) {
for (var j = 0; j < this.numVertices; j++) {
graph[i][j] = 0;
}
}
// filling up the adjacency matrix
Adj.forEach((value, key) => {
value.forEach((i) => {
graph[key][i] = 1;
});
// console.log(key + '---' + value);
});
// for (var i = 0; i < this.numVertices; i++) {
// for (var j = 0; j < this.numVertices; j++) {
// console.log(i + ',' + j + '=' + graph[i][j]);
// }
// }
var src = 0;
color[src] = 1;
queue.enqueue(0);
var ans = 0;
// Doing a DFS search and for every next element
// if it is not colored then coloring it with opposite color of previous element
// if colored then checking if it is of same color of previos vertex --- then not bipartite
while (!queue.isEmpty()) {
var u = queue.front();
queue.dequeue();
if (graph[u][u]) {
// for a graph with self cycle can never be bipartite
ans = 1;
} else {
for (var v = 0; v < this.numVertices; v++) {
if (graph[u][v] == 1 && color[v] == -1) {
color[v] = 1 - color[u];
queue.enqueue(v);
} else if (graph[u][v] == 1 && color[u] == color[v]) {
ans = 1;
}
}
}
}
if (ans == 1) {
var ans = 'NOT A BIPARTITE GRAPH';
} else {
var ans = 'A BIPARTITE GRAPH';
}
return ans;
}
}
// ----------Ex. of not bipartite graph
// const g = new Bipartite(5);
// vertices = [0, 1, 2, 3]
// for (let i = 0; i < vertices.length; i++) {
// g.addVertex(vertices[i]);
// }
// g.addEdge(0, 1);
// g.addEdge(0, 2);
// g.addEdge(1, 2);
// g.addEdge(2, 0);
// g.addEdge(2, 3);
// g.addEdge(3, 4);
// console.log(g.bipartiteCheck());
// ----------Ex. of a bipartite graph
// const g = new Bipartite(5);
// vertices = [0, 1, 2, 3]
// for (let i = 0; i < vertices.length; i++) {
// g.addVertex(vertices[i]);
// }
// g.addEdge(0, 1);
// g.addEdge(0, 2);
// g.addEdge(1, 3);
// g.addEdge(2, 3);
// console.log(g.bipartiteCheck());
module.exports = Bipartite;