Add cn/dvr: Distance Vector Routing simulator with count-to-infinity demos and captured output

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_017ic4b6V3xQR5XUmTbjaYAK
This commit is contained in:
Claude
2026-11-12 20:56:48 +05:30
parent abaf3840f2
commit f784f9b6ee
23 changed files with 1436 additions and 0 deletions
+207
View File
@@ -0,0 +1,207 @@
// dvr.cpp - Distance Vector Routing (DVR) simulator.
//
// Every router keeps ONE table: for each destination, (distance, next hop).
// Time moves in synchronous rounds. In each round every router reads the
// tables its neighbours had at the END of the previous round and applies the
// Bellman-Ford equation
//
// D_x(y) = min over neighbours v of [ c(x,v) + D_v(y) ]
//
// The next hop for y is the neighbour v that gave the minimum. The simulation
// stops when a whole round changes nothing: that is convergence.
//
// Build: g++ -std=c++17 -Wall -Wextra -O2 -o build/dvr dvr.cpp
// Run: build/dvr [--trace] [--watch X] [--poison] [--infinity N] < input/four-node.txt
//
// Input (whitespace separated, '#' starts a comment):
// n number of routers, named A, B, C, ...
// n*(n-1)/2 numbers link costs in the order A-B, A-C, ..., B-C, B-D, ...; -1 = no link
// fail X Y (optional, repeatable) remove link X-Y, then re-converge
// cost X Y c (optional, repeatable) change link X-Y to cost c, then re-converge
#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
static int INF = 16; // RIP-style "infinity": a distance >= INF means unreachable
static bool trace = false; // print every router's full table after every round
static bool poison = false; // split horizon with poison reverse
static int watchDest = -1; // print one column (distance to this router) every round
struct Network {
int n = 0;
vector<vector<int>> cost; // cost[x][y] = direct link cost, -1 = no link, 0 on the diagonal
vector<vector<int>> dist; // dist[x][y] = router x's current estimate to y
vector<vector<int>> next; // next[x][y] = neighbour x forwards to for y, -1 = none/self
};
static char name(int i) { return static_cast<char>('A' + i); }
static void initTables(Network& g) {
g.dist.assign(g.n, vector<int>(g.n, INF));
g.next.assign(g.n, vector<int>(g.n, -1));
for (int x = 0; x < g.n; x++) {
g.dist[x][x] = 0;
for (int y = 0; y < g.n; y++)
if (x != y && g.cost[x][y] >= 0) {
g.dist[x][y] = min(g.cost[x][y], INF);
g.next[x][y] = y;
}
}
}
static void printTable(const Network& g, int x) {
cout << name(x) << " Routing Table\n";
cout << "Dest Dist Next hop\n";
for (int y = 0; y < g.n; y++) {
cout << name(y) << " ";
if (g.dist[x][y] >= INF) cout << left << setw(6) << "INF" << "-\n";
else {
cout << left << setw(6) << g.dist[x][y];
cout << (g.next[x][y] < 0 ? '-' : name(g.next[x][y])) << "\n";
}
}
}
static void printAll(const Network& g) {
for (int x = 0; x < g.n; x++) { printTable(g, x); if (x + 1 < g.n) cout << "\n"; }
}
static void printWatch(const Network& g, int round) {
cout << "round " << setw(2) << right << round << " | to " << name(watchDest) << ":";
for (int x = 0; x < g.n; x++) {
if (x == watchDest) continue;
cout << " " << name(x) << "=";
if (g.dist[x][watchDest] >= INF) cout << "INF";
else cout << g.dist[x][watchDest] << "(" << name(g.next[x][watchDest]) << ")";
}
cout << "\n";
}
// One synchronous round. Returns true if any entry changed.
static bool exchangeRound(Network& g) {
const auto snapDist = g.dist; // what neighbours advertised at the end of the last round
const auto snapNext = g.next;
bool changed = false;
for (int x = 0; x < g.n; x++) {
for (int y = 0; y < g.n; y++) {
if (x == y) continue;
int best = INF, hop = -1;
for (int v = 0; v < g.n; v++) {
if (v == x || g.cost[x][v] < 0) continue; // v must be a neighbour
int advertised = snapDist[v][y];
if (poison && snapNext[v][y] == x) advertised = INF; // poison reverse
int cand = min(INF, g.cost[x][v] + advertised);
// strict '<' keeps the lowest-lettered neighbour on a tie, unless the
// current next hop ties, in which case we keep it (avoids route flapping)
if (cand < best || (cand == best && cand < INF && v == snapNext[x][y])) { best = cand; hop = v; }
}
if (best != g.dist[x][y] || hop != g.next[x][y]) changed = true;
g.dist[x][y] = best;
g.next[x][y] = (best >= INF) ? -1 : hop;
}
}
return changed;
}
// Independent check: Floyd-Warshall on the current links, with the same INF cap.
static bool matchesFloydWarshall(const Network& g) {
vector<vector<int>> d(g.n, vector<int>(g.n, INF));
for (int x = 0; x < g.n; x++)
for (int y = 0; y < g.n; y++)
d[x][y] = (x == y) ? 0 : (g.cost[x][y] >= 0 ? min(g.cost[x][y], INF) : INF);
for (int k = 0; k < g.n; k++)
for (int i = 0; i < g.n; i++)
for (int j = 0; j < g.n; j++)
d[i][j] = min(d[i][j], min(INF, d[i][k] + d[k][j]));
for (int x = 0; x < g.n; x++)
for (int y = 0; y < g.n; y++)
if (d[x][y] != g.dist[x][y]) return false;
return true;
}
static void converge(Network& g, const string& label) {
cout << "== " << label << " ==\n";
if (trace) { cout << "\n-- round 0 (direct links only) --\n"; printAll(g); cout << "\n"; }
if (watchDest >= 0) printWatch(g, 0);
int r = 0;
const int maxRounds = 200;
while (r < maxRounds) {
bool changed = exchangeRound(g);
if (!changed) break;
r++;
if (trace) { cout << "-- after round " << r << " --\n"; printAll(g); cout << "\n"; }
if (watchDest >= 0) printWatch(g, r);
}
cout << (r >= maxRounds ? "stopped at the round limit\n" : "converged: round " + to_string(r + 1) + " changed nothing\n");
cout << "rounds that changed something: " << r << "\n";
cout << "check vs Floyd-Warshall: " << (matchesFloydWarshall(g) ? "match" : "MISMATCH") << "\n";
if (!trace) { cout << "\n"; printAll(g); }
}
static int routerIndex(const string& s, int n) {
if (s.size() != 1 || s[0] < 'A' || s[0] >= 'A' + n) { cerr << "bad router name: " << s << "\n"; exit(2); }
return s[0] - 'A';
}
int main(int argc, char** argv) {
for (int i = 1; i < argc; i++) {
string a = argv[i];
if (a == "--trace") trace = true;
else if (a == "--poison") poison = true;
else if (a == "--infinity" && i + 1 < argc) INF = atoi(argv[++i]);
else if (a == "--watch" && i + 1 < argc) watchDest = argv[++i][0] - 'A';
else { cerr << "usage: dvr [--trace] [--watch X] [--poison] [--infinity N] < input\n"; return 2; }
}
// read all of stdin, dropping '#' comments
string all, line;
while (getline(cin, line)) { all += line.substr(0, line.find('#')); all += '\n'; }
istringstream in(all);
Network g;
if (!(in >> g.n) || g.n < 2 || g.n > 26) { cerr << "need 2..26 routers\n"; return 2; }
if (watchDest >= g.n) { cerr << "--watch router is outside the network\n"; return 2; }
g.cost.assign(g.n, vector<int>(g.n, -1));
for (int x = 0; x < g.n; x++) g.cost[x][x] = 0;
for (int x = 0; x < g.n; x++)
for (int y = x + 1; y < g.n; y++) {
int c;
if (!(in >> c)) { cerr << "missing link cost " << name(x) << "-" << name(y) << "\n"; return 2; }
if (c >= 0) g.cost[x][y] = g.cost[y][x] = c;
}
cout << "routers: " << g.n << ", infinity = " << INF << ", poison reverse = " << (poison ? "on" : "off") << "\nlinks:";
for (int x = 0; x < g.n; x++)
for (int y = x + 1; y < g.n; y++)
if (g.cost[x][y] >= 0) cout << " " << name(x) << "-" << name(y) << "=" << g.cost[x][y];
cout << "\n\n";
initTables(g);
converge(g, "initial convergence");
string cmd;
while (in >> cmd) {
string a, b;
if (cmd == "fail") {
in >> a >> b;
int x = routerIndex(a, g.n), y = routerIndex(b, g.n);
g.cost[x][y] = g.cost[y][x] = -1;
cout << "\n";
converge(g, "link " + a + "-" + b + " fails");
} else if (cmd == "cost") {
int c; in >> a >> b >> c;
int x = routerIndex(a, g.n), y = routerIndex(b, g.n);
g.cost[x][y] = g.cost[y][x] = c;
cout << "\n";
converge(g, "link " + a + "-" + b + " cost becomes " + to_string(c));
} else { cerr << "unknown command: " << cmd << "\n"; return 2; }
}
return 0;
}