Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <string>
  4. #include <vector>
  5. #include <unordered_map>
  6. #include <cassert>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. class NetworkDevice {
  12.     string label;
  13.     int capacity;
  14.     NetworkDevice* neighbours[256];
  15. public:
  16.     NetworkDevice(string label_, int capacity_);
  17.     bool attachTo(NetworkDevice* device);
  18.     int attachedDevices();
  19.     NetworkDevice* getAttachedDevice(int i);
  20.     bool isAttachedTo(NetworkDevice* device);
  21.     double currentThroughput();
  22.  
  23.     string getLabel();
  24.     void set_neighbour(NetworkDevice* device);
  25. };
  26.  
  27. vector<vector<NetworkDevice*>> recursive_helper(NetworkDevice* u, NetworkDevice* v, vector<NetworkDevice*> current_path, vector<vector<NetworkDevice*>> all_paths, unordered_map<string, bool> visited) {
  28.     current_path.push_back(u);
  29.     if (u == v) {
  30.         all_paths.push_back(current_path);
  31.         visited[u->getLabel()] = false;
  32.         return all_paths;
  33.     }
  34.     else {
  35.         for (int i = 0; i < u->attachedDevices(); i++) {
  36.             string name = u->getAttachedDevice(i)->getLabel();
  37.             if (!visited[name]) {
  38.                 visited[name] = true;
  39.                 recursive_helper(u->getAttachedDevice(i), v, current_path, all_paths, visited);
  40.             }
  41.         }
  42.     }
  43.     /*return all_paths;*/
  44. }
  45.  
  46. double getThroughput(vector<NetworkDevice*> v) {
  47.     double throughput = 1024;
  48.     for (int i = 0; i < v.size(); i++) {
  49.         if (v[i]->currentThroughput() < throughput) {
  50.             throughput = v[i]->currentThroughput();
  51.         }
  52.     }
  53.     return throughput;
  54. }
  55.  
  56. vector<string> maxThroughputRoute(NetworkDevice* d1, NetworkDevice* d2) {
  57.     unordered_map<string, bool> visited;
  58.     vector<NetworkDevice*> current_path;
  59.     assert(current_path.size() == 0);
  60.     vector<vector<NetworkDevice*>> paths;
  61.     assert(paths.size() == 0);
  62.     vector<vector<NetworkDevice*>> all_paths = recursive_helper(d1, d2, current_path, paths, visited);
  63.     double maxThroughput = getThroughput(all_paths[0]);
  64.     int max_index = 0;
  65.  
  66.     for (int i = 1; i < all_paths.size(); i++) {
  67.         if (maxThroughput < getThroughput(all_paths[i])) {
  68.             maxThroughput = getThroughput(all_paths[i]);
  69.             max_index = i;
  70.         }
  71.     }
  72.  
  73.     vector<string> res;
  74.     current_path = all_paths[max_index];
  75.     for (int i = 0; i < current_path.size(); i++) {
  76.         res.push_back(current_path[i]->getLabel());
  77.     }
  78.     return res;
  79. }
  80.  
  81. NetworkDevice::NetworkDevice(string label_, int capacity_) {
  82.     label = label_;
  83.     capacity = capacity_;
  84. }
  85.  
  86. bool NetworkDevice::attachTo(NetworkDevice* device) {
  87.     if (capacity == 256) {
  88.         return false;
  89.     }
  90.     if (isAttachedTo(device)) {
  91.         return false;
  92.     }
  93.     neighbours[capacity] = device;
  94.     capacity++;
  95.     device->set_neighbour(this);
  96.     return true;
  97. }
  98.  
  99. int NetworkDevice::attachedDevices() {
  100.     return capacity;
  101. }
  102.  
  103. NetworkDevice* NetworkDevice::getAttachedDevice(int i) {
  104.     if (i < capacity) {
  105.         return neighbours[i];
  106.     }
  107.     return nullptr;
  108. }
  109.  
  110. bool NetworkDevice::isAttachedTo(NetworkDevice* device) {
  111.     for (int i = 0; i < capacity; i++) {
  112.         if (neighbours[i] == device) {
  113.             return true;
  114.         }
  115.     }
  116.     return false;
  117. }
  118.  
  119. double NetworkDevice::currentThroughput() {
  120.     if (capacity == 0) {
  121.         return 0;
  122.     }
  123.     return 1024.0 / capacity;
  124. }
  125.  
  126. string NetworkDevice::getLabel() {
  127.     return label;
  128. }
  129.  
  130. void NetworkDevice::set_neighbour(NetworkDevice* device) {
  131.     neighbours[capacity] = device;
  132.     capacity++;
  133. }
  134.  
  135.  
  136. int main() {
  137.  
  138.     NetworkDevice* C3 = new NetworkDevice("C3", 2);
  139.     NetworkDevice* C4 = new NetworkDevice("C4", 1);
  140.     NetworkDevice* C5 = new NetworkDevice("C5", 1);
  141.     NetworkDevice* C6 = new NetworkDevice("C6", 1);
  142.     NetworkDevice* C7 = new NetworkDevice("C7", 2);
  143.     NetworkDevice* S0 = new NetworkDevice("S0", 4);
  144.     NetworkDevice* S1 = new NetworkDevice("S1", 4);
  145.     NetworkDevice* S2 = new NetworkDevice("S2", 2);
  146.     NetworkDevice* S8 = new NetworkDevice("S8", 2);
  147.     NetworkDevice* S9 = new NetworkDevice("S9", 3);
  148.  
  149.     C3->attachTo(S8);
  150.     C3->attachTo(S0);
  151.     S8->attachTo(S9);
  152.     S9->attachTo(C7);
  153.     S9->attachTo(S1);
  154.     C7->attachTo(S2);
  155.     S2->attachTo(S1);
  156.     S1->attachTo(C6);
  157.     S1->attachTo(S0);
  158.     S0->attachTo(C5);
  159.     S0->attachTo(C4);
  160.  
  161.     vector<string> res = maxThroughputRoute(C3, C7);
  162.    
  163.     for (int i = 0; i < res.size(); i++) {
  164.         cout << res[i] << " ";
  165.     }
  166.     cout << endl;
  167.  
  168.     return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement