33 #include "Core/include/DBaseObject.h"
63 template <
class NodeT =
size_t,
class WeightT =
size_t>
67 typedef WeightT WeightType;
133 inline bool operator==(
const Edge &rhs)
const
142 inline bool operator!=(
const Edge &rhs)
const
144 return !this->operator==(rhs);
147 inline bool operator<(
const Edge &rhs)
const
149 return weight > rhs.weight;
152 virtual void printSelf(ostream &os = std::cout,
string s =
"")
const
163 template <
class NodeT,
class WeightT>
167 if (e1.size() != e2.size())
171 typename vector<EdgeT>::const_iterator it1 = e1.begin(), it2 = e2.begin();
173 for (; it1 != e1.end() && it2 != e2.end(); it1++, it2++) {
174 if ((*it1) != (*it2))
176 if (it1->weight != it2->weight)
195 template <
class NodeT =
size_t,
class WeightT =
size_t>
201 typedef NodeT NodeType;
202 typedef WeightT NodeWeightType;
203 typedef std::map<NodeT, WeightT> NodeValuesType;
204 typedef set<NodeT> NodeListType;
207 typedef WeightT EdgeWeightType;
209 typedef std::vector<Edge<NodeT, WeightT>> EdgeListType;
210 typedef std::vector<size_t> NodeEdgesType;
211 typedef std::map<NodeT, NodeEdgesType> NodeEdgeListType;
216 NodeValuesType nodeValues;
218 NodeEdgeListType nodeEdgeList;
228 :
BaseObject(
"Graph"), edgeNbr(rhs.edgeNbr), nodes(rhs.nodes),
229 nodeValues(rhs.nodeValues), edges(rhs.edges),
230 nodeEdgeList(rhs.nodeEdgeList)
243 nodeValues = rhs.nodeValues;
245 nodeEdgeList = rhs.nodeEdgeList;
246 edgeNbr = rhs.edgeNbr;
256 nodeEdgeList.clear();
267 void addNode(
const NodeT &ind,
const WeightT &val)
270 nodeValues[ind] = val;
278 typename EdgeListType::iterator foundEdge =
279 find(edges.begin(), edges.end(), e);
280 if (foundEdge != edges.end())
281 return foundEdge - edges.begin();
310 nodeEdgeList[e.
source].push_back(edgeNbr);
311 nodeEdgeList[e.
target].push_back(edgeNbr);
325 void addEdge(
const NodeT src,
const NodeT targ, WeightT weight = 0,
326 bool checkIfExists =
true)
336 EdgeListType sEdges = edges;
338 sort(sEdges.begin(), sEdges.end());
340 sort(sEdges.rbegin(), sEdges.rend());
344 nodeEdgeList.clear();
347 for (
typename EdgeListType::const_iterator it = sEdges.begin();
348 it != sEdges.end(); it++)
377 void removeNode(
const NodeT ind)
379 typename NodeListType::iterator fNode = nodes.find(ind);
380 if (fNode != nodes.end())
385 void removeNodeEdge(
const NodeT node,
const size_t edgeIndex)
387 typename NodeEdgeListType::iterator nEdges = nodeEdgeList.find(node);
388 if (nEdges == nodeEdgeList.end())
391 NodeEdgesType &eList = nEdges->second;
393 typename NodeEdgesType::iterator ei =
394 find(eList.begin(), eList.end(), edgeIndex);
395 if (ei != eList.end())
405 typename NodeEdgeListType::iterator fNodeEdges = nodeEdgeList.find(node);
406 if (fNodeEdges == nodeEdgeList.end())
409 NodeEdgesType &nedges = fNodeEdges->second;
410 for (NodeEdgesType::iterator it = nedges.begin(); it != nedges.end();
414 removeNodeEdge(e.
target, *it);
416 removeNodeEdge(e.
source, *it);
427 if (
index >= edges.size())
442 typename EdgeListType::iterator foundEdge =
443 find(edges.begin(), edges.end(),
EdgeType(src, targ));
444 if (foundEdge == edges.end())
455 typename EdgeListType::iterator foundEdge =
456 find(edges.begin(), edges.end(), edge);
457 if (foundEdge == edges.end())
471 size_t nb_edges = edges.size();
474 if (e.
weight > EdgeThreshold)
487 size_t nb_edges = edges.size();
490 if (e.
weight < EdgeThreshold)
497 const NodeListType &
getNodes()
const
502 const EdgeListType &
getEdges()
const
524 NodeListType &getNodes()
560 typename NodeEdgeListType::iterator it = nodeEdgeList.find(node);
561 if (it != nodeEdgeList.end())
564 return vector<size_t>();
578 virtual void printSelf(ostream &os = std::cout,
string s =
"")
const
580 os << s <<
"Number of nodes: " << nodes.size() << endl;
581 os << s <<
"Number of edges: " << edges.size() << endl;
582 os << s <<
"Edges: " << endl <<
"source-target (weight) " << endl;
584 string s2 = s +
"\t";
585 for (
typename EdgeListType::const_iterator it = edges.begin();
586 it != edges.end(); it++)
587 if ((*it).isActive())
588 (*it).printSelf(os, s2);
600 map<NodeT, NodeT> lookup;
601 set<NodeT> nodeList(nodes);
603 while (!nodeList.empty()) {
604 propagateLabel(*(nodeList.begin()), *(nodeList.begin()), lookup,
611 void propagateLabel(
const NodeT ind,
const NodeT lbl,
612 map<NodeT, NodeT> &lookup, set<NodeT> &nList)
const
614 typename NodeListType::iterator foundNode = nList.find(ind);
615 if (foundNode == nList.end())
619 nList.erase(foundNode);
621 const NodeEdgesType &nEdges = nodeEdgeList.at(ind);
623 for (
typename NodeEdgesType::const_iterator it = nEdges.begin();
624 it != nEdges.end(); it++) {
625 const EdgeType &e = edges[*it];
627 propagateLabel(e.source, lbl, lookup, nList);
628 else if (e.target != ind)
629 propagateLabel(e.target, lbl, lookup, nList);
640 template <
class graphT>
643 typedef typename graphT::NodeType NodeType;
644 typedef typename graphT::EdgeType EdgeType;
645 typedef typename graphT::EdgeListType EdgeListType;
646 typedef typename graphT::NodeEdgesType NodeEdgesType;
647 typedef typename graphT::NodeEdgeListType NodeEdgeListType;
649 std::set<NodeType> visitedNodes;
650 std::priority_queue<EdgeType> pq;
653 const EdgeListType & edges = graph.
getEdges();
654 const NodeEdgeListType &nodeEdgeList = graph.
getNodeEdges();
656 NodeType curNode = (*nodeEdgeList.begin()).first;
657 visitedNodes.insert(curNode);
659 const NodeEdgesType &nodeEdges = nodeEdgeList.at(curNode);
660 for (
typename NodeEdgesType::const_iterator it = nodeEdges.begin();
661 it != nodeEdges.end(); it++)
664 while (!pq.empty()) {
665 EdgeType edge = pq.top();
668 if (visitedNodes.find(edge.source) == visitedNodes.end())
669 curNode = edge.source;
670 else if (visitedNodes.find(edge.target) == visitedNodes.end())
671 curNode = edge.target;
675 mst.addEdge(edge,
false);
676 visitedNodes.insert(curNode);
677 NodeEdgesType
const &nodeEdges = nodeEdgeList.at(curNode);
678 for (
typename NodeEdgesType::const_iterator it = nodeEdges.begin();
679 it != nodeEdges.end(); it++)
Base Smil Object.
Definition: DBaseObject.h:52
Non-oriented edge.
Definition: DGraph.hpp:65
Edge(NodeT a, NodeT b, WeightT w=1)
Constructor using two nodes and an optional weight (default 1).
Definition: DGraph.hpp:77
WeightT weight
Edge weight/value.
Definition: DGraph.hpp:110
Edge(const Edge &rhs)
Copy constructor.
Definition: DGraph.hpp:83
bool isActive() const
Check if the edge is active.
Definition: DGraph.hpp:118
Edge & operator=(const Edge &rhs)
Copy an edge.
Definition: DGraph.hpp:97
void desactivate()
Deactivate the Edge.
Definition: DGraph.hpp:126
NodeT source
Source node.
Definition: DGraph.hpp:106
Edge()
Default constructor.
Definition: DGraph.hpp:71
NodeT target
Target node.
Definition: DGraph.hpp:108
Non-oriented graph.
Definition: DGraph.hpp:197
Graph(const Graph &rhs)
Copy constructor.
Definition: DGraph.hpp:227
void addNode(const NodeT &ind)
Add a node given its index.
Definition: DGraph.hpp:261
size_t getEdgeNbr()
getEdgeNbr() -
Definition: DGraph.hpp:371
map< NodeT, NodeT > labelizeNodes() const
labelizeNodes() - Labelize the nodes.
Definition: DGraph.hpp:598
void addEdge(const EdgeType &e, bool checkIfExists=true)
Add an edge to the graph.
Definition: DGraph.hpp:301
void addNode(const NodeT &ind, const WeightT &val)
Add a node given its index and its optional value.
Definition: DGraph.hpp:267
GraphType clone()
clone() -
Definition: DGraph.hpp:355
NodeListType & getNodes()
getNodes() - get the list of nodes
Definition: DGraph.hpp:524
void removeEdge(const NodeT src, const NodeT targ)
Find and remove an edge linking src to targ.
Definition: DGraph.hpp:440
GraphType computeMST()
computeMST() - Compute the Minimum Spanning Tree graph
Definition: DGraph.hpp:570
virtual void printSelf(ostream &os=std::cout, string s="") const
printSelf() -
Definition: DGraph.hpp:578
NodeEdgeListType & getNodeEdges()
getNodeEdges()-
Definition: DGraph.hpp:550
void removeLowEdges(EdgeWeightType EdgeThreshold)
removeHighEdges() - remove edges whose weight are lesser then some threshold
Definition: DGraph.hpp:485
NodeEdgesType getNodeEdges(const size_t &node)
getNodeEdges() - Get a map containing the edges linked to a given node
Definition: DGraph.hpp:558
size_t getNodeNbr()
getNodeNbr() -
Definition: DGraph.hpp:363
void removeEdge(const size_t index)
Remove an edge.
Definition: DGraph.hpp:425
void removeHighEdges(EdgeWeightType EdgeThreshold)
removeHighEdges() - remove edges whose weight are greater then some threshold
Definition: DGraph.hpp:469
int findEdge(const NodeT &src, const NodeT &targ)
findEdge() - Find an edge by its nodes - return its index
Definition: DGraph.hpp:289
void addEdge(const NodeT src, const NodeT targ, WeightT weight=0, bool checkIfExists=true)
Add an edge to the graph given two nodes src and targ and an optional weight.
Definition: DGraph.hpp:325
EdgeListType & getEdges()
getEdges() - Get a vector containing the graph edges
Definition: DGraph.hpp:534
void sortEdges(bool reverse=false)
Sort edges (by weight as defined by the operator < of class Edge)
Definition: DGraph.hpp:334
void removeEdge(const EdgeType &edge)
Remove a given edge.
Definition: DGraph.hpp:453
void clear()
Clear graph content.
Definition: DGraph.hpp:251
void removeNodeEdges(const NodeT node)
Remove all edges linked to the node nodeIndex.
Definition: DGraph.hpp:403
NodeValuesType & getNodeValues()
getNodeValues() -
Definition: DGraph.hpp:542
int findEdge(const EdgeType &e)
findEdge() - Find an edge by its content - return its index
Definition: DGraph.hpp:276
Graph()
Default constructor.
Definition: DGraph.hpp:222
graphT graphMST(const graphT &graph)
graphMST() - create a Mininum Spanning Tree
Definition: DGraph.hpp:641
bool operator==(const vector< Edge< NodeT, WeightT >> &e1, const vector< Edge< NodeT, WeightT >> &e2)
Check if two vectors of edges are equal.
Definition: DGraph.hpp:164