-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exp7-link-state-routing.cpp
126 lines (113 loc) · 3.04 KB
/
Exp7-link-state-routing.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Implement Link state routing algorithm
// Sample Input for Graph:
/**
* 0 4 -1 -1
* 4 0 1 5
* -1 1 0 2
* -1 5 2 0
**/
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
using namespace std;
/* Vertex names */
vector<char> ch = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T'};
int fieldWidth = 10;
int inf = INT32_MAX;
void printSPT(vector<int> &dist, vector<int> &parent)
{
int V = dist.size();
for (int i = 0; i < V; i++)
{
vector<int> path;
for (int u = i; u != -1; u = parent[u])
path.push_back(u);
reverse(path.begin(), path.end());
cout << "Path for Node " << ch[i] << ": ";
for (int p = 0; p < path.size() - 1; p++)
cout << ch[path[p]] << "->";
cout << ch[path[path.size() - 1]] << "\n";
}
}
pair<vector<int>, vector<int>> dijkstra(vector<vector<int>> &graph, int src)
{
int V = graph.size();
vector<int> dist(V, inf);
vector<int> parent(V, src);
vector<bool> flag(V, false); // if flag[i] is true, then the node i is included in the spt otherwise not.
dist[src] = 0;
parent[src] = -1;
for (int c = 0; c < V - 1; c++)
{
int minVal = inf, minIndex;
for (int i = 0; i < V; i++)
{
if (flag[i] == false and dist[i] < minVal)
{
minIndex = i;
minVal = dist[i];
}
}
int u = minIndex;
flag[u] = true;
for (int v = 0; v < V; v++)
{
if (!flag[v] and graph[u][v] != inf and dist[u] + graph[u][v] < dist[v])
{
dist[v] = dist[u] + graph[u][v];
parent[v] = u;
}
}
}
cout << "\nSPT for router " << ch[src] << ":\n";
printSPT(dist, parent);
return {parent, dist};
}
vector<vector<int>> inputGraph()
{
int V;
cout << "\nEnter the no of nodes: ";
cin >> V;
vector<vector<int>> graph(V, vector<int>(V, -1));
cout << "Enter distances in the form of matrix\n (-1 if nodes are not connected):\n";
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
cin >> graph[i][j];
if (graph[i][j] < 0)
graph[i][j] = inf;
}
}
return graph;
}
void buildRoutingTable(vector<int> &parent, vector<int> &dist, int src)
{
int V = dist.size();
cout << "\nRouting table for " << ch[src] << ":\n";
cout << setw(fieldWidth) << "To" << setw(fieldWidth) << "Dist" << setw(fieldWidth) << "Via" << endl;
for (int i = 0; i < V; i++)
{
int memo = i;
for (int u = i; parent[u] != -1; u = parent[u])
memo = u;
cout << setw(fieldWidth) << ch[i] << setw(fieldWidth) << dist[i];
if (memo == i)
cout << setw(fieldWidth) << "-" << endl;
else
cout << setw(fieldWidth) << ch[memo] << endl;
}
}
int main()
{
vector<vector<int>> graph = inputGraph();
int V = graph.size();
for (int src = 0; src < V; src++)
{
auto p = dijkstra(graph, src);
vector<int> parent = p.first;
vector<int> dist = p.second;
buildRoutingTable(parent, dist, src);
}
}