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; +}