From 9cd56c01ad87cc282ddc815ff3bf2a9ec79801e9 Mon Sep 17 00:00:00 2001 From: arpanjain97 Date: Thu, 12 Oct 2017 17:23:14 +0530 Subject: [PATCH] Add Floyd-Warshall Algorithm --- Dynamic Programming/Floyd-Warshall.cpp | 106 +++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 Dynamic Programming/Floyd-Warshall.cpp diff --git a/Dynamic Programming/Floyd-Warshall.cpp b/Dynamic Programming/Floyd-Warshall.cpp new file mode 100644 index 000000000..9f037a64d --- /dev/null +++ b/Dynamic Programming/Floyd-Warshall.cpp @@ -0,0 +1,106 @@ +#include +#include +#include + +using namespace std; + +//Wrapper class for storing a graph +class Graph{ + public: + int vertexNum; + int** edges; + + //Constructs a graph with V vertices and E edges + Graph(int V){ + this->vertexNum = V; + this->edges =(int**) malloc(V * sizeof(int*)); + for(int i=0; iedges[i] = (int*) malloc(V * sizeof(int)); + for(int j=0; jedges[i][j] = INT_MAX; + this->edges[i][i] = 0; + } + } + + //Adds the given edge to the graph + void addEdge(int src, int dst, int weight){ + this->edges[src][dst] = weight; + } + + +}; + + +//Utility function to print distances +void print(int dist[], int V){ + cout<<"\nThe Distance matrix for Floyd - Warshall"<>V; + cout<<"Enter number of edges: "; + cin>>E; + Graph G(V); + for(int i=0; i>src; + cout<<"Enter destination: "; + cin>>dst; + cout<<"Enter weight: "; + cin>>weight; + G.addEdge(src, dst, weight); + } + FloydWarshall(G); + + return 0; +}