-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from SamanEN/lsrp-dvrp
Lsrp dvrp
- Loading branch information
Showing
9 changed files
with
234 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |