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() { GUM_DESTRUCTOR(UndiGraph); }
54 bool UndiGraph::hasUndirectedCycle()
const {
55 List< std::pair< NodeId, NodeId > > open_nodes;
56 NodeProperty<
bool > examined_nodes = nodesProperty(
false);
57 std::pair< NodeId, NodeId > thePair;
58 NodeId current, from_current;
60 for (
const auto node: nodes()) {
63 if (!examined_nodes[node]) {
65 examined_nodes[node] =
true;
69 thePair.second = node;
70 open_nodes.insert(thePair);
72 while (!open_nodes.empty()) {
74 thePair = open_nodes.front();
75 open_nodes.popFront();
77 current = thePair.first;
78 from_current = thePair.second;
81 for (
const auto new_node: neighbours(current))
84 if (new_node != from_current) {
85 if (examined_nodes[new_node])
88 examined_nodes[new_node] =
true;
89 thePair.first = new_node;
90 thePair.second = current;
91 open_nodes.insert(thePair);
101 std::string UndiGraph::toString()
const {
102 std::string s = NodeGraphPart::toString();
104 s += EdgeGraphPart::toString();
108 std::string UndiGraph::toDot()
const {
109 std::stringstream output;
110 std::stringstream nodeStream;
111 std::stringstream edgeStream;
112 List< NodeId > treatedNodes;
113 output <<
"digraph \"" 114 <<
"no_name\" {" << std::endl
115 <<
"edge [dir=none]" << std::endl;
116 nodeStream <<
"node [shape = ellipse];" << std::endl;
117 std::string tab =
" ";
119 for (
const auto node: nodes()) {
120 nodeStream << tab << node <<
";";
122 if (neighbours(node).size() > 0)
123 for (
const auto nei: neighbours(node))
124 if (!treatedNodes.exists(nei))
125 edgeStream << tab << node <<
" -> " << nei <<
";" << std::endl;
127 treatedNodes.insert(node);
130 output << nodeStream.str() << std::endl
131 << edgeStream.str() << std::endl
138 UndiGraph UndiGraph::partialUndiGraph(NodeSet nodes) {
139 UndiGraph partialGraph;
141 for (
const auto node: nodes) {
142 partialGraph.addNodeWithId(node);
144 for (
const auto nei: neighbours(node))
145 if (nodes.contains(nei) && partialGraph.existsNode(nei))
146 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();