Skip to content

Commit 532c444

Browse files
committed
chore: updated code
1 parent 1ed7d77 commit 532c444

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

‎README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ The DFS algorithm we start from starting point and go into depth of graph until
319319
- **Strategy**: `Recursive`
320320
- **Time Complexity**: `O(n logn)`
321321
- **Space Complexity**: `O(n logn)`
322-
- Use `Stack` while coding.
322+
- Use `Recursive Call Stack` while coding.
323323

324324
![](https://i.imgur.com/DdFyXGx.png)
325325

@@ -411,8 +411,14 @@ Find all possible routes from `PHX` to `BKK`. Also then you can decide which pat
411411

412412
![](https://i.imgur.com/CvPhRQx.png)
413413

414+
![]((https://i.imgur.com/DrWF78t.png)
415+
414416
#### Depth First Search (DFS)
415417

418+
![](https://i.imgur.com/wHevaTK.png)
419+
420+
[Exercise Answer Source Code: Find shortest path using DFS](./src/data-structure/5-graph/depth-first-search.mjs)
421+
416422
## Algorithms Q&A
417423

418424
### Merge Sort

‎src/data-structure/5-graph/breadth-first-search.mjs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
/**
22
* Search all possible routes from PHX and BKK
3+
* RunTime complexity O(V+E) V= Node, E = edges
4+
* Space complexity O(n) = linear
5+
* It takes 8 steps to find the destination
36
* @param start node
47
*/
58

69
import { createGraph } from './implement-graph.mjs';
710

8-
function bfs(graph, start, end) {
11+
function bfs(graph, start, toFind) {
912
const queue = [];
1013
queue.push(start);
1114
const visited = new Set();
@@ -18,8 +21,8 @@ function bfs(graph, start, end) {
1821
if (!destinations) continue;
1922

2023
for (const destination of destinations) {
21-
if (end === destination) {
22-
console.log(`====We found Bangkok====`);
24+
if (toFind === destination) {
25+
console.log('!! We found !!', toFind, 'in', visited.size, ' steps');
2326
}
2427

2528
if (!visited.has(destination)) {
@@ -34,6 +37,9 @@ function bfs(graph, start, end) {
3437
const graph = createGraph();
3538
bfs(graph, 'PHX', 'BKK');
3639

40+
41+
42+
bfs(graph, 'LIM', 'BKK');
3743
/**
3844
* PHX => [ ABC, XYZ ]
3945
* ABC => [ BBB, CCC ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { createGraph } from './implement-graph.mjs';
2+
3+
/**
4+
* RunTime complexity O(V+E) V= Node, E = edges
5+
* Space complexity O(n) = linear
6+
* It takes 3 steps or functions calls to find the destination
7+
* @param graph
8+
* @param start
9+
* @param toFind
10+
*/
11+
function dfs(graph, start, toFind, visited = new Set()) {
12+
console.log(start);
13+
const destinations = graph.getEdges(start);
14+
visited.add(start);
15+
16+
for (const destination of destinations) {
17+
if (destination === toFind) {
18+
console.log('!! We found !!', toFind, 'in', visited.size, ' steps');
19+
return;
20+
}
21+
if (!visited.has(destination)) {
22+
dfs(graph, destination, toFind, visited);
23+
}
24+
}
25+
}
26+
const graph = createGraph();
27+
dfs(graph, 'PHX', 'BKK');

‎src/data-structure/5-graph/implement-graph.mjs

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ export class Graph {
1616
? this.nodes.get(destination).push(origin)
1717
: this.nodes.set(destination, [origin]);
1818
}
19+
20+
getEdges(origin) {
21+
return this.nodes.get(origin);
22+
}
1923
}
2024

2125
export const routes = [

0 commit comments

Comments
 (0)