28 #include <agrum/tools/graphs/undiGraph.h> 31 # include <agrum/tools/graphs/undiGraph_inl.h> 39 UndiGraph::UndiGraph(Size nodes_size,
40 bool nodes_resize_policy,
42 bool edges_resize_policy) :
43 NodeGraphPart(nodes_size, nodes_resize_policy),
44 EdgeGraphPart(edges_size, edges_resize_policy) {
45 GUM_CONSTRUCTOR(UndiGraph);
48 UndiGraph::UndiGraph(
const UndiGraph& g) : NodeGraphPart(g), EdgeGraphPart(g) {
49 GUM_CONS_CPY(UndiGraph);
52 UndiGraph::~UndiGraph() {
53 GUM_DESTRUCTOR(UndiGraph);
57 bool UndiGraph::hasUndirectedCycle()
const {
58 List< std::pair< NodeId, NodeId > > open_nodes;
59 NodeProperty<
bool > examined_nodes = nodesProperty(
false);
60 std::pair< NodeId, NodeId > thePair;
61 NodeId current, from_current;
63 for (
const auto node: nodes()) {
66 if (!examined_nodes[node]) {
68 examined_nodes[node] =
true;
72 thePair.second = node;
73 open_nodes.insert(thePair);
75 while (!open_nodes.empty()) {
77 thePair = open_nodes.front();
78 open_nodes.popFront();
80 current = thePair.first;
81 from_current = thePair.second;
84 for (
const auto new_node: neighbours(current))
87 if (new_node != from_current) {
88 if (examined_nodes[new_node])
91 examined_nodes[new_node] =
true;
92 thePair.first = new_node;
93 thePair.second = current;
94 open_nodes.insert(thePair);
104 std::string UndiGraph::toString()
const {
105 std::string s = NodeGraphPart::toString();
107 s += EdgeGraphPart::toString();
111 std::string UndiGraph::toDot()
const {
112 std::stringstream output;
113 std::stringstream nodeStream;
114 std::stringstream edgeStream;
115 List< NodeId > treatedNodes;
116 output <<
"digraph \"" 117 <<
"no_name\" {" << std::endl
118 <<
"edge [dir=none]" << std::endl;
119 nodeStream <<
"node [shape = ellipse];" << std::endl;
120 std::string tab =
" ";
122 for (
const auto node: nodes()) {
123 nodeStream << tab << node <<
";";
125 if (neighbours(node).size() > 0)
126 for (
const auto nei: neighbours(node))
127 if (!treatedNodes.exists(nei))
128 edgeStream << tab << node <<
" -> " << nei <<
";" << std::endl;
130 treatedNodes.insert(node);
133 output << nodeStream.str() << std::endl << edgeStream.str() << std::endl <<
"}" << std::endl;
139 UndiGraph UndiGraph::partialUndiGraph(NodeSet nodes) {
140 UndiGraph partialGraph;
142 for (
const auto node: nodes) {
143 partialGraph.addNodeWithId(node);
145 for (
const auto nei: neighbours(node))
146 if (nodes.contains(nei) && partialGraph.existsNode(nei)) partialGraph.addEdge(node, nei);
152 NodeProperty< NodeId > UndiGraph::nodes2ConnectedComponent()
const {
153 NodeProperty< NodeId > res;
156 for (
const auto node: nodes()) {
157 if (res.exists(node))
continue;
159 while (!nodes.empty()) {
160 auto actual = *(nodes.begin());
162 res.insert(actual, numCC);
163 for (
const auto nei: neighbours(actual)) {
164 if (!res.exists(nei))
165 if (!nodes.exists(nei)) nodes.insert(nei);
175 std::ostream& operator<<(std::ostream& stream,
const UndiGraph& g) {
176 stream << g.toString();