Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions L-A/0003 DFSGRaph/DFSGraph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const DFSGraphTraversal = (graph, start, visited = new Set()) => {
console.log(start);
visited.add(start);
for (let child = 0; child < graph[start].length; child++) {
if (visited.has(graph[start][child]) === false) {
DFSGraphTraversal(graph, graph[start][child], visited);
}
}
};

graph_edges = [
[1, 2],
[1, 3],
[2, 3],
[3, 4],
[2, 4],
[4, 6],
[5, 6],
[1, 6],
];

graph = {};

for (let i = 0; i < graph_edges.length; i++) {
if (graph_edges[i][0] in graph === false) {
graph[graph_edges[i][0]] = [];
graph[graph_edges[i][0]].push(graph_edges[i][1]);
} else {
graph[graph_edges[i][0]].push(graph_edges[i][1]);
}

if (graph_edges[i][1] in graph === false) {
graph[graph_edges[i][1]] = [];
graph[graph_edges[i][1]].push(graph_edges[i][0]);
} else {
graph[graph_edges[i][1]].push(graph_edges[i][0]);
}
}

console.log(graph);

DFSGraphTraversal(graph, 5);
58 changes: 58 additions & 0 deletions L-A/0003 DFSGRaph/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 0003 DFSGraph ( L-A )

## Problem

Implement a JavaScript function that implements a depth-first search (DFS) algorithm to traverse a graph.

## Test Case

```javascript
graph_edges = [
[1, 2],
[1, 3],
[2, 3],
[3, 4],
[2, 4],
[4, 6],
[5, 6],
[1, 6],
];
```

## Solution

```javascript
const DFSGraphTraversal = (graph, start, visited = new Set()) => {
console.log(start);
visited.add(start);
for (let child = 0; child < graph[start].length; child++) {
if (visited.has(graph[start][child]) === false) {
DFSGraphTraversal(graph, graph[start][child], visited);
}
}
};
```

## How it works

- We first create the graph out of the given array of edges using an `Object`.
- We then run the `DFSGraphTraversal` function to execute the depth first search.
- In the function, we have three parameters; namely the `graph`, the `start` node and the `visited` set which is empty by default.
- Firstly, we print the `start` node and then we add the node to the `visited` set.
- We then run a for loop for the elements present in the `graph[start]` array.
- If the current element is not present in the `visited` set, then recursively call the `DFSGraphTraversal` function with the same `graph` and `visited` set, but with the current element as the `start` node.
- This function will run until all the nodes are visited in the `graph`.

## References

- [Youtube](https://youtu.be/IG1ABs6lVMw)

## Problem Added By

- [khairalanam](https://github.com/khairalanam)

## Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.