Skip to content

Commit

Permalink
Merge pull request #1 from SamanEN/lsrp-dvrp
Browse files Browse the repository at this point in the history
Lsrp dvrp
  • Loading branch information
alumpish authored Jun 5, 2023
2 parents 6e44efb + 2e9d9d3 commit 6851c96
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 7 deletions.
4 changes: 2 additions & 2 deletions CA3 - Routing Protocols/.vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "make&debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/network.out",
"program": "${workspaceFolder}/debug/main",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
Expand All @@ -24,7 +24,7 @@
"text": "skip -rfunction ^std::"
}
],
"preLaunchTask": "make"
"preLaunchTask": "make_debug"
}
]
}
28 changes: 24 additions & 4 deletions CA3 - Routing Protocols/.vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,35 @@
"version": "2.0.0",
"tasks": [
{
"label": "make",
"label": "make_debug",
"type": "shell",
"command": "make",
"command": "make debug",
"args": [],
"problemMatcher": "$gcc",
"group": "build"
},
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
},
"detail": "Task generated by Debugger."
}
]
}
}
4 changes: 4 additions & 0 deletions CA3 - Routing Protocols/src/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ int Graph::getCost(int src, int dest) const {
return adj_vec_[src - 1][dest - 1].cost_;
}

int Graph::getNodesCount() const {
return adj_vec_.size();
}

std::string Graph::toString() const {
std::ostringstream output_str;
output_str << std::setw(5) << " ";
Expand Down
3 changes: 2 additions & 1 deletion CA3 - Routing Protocols/src/graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class Graph {
void changeLinkCost(int src, int dest, int new_cost);
void removeLink(int src, int dest);
int getCost(int src, int dest) const;

int getNodesCount() const;

std::string toString() const;

private:
Expand Down
5 changes: 5 additions & 0 deletions CA3 - Routing Protocols/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#include "cli.hpp"
#include "graph.hpp"
#include "graph_handler.hpp"
#include "network.hpp"
#include "network_handler.hpp"

int main() {
Graph graph;
Cli cli;
GraphHandler graph_handler(graph);
Network network(graph);
NetworkHandler network_handler(network);
graph_handler.setupCli(cli);
network_handler.setupCli(cli);
cli.run();
return 0;
}
122 changes: 122 additions & 0 deletions CA3 - Routing Protocols/src/network.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include "network.hpp"

#include <limits.h>

#include <iostream>
#include <vector>

Network::Network(Graph& graph) : graph_(graph) {}

int Network::minDistance(std::vector<int> dist, std::vector<bool> visited) {
int min = INT_MAX, min_index = 0;

for (int v = 0; v < dist.size(); ++v) {
if (!visited[v] && dist[v] <= min) {
min = dist[v];
min_index = v;
}
}

return min_index;
}

void Network::printDist(std::vector<int> dist) {
std::cout << "Node\tDistance" << std::endl;
for (int i = 0; i < dist.size(); ++i) {
if (dist[i] == INT_MAX)
std::cout << i + 1 << "\t" << "-" << std::endl;
else
std::cout << i + 1 << "\t" << dist[i] << std::endl;
}
std::cout << std::endl;
}

void Network::printPath(std::vector<int> parent, int j) {
if (parent[j] == -1) {
return;
}

printPath(parent, parent[j] - 1);
std::cout << " > " << j + 1;
}

void Network::printNode(int source, std::vector<int> dist, std::vector<int> parent, int n) {
std::cout << "Source: " << source << std::endl;
std::cout << "Node\tDistance\tPath" << std::endl;
for (int i = 0; i < n; ++i) {
std::cout << i + 1 << "\t" << dist[i] << "\t\t" << source;
printPath(parent, i);
std::cout << std::endl;
}
std::cout << std::endl;
}

void Network::lsrp() {
int n = graph_.getNodesCount();
for (int i = 0; i < n; ++i) {
lsrp(i + 1);
}
}

void Network::lsrp(int source) {
int n = graph_.getNodesCount();
std::vector<int> dist(n, INT_MAX);
std::vector<bool> visited(n, false);
std::vector<int> parent(n, -1);

dist[source - 1] = 0;

for (int i = 0; i < n - 1; ++i) {
int u = minDistance(dist, visited);
visited[u] = true;

for (int v = 0; v < n; ++v) {
if (!visited[v] && graph_.getCost(u + 1, v + 1) != -1 &&
dist[u] != INT_MAX && dist[u] + graph_.getCost(u + 1, v + 1) < dist[v]) {
dist[v] = dist[u] + graph_.getCost(u + 1, v + 1);
parent[v] = u + 1;
}
}

std::cout << "Iter " << i << " Src:" << source << std::endl;
std::cout << "-----------------------------" << std::endl;
printDist(dist);
}

printNode(source, dist, parent, n);
}

void Network::dvrp() {
int n = graph_.getNodesCount();
for (int i = 0; i < n; ++i) {
dvrp(i + 1);
}
}

void Network::dvrp(int source) {
int n = graph_.getNodesCount();
std::vector<int> dist(n, INT_MAX);
std::vector<bool> visited(n, false);
std::vector<int> parent(n, -1);

dist[source - 1] = 0;

for (int i = 0; i < n - 1; ++i) {
int u = minDistance(dist, visited);
visited[u] = true;

for (int v = 0; v < n; ++v) {
if (!visited[v] && graph_.getCost(u + 1, v + 1) != -1 &&
dist[u] != INT_MAX && dist[u] + graph_.getCost(u + 1, v + 1) < dist[v]) {
dist[v] = dist[u] + graph_.getCost(u + 1, v + 1);
parent[v] = u + 1;
}
}

std::cout << "Iter " << i << " Src:" << source << std::endl;
std::cout << "-----------------------------" << std::endl;
printDist(dist);
}

printNode(source, dist, parent, n);
}
24 changes: 24 additions & 0 deletions CA3 - Routing Protocols/src/network.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef NETWORK_HPP
#define NETWORK_HPP

#include <vector>

#include "graph.hpp"

class Network {
public:
Network(Graph& graph);
int minDistance(std::vector<int> dist, std::vector<bool> visited);
void printDist(std::vector<int> dist);
void printPath(std::vector<int> parent, int j);
void printNode(int source, std::vector<int> dist, std::vector<int> parent, int n);
void lsrp();
void lsrp(int source);
void dvrp();
void dvrp(int source);

private:
Graph& graph_;
};

#endif
31 changes: 31 additions & 0 deletions CA3 - Routing Protocols/src/network_handler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "network_handler.hpp"

#include <functional>

#include "cli.hpp"
#include "network.hpp"

NetworkHandler::NetworkHandler(Network network)
: network_(network) {}

std::string NetworkHandler::lsrp(const Cli::ArgumentGroups& args) {
int node = std::stoi(args.getArgumentGroups()[0][0]);
network_.lsrp(node);
return "";
}

std::string NetworkHandler::dvrp(const Cli::ArgumentGroups& args) {
int node = std::stoi(args.getArgumentGroups()[0][0]);
network_.dvrp(node);
return "";
}

void NetworkHandler::setupCli(Cli& cli) {
using Command = Cli::Command;
auto bind = [this](std::string (NetworkHandler::*f)(const Cli::ArgumentGroups&)) {
return std::bind(f, this, std::placeholders::_1);
};

cli.addCommand(Command("lsrp", bind(&NetworkHandler::lsrp)));
cli.addCommand(Command("dvrp", bind(&NetworkHandler::dvrp)));
}
20 changes: 20 additions & 0 deletions CA3 - Routing Protocols/src/network_handler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef NETWORK_HANDLER_HPP
#define NETWORK_HANDLER_HPP

#include "cli.hpp"
#include "network.hpp"

class NetworkHandler {
public:
NetworkHandler(Network network);

std::string lsrp(const Cli::ArgumentGroups& args);
std::string dvrp(const Cli::ArgumentGroups& args);

void setupCli(Cli& cli);

private:
Network network_;
};

#endif

0 comments on commit 6851c96

Please sign in to comment.