|
1 | 1 | class GraphUnweightedUndirected {
|
2 | 2 | // Unweighted Undirected Graph class
|
3 | 3 | constructor() {
|
4 |
| - this.connections = {} |
| 4 | + this.connections = {}; |
5 | 5 | }
|
6 | 6 |
|
7 | 7 | addNode(node) {
|
8 | 8 | // Function to add a node to the graph (connection represented by set)
|
9 |
| - this.connections[node] = new Set() |
| 9 | + this.connections[node] = new Set(); |
10 | 10 | }
|
11 | 11 |
|
12 | 12 | addEdge(node1, node2) {
|
13 | 13 | // Function to add an edge (adds the node too if they are not present in the graph)
|
14 | 14 | if (!(node1 in this.connections)) {
|
15 |
| - this.addNode(node1) |
| 15 | + this.addNode(node1); |
16 | 16 | }
|
17 | 17 | if (!(node2 in this.connections)) {
|
18 |
| - this.addNode(node2) |
| 18 | + this.addNode(node2); |
19 | 19 | }
|
20 |
| - this.connections[node1].add(node2) |
21 |
| - this.connections[node2].add(node1) |
| 20 | + this.connections[node1].add(node2); |
| 21 | + this.connections[node2].add(node1); |
22 | 22 | }
|
23 | 23 |
|
24 | 24 | DFSIterative(node, value) {
|
25 | 25 | // DFS Function to search if a node with the given value is present in the graph
|
26 |
| - if (!(node in this.connections)) { // Changed 'startNode' to 'node' |
27 |
| - console.log(`Start node ${node} does not exist in the graph.`); // Updated the log message |
28 |
| - return false; // Early return if the node doesn't exist |
29 |
| - } |
30 |
| - |
31 |
| - const stack = [node] |
32 |
| - const visited = new Set() |
| 26 | + if (!(node in this.connections)) { // Changed 'startNode' to 'node' |
| 27 | + console.log(`Start node ${node} does not exist in the graph.`); // Updated the log message |
| 28 | + return false; // Early return if the node doesn't exist |
| 29 | + } |
| 30 | + |
| 31 | + const stack = [node]; |
| 32 | + const visited = new Set(); |
33 | 33 | while (stack.length > 0) {
|
34 |
| - const currNode = stack.pop() |
35 |
| - // if the current node contains the value being searched for, true is returned |
| 34 | + const currNode = stack.pop(); |
| 35 | + // If the current node contains the value being searched for, true is returned |
36 | 36 | if (currNode === value) {
|
37 |
| - return true |
| 37 | + return true; |
38 | 38 | }
|
39 |
| - // adding the current node to the visited set |
40 |
| - visited.add(currNode) |
41 |
| - // adding neighbours in the stack |
| 39 | + // Adding the current node to the visited set |
| 40 | + visited.add(currNode); |
| 41 | + // Adding neighbors in the stack |
42 | 42 | for (const neighbour of this.connections[currNode]) {
|
43 | 43 | if (!visited.has(neighbour)) {
|
44 |
| - stack.push(neighbour) |
| 44 | + stack.push(neighbour); |
45 | 45 | }
|
46 | 46 | }
|
47 | 47 | }
|
48 |
| - return false |
| 48 | + return false; |
49 | 49 | }
|
50 | 50 | }
|
51 | 51 |
|
52 |
| -export { GraphUnweightedUndirected } |
53 |
| - |
54 |
| -// Example |
| 52 | +export { GraphUnweightedUndirected }; |
55 | 53 |
|
56 |
| -// const graph = new GraphUnweightedUndirected() |
57 |
| -// graph.addEdge(1, 2) |
58 |
| -// graph.addEdge(2, 3) |
59 |
| -// graph.addEdge(2, 4) |
60 |
| -// graph.addEdge(3, 5) |
61 |
| -// graph.DFSIterative(5, 1) |
62 |
| -// graph.DFSIterative(5, 100) |
| 54 | +// Example usage |
| 55 | +// const graph = new GraphUnweightedUndirected(); |
| 56 | +// graph.addEdge(1, 2); |
| 57 | +// graph.addEdge(2, 3); |
| 58 | +// graph.addEdge(2, 4); |
| 59 | +// graph.addEdge(3, 5); |
| 60 | +// console.log(graph.DFSIterative(5, 1)); // Should print true or false |
| 61 | +// console.log(graph.DFSIterative(5, 100)); // Should print false |
0 commit comments