Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Web Development Articles
Page 403 of 801
AVL Tree class in Javascript
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 MoreCreating a Graph in Javascript
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 MoreGraph Traversals in Javascript
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 MoreBreadth-first search traversal in Javascript
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 MoreShortest path algorithms in Javascript
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 MoreDijkstra's algorithm in Javascript
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 MorePrim's algorithm in Javascript
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 MoreKruskal's algorithm in Javascript
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 MoreComplete Graph Class in Javascript
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 MoreLooping through an array in Javascript
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