-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.js
More file actions
167 lines (136 loc) · 3.51 KB
/
graph.js
File metadata and controls
167 lines (136 loc) · 3.51 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
class Graph {
constructor() {
this.adjacenecyList = {}
}
addVertex(vertex) {
if (!this.adjacenecyList[vertex]) {
this.adjacenecyList[vertex] = []
}
}
addEdge(vertex1, vertex2) {
this.adjacenecyList[vertex1].push(vertex2)
this.adjacenecyList[vertex2].push(vertex1)
}
printGraph() {
for (let vertex in this.adjacenecyList) {
console.log(vertex, '->', this.adjacenecyList[vertex])
}
}
}
const g = new Graph()
// g.addVertex('A')
// g.addVertex('B')
// g.addVertex('C')
// g.addEdge('A', 'B')
// g.addEdge('A', 'C')
// g.addEdge('B', 'C')
// g.printGraph()
// console.log(g.adjacenecyList)
class DirectedGraph {
constructor() {
this.adjacencyList = {};
}
addVertex(vertex) {
if (!this.adjacencyList[vertex]) {
this.adjacencyList[vertex] = [];
}
}
addEdge(vertex1, vertex2) {
this.adjacencyList[vertex1].push(vertex2);
}
printGraph() {
for (let vertex in this.adjacencyList) {
console.log(vertex, '->', this.adjacencyList[vertex]);
}
}
}
// Usage
const dg = new DirectedGraph();
// dg.addVertex('A');
// dg.addVertex('B');
// dg.addVertex('C');
// dg.addEdge('A', 'B');
// dg.addEdge('B', 'C');
// dg.printGraph();
function isBipartite(graph) {
const color = {};
for (let node in graph) {
if (!(node in color)) {
const queue = [node];
color[node] = 1;
while (queue.length) {
const cur = queue.shift();
for (let neighbor of graph[cur]) {
if (!(neighbor in color)) {
color[neighbor] = -color[cur];
queue.push(neighbor);
} else if (color[neighbor] === color[cur]) {
return false;
}
}
}
}
}
return true;
}
const graph = {
A: ["X"], X: ["A", "B"],
B: ["Y"], Y: ["B", "C"],
C: ["Z"], Z: ["C"]
};
// console.log(isBipartite(graph));
class Graph1 {
constructor() {
this.adjacencyList = {}
}
addVertex(vertex) {
if (!this.adjacencyList[vertex]) {
this.adjacencyList[vertex] = []
}
}
addEdge(vertex1, vertex2) {
this.adjacencyList[vertex1].push(vertex2)
this.adjacencyList[vertex2].push(vertex1)
}
dfs(start, visited = new Set()) {
console.log('Visiting:',start)
visited.add(start)
for(let neighbor of this.adjacencyList[start]){
if(!visited.has(neighbor)){
this.dfs(neighbor, visited)
}
}
}
bfs(start){
let queue = [start]
let visited = new Set()
visited.add(start)
while(queue.length > 0){
let vertex = queue.shift()
console.log('visiting: ', vertex)
for(let neighbor of this.adjacencyList[vertex]){
if(!visited.has(neighbor)){
visited.add(neighbor)
queue.push(neighbor)
}
}
}
}
printGraph() {
for (let vertex in this.adjacencyList) {
console.log(vertex, ' -> ', this.adjacencyList[vertex])
}
}
}
const g1 = new Graph1()
g1.addVertex('A')
g1.printGraph()
g1.addVertex('B')
g1.addVertex('C')
g1.addEdge('A', 'B')
g1.addEdge('A', 'C')
g1.addEdge('B', 'C')
g1.printGraph()
console.log(g1.adjacencyList)
g1.dfs('A')
g1.bfs('A')