-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdijakstras
57 lines (56 loc) · 926 Bytes
/
dijakstras
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
#include<iostream>
#include<climits>
using namespace std;
int minimumDist(int dist[], bool Tset[])
{
int min=INT_MAX,index;
for(int i=0;i<6;i++)
{
if(Tset[i]==false && dist[i]<=min)
{
min=dist[i];
index=i;
}
}
return index;
}
void Dijkstra(int graph[6][6],int src)
{
int dist[6];
bool Tset[6];
for(int i = 0; i<6; i++)
{
dist[i] = INT_MAX;
Tset[i] = false;
}
dist[src] = 0;
for(int i = 0; i<6; i++)
{
int m=minimumDist(dist,Tset);
Tset[m]=true;
for(int i = 0; i<6; i++)
{
if(!Tset[i] && graph[m][i] && dist[m]!=INT_MAX &&
dist[m]+graph[m][i]<dist[i])
dist[i]=dist[m]+graph[m][i];
}
}
cout<<"Vertex\t\tDistance from source"<<endl;
for(int i = 0; i<6; i++)
{
char str=65+i;
cout<<str<<"\t\t\t"<<dist[i]<<endl;
}
}
int main()
{
int graph[6][6]={
{0, 10, 20, 0, 0, 0},
{10, 0, 0, 50, 10, 0},
{20, 0, 0, 20, 33, 0},
{0, 50, 20, 0, 20, 2},
{0, 10, 33, 20, 0, 1},
{0, 0, 0, 2, 1, 0}};
Dijkstra(graph,0);
return 0;
}