How to resolve the algorithm Floyd-Warshall algorithm step by step in the JavaScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Floyd-Warshall algorithm step by step in the JavaScript programming language

Table of Contents

Problem Statement

The Floyd–Warshall algorithm is an algorithm for finding shortest paths in a weighted graph with positive or negative edge weights.

Find the lengths of the shortest paths between all pairs of vertices of the given directed graph. Your code may assume that the input has already been checked for loops, parallel edges and negative cycles. Print the pair, the distance and (optionally) the path.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Floyd-Warshall algorithm step by step in the JavaScript programming language

The code you provided is an implementation of the Floyd-Warshall algorithm in JavaScript, which is used to find the shortest path between all pairs of vertices in a weighted graph.

The code first initialises a 10x10 matrix graph to represent the weighted graph, where the weight of the edge between vertex i and vertex j is stored in graph[i][j].

It then initialises the diagonal elements of the matrix to 0 (since the weight of the edge between a vertex and itself is always 0) and the rest of the elements to a large value (9999999) to represent that there is no edge between those vertices.

Next, it populates the first row and first column of the matrix with random weights between 1 and 9 to represent the edges between the first vertex and the other vertices.

It then uses the Floyd-Warshall algorithm to find the shortest paths between all pairs of vertices. The algorithm iterates over all vertices in the graph and, for each vertex, it checks if there is a shorter path from one vertex to another through the current vertex. If there is, it updates the weight of the edge between those vertices to the shorter path.

Finally, it prints the resulting matrix, which now contains the shortest paths between all pairs of vertices in the graph. The time complexity of the Floyd-Warshall algorithm is O(V^3), where V is the number of vertices in the graph.

Source code in the javascript programming language

var graph = [];
for (i = 0; i < 10; ++i) {
  graph.push([]);
  for (j = 0; j < 10; ++j)
    graph[i].push(i == j ? 0 : 9999999);
}

for (i = 1; i < 10; ++i) {
  graph[0][i] = graph[i][0] = parseInt(Math.random() * 9 + 1);
}

for (k = 0; k < 10; ++k) {
  for (i = 0; i < 10; ++i) {
    for (j = 0; j < 10; ++j) {
      if (graph[i][j] > graph[i][k] + graph[k][j])
        graph[i][j] = graph[i][k] + graph[k][j]
    }
  }
}

console.log(graph);


  

You may also check:How to resolve the algorithm Averages/Pythagorean means step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Vigenère cipher step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Equilibrium index step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Pythagoras tree step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Binary search step by step in the JavaScript programming language