-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exp6-distance-vector-routing.cpp
83 lines (73 loc) · 1.94 KB
/
Exp6-distance-vector-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
// Implement distance vector 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 <iomanip>
#include <vector>
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;
int main()
{
int V;
cout << "\nEnter Number of Nodes: ";
cin >> V;
vector<vector<int>> graph(V, vector<int>(V, -1));
vector<vector<int>> via(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;
}
}
for (int i = 0; i < V; i++)
via[i][i] = i;
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (graph[i][j] != inf)
via[i][j] = i;
}
}
vector<vector<int>> dist = graph;
for (int k = 0; k < V; k++)
{
for (int i = 0; i < V; i++)
{
for (int j = 0; j < V; j++)
{
if (dist[i][k] != inf and dist[k][j] != inf and dist[i][k] + dist[k][j] < dist[i][j])
{
dist[i][j] = dist[i][k] + dist[k][j];
via[i][j] = via[i][k] == i ? k : via[i][k];
}
}
}
}
cout << "\n\nAfter update, Routing tables are:";
for (int i = 0; i < V; i++)
{
cout << "\nRouting table for " << ch[i] << ":\n";
cout << setw(fieldWidth) << "To" << setw(fieldWidth) << "Dist" << setw(fieldWidth) << "Via" << endl;
for (int j = 0; j < V; j++)
{
cout << setw(fieldWidth) << ch[j] << setw(fieldWidth) << dist[i][j];
if (i == via[i][j])
cout << setw(fieldWidth) << "-" << endl;
else
cout << setw(fieldWidth) << ch[via[i][j]] << endl;
}
}
}