GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — JavaScript - Graph Algorithms
Selected Reading
JavaScript - Graph AlgorithmsA graph is a data structure which consist nodes and edges. The node are simply vertices and the lines which connect them are edges. Graph is non-linear data structure. A Graph algorithms in JavaScript are used to solve the graph problems. These algorithms are used to traverse the graph, find the shortest path, etc. We can use these algorithms to solve the problems like finding the shortest path, finding the connected components, etc. Types of GraphBefore dwelling deep into this chapter let's learn about types of graphs.
Graph RepresentationThere are two ways to represent the graph:
Graph AlgorithmsWhen we talk about graph algorithms, there are plenty of the algorithms available. We mostly use these algorithms to solve the graph problems. We have listed some of them below:
Breadth First Search (BFS) AlgorithmThis algorithm we can use for traversing the graph. It is very useful for resolving many problems. In this algorithm, we start our traversal from the root node and then we go one level down, traverse all the nodes at that level and then move to the next level. We use queue data structure to implement this algorithm. AlgorithmWe can implement the BFS using the below steps given:
ImplementationFollowing is the implementation of BFS algorithm in JavaScript: function BFS(graph, root) {
let visited = [];
let queue = [];
queue.push(root);
while (queue.length > 0) {
let node = queue.shift();
if (!visited[node]) {
console.log(node); // Process the node
visited[node] = true;
}
// Ensure neighbours is defined
const neighbours = graph[node] || [];
for (let i = 0; i < neighbours.length; i++) {
let neighbour = neighbours[i];
if (!visited[neighbour]) {
queue.push(neighbour);
}
}
}
}
let graph = [[1, 2], [3, 4], [5], [6], [6], [7], [8], []];
BFS(graph, 0);
Following is the output of the above program − Following is the output of above code 0 2 5 7 1 4 6 8 3 Depth First Search (DFS) AlgorithmSimilar to BFS, this algorithm is also used for traversing the graph but in different way. In this algorithm, we start from the root node then move to left child or right child go to in-depth until we reach to leaf node then we backtrack and move to the next child. AlgorithmWe can implement the DFS using the below steps given:
ImplementationFollowing is the implementation of DFS algorithm in JavaScript: function DFS(graph, root) {
let visited = [];
let stack = [];
stack.push(root);
while (stack.length > 0) {
let node = stack.pop();
if (!visited[node]) {
console.log(node);
visited[node] = true;
}
// Set a default value if graph[node] is undefined
const neighbours = graph[node] || [];
for (let i = 0; i < neighbours.length; i++) {
let neighbour = neighbours[i];
if (!visited[neighbour]) {
stack.push(neighbour);
}
}
}
}
let graph = [[1, 2], [3, 4], [5], [6], [6], [7], [8], []];
DFS(graph, 0);
OutputFollowing is the output of above code 0 2 5 7 1 4 6 8 Topological Sorting AlgorithmUsing this algorithm we can actually sort the vertices of the graph in such a way that for every edge from vertex u to vertex v, u comes before v in the ordering. AlgorithmWe can implement the Topological Sorting using the below steps given:
ImplementationFollowing is the implementation of Topological Sorting algorithm in JavaScript: function topologicalSort(graph) {
let visited = [];
let stack = [];
for (let i = 0; i < graph.length; i++) {
if (!visited[i]) {
topologicalSortUtil(graph, i, visited, stack);
}
}
while (stack.length > 0) {
console.log(stack.pop());
}
}
function topologicalSortUtil(graph, node, visited, stack) {
visited[node] = true;
const neighbours = graph[node] || [];
for (let i = 0; i < neighbours.length; i++) {
let neighbour = neighbours[i];
if (!visited[neighbour]) {
topologicalSortUtil(graph, neighbour, visited, stack);
}
}
stack.push(node);
}
// Valid DAG
let graph = [
[1, 2], // Node 0 -> 1, 2
[3], // Node 1 -> 3
[3, 4], // Node 2 -> 3, 4
[], // Node 3 -> No outgoing edges
[5], // Node 4 -> 5
[] // Node 5 -> No outgoing edges
];
topologicalSort(graph);
OutputFollowing is the output of above code 0 1 2 3 4 5 6 7 8 Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |