#include<iostream>
#include<climits>
using namespace std;
int miniDist(int distance[], bool Tset[]) // finding minimum distance
{
int minimum=INT_MAX,ind;
for(int k=0;k<6;k++)
{
if(Tset[k]==false && distance[k]<=minimum)
{
minimum=distance[k];
ind=k;
}
}
return ind;
}
void DijkstraAlgo(int graph[6][6],int src) // adjacency matrix
{
int distance[6]; // // array to calculate the minimum distance for each node
bool Tset[6];// boolean array to mark visited and unvisited for each node
for(int k = 0; k<6; k++)
{
distance[k] = INT_MAX;
Tset[k] = false;
}
distance[src] = 0; // Source vertex distance is set 0
for(int k = 0; k<6; k++)
{
int m=miniDist(distance,Tset);
Tset[m]=true;
for(int k = 0; k<6; k++)
{
// updating the distance of neighbouring vertex
if(!Tset[k] && graph[m][k] && distance[m]!=INT_MAX && distance[m]+graph[m][k]<distance[k])
distance[k]=distance[m]+graph[m][k];
}
}
cout<<"Vertex Distance from source vertex"<<endl;
for(int k = 0; k<6; k++)
{
char str=65+k;
cout<<str<<" "<<distance[k]<<endl;
}
}
int main()
{
int graph[6][6]={
{0, 1, 2, 0, 0, 0},
{1, 0, 0, 5, 1, 0},
{2, 0, 0, 2, 3, 0},
{0, 5, 2, 0, 2, 2},
{0, 1, 3, 2, 0, 1},
{0, 0, 0, 2, 1, 0}};
DijkstraAlgo(graph,0);
return 0;
}
Code Example |
---|
Cpp :: how to turn int into string c++ |
Cpp :: opengl draw semi circle c++ |
Cpp :: Reverse words in a given string solution in c++ |
Cpp :: how can we create 4 digit random number in c++ |
Cpp :: ascii cpp |
Cpp :: Max element in an array with the index in c++ |
Cpp :: stack c++ |
Cpp :: c++ output current timestamp |
Cpp :: set to vector |
Cpp :: insert in vector |
Cpp :: c ++ splitlines |
Cpp :: ++ how to write quotation mark in a string |
Cpp :: c++ write to csv file append |
Cpp :: c++ check if debug or release visual studio |
Cpp :: c++ client service ros |
Cpp :: template c++ |
Cpp :: cpp linked list |
Cpp :: abstraction in cpp |
Cpp :: add matic mainnet to metamask mobile |
Cpp :: c++ std string to float |
Cpp :: create new node in tree |
Cpp :: vector<intv[] |
Cpp :: dequeue c++ |
Cpp :: potato |
Cpp :: cout stack in c++ |
Cpp :: cyclically rotate an array by once |
Cpp :: c++ awitch statements |
Cpp :: what algorithm does bitcoin use |
Cpp :: copy vector c++ |
Cpp :: know what the input data is whether integer or not |