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

Difference Between for...of and for...in Statements in JavaScript

Ankitha Reddy
Updated on 15-Jun-2020 12:16:31

274 Views

for…in loopThe “for...in” loop is used to loop through an object's properties.Here’s the syntax −Syntaxfor (variablename in object) { statement or block to execute }You can try to run the following example to implement ‘for-in’ loop. It prints the web browser’s Navigator objectExampleLive Demo var aProperty; document.write("Navigator Object Properties "); for (aProperty in navigator) { document.write(aProperty); document.write(""); } document.write ("Exiting from the loop!"); for…of loopThe “for…of” loop is used to loop through iterable objects, which includes Map, Array, arguments, etc.SyntaxHere’s the syntax −for (variablename of iterable){ statement or block to execute }ExampleHere’s an example showing ... 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

HTML DOM Input Datetime Name Property

AmitDiwan
Updated on 15-Jun-2020 12:15:23

120 Views

The HTML DOM Input Datetime name property returns a string, which is the value of the name attribute of input datetime. User can also set it to a new string.SyntaxFollowing is the syntax −Returning string valueinputDatetimeObject.nameSetting name attribute to a string valueinputDatetimeObject.name = ‘String’ExampleLet us see an example of Input Datetime name property − Live Demo Input Datetime Name Datetime: Change name value    var inputDatetime = document.getElementById("datetime");    var divDisplay = document.getElementById("divDisplay");    divDisplay.textContent = 'Name of datetime input: '+inputDatetime.name;    function changeNameValue() {       if(inputDatetime.name == "Mother's Birth ... Read More

Lifetime of JavaScript Variables

Govinda Sai
Updated on 15-Jun-2020 12:14:35

1K+ Views

The lifetime of a JavaScript variable begins when it is declared −var rank;A local variable will be visible only within a function where it is defined. Function parameters are always local to that function.The completion of a function deletes the local variable.A global variable has a global scope which means it can be defined anywhere in your JavaScript code. Global variables delete when the web browser is closed. However if a new page is loaded in the same browser window, then it remains.Here’s the usage of global variables −ExampleYou can try to run the following code to learn how to ... Read More

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

Label a Block in JavaScript

Ramu Prasad
Updated on 15-Jun-2020 12:13:36

507 Views

A block statement groups zero or more statements. In languages other than JavaScript, it is known as a compound statement.SyntaxHere’s the syntax −{    //List of statements }To add a label to a block, use the following −Identifier_for_label: {    StatementList }Let’s use it for break statement. You can try to run the following code to use labels to control the flow, with break statementExampleLive Demo                    document.write("Entering the loop! ");          outerloop:   // This is the label name          for (var i = ... Read More

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

Minimum Spanning Tree (MST) in JavaScript

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

641 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.

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

Advertisements