46 #include "../Exceptions.h"
47 #include "../Text/TextTools.h"
55 directed_(directed_p),
66 directed_(gg.directed_),
67 observers_(gg.observers_),
68 highestNodeID_(gg.highestNodeID_),
69 highestEdgeID_(gg.highestEdgeID_),
70 nodeStructure_(gg.nodeStructure_),
71 edgeStructure_(gg.edgeStructure_),
134 vector<GlobalGraph::Edge> deletedEdges;
137 for (
auto& currEdgeToDelete : deletedEdges)
152 nodeStructureType::iterator nodeARow =
nodeStructure_.find(nodeA);
153 nodeStructureType::iterator nodeBRow =
nodeStructure_.find(nodeB);
154 nodeStructureType::iterator nodeSonRow, nodeFatherRow;
157 map<GlobalGraph::Node, GlobalGraph::Edge>::iterator foundForwardRelation = nodeARow->second.first.find(nodeB);
158 if (foundForwardRelation == nodeARow->second.first.end())
160 foundForwardRelation = nodeBRow->second.first.find(nodeA);
161 if (foundForwardRelation == nodeBRow->second.first.end())
165 nodeFatherRow = nodeBRow;
166 nodeSonRow = nodeARow;
172 nodeFatherRow = nodeARow;
173 nodeSonRow = nodeBRow;
180 map<GlobalGraph::Node, GlobalGraph::Edge>::iterator foundBackwardsRelation = nodeSonRow->second.second.find(father);
184 nodeFatherRow->second.first.erase(foundForwardRelation);
185 nodeSonRow->second.second.erase(foundBackwardsRelation);
188 nodeSonRow->second.first[father] = foundEdge;
190 nodeFatherRow->second.second[son] = foundEdge;
203 edgeStructureType::iterator foundEdge =
edgeStructure_.find(edge);
221 nodeStructureType::iterator nodeARow =
nodeStructure_.find(nodeA);
222 map<GlobalGraph::Node, GlobalGraph::Edge>::iterator foundForwardRelation = nodeARow->second.first.find(nodeB);
223 if (foundForwardRelation == nodeARow->second.first.end())
227 nodeARow->second.first.erase(foundForwardRelation);
230 nodeStructureType::iterator nodeBRow =
nodeStructure_.find(nodeB);
231 map<GlobalGraph::Node, GlobalGraph::Edge>::iterator foundBackwardsRelation = nodeBRow->second.second.find(nodeA);
232 if (foundBackwardsRelation == nodeBRow->second.first.end())
235 nodeBRow->second.second.erase(foundBackwardsRelation);
245 ita->second.first.insert( pair<GlobalGraph::Node, GlobalGraph::Edge>(nodeB, edge));
249 nodeStructure_.find(nodeB)->second.second.insert( pair<GlobalGraph::Node, GlobalGraph::Edge>(nodeA, edge));
257 nodeStructure_[newNode] = std::pair<std::map<GlobalGraph::Node, GlobalGraph::Edge>, std::map<GlobalGraph::Node, GlobalGraph::Edge> >();
266 link(origin, newNode);
279 pair<GlobalGraph::Node, GlobalGraph::Node> nodes =
edgeStructure_[edge];
284 link(nodeA, newNode);
285 link(newNode, nodeB);
309 throw (
Exception(
"This GraphObserver was already an observer of this Graph"));
316 throw (
Exception(
"This GraphObserver was not an observer of this Graph"));
324 nodeStructureType::const_iterator foundNode =
nodeStructure_.find(node);
326 throw (
Exception(
"The requested node is not in the structure."));
327 const std::map<GlobalGraph::Node, GlobalGraph::Edge>& forOrBack = (outgoing ? foundNode->second.first : foundNode->second.second);
328 vector<GlobalGraph::Node> result;
329 for (
auto& currNeighbor : forOrBack)
331 result.push_back(currNeighbor.first);
339 nodeStructureType::const_iterator foundNode =
nodeStructure_.find(node);
341 throw (
Exception(
"The requested node is not in the structure."));
342 const std::map<GlobalGraph::Node, GlobalGraph::Edge>& forOrBack = (outgoing ? foundNode->second.first : foundNode->second.second);
344 vector<GlobalGraph::Edge> result;
345 for (
const auto& currNeighbor : forOrBack)
347 result.push_back(currNeighbor.second);
421 nodeStructureType::const_iterator foundNode =
nodeStructure_.find(node);
425 return isDirected() ? foundNode->second.first.size() + foundNode->second.second.size() : foundNode->second.first.size();
431 nodeStructureType::const_iterator foundNode =
nodeStructure_.find(node);
435 const auto& assoc = foundNode->second;
436 return (!
isDirected() && (assoc.first.size() <= 1))
438 (assoc.first.size() + assoc.second.size() <= 1)
439 || (assoc.first.size() == 1 && assoc.second.size() == 1 &&
440 assoc.first.begin()->first == assoc.second.begin()->first)));
446 nodeStructureType::const_iterator foundNode =
nodeStructure_.find(node);
448 throw (
Exception(
"The requested node is not in the structure."));
451 return foundNode->second.first.size() + foundNode->second.second.size();
453 return foundNode->second.first.size();
458 nodeStructureType::const_iterator foundNode =
nodeStructure_.find(node);
460 throw (
Exception(
"The requested node is not in the structure."));
461 return foundNode->second.first.size();
466 nodeStructureType::const_iterator foundNode =
nodeStructure_.find(node);
468 throw (
Exception(
"The requested node is not in the structure."));
469 return foundNode->second.second.size();
474 vector<Graph::NodeId> result;
475 vector<Graph::NodeId> neighborsToInsert;
477 result.insert(result.end(), neighborsToInsert.begin(), neighborsToInsert.end());
479 result.insert(result.end(), neighborsToInsert.begin(), neighborsToInsert.end());
486 edgeStructureType::const_iterator found =
edgeStructure_.find(edge);
487 return found->second;
518 for (
auto& currNeighbor : oneighbors)
520 unlink(node, currNeighbor);
524 for (
auto& currNeighbor : ineighbors)
526 unlink(currNeighbor, node);
532 vector<Graph::EdgeId> listOfEdges;
535 listOfEdges.push_back(it.first);
557 vector<Graph::NodeId> listOfLeaves;
560 if (this->
isLeaf(it.first))
561 listOfLeaves.push_back(it.first);
569 set<Graph::NodeId> listOfLeaves;
572 if (this->
isLeaf(it.first))
573 listOfLeaves.insert(it.first);
581 vector<Graph::NodeId> listOfNodes;
584 listOfNodes.push_back(it.first);
592 vector<Graph::NodeId> listOfInNodes;
596 listOfInNodes.push_back(it.first);
598 return listOfInNodes;
604 const vector<Graph::NodeId> neighbors =
getNeighbors(startingNode);
605 if (neighbors.size() > 1)
607 if (maxRecursions > 0)
608 for (
const auto& currNeighbor : neighbors)
610 if (currNeighbor != originNode)
615 foundLeaves.push_back(startingNode);
621 vector<Graph::NodeId> listOfLeaves;
629 const std::map<Node, Edge>& children =
nodeStructure_.at(node).first;
631 for (
const auto& currChild : children)
633 if (alreadyFigured.find(pair<Node, Node>(node, currChild.first)) != alreadyFigured.end() || (!
directed_ && alreadyFigured.find(pair<Node, Node>(currChild.first, node)) != alreadyFigured.end()))
635 alreadyFigured.insert(pair<Node, Node>(node, currChild.first));
639 nodeToDot_(currChild.first, out, alreadyFigured);
648 set<GlobalGraph::Node> metNodes;
651 if (!nodesAreMetOnlyOnce)
657 if (metNodes.find(currNode.first) == metNodes.end())
668 if (!metNodes.insert(node).second)
672 for (
auto currNeighbor:neighbors)
674 if (currNeighbor == originNode)
689 std::vector<Graph::NodeId> vL;
692 for ( ; !it->end(); it->next())
698 while (vL.size() != 0)
711 for ( ; !it->end(); it->next())
734 std::set<Graph::NodeId> nextNodes;
735 nextNodes.insert(node);
744 std::set<Graph::NodeId>::iterator it = nextNodes.begin();
745 for ( ; it != nextNodes.end(); it++)
752 if (it == nextNodes.end())
754 size_t nbF = numeric_limits<size_t>::infinity();
755 it = nextNodes.begin();
757 for ( ; it != nextNodes.end(); it++)
783 nextNodes.insert(it2);
789 nextNodes.insert(it2);
793 nextNodes.erase(nbgg);
821 it.second = std::pair<std::map<Node, Edge>, std::map<Node, Edge> >();
828 std::set<pair<Node, Node> > alreadyConvertedRelations;
829 for (
auto& currNodeRow : undirectedStructure)
831 Node nodeA = currNodeRow.first;
833 for (
auto& currRelation : currNodeRow.second.first)
835 Node nodeB = currRelation.first;
836 Edge edge = currRelation.second;
837 if (alreadyConvertedRelations.insert(pair<Node, Node>(min(nodeA, nodeB), max(nodeA, nodeB))).second)
850 throw Exception(
"Cannot make an undirected graph from a directed one containing reciprocal relations.");
855 it.second = std::pair<std::map<Node, Edge>, std::map<Node, Edge> >();
861 for (
auto& currNodeRow : directedStructure)
863 Node nodeA = currNodeRow.first;
864 for (
auto currRelation : currNodeRow.second.first)
866 Node nodeB = currRelation.first;
867 Edge edge = currRelation.second;
879 throw Exception(
"Cannot state reciprocal link in an undirected graph.");
880 std::set<pair<Node, Node> > alreadyMetRelations;
883 Node nodeA = currNodeRow.first;
884 for (
const auto& currRelation : currNodeRow.second.first)
886 Node nodeB = currRelation.first;
887 if (!alreadyMetRelations.insert(pair<Node, Node>(min(nodeA, nodeB), max(nodeA, nodeB))).second)
926 nodeStructureType::const_iterator firstNodeFound =
nodeStructure_.find(nodeA);
928 throw (
Exception(
"The fist node was not the origin of an edge."));
929 map<Node, Edge>::const_iterator secondNodeFound = firstNodeFound->second.first.find(nodeB);
930 if (secondNodeFound == firstNodeFound->second.first.end())
931 throw (
Exception(
"The second node was not in a relation with the first one."));
932 return secondNodeFound->second;
937 vector<Graph::EdgeId> result;
938 vector<Graph::EdgeId> edgesToInsert;
940 result.insert(result.end(), edgesToInsert.begin(), edgesToInsert.end());
942 result.insert(result.end(), edgesToInsert.begin(), edgesToInsert.end());
948 out << (
directed_ ?
"digraph" :
"graph") <<
" " << name <<
" {\n ";
949 set<pair<Node, Node> > alreadyFigured;
953 if (node.first !=
root_)
956 out <<
"\r}" << endl;
963 currObserver->deletedEdgesUpdate(edgesToDelete);
971 currObserver->deletedNodesUpdate(nodesToDelete);
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
Graph::EdgeId getAnyEdge(Graph::NodeId nodeA, Graph::NodeId nodeB) const
size_t getNumberOfIncomingNeighbors(Graph::NodeId node) const
std::unique_ptr< Graph::NodeIterator > outgoingNeighborNodesIterator(NodeId node)
std::vector< Graph::NodeId > getNeighbors(Graph::NodeId node) const
GlobalGraph & operator=(const GlobalGraph &gg)
std::vector< Graph::EdgeId > getEdges(Graph::NodeId node) const
void fillListOfLeaves_(const Node &startingNode, std::vector< Node > &foundLeaves, const Node &originNode, unsigned int maxRecursions) const
void deleteNode(Graph::NodeId node)
void edgeMustExist_(const Edge &edge, std::string name="") const
bool nodesAreMetOnlyOnce_(const Node &node, std::set< Node > &metNodes, const Node &originNode) const
Graph::EdgeId link(Graph::NodeId nodeA, Graph::NodeId nodeB)
std::unique_ptr< EdgeIterator > allEdgesIterator()
size_t getNumberOfOutgoingNeighbors(Graph::NodeId node) const
std::set< NodeId > getSetOfAllLeaves() const
std::vector< Graph::NodeId > getLeavesFromNode(Graph::NodeId node, unsigned int maxDepth) const
bool containsReciprocalRelations() const
void nodeToDot_(const Node &node, std::ostream &out, std::set< std::pair< Node, Node > > &alreadyFigured) const
edgeStructureType edgeStructure_
Graph::EdgeId getEdge(Graph::NodeId nodeA, Graph::NodeId nodeB) const
Graph::NodeId createNodeFromNode(Graph::NodeId origin)
void switchNodes(Graph::NodeId nodeA, Graph::NodeId nodeB)
void unregisterObserver(GraphObserver *observer)
std::unique_ptr< Graph::NodeIterator > incomingNeighborNodesIterator(NodeId node)
Graph::NodeId createNodeOnEdge(Graph::EdgeId edge)
std::pair< Graph::NodeId, Graph::NodeId > getNodes(Graph::EdgeId edge) const
size_t getNumberOfNodes() const
size_t getNumberOfNeighbors(Graph::NodeId node) const
Graph::NodeId getRoot() const
std::unique_ptr< EdgeIterator > incomingEdgesIterator(NodeId node)
std::vector< Edge > getEdges_(const Node &node, bool outgoing=true) const
std::vector< Graph::NodeId > getAllNodes() const
std::vector< Node > getNeighbors_(const Node &node, bool outgoing=true) const
std::map< Node, std::pair< std::map< Node, Edge >, std::map< Node, Edge > > > nodeStructureType
Graph::NodeId createNodeFromEdge(Graph::NodeId origin)
nodeStructureType nodeStructure_
std::vector< Graph::EdgeId > unlink(Graph::NodeId nodeA, Graph::NodeId nodeB)
void linkInEdgeStructure_(const Node &nodeA, const Node &nodeB, const Edge &edge)
std::set< GraphObserver * > observers_
bool isLeaf(Graph::NodeId node) const
size_t getNumberOfEdges() const
void linkInNodeStructure_(const Node &nodeA, const Node &nodeB, const Edge &edge)
std::vector< Graph::NodeId > getAllInnerNodes() const
std::unique_ptr< Graph::NodeIterator > allNodesIterator()
std::vector< Graph::NodeId > getIncomingNeighbors(Graph::NodeId node) const
Graph::NodeId getTop(Graph::EdgeId edge) const
Edge unlinkInNodeStructure_(const Node &nodeA, const Node &nodeB)
void unlinkInEdgeStructure_(const Edge &edge)
std::vector< Graph::EdgeId > getIncomingEdges(Graph::NodeId node) const
void isolate_(Node &node)
std::unique_ptr< EdgeIterator > outgoingEdgesIterator(NodeId node)
void registerObserver(GraphObserver *observer)
virtual void topologyHasChanged_() const
std::vector< Graph::EdgeId > getOutgoingEdges(Graph::NodeId node) const
void outputToDot(std::ostream &out, const std::string &name) const
std::map< Edge, std::pair< Node, Node > > edgeStructureType
size_t getDegree(Graph::NodeId node) const
std::vector< Graph::EdgeId > getAllEdges() const
std::vector< Graph::NodeId > getAllLeaves() const
void notifyDeletedNodes(const std::vector< Graph::NodeId > &nodesToDelete) const
Graph::NodeId createNode()
GlobalGraph(bool directed=false)
void nodeMustExist_(const Node &node, std::string name="") const
Graph::NodeId getBottom(Graph::EdgeId edge) const
std::vector< Graph::NodeId > getOutgoingNeighbors(Graph::NodeId node) const
void setRoot(Graph::NodeId newRoot)
void notifyDeletedEdges(const std::vector< Graph::EdgeId > &edgesToDelete) const
Defines a Graph Observer. It is a template which follows (subscribed to) a Graph. The graph and the g...
std::string toString(T t)
General template method to convert to a string.