Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

simple_graph_alg.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/NodeArray.h>
35 #include <ogdf/basic/EdgeArray.h>
36 #include <ogdf/basic/SList.h>
37 
38 namespace ogdf {
39 
42 
48 OGDF_EXPORT void removeSelfLoops(Graph &graph, node v);
49 
51 
57 OGDF_EXPORT bool isLoopFree(const Graph &G);
58 
60 
67 template<class NODELIST>
68 void makeLoopFree(Graph &G, NODELIST &L)
69 {
70  L.clear();
71 
72  safeForEach(G.edges, [&](edge e) {
73  if (e->isSelfLoop()) {
74  L.pushBack(e->source());
75  G.delEdge(e);
76  }
77  });
78 }
79 
81 OGDF_EXPORT bool hasNonSelfLoopEdges(const Graph &G);
82 
84 
89 OGDF_EXPORT void makeLoopFree(Graph &G);
90 
91 
95 
97 
103 OGDF_EXPORT void parallelFreeSort(const Graph &G, SListPure<edge> &edges);
104 
105 
107 
117 OGDF_EXPORT bool isParallelFree(const Graph &G);
118 
119 
121 
134 template <bool ONLY_ONCE = false>
135 int numParallelEdges(const Graph &G) {
136  if (G.numberOfEdges() <= 1) return 0;
137 
138  SListPure<edge> edges;
139  parallelFreeSort(G,edges);
140 
141  int num = 0;
142  SListConstIterator<edge> it = edges.begin();
143  edge ePrev = *it, e;
144  for(it = ++it; it.valid(); ++it, ePrev = e) {
145  e = *it;
146  if (ePrev->isParallelDirected(e)) {
147  ++num;
148  if (ONLY_ONCE) {
149  return num;
150  }
151  }
152  }
153 
154  return num;
155 }
156 
158 
170 template <class EDGELIST>
171 void makeParallelFree(Graph &G, EDGELIST &parallelEdges)
172 {
173  parallelEdges.clear();
174  if (G.numberOfEdges() <= 1) return;
175 
176  SListPure<edge> edges;
177  parallelFreeSort(G,edges);
178 
179  SListConstIterator<edge> it = edges.begin();
180  edge ePrev = *it++, e;
181  bool bAppend = true;
182  while(it.valid()) {
183  e = *it++;
184  if (e->isParallelDirected(ePrev)) {
185  G.delEdge(e);
186  if (bAppend) { parallelEdges.pushBack(ePrev); bAppend = false; }
187  } else {
188  ePrev = e; bAppend = true;
189  }
190  }
191 }
192 
193 
195 
204 inline void makeParallelFree(Graph &G) {
205  List<edge> parallelEdges;
206  makeParallelFree(G,parallelEdges);
207 }
208 
209 
210 
212 
224  const Graph &G,
225  SListPure<edge> &edges,
226  EdgeArray<int> &minIndex,
227  EdgeArray<int> &maxIndex);
228 
229 
231 
241 
242 
244 
256 template <bool ONLY_ONCE = false>
258 {
259  if (G.numberOfEdges() <= 1) return 0;
260 
261  SListPure<edge> edges;
262  EdgeArray<int> minIndex(G), maxIndex(G);
263  parallelFreeSortUndirected(G,edges,minIndex,maxIndex);
264 
265  int num = 0;
266  SListConstIterator<edge> it = edges.begin();
267  edge ePrev = *it, e;
268  for(it = ++it; it.valid(); ++it, ePrev = e) {
269  e = *it;
270  if (minIndex[ePrev] == minIndex[e] && maxIndex[ePrev] == maxIndex[e]) {
271  ++num;
272  if (ONLY_ONCE) {
273  return num;
274  }
275  }
276  }
277 
278  return num;
279 }
280 
281 
283 
294 template <class EDGELIST>
296 {
297  if (G.numberOfEdges() <= 1) {
298  return;
299  }
300 
301  SListPure<edge> edges;
302  EdgeArray<int> minIndex(G), maxIndex(G);
303  parallelFreeSortUndirected(G,edges,minIndex,maxIndex);
304 
305  SListConstIterator<edge> it = edges.begin();
306  edge ePrev = *it++, e;
307  while (it.valid()) {
308  e = *it++;
309  if (minIndex[ePrev] == minIndex[e] && maxIndex[ePrev] == maxIndex[e]) {
310  parallelEdges[ePrev].pushBack(e);
311  } else {
312  ePrev = e;
313  }
314  }
315 }
316 
317 
319 
336 template <class EDGELIST = SListPure<edge>>
338  Graph &G,
339  EDGELIST *parallelEdges = nullptr,
340  EdgeArray<int> *cardPositive = nullptr,
341  EdgeArray<int> *cardNegative = nullptr)
342 {
343  if (parallelEdges != nullptr) { parallelEdges->clear(); }
344  if (cardPositive != nullptr) { cardPositive->fill(0); }
345  if (cardNegative != nullptr) { cardNegative->fill(0); }
346 
347  if (G.numberOfEdges() <= 1) {
348  return;
349  }
350 
351  EdgeArray<SListPure<edge>> parEdges(G);
352  getParallelFreeUndirected(G, parEdges);
353 
354  for (edge e : G.edges) {
355  for (edge parEdge : parEdges(e)) {
356  if (cardPositive != nullptr && e->source() == parEdge->source()) {
357  (*cardPositive)[e]++;
358  }
359  if (cardNegative != nullptr && e->source() == parEdge->target()) {
360  (*cardNegative)[e]++;
361  }
362  G.delEdge(parEdge);
363  if (parallelEdges != nullptr) {
364  parallelEdges->pushBack(e);
365  }
366  }
367  }
368 }
369 
370 
374 template <class EDGELIST>
375 OGDF_DEPRECATED("The pointer-based makeParallelFreeUndirected() should be used instead.")
376 void makeParallelFreeUndirected(Graph &G, EDGELIST &parallelEdges) {
377  makeParallelFreeUndirected(G, &parallelEdges);
378 }
379 
383 template <class EDGELIST>
384 OGDF_DEPRECATED("The pointer-based makeParallelFreeUndirected() should be used instead.")
386  EDGELIST &parallelEdges,
387  EdgeArray<int> &cardPositive,
388  EdgeArray<int> &cardNegative) {
389  makeParallelFreeUndirected(G, &parallelEdges, &cardPositive, &cardNegative);
390 }
391 
395 
397 
403 inline bool isSimple(const Graph &G) {
404  return isLoopFree(G) && isParallelFree(G);
405 }
406 
407 
409 
414 inline void makeSimple(Graph &G) {
415  makeLoopFree(G);
416  makeParallelFree(G);
417 }
418 
419 
421 
428 inline bool isSimpleUndirected(const Graph &G) {
429  return isLoopFree(G) && isParallelFreeUndirected(G);
430 }
431 
432 
434 
439 inline void makeSimpleUndirected(Graph &G) {
440  makeLoopFree(G);
442 }
443 
447 
449 
455 OGDF_EXPORT bool isConnected(const Graph &G);
456 
457 
459 
465 OGDF_EXPORT void makeConnected(Graph &G, List<edge> &added);
466 
467 
469 
474 inline void makeConnected(Graph &G) {
475  List<edge> added;
476  makeConnected(G,added);
477 }
478 
479 
482 
495  NodeArray<int> &component,
496  List<node> *isolated = nullptr);
497 
498 
499 OGDF_DEPRECATED("connectedComponents() should be used instead.")
504 inline int connectedIsolatedComponents(const Graph &G,
505  List<node> &isolated,
506  NodeArray<int> &component) {
507  return connectedComponents(G, component, &isolated);
508 }
509 
510 
512 
519 OGDF_EXPORT bool isBiconnected(const Graph &G, node &cutVertex);
520 
521 
523 
528 inline bool isBiconnected(const Graph &G) {
529  node cutVertex;
530  return isBiconnected(G,cutVertex);
531 }
532 
533 
535 
542 
543 
545 
550 inline void makeBiconnected(Graph &G) {
551  List<edge> added;
552  makeBiconnected(G,added);
553 }
554 
555 
562 OGDF_EXPORT int biconnectedComponents(const Graph &G, EdgeArray<int> &component, int &nonEmptyComponents);
563 
565 
578 inline int biconnectedComponents(const Graph &G, EdgeArray<int> &component) {
579  int doNotNeedTheValue;
580  return biconnectedComponents(G, component, doNotNeedTheValue);
581 }
582 
583 
589 OGDF_EXPORT bool isTwoEdgeConnected(const Graph &graph, edge &bridge);
590 
604 inline bool isTwoEdgeConnected(const Graph &graph) {
605  edge bridge;
606  return isTwoEdgeConnected(graph, bridge);
607 }
608 
609 
611 
624 OGDF_EXPORT bool isTriconnected(const Graph &G, node &s1, node &s2);
625 
626 
628 
634 inline bool isTriconnected(const Graph &G) {
635  node s1, s2;
636  return isTriconnected(G,s1,s2);
637 }
638 
639 
641 
657 OGDF_EXPORT bool isTriconnectedPrimitive(const Graph &G, node &s1, node &s2);
658 
659 
661 
670 inline bool isTriconnectedPrimitive(const Graph &G) {
671  node s1, s2;
672  return isTriconnectedPrimitive(G,s1,s2);
673 }
674 
675 
677 
687 void triangulate(Graph &G);
688 
689 
693 
695 
702 OGDF_EXPORT bool isAcyclic(const Graph &G, List<edge> &backedges);
703 
704 
706 
712 inline bool isAcyclic(const Graph &G) {
713  List<edge> backedges;
714  return isAcyclic(G,backedges);
715 }
716 
717 
719 
726 OGDF_EXPORT bool isAcyclicUndirected(const Graph &G, List<edge> &backedges);
727 
728 
730 
736 inline bool isAcyclicUndirected(const Graph &G) {
737  List<edge> backedges;
738  return isAcyclicUndirected(G,backedges);
739 }
740 
741 
743 
750 OGDF_EXPORT void makeAcyclic(Graph &G);
751 
752 
754 
762 
763 
765 
772 OGDF_EXPORT bool hasSingleSource(const Graph &G, node &source);
773 
774 
776 
782 inline bool hasSingleSource(const Graph &G) {
783  node source;
784  return hasSingleSource(G,source);
785 }
786 
787 
789 
796 OGDF_EXPORT bool hasSingleSink(const Graph &G, node &sink);
797 
798 
800 
806 inline bool hasSingleSink(const Graph &G) {
807  node sink;
808  return hasSingleSink(G,sink);
809 }
810 
811 
813 
825 OGDF_EXPORT bool isStGraph(const Graph &G, node &s, node &t, edge &st);
826 
827 
829 
837 inline bool isStGraph(const Graph &G) {
838  node s, t;
839  edge st;
840  return isStGraph(G,s,t,st);
841 }
842 
843 
845 
854 
855 
857 
866 OGDF_EXPORT int strongComponents(const Graph& G, NodeArray<int>& component);
867 
868 
870 
879 OGDF_EXPORT void makeBimodal(Graph &G, List<edge> &newEdges);
880 
881 
883 
890 inline void makeBimodal(Graph &G) {
891  List<edge> dummy;
892  makeBimodal(G, dummy);
893 }
894 
895 
899 
900 OGDF_DEPRECATED("isAcyclicUndirected() should be used instead.")
905 inline bool isFreeForest(const Graph &G) {
906  return isAcyclicUndirected(G);
907 }
908 
909 
911 
917 inline bool isTree(const Graph &G)
918 {
919  return G.empty() || ((G.numberOfNodes() == G.numberOfEdges() + 1) && isConnected(G));
920 }
921 
922 
924 
932 OGDF_EXPORT bool isArborescenceForest(const Graph& G, List<node> &roots);
933 
934 
936 
942 inline bool isArborescenceForest(const Graph &G) {
943  List<node> roots;
944  return isArborescenceForest(G,roots);
945 }
946 
947 
948 OGDF_DEPRECATED("isArborescenceForest() should be used instead.")
953 inline bool isForest(const Graph& G, List<node> &roots) {
954  return isArborescenceForest(G, roots);
955 }
956 
957 
958 OGDF_DEPRECATED("isArborescenceForest() should be used instead.")
963 inline bool isForest(const Graph &G) {
964  return isArborescenceForest(G);
965 }
966 
967 
969 
976 OGDF_EXPORT bool isArborescence(const Graph& G, node &root);
977 
978 
980 
986 inline bool isArborescence(const Graph &G) {
987  node root;
988  return isArborescence(G,root);
989 }
990 
992 
994 
998 OGDF_EXPORT bool isRegular(const Graph& G);
999 
1000 
1002 
1007 OGDF_EXPORT bool isRegular(const Graph& G, int d);
1008 
1009 
1011 
1017 OGDF_EXPORT bool isBipartite(const Graph &G, NodeArray<bool> &color);
1018 
1019 
1021 
1025 inline bool isBipartite(const Graph &G) {
1026  NodeArray<bool> color(G);
1027  return isBipartite(G, color);
1028 }
1029 
1060 OGDF_EXPORT void nodeDistribution(const Graph& G, Array<int> &degdist, std::function<int(node)> func);
1061 
1067 inline void degreeDistribution(const Graph& G, Array<int> &degdist) {
1068  nodeDistribution(G, degdist, [](node v) {
1069  return v->degree();
1070  });
1071 }
1072 
1073 }
Declaration of singly linked lists and iterators.
bool hasNonSelfLoopEdges(const Graph &G)
Returns whether G has edges which are not self-loops.
void removeSelfLoops(Graph &graph, node v)
Removes all self-loops for a given node v in graph.
bool isForest(const Graph &G, List< node > &roots)
Returns true iff G is a forest consisting only of arborescences.
bool isRegular(const Graph &G)
Checks if a graph is regular.
bool valid() const
Returns true iff the iterator points to an element.
Definition: SList.h:115
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:492
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition: config.h:99
void makeLoopFree(Graph &G, NODELIST &L)
Removes all self-loops from G and returns all nodes with self-loops in L.
void topologicalNumbering(const Graph &G, NodeArray< int > &num)
Computes a topological numbering of an acyclic digraph G.
Singly linked lists.
Definition: SList.h:38
int degree() const
Returns the degree of the node (indegree + outdegree).
Definition: Graph_d.h:210
void getParallelFreeUndirected(const Graph &G, EdgeArray< EDGELIST > &parallelEdges)
Computes the bundles of undirected parallel edges in G.
bool isTriconnectedPrimitive(const Graph &G, node &s1, node &s2)
Returns true iff G is triconnected (using a quadratic time algorithm!).
virtual void delEdge(edge e)
Removes edge e from the graph.
int numParallelEdgesUndirected(const Graph &G)
Returns the number of undirected parallel edges in G.
void makeSimple(Graph &G)
Removes all self-loops and all but one edge of each bundle of parallel edges.
Encapsulates a pointer to an ogdf::SList element.
Definition: SList.h:39
int connectedIsolatedComponents(const Graph &G, List< node > &isolated, NodeArray< int > &component)
Computes the connected components of G and optionally generates a list of isolated nodes...
int connectedComponents(const Graph &G, NodeArray< int > &component, List< node > *isolated=nullptr)
Computes the connected components of G and optionally generates a list of isolated nodes...
void parallelFreeSort(const Graph &G, SListPure< edge > &edges)
Sorts the edges of G such that parallel edges come after each other in the list.
void makeSimpleUndirected(Graph &G)
Removes all self-loops and all but one edge of each bundle of undirected parallel edges...
int numParallelEdges(const Graph &G)
Returns the number of parallel edges in G.
void parallelFreeSortUndirected(const Graph &G, SListPure< edge > &edges, EdgeArray< int > &minIndex, EdgeArray< int > &maxIndex)
Sorts the edges of G such that undirected parallel edges come after each other in the list...
void makeConnected(Graph &G, List< edge > &added)
Makes G connected by adding a minimum number of edges.
bool isStGraph(const Graph &G, node &s, node &t, edge &st)
Returns true iff G is an st-digraph.
bool hasSingleSink(const Graph &G, node &sink)
Returns true iff the digraph G contains exactly one sink node (or is empty).
bool isTriconnected(const Graph &G, node &s1, node &s2)
Returns true iff G is triconnected.
internal::GraphObjectContainer< EdgeElement > edges
The container containing all edge objects.
Definition: Graph_d.h:568
bool isFreeForest(const Graph &G)
Returns true iff the undirected graph G is acyclic.
int numberOfEdges() const
Returns the number of edges in the graph.
Definition: Graph_d.h:601
void nodeDistribution(const Graph &G, Array< int > &degdist, std::function< int(node)> func)
Fills dist with the distribution given by a function func in graph G.
iterator begin()
Returns an iterator to the first element of the list.
Definition: SList.h:318
bool isAcyclicUndirected(const Graph &G, List< edge > &backedges)
Returns true iff the undirected graph G is acyclic.
Declaration and implementation of EdgeArray class.
bool isArborescenceForest(const Graph &G, List< node > &roots)
Returns true iff G is a forest consisting only of arborescences.
bool isLoopFree(const Graph &G)
Returns true iff G contains no self-loop.
bool isBiconnected(const Graph &G, node &cutVertex)
Returns true iff G is biconnected.
Class for the representation of edges.
Definition: Graph_d.h:292
void makeBimodal(Graph &G, List< edge > &newEdges)
Makes the digraph G bimodal.
void makeAcyclicByReverse(Graph &G)
Makes the digraph G acyclic by reversing edges.
NodeElement * node
The type of nodes.
Definition: Graph_d.h:63
Class for the representation of nodes.
Definition: Graph_d.h:169
void makeAcyclic(Graph &G)
Makes the digraph G acyclic by removing edges.
Declaration and implementation of NodeArray class.
bool isAcyclic(const Graph &G, List< edge > &backedges)
Returns true iff the digraph G is acyclic.
void makeParallelFreeUndirected(Graph &G, EDGELIST *parallelEdges=nullptr, EdgeArray< int > *cardPositive=nullptr, EdgeArray< int > *cardNegative=nullptr)
Removes all but one edge of each bundle of undirected parallel edges.
bool isParallelFree(const Graph &G)
Returns true iff G contains no parallel edges.
bool isSimpleUndirected(const Graph &G)
Returns true iff G contains neither self-loops nor undirected parallel edges.
bool isArborescence(const Graph &G, node &root)
Returns true iff G represents an arborescence.
bool isParallelFreeUndirected(const Graph &G)
Returns true iff G contains no undirected parallel edges.
bool isTwoEdgeConnected(const Graph &graph, edge &bridge)
Returns true iff graph is 2-edge-connected.
bool hasSingleSource(const Graph &G, node &source)
Returns true iff the digraph G contains exactly one source node (or is empty).
void safeForEach(CONTAINER &container, std::function< void(typename CONTAINER::value_type)> func)
Calls (possibly destructive) func for each element of container.
bool isTree(const Graph &G)
Returns true iff G is a tree, i.e. contains no undirected cycle and is connected. ...
bool isBipartite(const Graph &G, NodeArray< bool > &color)
Checks whether a graph is bipartite.
#define OGDF_DEPRECATED(reason)
Mark a class / member / function as deprecated.
Definition: config.h:118
void degreeDistribution(const Graph &G, Array< int > &degdist)
Fills degdist with the degree distribution of graph G.
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:326
void triangulate(Graph &G)
Triangulates a planarly embedded graph G by adding edges.
int biconnectedComponents(const Graph &G, EdgeArray< int > &component, int &nonEmptyComponents)
Computes the biconnected components of G.
bool isSimple(const Graph &G)
Returns true iff G contains neither self-loops nor parallel edges.
The namespace for all OGDF objects.
int strongComponents(const Graph &G, NodeArray< int > &component)
Computes the strongly connected components of the digraph G.
int numberOfNodes() const
Returns the number of nodes in the graph.
Definition: Graph_d.h:598
void makeBiconnected(Graph &G, List< edge > &added)
Makes G biconnected by adding edges.
bool isConnected(const Graph &G)
Returns true iff G is connected.
void makeParallelFree(Graph &G, EDGELIST &parallelEdges)
Removes all but one of each bundle of parallel edges.
bool empty() const
Returns true iff the graph is empty, i.e., contains no nodes.
Definition: Graph_d.h:595