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

Feature getters for set of neighbors #318

Merged
merged 36 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
45760d9
Remove static_cast of 0 to template type T in best_first_search
May 12, 2023
61a1ff1
Merge branch 'ZigRazor:master' into master
sbaldu May 19, 2023
5580070
Merge branch 'master' of github.com:sbaldu/CXXGraph
sbaldu May 22, 2023
4c51acb
Change all the raw pointers in the library to smart pointers
sbaldu May 24, 2023
f0cdb12
Rewrite tests to use smart pointers
sbaldu May 24, 2023
edc2a41
Finish converting tests to smart pointers
sbaldu May 25, 2023
74805e5
Add setter for nodes in Edge nodePair
sbaldu May 26, 2023
12b1c22
Fix addition of edges and construction of adjacency matrix
sbaldu May 26, 2023
f848924
Add overloads of algorithms which take raw pointers to mantain interface
sbaldu May 26, 2023
f2d2be9
Fix typos in tests
sbaldu May 26, 2023
177bb04
Change dynamic_casts into dynamic_pointer_casts
sbaldu May 26, 2023
703dc83
Decrease the number of random edges and nodes in RW and partition tests
sbaldu May 26, 2023
7e4360a
Merge branch 'master' of github.com:sbaldu/CXXGraph
sbaldu May 27, 2023
47e64fb
Add out/inOutEdges convenience methods for Graph
sbaldu May 30, 2023
cedbfbd
Add tests for the out/inOutEdges methods
sbaldu May 30, 2023
e5be848
Merge branch 'ZigRazor:master' into feature_inoutedges_getters
sbaldu May 30, 2023
f4f1e84
Add overload of equality operators and hash functions for pointers of…
sbaldu May 30, 2023
28ba46a
Implement new hash functions for pointers of nodes and edges
sbaldu May 30, 2023
0d1d510
Increase number of nodes and edges in partition and RW tests
sbaldu May 30, 2023
a063378
Fix merge conflits with master
sbaldu May 31, 2023
5236a87
Fix typo
sbaldu May 31, 2023
a930216
Use shared pointers in random node/edge generators
sbaldu May 31, 2023
bbd0684
Convert pointers in TransitiveReductionTest.cpp
sbaldu May 31, 2023
bb8372d
Add overload of addEdge for raw pointers
sbaldu May 31, 2023
c94a1dd
Fix typo
sbaldu May 31, 2023
f5f2297
Fix edge insertion in FloydWarshall benchmark
sbaldu May 31, 2023
dbe189e
Fix typo in benchmark
sbaldu May 31, 2023
ea732bc
Remove all the calls to new in the tests
sbaldu May 31, 2023
57dc2a4
Fix typo in benchmark FloydWarshall
sbaldu May 31, 2023
b6a73fc
Conversion of example codes
sbaldu May 31, 2023
92abb38
Merge conflict with smart pointers rework
sbaldu May 31, 2023
4e6ac48
Formatting
sbaldu May 31, 2023
2947342
Merge branch 'master' into feature_inoutedges_getters
sbaldu Jun 1, 2023
cc82485
Add test for overloads of out/inOutEdges
sbaldu Jun 1, 2023
7d89781
Change name of out/inOutEdges methods to out/inOutNeighbors
sbaldu Jun 1, 2023
adeb13f
Fix merge conflicts with master
sbaldu Jun 1, 2023
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
152 changes: 131 additions & 21 deletions include/Graph/Graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#include "Partitioning/Utility/Globals.hpp"
#include "Utility/ConstString.hpp"
#include "Utility/ConstValue.hpp"
#include "Utility/PointerHash.hpp"
#include "Utility/Reader.hpp"
#include "Utility/ThreadSafe.hpp"
#include "Utility/Typedef.hpp"
Expand Down Expand Up @@ -209,7 +210,8 @@ class Graph {
* @returns a list of Nodes of the graph
*
*/
virtual const std::unordered_set<shared<const Node<T>>, nodeHash<T>> getNodeSet() const;
virtual const std::unordered_set<shared<const Node<T>>, nodeHash<T>>
getNodeSet() const;
/**
* \brief
* Function that sets the data contained in a node
Expand Down Expand Up @@ -244,6 +246,47 @@ class Graph {
* corrispondent to the link Note: No Thread Safe
*/
virtual const std::shared_ptr<AdjacencyMatrix<T>> getAdjMatrix() const;
/**
* \brief This function generates a set of nodes linked to the provided node in a
* directed graph
* Note: No Thread Safe
*
* @param Pointer to the node
*
*/
virtual const std::unordered_set<shared<const Node<T>>, nodeHash<T>> outNeighbors(
const Node<T> *node) const;
/**
* \brief This function generates a set of nodes linked to the provided node in a
* directed graph
* Note: No Thread Safe
*
* @param Pointer to the node
*
*/
virtual const std::unordered_set<shared<const Node<T>>, nodeHash<T>> outNeighbors(
shared<const Node<T>> node) const;
/**
* \brief This function generates a set of nodes linked to the provided node in
* any graph
* Note: No Thread Safe
*
* @param Pointer to the node
*
*/
virtual const std::unordered_set<shared<const Node<T>>, nodeHash<T>>
inOutNeighbors(const Node<T> *node) const;
/**
* \brief
* \brief This function generates a set of nodes linked to the provided node in
* any graph
* Note: No Thread Safe
*
* @param Pointer to the node
*
*/
virtual const std::unordered_set<shared<const Node<T>>, nodeHash<T>>
inOutNeighbors(shared<const Node<T>> node) const;
/**
* @brief This function finds the subset of given a nodeId
* Subset is stored in a map where keys are the hash-id of the node & values
Expand All @@ -261,7 +304,8 @@ class Graph {
* @brief This function finds the subset of given a nodeId
* Subset is stored in a map where keys are the hash-id of the node & values
* is the subset.
* @param shared pointer to subset query subset, we want to find target in this subset
* @param shared pointer to subset query subset, we want to find target in
* this subset
* @param elem elem that we wish to find in the subset
*
* @return parent node of elem
Expand Down Expand Up @@ -744,7 +788,8 @@ bool Graph<T>::findEdge(shared<const Node<T>> v1, shared<const Node<T>> v2,
}

template <typename T>
const std::unordered_set<shared<const Node<T>>, nodeHash<T>> Graph<T>::getNodeSet() const {
const std::unordered_set<shared<const Node<T>>, nodeHash<T>>
Graph<T>::getNodeSet() const {
std::unordered_set<shared<const Node<T>>, nodeHash<T>> nodeSet;
for (const auto &edgeSetIt : edgeSet) {
nodeSet.insert(edgeSetIt->getNodePair().first);
Expand Down Expand Up @@ -863,11 +908,11 @@ int Graph<T>::writeToDot(const std::string &workingDir,
}
if (edgePtr->isWeighted().has_value() && edgePtr->isWeighted().value()) {
// Weights in dot files must be integers
edgeLine +=
" [weight=" +
std::to_string(static_cast<int>(
std::dynamic_pointer_cast<const Weighted>(edgePtr)->getWeight())) +
']';
edgeLine += " [weight=" +
std::to_string(static_cast<int>(
std::dynamic_pointer_cast<const Weighted>(edgePtr)
->getWeight())) +
']';
}
edgeLine += ";\n";
ofileGraph << edgeLine;
Expand Down Expand Up @@ -917,7 +962,8 @@ void Graph<T>::writeGraphToStream(std::ostream &oGraph, std::ostream &oNodeFeat,
oEdgeWeight
<< edge->getId() << sep
<< (edge->isWeighted().has_value() && edge->isWeighted().value()
? (std::dynamic_pointer_cast<const Weighted>(edge))->getWeight()
? (std::dynamic_pointer_cast<const Weighted>(edge))
->getWeight()
: 0.0)
<< sep
<< (edge->isWeighted().has_value() && edge->isWeighted().value() ? 1
Expand Down Expand Up @@ -1179,7 +1225,8 @@ template <typename T>
unsigned long long Graph<T>::setFind(
std::unordered_map<unsigned long long, Subset> *subsets,
const unsigned long long nodeId) const {
auto subsets_ptr = make_shared<std::unordered_map<unsigned long long, Subset>>(*subsets);
auto subsets_ptr =
make_shared<std::unordered_map<unsigned long long, Subset>>(*subsets);
// find root and make root as parent of i
// (path compression)
if ((*subsets)[nodeId].parent != nodeId) {
Expand All @@ -1205,18 +1252,21 @@ unsigned long long Graph<T>::setFind(
}

template <typename T>
void Graph<T>::setUnion(
std::unordered_map<unsigned long long, Subset> *subsets,
const unsigned long long elem1, const unsigned long long elem2) const {
/* auto subsets_ptr = make_shared<std::unordered_map<unsigned long long, Subset>>(*subsets); */
void Graph<T>::setUnion(std::unordered_map<unsigned long long, Subset> *subsets,
const unsigned long long elem1,
const unsigned long long elem2) const {
/* auto subsets_ptr = make_shared<std::unordered_map<unsigned long long,
* Subset>>(*subsets); */
// if both sets have same parent
// then there's nothing to be done
/* if ((*subsets_ptr)[elem1].parent == (*subsets_ptr)[elem2].parent) return; */
/* if ((*subsets_ptr)[elem1].parent == (*subsets_ptr)[elem2].parent) return;
*/
/* auto elem1Parent = Graph<T>::setFind(subsets_ptr, elem1); */
/* auto elem2Parent = Graph<T>::setFind(subsets_ptr, elem2); */
/* if ((*subsets_ptr)[elem1Parent].rank < (*subsets_ptr)[elem2Parent].rank) */
/* (*subsets_ptr)[elem1].parent = elem2Parent; */
/* else if ((*subsets_ptr)[elem1Parent].rank > (*subsets_ptr)[elem2Parent].rank) */
/* else if ((*subsets_ptr)[elem1Parent].rank >
* (*subsets_ptr)[elem2Parent].rank) */
/* (*subsets_ptr)[elem2].parent = elem1Parent; */
/* else { */
/* (*subsets_ptr)[elem2].parent = elem1Parent; */
Expand Down Expand Up @@ -1329,6 +1379,59 @@ const std::shared_ptr<AdjacencyMatrix<T>> Graph<T>::getAdjMatrix() const {
return adj;
}

template <typename T>
const std::unordered_set<shared<const Node<T>>, nodeHash<T>> Graph<T>::outNeighbors(
const Node<T> *node) const {
auto node_shared = make_shared<const Node<T>>(*node);

return outNeighbors(node_shared);
}

template <typename T>
const std::unordered_set<shared<const Node<T>>, nodeHash<T>> Graph<T>::outNeighbors(
shared<const Node<T>> node) const {
auto adj = getAdjMatrix();
if (adj->find(node) == adj->end()) {
return std::unordered_set<shared<const Node<T>>, nodeHash<T>>();
}
auto nodeEdgePairs = adj->at(node);

std::unordered_set<shared<const Node<T>>, nodeHash<T>> outNeighbors;
for (auto pair : nodeEdgePairs) {
if (pair.second->isDirected().has_value() &&
pair.second->isDirected().value()) {
outNeighbors.insert(pair.first);
}
}

return outNeighbors;
}

template <typename T>
const std::unordered_set<shared<const Node<T>>, nodeHash<T>>
Graph<T>::inOutNeighbors(const Node<T> *node) const {
auto node_shared = make_shared<const Node<T>>(*node);

return inOutNeighbors(node_shared);
}

template <typename T>
const std::unordered_set<shared<const Node<T>>, nodeHash<T>>
Graph<T>::inOutNeighbors(shared<const Node<T>> node) const {
auto adj = Graph<T>::getAdjMatrix();
if (adj->find(node) == adj->end()) {
return std::unordered_set<shared<const Node<T>>, nodeHash<T>>();
}
auto nodeEdgePairs = adj->at(node);

std::unordered_set<shared<const Node<T>>, nodeHash<T>> inOutNeighbors;
for (auto pair : nodeEdgePairs) {
inOutNeighbors.insert(pair.first);
}

return inOutNeighbors;
}

template <typename T>
const DijkstraResult Graph<T>::dijkstra(const Node<T> &source,
const Node<T> &target) const {
Expand Down Expand Up @@ -1466,7 +1569,8 @@ const BellmanFordResult Graph<T>::bellmanford(const Node<T> &source,
return result;
}
// setting all the distances initially to INF_DOUBLE
std::unordered_map<shared<const Node<T>>, double, nodeHash<T>> dist, currentDist;
std::unordered_map<shared<const Node<T>>, double, nodeHash<T>> dist,
currentDist;
// n denotes the number of vertices in graph
auto n = nodeSet.size();
for (const auto &elem : nodeSet) {
Expand Down Expand Up @@ -1555,7 +1659,8 @@ const Graph<T> Graph<T>::transitiveReduction() const {
Graph<T> result(this->edgeSet);

unsigned long long edgeId = 0;
std::unordered_set<shared<const Node<T>>, nodeHash<T>> nodes = this->getNodeSet();
std::unordered_set<shared<const Node<T>>, nodeHash<T>> nodes =
this->getNodeSet();
for (auto x : nodes) {
for (auto y : nodes) {
if (this->findEdge(x, y, edgeId)) {
Expand Down Expand Up @@ -1849,7 +1954,8 @@ const MstResult Graph<T>::kruskal() const {
sortedEdges;
for (const auto &edge : edgeSet) {
if (edge->isWeighted().has_value() && edge->isWeighted().value()) {
auto weight = (std::dynamic_pointer_cast<const Weighted>(edge))->getWeight();
auto weight =
(std::dynamic_pointer_cast<const Weighted>(edge))->getWeight();
sortedEdges.push(std::make_pair(weight, edge));
} else {
// No Weighted Edge
Expand Down Expand Up @@ -2838,8 +2944,12 @@ double Graph<T>::fordFulkersonMaxFlow(const Node<T> &source,
return -1;
}
double maxFlow = 0;
std::unordered_map<shared<const Node<T>>, shared<const Node<T>>, nodeHash<T>> parent;
std::unordered_map<shared<const Node<T>>, std::unordered_map<shared<const Node<T>>, double, nodeHash<T>>, nodeHash<T>>
std::unordered_map<shared<const Node<T>>, shared<const Node<T>>, nodeHash<T>>
parent;
std::unordered_map<
shared<const Node<T>>,
std::unordered_map<shared<const Node<T>>, double, nodeHash<T>>,
nodeHash<T>>
weightMap;
// build weight map
auto edgeSet = this->getEdgeSet();
Expand Down
Loading