diff --git a/Dynamic Programming/Bellman-Ford.cpp b/Dynamic Programming/Bellman-Ford.cpp new file mode 100644 index 000000000..ca1cd49ef --- /dev/null +++ b/Dynamic Programming/Bellman-Ford.cpp @@ -0,0 +1,116 @@ +#include +#include + +using namespace std; + +//Wrapper class for storing an edge +class Edge{ + public: int src,dst,weight; +}; + +//Wrapper class for storing a graph +class Graph{ + public: + int vertexNum,edgeNum; + Edge* edges; + + //Constructs a graph with V vertices and E edges + Graph(int V,int E){ + this->vertexNum = V; + this->edgeNum = E; + this->edges =(Edge*) malloc(E * sizeof(Edge)); + } + + //Adds the given edge to the graph + void addEdge(int src, int dst, int weight){ + static int edgeInd = 0; + if(edgeInd < this->edgeNum){ + Edge newEdge; + newEdge.src = src; + newEdge.dst = dst; + newEdge.weight = weight; + this->edges[edgeInd++] = newEdge; + } + + } + +}; + +//Utility function to print distances +void print(int dist[], int V){ + cout<<"\nVertex Distance"<>V; + cout<<"Enter number of edges: "; + cin>>E; + Graph G(V,E); + for(int i=0; i>src; + cout<<"Enter destination: "; + cin>>dst; + cout<<"Enter weight: "; + cin>>weight; + G.addEdge(src, dst, weight); + } + cout<<"\nEnter source: "; + cin>>gsrc; + BellmanFord(G,gsrc); + + return 0; +} 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; +} diff --git a/Greedy Algorithms/Dijkstra.cpp b/Greedy Algorithms/Dijkstra.cpp new file mode 100644 index 000000000..e53f227c0 --- /dev/null +++ b/Greedy Algorithms/Dijkstra.cpp @@ -0,0 +1,107 @@ +#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*) calloc(V, sizeof(int)); + + } + + //Adds the given edge to the graph + void addEdge(int src, int dst, int weight){ + this->edges[src][dst] = weight; + } + + +}; +//Utility function to find minimum distance vertex in mdist +int minDistance(int mdist[], bool vset[], int V){ + int minVal = INT_MAX, minInd; + for(int i=0; i>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); + } + cout<<"\nEnter source:"; + cin>>gsrc; + Dijkstra(G,gsrc); + + return 0; +}