Search
 
SCRIPT & CODE EXAMPLE
 

CPP

floyd algorithm

// Floyd-Warshall Algorithm in C++

#include <iostream>
using namespace std;

// defining the number of vertices
#define nV 4

#define INF 999

void printMatrix(int matrix[][nV]);

// Implementing floyd warshall algorithm
void floydWarshall(int graph[][nV]) {
  int matrix[nV][nV], i, j, k;

  for (i = 0; i < nV; i++)
    for (j = 0; j < nV; j++)
      matrix[i][j] = graph[i][j];

  // Adding vertices individually
  for (k = 0; k < nV; k++) {
    for (i = 0; i < nV; i++) {
      for (j = 0; j < nV; j++) {
        if (matrix[i][k] + matrix[k][j] < matrix[i][j])
          matrix[i][j] = matrix[i][k] + matrix[k][j];
      }
    }
  }
  printMatrix(matrix);
}

void printMatrix(int matrix[][nV]) {
  for (int i = 0; i < nV; i++) {
    for (int j = 0; j < nV; j++) {
      if (matrix[i][j] == INF)
        printf("%4s", "INF");
      else
        printf("%4d", matrix[i][j]);
    }
    printf("
");
  }
}

int main() {
  int graph[nV][nV] = {{0, 3, INF, 5},
             {2, 0, INF, 4},
             {INF, 1, 0, INF},
             {INF, INF, 2, 0}};
  floydWarshall(graph);
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ allocate dynamic with initial values 
Cpp :: unordered_map in c++ 
Cpp :: is there garbage collection in c++ 
Cpp :: 83. remove duplicates from sorted list solution in c++ 
Cpp :: create a copy of a vector c++ 
Cpp :: std::enable_shared_from_this include 
Cpp :: Programming Languages codechef solution in c++ 
Cpp :: lcm in c++ 
Cpp :: c ++ The output should be (abc),(def),(ghw) 
Cpp :: string class in c++ 
Cpp :: store arbitrarly large vector of doubles c++ 
Cpp :: remove item from layout 
Cpp :: curl upload folder and subfolders 
Cpp :: input numbers to int c++ 
Cpp :: Corong_ExerciseNo3 
Cpp :: qtextedit no line break 
Cpp :: c++ to mips converter online 
Cpp :: sort an array using stl 
Cpp :: 10^18 data type in c++ 
Cpp :: gcd multi num 
Cpp :: how to install open cv2 in c++ on ubuntu 
Cpp :: c++ to c code converter online 
Cpp :: c++ map change order 
Cpp :: c++ correct upto 3 decimal places 
Cpp :: C++ Single Line Comments 
Cpp :: converting a string to lowercase inbuld function in cpp 
Cpp :: what is vector capacity in c++ 
Cpp :: 191. Number of 1 Bits leetcode solution in c++ 
Cpp :: stp 
Cpp :: 1047. Remove All Adjacent Duplicates In String solution leetcode in c++ 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =