From 791e0b1ed4e87fc9299072f5cab1c47da43ec09d Mon Sep 17 00:00:00 2001 From: arpanjain97 Date: Thu, 12 Oct 2017 17:12:54 +0530 Subject: [PATCH] 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; +}