From 791e0b1ed4e87fc9299072f5cab1c47da43ec09d Mon Sep 17 00:00:00 2001 From: arpanjain97 Date: Thu, 12 Oct 2017 17:12:54 +0530 Subject: [PATCH 1/4] Add Dijkstra's single source shortest path Algorithm in Greedy Algorithms directory --- Greedy Algorithms/Dijkstra.cpp | 103 +++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 Greedy Algorithms/Dijkstra.cpp diff --git a/Greedy Algorithms/Dijkstra.cpp b/Greedy Algorithms/Dijkstra.cpp new file mode 100644 index 000000000..9cdcf077a --- /dev/null +++ b/Greedy Algorithms/Dijkstra.cpp @@ -0,0 +1,103 @@ +#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; +} From dbeacd4e176bdc3b930f554640ac07aea6b30604 Mon Sep 17 00:00:00 2001 From: arpanjain97 Date: Thu, 12 Oct 2017 17:20:34 +0530 Subject: [PATCH 2/4] Add Bellman-Ford Algorithm --- Dynamic Programming/Bellman-Ford.cpp | 112 +++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 Dynamic Programming/Bellman-Ford.cpp diff --git a/Dynamic Programming/Bellman-Ford.cpp b/Dynamic Programming/Bellman-Ford.cpp new file mode 100644 index 000000000..83d2aead0 --- /dev/null +++ b/Dynamic Programming/Bellman-Ford.cpp @@ -0,0 +1,112 @@ +#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; +} From 9cd56c01ad87cc282ddc815ff3bf2a9ec79801e9 Mon Sep 17 00:00:00 2001 From: arpanjain97 Date: Thu, 12 Oct 2017 17:23:14 +0530 Subject: [PATCH 3/4] 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; +} From 8b6548250039bd838d0a461e04fba3ee43ce29cd Mon Sep 17 00:00:00 2001 From: arpanjain97 Date: Fri, 13 Oct 2017 11:54:05 +0530 Subject: [PATCH 4/4] Fix output display --- Dynamic Programming/Bellman-Ford.cpp | 8 ++++++-- Greedy Algorithms/Dijkstra.cpp | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Dynamic Programming/Bellman-Ford.cpp b/Dynamic Programming/Bellman-Ford.cpp index 83d2aead0..ca1cd49ef 100644 --- a/Dynamic Programming/Bellman-Ford.cpp +++ b/Dynamic Programming/Bellman-Ford.cpp @@ -39,8 +39,12 @@ class Graph{ //Utility function to print distances void print(int dist[], int V){ cout<<"\nVertex Distance"<