Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change value type from int64_t to double in graph.h header #52 #53

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/classes/graph/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -556,11 +556,11 @@ template <typename T> class weighted_graph {
/**
* @brief Constructor for weighted graph.
* @param __type: type of the graph, either "directed" or "undirected".
* @param __adj: vector<pair<pair<T,T>, int64_t>>, you can pass a vector of
* @param __adj: vector<pair<pair<T,T>, double>>, you can pass a vector of
* pairs to construct the graph without doing multiple add_edge.
*/
weighted_graph(std::string __type,
std::vector<std::pair<std::pair<T, T>, int64_t>> __adj = {}) {
std::vector<std::pair<std::pair<T, T>, double>> __adj = {}) {
try {
if (__type == "directed" || __type == "undirected") {
this->__type = __type;
Expand Down Expand Up @@ -613,7 +613,7 @@ template <typename T> class weighted_graph {
* @param v: second node.
* @param w: weight between u and v.
*/
void add_edge(T u, T v, int64_t w) {
void add_edge(T u, T v, double w) {
if (__type == "undirected") {
adj[u].push_back(std::make_pair(v, w));
adj[v].push_back(std::make_pair(u, w));
Expand All @@ -634,7 +634,7 @@ template <typename T> class weighted_graph {
if (__elements.find(start) == __elements.end()) {
return false;
}
for (std::pair<T, int64_t> &x : adj[start]) {
for (std::pair<T, double> &x : adj[start]) {
if (x.first == end) {
return true;
}
Expand Down Expand Up @@ -682,7 +682,7 @@ template <typename T> class weighted_graph {
* @param end: ending node.
* @returns int64_t, the total cost of the path.
*/
int64_t shortest_path(T start, T end);
double shortest_path(T start, T end);

/**
* @brief connected_components function.
Expand All @@ -707,7 +707,7 @@ template <typename T> class weighted_graph {
* @param start: starting node.
* @returns int64_t, the total cost of the minimum spanning tree of the graph.
*/
int64_t prim(T start);
double prim(T start);

/**
* @brief bipartite function.
Expand Down Expand Up @@ -774,7 +774,7 @@ template <typename T> class weighted_graph {
* @param __type: type of the graph, either "directed" or "undirected".
* @param __elements: set of total elements of the graph.
*/
std::unordered_map<T, std::vector<std::pair<T, int64_t>>> adj;
std::unordered_map<T, std::vector<std::pair<T, double>>> adj;
std::string __type;
std::unordered_set<T> __elements;

Expand All @@ -788,7 +788,7 @@ template <typename T> class weighted_graph {
std::vector<std::vector<T>> &bridges) {
visited[start] = true;
in[start] = out[start] = time++;
for (std::pair<T, int64_t> &x : adj[start]) {
for (std::pair<T, double> &x : adj[start]) {
if (x.first != parent) {
if (visited.find(x.first) == visited.end()) {
dfs_bridge(x.first, start, time, visited, in, out, bridges);
Expand All @@ -806,7 +806,7 @@ template <typename T> size_t weighted_graph<T>::size() {
return __elements.size();
}

template <typename T> int64_t weighted_graph<T>::shortest_path(T start, T end) {
template <typename T> double weighted_graph<T>::shortest_path(T start, T end) {
if (__elements.find(start) == __elements.end()) {
std::cout << "Element: " << start << " is not found in the Graph" << '\n';
return -1;
Expand Down
Loading