-
Notifications
You must be signed in to change notification settings - Fork 1
/
ospf.cpp
172 lines (148 loc) · 4.6 KB
/
ospf.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Jeff Ruhl
// OSPF Routing
#include <functional>
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#include <limits>
using namespace std;
typedef float Cost;
typedef vector<vector<Cost> > RoutingTable;
struct RouterCost
{
int router_id;
Cost cost;
RouterCost( int router_id, Cost cost ) {
this->router_id = router_id;
this->cost = cost;
}
bool operator>( const RouterCost& rhs ) const {
return this->cost > rhs.cost;
}
};
double calculate_bandwidth_cost( Cost );
vector<Cost> find_costs( int, const RoutingTable& );
int main( int argc, char const *argv[] ) {
ifstream fin("ospf.in");
if ( fin ) {
ofstream fout("ospf.out");
fout << setprecision( 2 ) << fixed;
bool first_routing_table = true;
int router_count = 0;
fin >> router_count;
while ( 0 != router_count ) {
RoutingTable routing_table( router_count + 1, RoutingTable::value_type( router_count + 1, 0 ) );
if ( first_routing_table ) {
first_routing_table = false;
}
else {
fout << "\n*****" << endl;
}
for ( int router = 1; router <= router_count; ++router ) {
for ( int connected_router = 1; connected_router <= router_count; ++connected_router ) {
long long bps = 0;
fin >> bps;
routing_table[router][connected_router] = calculate_bandwidth_cost( bps );
}
}
int from_router = 0;
bool first_from_router = true;
fin >> from_router;
while ( from_router != 0 ) {
auto costs = find_costs( from_router, routing_table );
if ( first_from_router ) {
first_from_router = false;
}
else {
fout << endl;
}
fout << from_router << ": ";
bool first_to_router = true;
for ( int to_router = 1; to_router <= router_count; ++to_router ) {
if ( to_router == from_router ) {
continue;
}
if ( first_to_router ) {
first_to_router = false;
}
else {
fout << " ";
}
fout << to_router << "(" << costs[to_router] << ")";
}
fin >> from_router;
}
fin >> router_count;
}
fout.close();
fin.close();
}
else {
cout << "Couldn't open file 'ospf.in'" << endl;
}
return 0;
}
inline double calculate_bandwidth_cost( Cost bandwidth ) {
return (0 == bandwidth ? 0 : 100000000.0 / bandwidth);
}
vector<Cost> find_costs( int from_router_id,
const RoutingTable& routing_table ) {
vector<int> parents;
vector<Cost> distances;
vector<bool> visited;
priority_queue<RouterCost, vector<RouterCost>, greater<RouterCost> > cheapest;
// scans a router, finding any shorter paths from it to other routers
auto scan = [&]( int router_id ) {
auto adjacent_router_costs = routing_table[router_id];
for ( size_t connected_router_id = 1;
connected_router_id < adjacent_router_costs.size();
++connected_router_id )
{
Cost cost = adjacent_router_costs[connected_router_id];
// if connected_router really is connected
if ( 0 != cost && !visited[connected_router_id] ) {
// if we've come across a shorter path then re-assign the connected
// router's parent & distance
Cost total_cost = distances[router_id] + cost;
if ( total_cost < distances[connected_router_id] ) {
distances[connected_router_id] = total_cost;
parents[connected_router_id] = router_id;
cheapest.push( RouterCost( connected_router_id, total_cost ) );
}
}
}
};
// router with id of 0 is never referenced but we need something here to
// keep the parallel vector indices aligned with the router ids
distances.push_back( 0 );
parents.push_back( 0 );
visited.push_back( false );
// initialize arrays
for ( int router_id = 1; (size_t)router_id < routing_table.size(); ++router_id ) {
parents.push_back( 0 );
visited.push_back( false );
// initialize distances for all routers to infinity except the first router
if ( router_id == from_router_id ) {
distances.push_back( 0 );
}
else {
distances.push_back( numeric_limits<Cost>::max() );
}
}
cheapest.push( RouterCost( from_router_id, distances[from_router_id] ) );
// perform dijkstra's algorithm
while ( !cheapest.empty() ) {
// select shortest distance
auto router_distance = cheapest.top(); cheapest.pop();
// scan it
if ( !visited[router_distance.router_id] ) {
visited[router_distance.router_id] = true;
scan( router_distance.router_id );
}
}
return distances;
}