Found 6710 Articles for Javascript

Dynamic Programming in JavaScript

Samual Sam
Updated on 30-Jul-2019 22:30:23

3K+ Views

Dynamic programming breaks down the problem into smaller and yet smaller possible sub-problems. These sub-problems are not solved independently. Rather, results of these smaller sub-problems are remembered and used for similar or overlapping sub-problems. Dynamic programming is used where we have problems, which can be divided into similar sub-problems so that their results can be re-used. Mostly, these algorithms are used for optimization. Before solving the in-hand sub-problem, the dynamic algorithm will try to examine the results of the previously solved sub-problems. The solutions of sub-problems are combined in order to achieve the best solution. For a problem to be ... Read More

Looping through an array in Javascript

Nikhilesh Aleti
Updated on 18-Nov-2022 07:32:21

679 Views

Array is a linear data structure, which is able to store elements of different datatypes. Arrays are also defined as ordered collections. In array each of the value will be referred to as an element and those can be identified with index numbers. const array_name = [item1, item2, ...]; const movies = [Bahubali, RRR, KGF, Pushpa]; //Index values of above elements Bahubali – [0] RRR – [1] KGF – [2] Pushpa – [3] Loops is a programming element, there will be a sequence of instructions defined in the loop and it will continue to iterate till ... Read More

Multi-Dimensional Array in Javascript

Sharon Christine
Updated on 15-Jun-2020 14:37:44

728 Views

Basically, multi-dimension arrays are used if you want to put arrays inside an array. Let's take an example. Say you wanted to store every 6 hour's temperature for every weekday. You could do something like:let monday = [35, 28, 29, 31]; let tuesday = [33, 24, 25, 29]; //...This is a good place to use a multidimensional array instead. A multidimensional array is nothing but an array of arrays. If we take forward our example, each row will represent a day while each entry in the row will represent a temp entry. For example, let temps = [    [35, ... Read More

Removing an element from an Array in Javascript

Sharon Christine
Updated on 18-Nov-2024 15:29:37

353 Views

Removing an element from an array in Javascript can be achieved using various approaches. Removal of an element can be done from the begining, end or from any specific position. We will be understanding various approaches for removing element from an array in Javascript based on our need. In this article, we are having an array of numbers and our task is to remove an element from an array in Javascript. Approaches to Remove Element from Javascript Array Here is a list of approaches for removing an element from an array in Javascript which we will be discussing in ... Read More

Adding an element in an array using Javascript

Samual Sam
Updated on 15-Jun-2020 14:41:58

227 Views

Adding an element to an array can be done using different functions for different positions.Adding an element at the end of the arrayThis can be accomplished using the push method. For example, let veggies = ["Onion", "Raddish"]; veggies.push("Cabbage"); console.log(veggies);This will give the output −["Onion", "Raddish", "Cabbage"]You can also use this to push multiple items at the same time as it supports a variable number ofarguments. For example, let veggies = ["Onion", "Raddish"]; veggies.push("Cabbage", "Carrot", "Broccoli"); console.log(veggies);This will give the output −["Onion", "Raddish", "Cabbage", "Carrot", "Broccoli"]Adding an element at the start of the arrayThis can be accomplished using the unshift method. ... Read More

Complete Graph Class in Javascript

karthikeya Boyini
Updated on 15-Jun-2020 12:01:49

244 Views

Functions which have been commented out in this code. You can switch to those as well. We've also moved the Queue, Stack, and PriorityQueue classes in different modules that can be imported using either import statements or using require calls. Here is the complete implementation of the Graph class − Exampleconst Queue = require("./Queue"); const Stack = require("./Stack"); const PriorityQueue = require("./PriorityQueue"); class Graph {    constructor() {       this.edges = {};       this.nodes = [];    }    addNode(node) {       this.nodes.push(node);       this.edges[node] = [];    }   ... Read More

Kruskal's algorithm in Javascript

Samual Sam
Updated on 15-Jun-2020 12:06:01

1K+ Views

Kruskal's algorithm is a greedy algorithm that works as follows −1. It Creates a set of all edges in the graph.2. While the above set is not empty and not all vertices are covered, It removes the minimum weight edge from this setIt checks if this edge is forming a cycle or just connecting 2 trees. If it forms a cycle, we discard this edge, else we add it to our tree.3. When the above processing is complete, we have a minimum spanning tree.In order to implement this algorithm, we need 2 more data structures.First, we need a priority queue ... Read More

Prim's algorithm in Javascript

karthikeya Boyini
Updated on 15-Jun-2020 12:09:47

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 does Prim's algorithm work?Let us look at an illustration of how Prim's algorithm works −1. Choose any arbitrary node as the root node: In this ... Read More

Minimum spanning tree (MST) in Javascript

Samual Sam
Updated on 15-Jun-2020 12:10:17

628 Views

A minimum spanning tree (MST) or minimum weight spanning tree is a subset of the edges of a connected, edge-weighted (un)directed graph that connects all the vertices together, without any cycles and with the minimum possible total edge weight. That is, it is a spanning tree whose sum of edge weights is as small as possible.

The Floyd-Warshall algorithm in Javascript

karthikeya Boyini
Updated on 15-Jun-2020 12:12:37

1K+ Views

Djikstra's algorithm is used to find distance/path of the shortest path from one node to all other nodes. There are cases where we need to find shortest paths from all nodes to all other nodes. This is where the All pairs shortest path algorithms come in handy. The most used all pairs shortest path algorithm is Floyd Warshall algorithm.Floyd Warshall algorithm works as follows −We initialize an N x N matrix of distances to be Infinity.Then for each edge u, v, we update this matrix to be showing the weight of this edge and for edges v, v we update ... Read More

Advertisements