Found 6710 Articles for Javascript

Dijkstra's algorithm in Javascript

Samual Sam
Updated on 15-Jun-2020 12:14:29

6K+ Views

Dijkstra's algorithm is an algorithm for finding the shortest paths between nodes in a weighted graph. We'll use the new addEdge and addDirectedEdge methods to add weights to the edges when creating a graph. Let us look at how this algorithm works −Create a distance collection and set all vertices distances as infinity except the source node.Enqueue the source node in a min-priority queue with priority 0 as the distance is 0.Start a loop till the priority queue is empty and dequeue the node with the minimum distance from it.Update the distances of the connected nodes to the popped node ... Read More

Shortest path algorithms in Javascript

Raju Kumar
Updated on 15-Jun-2020 12:16:12

1K+ Views

In graph theory, the shortest path problem is the problem of finding a path between two vertices (or nodes) in a graph such that the sum of the weights of its constituent edges is minimized. Here we need to modify our add edge and add directed methods to allow adding weights to the edges as well. Let us look at how we can add this −Example/**    * Adds 2 edges with the same weight in either direction    *    *             weight    * node1 node2    *           ... Read More

Topological sorting using Javascript DFS

karthikeya Boyini
Updated on 15-Jun-2020 12:21:44

2K+ Views

A topological sort or topological ordering of a directed graph is a linear ordering of its vertices such that for every directed edge UV from vertex u to vertex v, u comes before v in the ordering. This only makes sense in directed graphs.There are many places where topological sort makes a lot of sense. For example, let's say you're following a recipe, in this, there are some steps that are must for going to the next steps. But some of these can be performed in parallel. In a similar fashion, during college when selecting courses, there are some prerequisites ... Read More

Depth-first search traversal in Javascript

Sai Subramanyam
Updated on 15-Jun-2020 12:24:59

4K+ Views

DFS visits the child vertices before visiting the sibling vertices; that is, it traverses the depth of any particular path before exploring its breadth. A stack (often the program's call stack via recursion) is generally used when implementing the algorithm.Following is how a DFS works −Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push it in a stack.If no adjacent vertex is found, pop up a vertex from the stack. (It will pop up all the vertices from the stack, which do not have adjacent vertices.)Repeat Rule 1 and Rule 2 until the stack is empty.Let us ... Read More

Breadth-first search traversal in Javascript

karthikeya Boyini
Updated on 15-Jun-2020 11:33:43

4K+ Views

BFS visits the neighbor vertices before visiting the child vertices, and a queue is used in the search process. Following is how a BFS works −Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it in a queue.If no adjacent vertex is found, remove the first vertex from the queue.Repeat Rule 1 and Rule 2 until the queue is empty.Let us look at an illustration of how BFS Traversal works:StepTraversalDescription1Initialize the queue.2We start by visiting S (starting node) and mark it as visited.3We then see an unvisited adjacent node from S. In this example, we have three ... Read More

Graph Traversals in Javascript

Sai Subramanyam
Updated on 15-Jun-2020 11:34:08

236 Views

Graph traversal (also known as graph search) refers to the process of visiting (checking and/or updating) each vertex in a graph. Such traversals are classified by the order in which the vertices are visited.

Creating a Graph in Javascript

karthikeya Boyini
Updated on 15-Jun-2020 11:36:09

1K+ Views

We'll be creating a graph class that supports weights and both directed and undirected types. This will be implemented using an adjacency list. As we move to more advanced concepts, both weights and directed nature of the graphs will come in handy.An adjacency list is an array A of separate lists. Each element of the array Ai is a list, which contains all the vertices that are adjacent to vertex i. We're defining it using 2 members, nodes and edges.Let's set up the graph class by defining our class and some methods that we'll use to add nodes and edges ... Read More

Graph Data Structure in Javascript

Sai Subramanyam
Updated on 15-Jun-2020 11:46:10

2K+ Views

A graph is a pictorial representation of a set of objects where some pairs of objects are connected by links. The interconnected objects are represented by points termed as vertices, and the links that connect the vertices are called edges.Formally, a graph is a pair of sets (V, E), where V is the set of vertices and E is the set of edges, connecting the pairs of vertices. Take a look at the following graph −In the above graph, V = {a, b, c, d, e} E = {ab, ac, bd, cd, de}TerminologyMathematical graphs can be represented in the data ... Read More

AVL Tree class in Javascript

Sai Subramanyam
Updated on 15-Jun-2020 11:50:25

767 Views

Here is the complete implementation of the AVL Tree Class −Exampleclass AVLTree {    constructor() {       // Initialize a root element to null.       this.root = null;    }    getBalanceFactor(root) {       return this.getHeight(root.left) - this.getHeight(root.right);    }    getHeight(root) {       let height = 0;       if (root === null || typeof root == "undefined") {          height = -1;       } else {          height = Math.max(this.getHeight(root.left), this.getHeight(root.right)) + 1;       }     ... Read More

Inserting a node in a Javascript AVL Tree

karthikeya Boyini
Updated on 15-Jun-2020 11:51:43

249 Views

We can learn how we can insert a node in an AVL Tree. Insertions in AVL trees are the same as BST, we just need to perform one extra step called balance tree during insert whenever we move down the tree.This requires calculating the balance factor which we already saw before. And according to the configurations, we need to call appropriate rotation methods. These are pretty intuitive with the help of the above explanation.We again create a class method and a helper function for recursive calls − Exampleinsert(data) {    let node = new this.Node(data);    // Check if the tree ... Read More

Advertisements