1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
This is my code.
#include <stdio.h>
#include <limits.h>
#include<iostream>
using namespace std;
int V;
int minDistance(int dist[], bool sptSet[], int V)
{
int Radius[V], Diameter[V];
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
int printSolution(int dist[], int n, int V)
{
printf("Vertex Distance from Source\n");
for (int i = 0; i < V; i++)
printf("%d tt %d\n", i, dist[i]);
return 0;
}
void dijkstra(int graph[V][V], int src)
{
int dist[V];
bool sptSet[V];
for (int i = 0; i < V; i++)
dist[i] = INT_MAX, sptSet[i] = false;
dist[src] = 0;
for (int count = 0; count < V-1; count++)
{
int u = minDistance(dist, sptSet);
sptSet[u] = true;
for (int v = 0; v < V; v++)
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX
&& dist[u]+graph[u][v] < dist[v])
dist[v] = dist[u] + graph[u][v];
}
printSolution(dist, V);
}
int main()
{
int v;
cout << "vertex number: "; cin >> v;
int graph[V][V];
for(int c = 0; c < v; c++)
{
for(int c1 = 0; c1 < v; c1++)
{
cout << "Vertex which connected " << c << " " << c1 << " vertex: "; cin >> graph[c][c1];
}
}
for(int c2 = 0; c2< 9; c2++)
{
dijkstra(graph, c2);
}
return 0;
}
|