Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

simple_graph_alg.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/EdgeArray.h>
35 #include <ogdf/basic/SList.h>
37 
38 namespace ogdf {
39 
42 
44 
50 OGDF_EXPORT bool isLoopFree(const Graph &G);
51 
53 
60 template<class NODELIST>
61 void makeLoopFree(Graph &G, NODELIST &L)
62 {
63  L.clear();
64 
65  edge e, eNext;
66  for (e = G.firstEdge(); e; e = eNext) {
67  eNext = e->succ();
68  if (e->isSelfLoop()) {
69  L.pushBack(e->source());
70  G.delEdge(e);
71  }
72  }
73 }
74 
75 
77 
83 
84 
88 
90 
96 OGDF_EXPORT void parallelFreeSort(const Graph &G, SListPure<edge> &edges);
97 
98 
100 
110 OGDF_EXPORT bool isParallelFree(const Graph &G);
111 
112 
114 
125 OGDF_EXPORT int numParallelEdges(const Graph &G);
126 
127 
129 
141 template <class EDGELIST>
142 void makeParallelFree(Graph &G, EDGELIST &parallelEdges)
143 {
144  parallelEdges.clear();
145  if (G.numberOfEdges() <= 1) return;
146 
147  SListPure<edge> edges;
148  parallelFreeSort(G,edges);
149 
150  SListConstIterator<edge> it = edges.begin();
151  edge ePrev = *it++, e;
152  bool bAppend = true;
153  while(it.valid()) {
154  e = *it++;
155  if (ePrev->source() == e->source() && ePrev->target() == e->target()) {
156  G.delEdge(e);
157  if (bAppend) { parallelEdges.pushBack(ePrev); bAppend = false; }
158  } else {
159  ePrev = e; bAppend = true;
160  }
161  }
162 }
163 
164 
166 
175 inline void makeParallelFree(Graph &G) {
176  List<edge> parallelEdges;
177  makeParallelFree(G,parallelEdges);
178 }
179 
180 
181 
183 
195  const Graph &G,
196  SListPure<edge> &edges,
197  EdgeArray<int> &minIndex,
198  EdgeArray<int> &maxIndex);
199 
200 
202 
212 
213 
215 
226 
227 
229 
241 template <class EDGELIST>
242 void makeParallelFreeUndirected(Graph &G, EDGELIST &parallelEdges)
243 {
244  parallelEdges.clear();
245  if (G.numberOfEdges() <= 1) return;
246 
247  SListPure<edge> edges;
248  EdgeArray<int> minIndex(G), maxIndex(G);
249  parallelFreeSortUndirected(G,edges,minIndex,maxIndex);
250 
251  SListConstIterator<edge> it = edges.begin();
252  edge ePrev = *it++, e;
253  bool bAppend = true;
254  while(it.valid()) {
255  e = *it++;
256  if (minIndex[ePrev] == minIndex[e] && maxIndex[ePrev] == maxIndex[e]) {
257  G.delEdge(e);
258  if (bAppend) { parallelEdges.pushBack(ePrev); bAppend = false; }
259  } else {
260  ePrev = e; bAppend = true;
261  }
262  }
263 }
264 
265 
267 
277  List<edge> parallelEdges;
278  makeParallelFreeUndirected(G,parallelEdges);
279 }
280 
281 
283 
299 template <class EDGELIST>
301  Graph &G,
302  EDGELIST &parallelEdges,
303  EdgeArray<int> &cardPositive,
304  EdgeArray<int> &cardNegative)
305 {
306  parallelEdges.clear();
307  cardPositive.fill(0);
308  cardNegative.fill(0);
309  if (G.numberOfEdges() <= 1) return;
310 
311  SListPure<edge> edges;
312  EdgeArray<int> minIndex(G), maxIndex(G);
313  parallelFreeSortUndirected(G,edges,minIndex,maxIndex);
314 
315  SListConstIterator<edge> it = edges.begin();
316  edge ePrev = *it++, e;
317  bool bAppend = true;
318  while(it.valid())
319  {
320  e = *it++;
321  if (minIndex[ePrev] == minIndex[e] && maxIndex[ePrev] == maxIndex[e])
322  {
323  if (ePrev->source() == e->source() && ePrev->target() == e->target())
324  cardPositive[ePrev]++;
325  else if (ePrev->source() == e->target() && ePrev->target() == e->source())
326  cardNegative[ePrev]++;
327  G.delEdge(e);
328  if (bAppend)
329  {
330  parallelEdges.pushBack(ePrev);
331  bAppend = false;
332  }
333  }
334  else
335  {
336  ePrev = e; bAppend = true;
337  }
338  }
339 }
340 
341 
343 
354 template <class EDGELIST>
356 {
357  if (G.numberOfEdges() <= 1) return;
358 
359  SListPure<edge> edges;
360  EdgeArray<int> minIndex(G), maxIndex(G);
361  parallelFreeSortUndirected(G,edges,minIndex,maxIndex);
362 
363  SListConstIterator<edge> it = edges.begin();
364  edge ePrev = *it++, e;
365  while(it.valid())
366  {
367  e = *it++;
368  if (minIndex[ePrev] == minIndex[e] && maxIndex[ePrev] == maxIndex[e])
369  parallelEdges[ePrev].pushBack(e);
370  else
371  ePrev = e;
372  }
373 }
374 
378 
380 
386 inline bool isSimple(const Graph &G) {
387  return isLoopFree(G) && isParallelFree(G);
388 }
389 
390 
392 
397 inline void makeSimple(Graph &G) {
398  makeLoopFree(G);
399  makeParallelFree(G);
400 }
401 
402 
404 
411 inline bool isSimpleUndirected(const Graph &G) {
412  return isLoopFree(G) && isParallelFreeUndirected(G);
413 }
414 
415 
417 
422 inline void makeSimpleUndirected(Graph &G) {
423  makeLoopFree(G);
425 }
426 
430 
432 
438 OGDF_EXPORT bool isConnected(const Graph &G);
439 
440 
442 
448 OGDF_EXPORT void makeConnected(Graph &G, List<edge> &added);
449 
450 
452 
457 inline void makeConnected(Graph &G) {
458  List<edge> added;
459  makeConnected(G,added);
460 }
461 
462 
464 
474 OGDF_EXPORT int connectedComponents(const Graph &G, NodeArray<int> &component);
475 
476 
478 
491  const Graph &G,
492  List<node> &isolated,
493  NodeArray<int> &component);
494 
495 
497 
504 OGDF_EXPORT bool isBiconnected(const Graph &G, node &cutVertex);
505 
506 
508 
513 inline bool isBiconnected(const Graph &G) {
514  node cutVertex;
515  return isBiconnected(G,cutVertex);
516 }
517 
518 
520 
527 
528 
530 
535 inline void makeBiconnected(Graph &G) {
536  List<edge> added;
537  makeBiconnected(G,added);
538 }
539 
540 
547 OGDF_EXPORT int biconnectedComponents(const Graph &G, EdgeArray<int> &component, int &nonEmptyComponents);
548 
550 
563 inline int biconnectedComponents(const Graph &G, EdgeArray<int> &component) {
564  int doNotNeedTheValue;
565  return biconnectedComponents(G, component, doNotNeedTheValue);
566 }
567 
568 
570 
583 OGDF_EXPORT bool isTriconnected(const Graph &G, node &s1, node &s2);
584 
585 
587 
593 inline bool isTriconnected(const Graph &G) {
594  node s1, s2;
595  return isTriconnected(G,s1,s2);
596 }
597 
598 
600 
616 OGDF_EXPORT bool isTriconnectedPrimitive(const Graph &G, node &s1, node &s2);
617 
618 
620 
629 inline bool isTriconnectedPrimitive(const Graph &G) {
630  node s1, s2;
631  return isTriconnectedPrimitive(G,s1,s2);
632 }
633 
634 
636 
646 void triangulate(Graph &G);
647 
648 
652 
654 
661 OGDF_EXPORT bool isAcyclic(const Graph &G, List<edge> &backedges);
662 
663 
665 
671 inline bool isAcyclic(const Graph &G) {
672  List<edge> backedges;
673  return isAcyclic(G,backedges);
674 }
675 
676 
678 
685 OGDF_EXPORT bool isAcyclicUndirected(const Graph &G, List<edge> &backedges);
686 
687 
689 
695 inline bool isAcyclicUndirected(const Graph &G) {
696  List<edge> backedges;
697  return isAcyclicUndirected(G,backedges);
698 }
699 
700 
702 
709 OGDF_EXPORT void makeAcyclic(Graph &G);
710 
711 
713 
721 
722 
724 
731 OGDF_EXPORT bool hasSingleSource(const Graph &G, node &source);
732 
733 
735 
741 inline bool hasSingleSource(const Graph &G) {
742  node source;
743  return hasSingleSource(G,source);
744 }
745 
746 
748 
755 OGDF_EXPORT bool hasSingleSink(const Graph &G, node &sink);
756 
757 
759 
765 inline bool hasSingleSink(const Graph &G) {
766  node sink;
767  return hasSingleSink(G,sink);
768 }
769 
770 
772 
784 OGDF_EXPORT bool isStGraph(const Graph &G, node &s, node &t, edge &st);
785 
786 
788 
796 inline bool isStGraph(const Graph &G) {
797  node s, t;
798  edge st;
799  return isStGraph(G,s,t,st);
800 }
801 
802 
804 
813 
814 
816 
825 OGDF_EXPORT int strongComponents(const Graph& G, NodeArray<int>& component);
826 
827 
829 
838 OGDF_EXPORT void makeBimodal(Graph &G, List<edge> &newEdges);
839 
840 
842 
849 inline void makeBimodal(Graph &G) {
850  List<edge> dummy;
851  makeBimodal(G, dummy);
852 }
853 
854 
858 
860 
866 OGDF_EXPORT bool isFreeForest(const Graph &G);
867 
869 
875 inline bool isTree(const Graph &G)
876 {
877  return G.empty() || ((G.numberOfNodes() == G.numberOfEdges() + 1) && isConnected(G));
878 }
879 
880 
882 
890 OGDF_EXPORT bool isForest(const Graph& G, List<node> &roots);
891 
892 
894 
900 inline bool isForest(const Graph &G)
901 {
902  List<node> roots;
903  return isForest(G,roots);
904 }
905 
906 
908 
915 OGDF_EXPORT bool isArborescence (const Graph& G, node &root);
916 
917 
919 
925 inline bool isArborescence(const Graph &G) {
926  node root;
927  return isArborescence(G,root);
928 }
929 
931 
933 
937 OGDF_EXPORT bool isRegular(const Graph& G);
938 
939 
941 
946 OGDF_EXPORT bool isRegular(const Graph& G, int d);
947 
978 OGDF_EXPORT void nodeDistribution(const Graph& G, Array<int> &degdist, std::function<int(node)> func);
979 
985 inline void degreeDistribution(const Graph& G, Array<int> &degdist) {
986  nodeDistribution(G, degdist, [](node v) {
987  return v->degree();
988  });
989 }
990 
991 } // end namespace ogdf
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:327
Declaration of singly linked lists and iterators.
bool isForest(const Graph &G, List< node > &roots)
Returns true iff G represents a forest, i.e., a collection 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:113
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:472
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition: config.h:99
static const int Graph
Definition: GmlParser.h:59
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:208
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.
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:40
int connectedIsolatedComponents(const Graph &G, List< node > &isolated, NodeArray< int > &component)
Computes the connected components of G and returns the 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.
Declaration and implementation of bounded stack class.
void fill(const T &x)
Sets all array elements to x.
Definition: EdgeArray.h:339
void makeSimpleUndirected(Graph &G)
Removes all self-loops and all but one edge of each bundle of undirected parallel edges...
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.
bool isFreeForest(const Graph &G)
Returns true iff G is a forest, i.e. contains no undirected cycle.
int numberOfEdges() const
Returns the number of edges in the graph.
Definition: Graph_d.h:581
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:316
bool isAcyclicUndirected(const Graph &G, List< edge > &backedges)
Returns true iff the undirected graph G is acyclic.
Declaration and implementation of EdgeArray class.
edge firstEdge() const
Returns the first edge in the list of all edges.
Definition: Graph_d.h:603
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:291
void makeBimodal(Graph &G, List< edge > &newEdges)
Makes the digraph G bimodal.
void makeAcyclicByReverse(Graph &G)
Makes the digraph G acyclic by reversing edges.
Class for the representation of nodes.
Definition: Graph_d.h:167
void makeAcyclic(Graph &G)
Makes the digraph G acyclic by removing edges.
int numParallelEdges(const Graph &G)
Returns the number of parallel edges in G.
bool isAcyclic(const Graph &G, List< edge > &backedges)
Returns true iff the digraph G is acyclic.
bool isParallelFree(const Graph &G)
Returns true iff G contains no paralle 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 a tree.
bool isParallelFreeUndirected(const Graph &G)
Returns true iff G contains no undirected parallel edges.
bool isSelfLoop() const
Definition: Graph_d.h:337
edge succ() const
Returns the successor in the list of all edges.
Definition: Graph_d.h:340
bool hasSingleSource(const Graph &G, node &source)
Returns true iff the digraph G contains exactly one source node (or is empty).
bool isTree(const Graph &G)
Returns true iff G is a tree, i.e. contains no undirected cycle and is connected. ...
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:325
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:578
void makeParallelFreeUndirected(Graph &G, EDGELIST &parallelEdges)
Removes all but one of each bundle of undirected parallel edges.
void makeBiconnected(Graph &G, List< edge > &added)
Makes G biconnected by adding edges.
bool isConnected(const Graph &G)
Returns true iff G is connected.
int connectedComponents(const Graph &G, NodeArray< int > &component)
Computes the connected components of G.
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:575
int numParallelEdgesUndirected(const Graph &G)
Returns the number of undirected parallel edges in G.