Question: Please convert these c++ codes into c language, and run it sucessfully!!!! Thank you! #include #include using namespace std; #define V 14 //No. of vertices
Please convert these c++ codes into c language, and run it sucessfully!!!! Thank you!
#include
#include
using namespace std;
#define V 14 //No. of vertices in a graph
int minDist(int dist[], bool sptSet[]) // a function to find the node with minimum distance value from the set of
//vertices not included in the shortest path tree.
{ int min=INT_MAX, min_index;
for(int v=0;v
if(sptSet[v] == false && dist[v] <= min)
{ min = dist[v];
min_index = v; }
return min_index;
}
// A below function to print the distance array
void printFinal(int dist[], int n)
{
cout<<"Node Distance from source " ;
for(int i=0;i
{ cout<
cout<
}
// Below funtion that implements Dijkstra's single source shortest path algorithm
// for a graph represented using adjacency matrix representation.
void dijkstra(int graph[][],int src)
{
int dist[V]; //The output array. dist[i] will hold the shortest distance from src to i
bool sptSet[V]; // sptSet[i] will true if vertex i is included in shortest
//path tree or shortest distance from src to i is finalized
// Initialize all distances as INFINITE and stpSet[] as false
for(i=0;i
{
dist[i]=INT_MAX;
sptSet[i]=false;
}
dist[src]=0; // Distance of source vertex from itself is always 0
for(int count=0;count
{
int u=minDist(dist,sptSet); // Pick the minimum distance vertex from the set of vertices not
//yet processed. u is always equal to src in first iteration.
sptSet[u]=true; // Mark the picked vertex as processed
for(int v=0;v
{
// Update dist[v] only if is not in sptSet, there is an edge from
// u to v, and total weight of path from src to v through u is
// smaller than current value of dist[v]
if(!sptSet[v] && graph[u][v] && distt[u] != INT_MAX && dist[u] + graph[u][v] < dist[v])
dist[v]=dist[u] + graph[u][v] ;
}
printFinal(dist, V);
} //end of dijkstra function
int main()
{
int graph[V][V]= { {0,808,0,0,0,0,0,0,0,0,0,0,2060,0},{808,0,414,0,0,0,0,0,2257,0,0,0,0,0},{0,414,0,1440,0,0,0,0,0,0,0,0,0,272},{0,0,1440,0,517,0,0,1614,0,949,0,0,0,0},{0,0,0,517,0,0,0,0,0,0,0,0,1425,0},{0,0,0,0,0,0,0,0,0,0,0,0,1423,0,0},{0,0,0,0,0,0,0,237,811,0,0,0,0,0},{0,0,0,1614,0,0,237,0,0,1217,0,0,0,0},{0,2257,0,0,0,0,811,0,0,0,0,1771,0,0},{0,0,0,949,0,0,0,1217,0,0,797,0,0,0},{0,0,0,0,0,0,0,0,0,0,792,0,0,0},{0,0,0,0,0,0,0,0,1771,0,0},{2060,0,0,0,948,1423,0,0,0,0,0,0,0,0},{0,0,272,0,0,0,0,0,0,0,0,0,1780,0}};
dijkstra(graph,0);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
