Skip to content

Commit ed09ca8

Browse files
committed
Implemented inNotOutEdges/Neighbors and associated tests for completeness with outNotInEdges/Neighbors
1 parent 2f73b59 commit ed09ca8

3 files changed

Lines changed: 117 additions & 17 deletions

File tree

include/CXXGraph/Graph/Graph_decl.h

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -375,14 +375,13 @@ class Graph {
375375
const std::string &nodeUserId) const;
376376

377377
/**
378-
* @brief This function generates a list of adjacency matrix with every element
379-
* of the matrix contain the node where is directed the link and the Edge
380-
* corrispondent to the link
381-
* Note: No Thread Safe
378+
* @brief This function generates a list of adjacency matrix with every
379+
* element of the matrix contain the node where is directed the link and the
380+
* Edge corrispondent to the link Note: No Thread Safe
382381
*/
383382
virtual shared<AdjacencyMatrix<T>> getAdjMatrixOut() const;
384383

385-
/**
384+
/**
386385
* @brief This function generate a list of adjacency matrix with every element
387386
* of the matrix contain the node where is the origin of the Edge and the Edge
388387
* corrispondent to the link
@@ -437,8 +436,8 @@ class Graph {
437436
virtual void cacheTransitionMatrix();
438437

439438
/**
440-
* \brief This function generates a set of nodes linked only out (not in) from the provided node
441-
* in a directed graph
439+
* \brief This function generates a set of nodes linked only out (not in) from
440+
* the provided node in a directed graph
442441
*
443442
* @param Pointer to the node
444443
*
@@ -447,15 +446,35 @@ class Graph {
447446
outNotInNeighbors(const Node<T> *node) const;
448447

449448
/**
450-
* \brief This function generates a set of nodes linked only out (not in) from the provided node
451-
* in a directed graph
449+
* \brief This function generates a set of nodes linked only out (not in) from
450+
* the provided node in a directed graph
452451
*
453452
* @param Pointer to the node
454453
*
455454
*/
456455
virtual const std::unordered_set<shared<const Node<T>>, nodeHash<T>>
457456
outNotInNeighbors(shared<const Node<T>> node) const;
458457

458+
/**
459+
* \brief This function generates a set of nodes linked only in (not out) of
460+
* the provided node in a directed graph
461+
*
462+
* @param Pointer to the node
463+
*
464+
*/
465+
virtual const std::unordered_set<shared<const Node<T>>, nodeHash<T>>
466+
inNotOutNeighbors(const Node<T> *node) const;
467+
468+
/**
469+
* \brief This function generates a set of nodes linked only in (not out) of
470+
* the provided node in a directed graph
471+
*
472+
* @param Pointer to the node
473+
*
474+
*/
475+
virtual const std::unordered_set<shared<const Node<T>>, nodeHash<T>>
476+
inNotOutNeighbors(shared<const Node<T>> node) const;
477+
459478
/**
460479
* \brief This function generates a set of nodes linked to the provided node
461480
* in any graph
@@ -479,24 +498,45 @@ class Graph {
479498

480499
/**
481500
* \brief
482-
* \brief This function generates a set of directed Edges going only out of (not in) to a node
483-
* in any graph
501+
* \brief This function generates a set of directed Edges going only out of
502+
* (not in) to a node in any graph
484503
*
485504
* @param Pointer to the node
486505
*
487506
*/
488-
virtual const std::unordered_set<shared<const Edge<T>>, edgeHash<T>> outNotInEdges(
489-
const Node<T> *node) const;
507+
virtual const std::unordered_set<shared<const Edge<T>>, edgeHash<T>>
508+
outNotInEdges(const Node<T> *node) const;
490509

491510
/**
492-
* \brief This function generates a set of directed Edges going only out of (not in) to a node
493-
* in any graph
511+
* \brief This function generates a set of directed Edges going only out of
512+
* (not in) to a node in any graph
513+
*
514+
* @param Shared pointer to the node
515+
*
516+
*/
517+
virtual const std::unordered_set<shared<const Edge<T>>, edgeHash<T>>
518+
outNotInEdges(shared<const Node<T>> node) const;
519+
520+
/**
521+
* \brief
522+
* \brief This function generates a set of directed Edges going only out of
523+
* (not in) to a node in any graph
524+
*
525+
* @param Pointer to the node
526+
*
527+
*/
528+
virtual const std::unordered_set<shared<const Edge<T>>, edgeHash<T>>
529+
inNotOutEdges(const Node<T> *node) const;
530+
531+
/**
532+
* \brief This function generates a set of directed Edges going only out of
533+
* (not in) to a node in any graph
494534
*
495535
* @param Shared pointer to the node
496536
*
497537
*/
498-
virtual const std::unordered_set<shared<const Edge<T>>, edgeHash<T>> outNotInEdges(
499-
shared<const Node<T>> node) const;
538+
virtual const std::unordered_set<shared<const Edge<T>>, edgeHash<T>>
539+
inNotOutEdges(shared<const Node<T>> node) const;
500540

501541
/**
502542
* \brief

include/CXXGraph/Graph/Graph_impl.hpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,32 @@ Graph<T>::outNotInNeighbors(shared<const Node<T>> node) const {
701701
return outNotInNeighbors;
702702
}
703703

704+
template <typename T>
705+
const std::unordered_set<shared<const Node<T>>, nodeHash<T>>
706+
Graph<T>::inNotOutNeighbors(const Node<T> *node) const {
707+
auto node_shared = make_shared<const Node<T>>(*node);
708+
709+
return inNotOutNeighbors(node_shared);
710+
}
711+
712+
template <typename T>
713+
const std::unordered_set<shared<const Node<T>>, nodeHash<T>>
714+
Graph<T>::inNotOutNeighbors(shared<const Node<T>> node) const {
715+
if (cachedAdjMatrixIn->find(node) == cachedAdjMatrixIn->end()) {
716+
return std::unordered_set<shared<const Node<T>>, nodeHash<T>>();
717+
}
718+
auto nodeEdgePairsIn = cachedAdjMatrixIn->at(node);
719+
720+
std::unordered_set<shared<const Node<T>>, nodeHash<T>> inNotOutNeighbors;
721+
for (auto pair : nodeEdgePairsIn) {
722+
if (pair.second->isDirected().value_or(false)) {
723+
inNotOutNeighbors.insert(pair.first);
724+
}
725+
}
726+
727+
return inNotOutNeighbors;
728+
}
729+
704730
template <typename T>
705731
const std::unordered_set<shared<const Node<T>>, nodeHash<T>>
706732
Graph<T>::inOrOutNeighbors(const Node<T> *node) const {
@@ -760,6 +786,32 @@ Graph<T>::outNotInEdges(shared<const Node<T>> node) const {
760786
return outNotInEdges;
761787
}
762788

789+
template <typename T>
790+
const std::unordered_set<shared<const Edge<T>>, edgeHash<T>>
791+
Graph<T>::inNotOutEdges(const Node<T> *node) const {
792+
auto node_shared = make_shared<const Node<T>>(*node);
793+
794+
return inNotOutEdges(node_shared);
795+
}
796+
797+
template <typename T>
798+
const std::unordered_set<shared<const Edge<T>>, edgeHash<T>>
799+
Graph<T>::inNotOutEdges(shared<const Node<T>> node) const {
800+
if (cachedAdjMatrixIn->find(node) == cachedAdjMatrixIn->end()) {
801+
return std::unordered_set<shared<const Edge<T>>, edgeHash<T>>();
802+
}
803+
auto nodeEdgePairsIn = cachedAdjMatrixIn->at(node);
804+
805+
std::unordered_set<shared<const Edge<T>>, edgeHash<T>> inNotOutEdges;
806+
for (auto pair : nodeEdgePairsIn) {
807+
if (pair.second->isDirected().value_or(false)) {
808+
inNotOutEdges.insert(pair.second);
809+
}
810+
}
811+
812+
return inNotOutEdges;
813+
}
814+
763815
template <typename T>
764816
const std::unordered_set<shared<const Edge<T>>, edgeHash<T>>
765817
Graph<T>::inOrOutEdges(const Node<T> *node) const {

test/DirectedEdgeTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ TEST(DirectedEdgeTest, inOutNeigbors_1) {
166166
ASSERT_EQ(g.outNotInNeighbors(&n2).size(), 1);
167167
ASSERT_EQ(g.outNotInNeighbors(&n3).size(), 0);
168168

169+
ASSERT_EQ(g.inNotOutNeighbors(&n1).size(), 0);
170+
ASSERT_EQ(g.inNotOutNeighbors(&n2).size(), 1);
171+
ASSERT_EQ(g.inNotOutNeighbors(&n3).size(), 1);
172+
169173
ASSERT_EQ(g.inOrOutNeighbors(&n1).size(), 1);
170174
ASSERT_EQ(g.inOrOutNeighbors(&n2).size(), 2);
171175
ASSERT_EQ(g.inOrOutNeighbors(&n3).size(), 1);
@@ -174,6 +178,10 @@ TEST(DirectedEdgeTest, inOutNeigbors_1) {
174178
ASSERT_EQ(g.outNotInEdges(&n2).size(), 1);
175179
ASSERT_EQ(g.outNotInEdges(&n3).size(), 0);
176180

181+
ASSERT_EQ(g.inNotOutEdges(&n1).size(), 0);
182+
ASSERT_EQ(g.inNotOutEdges(&n2).size(), 1);
183+
ASSERT_EQ(g.inNotOutEdges(&n3).size(), 1);
184+
177185
ASSERT_EQ(g.inOrOutEdges(&n1).size(), 1);
178186
ASSERT_EQ(g.inOrOutEdges(&n2).size(), 2);
179187
ASSERT_EQ(g.inOrOutEdges(&n3).size(), 1);

0 commit comments

Comments
 (0)