-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGFG Graph-6 Dijkstra.cpp
80 lines (70 loc) · 1.93 KB
/
GFG Graph-6 Dijkstra.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <stdlib.h>
#include <bits/stdc++.h>
#include <vector>
#include <queue>
using namespace std;
vector <vector <int> > v;
vector <bool> vis;
vector <int> dis; //dis[i] contains shortest distance of i node from source node. Is updated continuously
int mindist(int n) //Returns index of node which is closest to the current finalized Minimum Spanning Tree
{
int minn=9999,index;
for (int i=0;i<n;i++)
{ if (vis[i]==false && dis[i]<minn )
{
minn=dis[i];
index=i;
}
}
return index;
}
void dijstra(int s,int n) //Gives shortest distance of each node from Source node
{
dis[s]=0; //distance of source updated to Zero
for (int i=0;i<n;i++) //Each time we pick the closest node and the distance of all the nodes is updated to new value. We keep doing this unless all nodes are covered.
{
int u = mindist(n); //Currently closest node to MST taken
vis[u]=true; // Updated
for (int a=0;a<n;a++)
{
if (vis[a]!=true && v[u][a] && (dis[u]+v[u][a])<dis[a] ) //If not visited, edge exists, distance of node 'a' is updated to current distance it has from MST
dis[a]=dis[u]+v[u][a];
}
}
}
void printgraph()
{
for (int i=0;i<v.size();i++)
{
for (int j=0;j<v[i].size();j++)
{
cout << v[i][j] << " ";
}
cout << endl;
}
}
int main()
{
int n;
cin >> n;
v.assign(n,vector <int> ());
vis.assign(n,false);
dis.assign(n,99999);
int t;
for (int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
{
cin >> t;
v[i].push_back(t); // Adjacency Matrix Representation
}
}
int s;
cin >> s;
printgraph();
dijstra(s,n);
cout << endl;
for (int i=0;i<n;i++)
cout << dis[i] << " ";
}