Question

In: Computer Science

Write a heuristic computer program to solve the shortest path routing problem using the Floyd-Warshall algorithm...

Write a heuristic computer program to solve the shortest path routing problem using the Floyd-Warshall algorithm discussed in the chapter using C++. The user should be able to input the node positions and link costs.

Solutions

Expert Solution

// Hope this code helps you

// If u face any doubt feel free to ask in the comments

// I will be happy to solve them

#include <iostream>
using namespace std;
// defining the number of vertices
#define V 4// You can take any size
#define ll 999// This is infinity or you can say INF short form
void printMatrix(int matrix[][V]);
// Implementing floyd warshall algorithm for shortest path routing
void shortestPathRouting(int graph[][V]) {
  int matrix[V][V], i, j, k;
  for (i = 0; i < V; i++)
    for (j = 0; j < V; j++)
      matrix[i][j] = graph[i][j];
  // Adding vertices
  for (k = 0; k < V; k++) {
    for (i = 0; i < V; i++) {
      for (j = 0; j < V; j++) {
        if (matrix[i][k] + matrix[k][j] < matrix[i][j])
          matrix[i][j] = matrix[i][k] + matrix[k][j];
      }
    }
  }
  printMatrix(matrix);
}
// Printing the matrice
void printMatrix(int matrix[][V]) {
  for (int i = 0; i < V; i++) {
    for (int j = 0; j < V; j++) {
      if (matrix[i][j] == ll)
        printf("%4s", "INF");
      else
        printf("%4d", matrix[i][j]);
    }
    printf("\n");
  }
}
int main() 
{
  int graph[V][V] = {{0, 3, ll, 5},
             {2, 0, ll, 4},
             {ll, 1, 0, ll},
             {ll, ll, 2, 0}};
             cout<<"Shortest path matrix is that contains shortest distance between each pair of node:\n";
  shortestPathRouting(graph);
}

SCREENSHOTS:

Time Complexity

The time complexity of the Floyd-Warshall algorithm is O(n3).

Space Complexity

The space complexity of the Floyd-Warshall algorithm is O(n2).


Related Solutions

Using the provided network diagram, write a program in c ++ that finds the shortest path...
Using the provided network diagram, write a program in c ++ that finds the shortest path routing using the Bellman-Ford algorithm. Your program should represent the fact that your node is U. Show how the iterative process generates the routing table for your node. One of the keys to your program will be in determining when the iterative process is done. Deliverables 1. Provide an output that shows the routing table for your node after each iteration. Add a second...
In the shortest-path algorithm we are concerned with the total length of the path between a...
In the shortest-path algorithm we are concerned with the total length of the path between a source and every other node. Suppose instead that we are concerned with the length of the longest edge between the source and every node. That is, the bottleneck of a path is defined to be the length of the longest edge in the path. Design an efficient algorithm to solve the single source smallest bottleneck problem; i.e. find the paths from a source to...
Algorithm questions. 1.When choosing an algorithm for the shortest path between two nodes in an unweighted...
Algorithm questions. 1.When choosing an algorithm for the shortest path between two nodes in an unweighted graph, should we use Breadth First Search or Depth First Search ? 2. When choosing a data structure to store a graph for an algorithm, how should we store the graph knowing that the graph is dense and this algorithm needs to determine many times if two nodes are directly connected ? Should it be an Adjacency Matrix or an Adjacency List ?
Longest ‘A’ Path Objectives Manipulating two-dimensional lists Using recursion to solve a problem Problem Specification Write...
Longest ‘A’ Path Objectives Manipulating two-dimensional lists Using recursion to solve a problem Problem Specification Write a Python application to find the longest ‘A’ path on a map of capital (uppercase) letters. The map is represented as a matrix (2-dimensional list) of capital letters. Starting from any point, you can go left, right, up and down (but not diagonally). A path is defined as the unbroken sequence of letters that only covers the spots with the letter A. The length...
Bellman-Ford and Dijkstra's algorithms solve the single-source shortest path (SSSP) problem. Explain the circumstances when you...
Bellman-Ford and Dijkstra's algorithms solve the single-source shortest path (SSSP) problem. Explain the circumstances when you would use each of these algorithms.   
Write a program that implements the A* algorithm to find a path from any two given...
Write a program that implements the A* algorithm to find a path from any two given nodes. You may use any of the following languages: C++, C#, Java, ActionScript. Problem Overview & Algorithm Description In a fully-observable environment where there are both pathable and blocked nodes, an agent must find a good path from their starting node to the goal node. The agent must use the A* algorithm to determine its path. For this program, you must use the Manhattan...
i want a program in java that finds the shortest path in a 2D array with...
i want a program in java that finds the shortest path in a 2D array with obstacles from source to destination using BFS and recursion. The path must be stored in a queue. The possible moves are left,right,up and down.
Write and Compile a python script to solve the 4-queens problem using Forward Checking algorithm ....
Write and Compile a python script to solve the 4-queens problem using Forward Checking algorithm . The code should allow for random starting, and for placed starting. Random Starting means randomly place a queen position on the chessboard. Placed Starting means asked for the user input to place a queen position on the chessboard. Display the iterations until the final solution "The 4-Queens Problem[1] consists in placing four queens on a 4 x 4 chessboard so that no two queens...
Below is the linear programming for the Shortest Path Problem. Considering the second contraint in the...
Below is the linear programming for the Shortest Path Problem. Considering the second contraint in the mathematical model : ∑ixji−∑ixij=0∀j≠s,j≠t What is the logic behind this contraint? To make sure there is only one solution To make sure that the path is connected between the nodes To make sure the variable stays binary This contraint is redundant and not necessary Which of the following statements are true? (select all that apply) The shape of a student t-distribution curve depends on...
Below is the linear programming for the Shortest Path Problem. Considering the second contraint in the...
Below is the linear programming for the Shortest Path Problem. Considering the second contraint in the mathematical model : ∑ixji−∑ixij=0∀j≠s,j≠t What is the logic behind this contraint? To make sure there is only one solution To make sure that the path is connected between the nodes To make sure the variable stays binary This contraint is redundant and not necessary Which of the following statements are true? (select all that apply) The shape of a student t-distribution curve depends on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT