We will be creating three modules:
- A Graph Generator module that helps us generate a graph data structure.
- A Graph is a data structure that contains nodes
- Nodes are connected to each other via edges
- A Depth-first Search (DFS) module that takes a graph and traverses it depth-first.
- A Breadth-first Search (BFS) module that takes a graph and traverses it breadth-first.
A
^
B C
^ |
D E F
Is represented in memory as:
{ name: 'A',
value: 'foo1',
neighbors: [
{ name: 'B',
value: 'foo2',
neighbors: [
{ name: 'D',
value: 'foo4',
neighbors: []
},
{ name: 'E',
value: 'foo5',
neighbors: []
}
]
},
{ name: 'C',
value: 'foo3',
neighbors: [
{ name: 'F',
value: 'foo6',
neighbors: []
}
]
}
]
}
The basic operations provided by a graph data structure include:
- Define a
Nodeclass that has aname {{string}},value{{*}}, andneighbors{{array}} Node.addNeighbors([x {{node}}, y {{node}}, z {{node}} ...]): adds an array of nodes x, y, z tonode. Return an array with all of the nodes neighbors.Node.getNeighbors(): lists all vertices such that there is an edge from the vertices x to y.- [Optional]
Node.removeNode(x {{node}}): removes the vertex x, if it is there.
Using these example methods, you should be able to make the graph above like the following:
let A = new Node("A", "foo1");
let B = new Node("B", "foo2");
let C = new Node("C", "foo3");
let D = new Node("D", "foo4");
let E = new Node("E", "foo5");
let F = new Node("F", "foo6");
A.addNeighbors([B, C]);
B.addNeighbors([D, E]);
C.addNeighbors([F]);
DFS(start, searchFor): Starting at the node start, traverse the graph depth-first and return the node whos name matches searchFor. If there are no matches, return false.
BFS(start): Starting at the node start traverse the graph breadth-first and return an array of Strings that represent the path that is traversed. For example, in the graph above, BFS(A) should return ["A", "B", "C", "D", "E", "F"].
- Fork this repository and clone it from your personal GitHub Account
- In the Terminal, navigate to the newly created folder for this repository.
- Install dependencies by running the command:
npm install - Run tests by running the command:
npm test - Your work will be done in the files named:
graphGenerator.jsdepthFirstSearch.jsbreadthFirstSearch.js
- Pay attention to the tests for hints.
- Make your tests pass!
- Write a blog post ELI5 the differences between depth and breadth-first Search.
- Write Pseudocode for each implementation
- Explain the Big O time and space complexities for each graph traversing method
- Provide examples of when you would favor one graph traversal algorithm over the other.
- Implement a Queue Module for Breadth-first search.
- Implement a Stack Module for Depth-first search.
- Implement a Remove Node method for Graph Generator module.
- Write a recursive and non-recursive implementation of BFS and DFS.
- Visualize each method in the DOM.
- Link: Graph (Abstract Data Type)
- Concepts: Graph Node, Graph theory, search and depth first search
- Link: Depth First Search
- Concepts: Graph Node, Graph theory, search and depth first search
- Link: Breadth First Search
- Concepts: Graph Node, Graph theory, search and breadth first search