Web Development Articles

Page 403 of 801

AVL Tree class in Javascript

Sai Subramanyam
Sai Subramanyam
Updated on 15-Mar-2026 870 Views

An AVL Tree is a self-balancing binary search tree where the heights of left and right subtrees differ by at most one. This implementation provides insertion with automatic rebalancing through rotations. 10 5 15 3 7 Balanced AVL Tree (height difference ...

Read More

Creating a Graph in Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 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. Graph Class Structure Let's set up the graph class by defining our class and some methods ...

Read More

Graph Traversals in Javascript

Sai Subramanyam
Sai Subramanyam
Updated on 15-Mar-2026 286 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. In JavaScript, the two most common graph traversal algorithms are Breadth-First Search (BFS) and Depth-First Search (DFS). Both algorithms systematically explore graph nodes but use different strategies. Graph Representation First, let's create a simple graph using an adjacency list representation: class Graph { constructor() { this.adjacencyList = {}; ...

Read More

Breadth-first search traversal in Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 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. How BFS Traversal Works Let us look at an illustration of how BFS Traversal works: ...

Read More

Shortest path algorithms in Javascript

Raju Kumar
Raju Kumar
Updated on 15-Mar-2026 1K+ Views

In graph theory, the shortest path problem is finding a path between two vertices in a graph such that the sum of edge weights is minimized. To implement shortest path algorithms, we need to modify our graph structure to support weighted edges. Setting Up Weighted Graphs First, let's modify our graph methods to handle weights: class WeightedGraph { constructor() { this.nodes = []; this.edges = {}; } ...

Read More

Dijkstra's algorithm in Javascript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 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 ...

Read More

Prim's algorithm in Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 2K+ Views

Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for a weighted undirected graph. It finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. The algorithm operates by building this tree one vertex at a time, from an arbitrary starting vertex, at each step adding the cheapest possible connection from the tree to another vertex. How Prim's Algorithm Works Let us look at an illustration of how Prim's algorithm works: 1. Choose any arbitrary node ...

Read More

Kruskal's algorithm in Javascript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 2K+ Views

Kruskal's algorithm is a greedy algorithm used to find the minimum spanning tree (MST) of a weighted, undirected graph. It works by systematically selecting the smallest weight edges while avoiding cycles. How Kruskal's Algorithm Works The algorithm follows these steps: Create a set of all edges in the graph Sort all edges by weight in ascending order While the set is not empty and not all vertices are covered: Remove the minimum weight edge from the set Check if this edge forms a cycle. If not, ...

Read More

Complete Graph Class in Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 306 Views

This article presents a comprehensive Graph class implementation in JavaScript with various graph algorithms including traversal, shortest path, and minimum spanning tree algorithms. Graph Class Structure The Graph class uses an adjacency list representation with two main properties: nodes - Array storing all graph vertices edges - Object mapping each node to its connected neighbors with weights Basic Graph Operations const Queue = require("./Queue"); const Stack = require("./Stack"); const PriorityQueue = require("./PriorityQueue"); class Graph { constructor() { this.edges = {}; ...

Read More

Looping through an array in Javascript

Nikhilesh Aleti
Nikhilesh Aleti
Updated on 15-Mar-2026 821 Views

Arrays are linear data structures that store multiple elements in an ordered collection. Each element can be accessed using its index number, starting from 0. const array_name = [item1, item2, ...]; const movies = ["Bahubali", "RRR", "KGF", "Pushpa"]; // Index values: // Bahubali – [0] // RRR – [1] // KGF – [2] // Pushpa – [3] Loops are programming constructs that execute a sequence of instructions repeatedly until a specified condition is met. They're essential for iterating through arrays efficiently. Traditional for Loop The for loop provides complete control over initialization, condition, ...

Read More
Showing 4021–4030 of 8,010 articles
« Prev 1 401 402 403 404 405 801 Next »
Advertisements