Weighted Round Robin (WRR) Algorithm
Description
The Weighted Round Robin (WRR) algorithm is a network scheduling technique that allocates a proportion of the bandwidth to each flow based on its weight. Each flow is assigned a weight, and the algorithm serves packets from each flow in a cyclic order, but the number of packets served from each flow in one cycle is proportional to its weight.
C++ Code
#include <iostream>
#include <vector>
using namespace std;
struct Flow {
int id;
int weight;
int packets;
};
void WRR(vector<Flow>& flows) {
int totalWeight = 0;
for (const auto& flow : flows) {
totalWeight += flow.weight;
}
while (true) {
bool packetsRemaining = false;
for (auto& flow : flows) {
if (flow.packets > 0) {
packetsRemaining = true;
int packetsToSend = min(flow.packets, flow.weight);
flow.packets -= packetsToSend;
cout << "Flow " << flow.id << " sends " << packetsToSend << " packets." << endl;
}
}
if (!packetsRemaining) break;
}
}
int main() {
vector<Flow> flows = {
{1, 3, 10},
{2, 1, 5},
{3, 2, 8}
};
WRR(flows);
return 0;
}
Time and Space Complexity
Operation | Time Complexity | Space Complexity |
---|---|---|
Weighted Round Robin (WRR) | O(N * M) | O(1) |
Where:
- N: Number of flows.
- M: Total number of packets to be sent.