Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

LayersBlockEmbedder.h
Go to the documentation of this file.
1 
33 #pragma once
34 
39 
40 
41 namespace ogdf {
42 namespace embedder {
43 
45 template<class BaseEmbedder, class T>
46 class LayersBlockEmbedder : public BaseEmbedder {
47 protected:
49  Graph &SG,
50  NodeArray<T> &nodeLengthSG,
51  EdgeArray<T> &edgeLengthSG,
52  NodeArray<node> &nSG_to_nG,
53  EdgeArray<edge> &eSG_to_eG,
54  node nodeInBlockSG,
55  node cT,
56  ListIterator<adjEntry> &after) {
57  adjEntry m_adjExternal = nullptr;
58  // 1. Compute embedding of block
60  SG, m_adjExternal, nodeLengthSG, edgeLengthSG, nodeInBlockSG);
61 
62  // 2. Copy block embedding into graph embedding and call recursively
63  // embedBlock for all cut vertices in bT
65  face f = CE.leftFace(m_adjExternal);
66 
67  if (*BaseEmbedder::pAdjExternal == nullptr) {
68  node on = BaseEmbedder::pBCTree->original(nSG_to_nG[m_adjExternal->theNode()]);
69 
70  for (adjEntry ae : on->adjEntries) {
71  if (ae->theEdge() == BaseEmbedder::pBCTree->original(eSG_to_eG[m_adjExternal->theEdge()])) {
72  *BaseEmbedder::pAdjExternal = ae->twin();
73  break;
74  }
75  }
76  }
77 
78  bool DGcomputed = false;
79  int extFaceID = 0;
80 
81  // when the following objects get allocated,
82  // the DGcomputed bool is set to true
83  Graph* p_DG = nullptr;
84  ArrayBuffer<node>* p_fPG_to_nDG = nullptr;
85  NodeArray<List<adjEntry>>* p_adjacencyList = nullptr;
86  List<List<adjEntry>>* p_faces = nullptr;
87  NodeArray<int>* p_distances = nullptr;
88 
89  for (node nSG : SG.nodes) {
90  node nH = nSG_to_nG[nSG];
91  node nG = BaseEmbedder::pBCTree->original(nH);
92  adjEntry ae = nSG->firstAdj();
93 
94  ListIterator<adjEntry>* pAfter;
95  if (BaseEmbedder::pBCTree->bcproper(nG) == cT) {
96  pAfter = &after;
97  } else {
98  pAfter = new ListIterator<adjEntry>();
99  }
100 
101  if (BaseEmbedder::pBCTree->typeOfGNode(nG) == BCTree::GNodeType::CutVertex) {
102  node cT2 = BaseEmbedder::pBCTree->bcproper(nG);
103  bool doRecurse = true;
104 
105  if (cT2 == cT) {
106  node parent_bT_of_cT2 = nullptr;
107 
108  for (adjEntry adj : cT2->adjEntries) {
109  edge e_cT2_to_bT2 = adj->theEdge();
110 
111  if (e_cT2_to_bT2->source() == cT2) {
112  parent_bT_of_cT2 = e_cT2_to_bT2->target();
113  break;
114  }
115  }
116 
117  OGDF_ASSERT(parent_bT_of_cT2 != nullptr);
118 
119  if (BaseEmbedder::treeNodeTreated[parent_bT_of_cT2]) {
120  doRecurse = false;
121  }
122  }
123 
124 
125  // find adjacency entry of nSG which lies on external face f:
126  bool aeExtExists = false;
127  for (adjEntry aeFace : f->entries) {
128  if (aeFace->theNode() == nSG) {
129  ae = aeFace->succ() == nullptr ? nSG->firstAdj() : aeFace->succ();
130  aeExtExists = true;
131  break;
132  }
133  }
134 
135  if (doRecurse) {
136  if (!aeExtExists) {
137  if (!DGcomputed) {
138  p_DG = new Graph();
139  p_fPG_to_nDG = new ArrayBuffer<node>();
140  p_adjacencyList = new NodeArray< List<adjEntry> >();
141  p_faces = new List< List<adjEntry> >;
142  p_distances = new NodeArray<int>;
143  DGcomputed = true;
144 
145  //compute dual graph of skeleton graph:
146  p_adjacencyList->init(SG);
147  for (node nBG : SG.nodes) {
148  for (adjEntry ae_nBG : nBG->adjEntries) {
149  (*p_adjacencyList)[nBG].pushBack(ae_nBG);
150  }
151  }
152 
153  NodeArray< List<adjEntry> > adjEntryTreated(SG);
154  for (node nBG : SG.nodes) {
155  for (adjEntry adj : nBG->adjEntries) {
156  if (!adjEntryTreated[nBG].search(adj).valid()) {
157  List<adjEntry> newFace;
158  adjEntry adj2 = adj;
159 
160  do {
161  newFace.pushBack(adj2);
162  adjEntryTreated[adj2->theNode()].pushBack(adj2);
163  List<adjEntry> &ladj = (*p_adjacencyList)[adj2->twinNode()];
164  adj2 = *ladj.cyclicPred(ladj.search(adj2->twin()));
165  } while (adj2 != adj);
166 
167  p_faces->pushBack(newFace);
168  }
169  }
170  }
171 
172  for (int i = 0; i < p_faces->size(); i++) {
173  p_fPG_to_nDG->push(p_DG->newNode());
174  }
175 
176  NodeArray<List<node>> adjFaces(*p_DG);
177  int i = 0;
178 
179  for (const List<adjEntry> &Li : *p_faces) {
180  int f1_id = i;
181 
182  for (adjEntry adj2 : Li) {
183  int f2_id = 0;
184  int j = 0;
185 
186  for (List<adjEntry> &Lj : *p_faces) {
187  bool do_break = false;
188 
189  for (adjEntry adj4 : Lj) {
190  if (adj4 == adj2->twin()) {
191  f2_id = j;
192  do_break = true;
193  break;
194  }
195  }
196 
197  if (do_break) {
198  break;
199  }
200 
201  j++;
202  }
203 
204  if (f1_id != f2_id
205  && !adjFaces[(*p_fPG_to_nDG)[f1_id]].search((*p_fPG_to_nDG)[f2_id]).valid()
206  && !adjFaces[(*p_fPG_to_nDG)[f2_id]].search((*p_fPG_to_nDG)[f1_id]).valid()) {
207  adjFaces[(*p_fPG_to_nDG)[f1_id]].pushBack((*p_fPG_to_nDG)[f2_id]);
208  p_DG->newEdge((*p_fPG_to_nDG)[f1_id], (*p_fPG_to_nDG)[f2_id]);
209  }
210 
211  if (adj2 == f->firstAdj()) {
212  extFaceID = f1_id;
213  }
214  }
215 
216  i++;
217  }
218 
219  // compute shortest path from every face to the external face:
220  List<edge> DG_edges;
221  p_DG->allEdges(DG_edges);
222 
223  for (edge e : DG_edges) {
224  node s = e->source();
225  node t = e->target();
226  p_DG->newEdge(t, s);
227  }
228 
229  ShortestPathWithBFM shortestPath;
230  node efDG = (*p_fPG_to_nDG)[extFaceID];
231  EdgeArray<int> el(*p_DG, 1);
232  p_distances->init(*p_DG);
233  NodeArray<edge> pi(*p_DG);
234  shortestPath.call(*p_DG, efDG, el, *p_distances, pi);
235  }
236 
237  // choose face with minimal shortest path:
238  List<adjEntry> optFace;
239  int optFaceDist = -1;
240 
241  for (int fID = 0; fID < p_faces->size(); ++fID) {
242  List<adjEntry> theFace = *(p_faces->get(fID));
243  adjEntry ae_nSG;
244  bool contains_nSG = false;
245 
246  for (adjEntry adj : theFace) {
247  if (adj->theNode() == nSG) {
248  contains_nSG = true;
249  ae_nSG = adj;
250  break;
251  }
252  }
253 
254  if (contains_nSG) {
255  int thisDist = (*p_distances)[(*p_fPG_to_nDG)[fID]];
256 
257  if (optFaceDist == -1 || optFaceDist > thisDist) {
258  optFace = theFace;
259  optFaceDist = thisDist;
260  ae = ae_nSG->succ() == nullptr ? nSG->firstAdj() : ae_nSG->succ();
261  }
262  }
263  }
264  }
265 
266  for (adjEntry adj : cT2->adjEntries) {
267  node bT2 = adj->theEdge()->opposite(cT2);
268 
269  if (!BaseEmbedder::treeNodeTreated[bT2]) {
270  this->embedBlock(bT2, cT2, *pAfter);
271  }
272  }
273  }
274  }
275 
276  // embed all edges of block bT:
277  bool after_ae = true;
278  for (adjEntry aeNode = ae; after_ae || aeNode != ae; aeNode = aeNode->succ() == nullptr ? nSG->firstAdj() : aeNode->succ()) {
279  edge eG = BaseEmbedder::pBCTree->original(eSG_to_eG[aeNode->theEdge()]);
280  if (nG == eG->source()) {
281  if (pAfter->valid()) {
282  *pAfter = BaseEmbedder::newOrder[nG].insertAfter(eG->adjSource(), *pAfter);
283  } else {
284  *pAfter = BaseEmbedder::newOrder[nG].pushBack(eG->adjSource());
285  }
286  } else {
287  if (pAfter->valid()) {
288  *pAfter = BaseEmbedder::newOrder[nG].insertAfter(eG->adjTarget(), *pAfter);
289  } else {
290  *pAfter = BaseEmbedder::newOrder[nG].pushBack(eG->adjTarget());
291  }
292  }
293 
294  after_ae &= aeNode->succ() != nullptr;
295  }
296 
297  if (*pAfter != after) {
298  delete pAfter;
299  }
300  }
301 
302  if (DGcomputed) {
303  delete p_DG;
304  delete p_fPG_to_nDG;
305  delete p_adjacencyList;
306  delete p_faces;
307  delete p_distances;
308  }
309  }
310 };
311 
312 }}
node target() const
Returns the target node of the edge.
Definition: Graph_d.h:328
Common functionality for layer-based embedding algorithms.
edge theEdge() const
Returns the edge associated with this adjacency entry.
Definition: Graph_d.h:96
ListIteratorBase< E, isConst, isReverse > succ() const
Returns successor iterator.
Definition: List.h:145
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1373
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:492
adjEntry adjSource() const
Returns the corresponding adjacancy entry at source node.
Definition: Graph_d.h:333
void init()
Reinitializes the array. Associates the array with no graph.
Definition: NodeArray.h:255
adjEntry firstAdj() const
Returns the first adjacency element in the face.
void push(E e)
Puts a new element in the buffer.
Definition: ArrayBuffer.h:102
An array that keeps track of the number of inserted elements; also usable as an efficient stack...
Definition: Array.h:45
const_iterator cyclicPred(const_iterator it) const
Returns a const iterator to the cyclic predecessor of it.
Definition: List.h:462
Declaration of CombinatorialEmbedding and face.
Dynamic arrays indexed with edges.
Definition: EdgeArray.h:112
Computes single-source shortest-paths with Bellman-Ford-Moore&#39;s algorithm.
bool valid() const
Returns true iff the iterator points to an element.
Definition: List.h:124
Declaration of class BCTree.
constexpr double pi
The constant .
Definition: Math.h:62
Dynamic arrays indexed with nodes.
Definition: Graph_d.h:438
int size() const
Returns the size (number of elements) of the array.
Definition: Array.h:282
virtual bool call(const Graph &G, const node s, const EdgeArray< int > &length, NodeArray< int > &d, NodeArray< edge > &pi) override
Doubly linked lists (maintaining the length of the list).
Definition: List.h:40
edge newEdge(node v, node w)
Creates a new edge (v,w) and returns it.
internal::FaceAdjContainer entries
Container maintaining the adjacency entries in the face.
Faces in a combinatorial embedding.
node twinNode() const
Returns the associated node of the corresponding adjacency entry (shorthand for twin()->theNode()).
Definition: Graph_d.h:108
Class for the representation of edges.
Definition: Graph_d.h:292
adjEntry twin() const
Returns the corresponding adjacency element associated with the same edge.
Definition: Graph_d.h:105
const_iterator get(int pos) const
Returns a const iterator pointing to the element at position pos.
Definition: List.h:310
Class for the representation of nodes.
Definition: Graph_d.h:169
bool valid() const
Returns true iff the array is associated with a graph.
Definition: NodeArray.h:157
Encapsulates a pointer to a list element.
Definition: List.h:42
node opposite(node v) const
Returns the adjacent node different from v.
Definition: Graph_d.h:338
adjEntry succ() const
Returns the successor in the adjacency list.
Definition: Graph_d.h:148
int size() const
Returns the number of elements in the list.
Definition: List.h:1312
face leftFace(adjEntry adj) const
Returns the face to the left of adj, i.e., the face containing the twin of adj.
static void embed(Graph &G, adjEntry &adjExternal, const NodeArray< T > &nodeLength, const EdgeArray< T > &edgeLength, const node &n=nullptr)
Embeds G by computing and extending a maximum face in G containing n.
node source() const
Returns the source node of the edge.
Definition: Graph_d.h:326
void internalEmbedBlock(Graph &SG, NodeArray< T > &nodeLengthSG, EdgeArray< T > &edgeLengthSG, NodeArray< node > &nSG_to_nG, EdgeArray< edge > &eSG_to_eG, node nodeInBlockSG, node cT, ListIterator< adjEntry > &after)
Computes an embedding of a biconnected graph with maximum external face.
The namespace for all OGDF objects.
void allEdges(CONTAINER &edgeContainer) const
Returns a container with all edges of the graph.
Definition: Graph_d.h:661
Declaration of class ShortestPathWithBFM which computes shortest paths via Bellman-Ford-Moore.
void init()
Reinitializes the array, clearing it, and without initial memory allocation.
Definition: ArrayBuffer.h:89
node newNode()
Creates a new node and returns it.
internal::GraphObjectContainer< NodeElement > nodes
The container containing all node objects.
Definition: Graph_d.h:565
adjEntry firstAdj() const
Returns the first entry in the adjaceny list.
Definition: Graph_d.h:213
Combinatorial embeddings of planar graphs with modification functionality.
ListConstIterator< E > search(const E &e) const
Scans the list for the specified element and returns an iterator to the first occurrence in the list...
Definition: List.h:1118
adjEntry adjTarget() const
Returns the corresponding adjacancy entry at target node.
Definition: Graph_d.h:335
node theNode() const
Returns the node whose adjacency list contains this element.
Definition: Graph_d.h:100
Class for adjacency list elements.
Definition: Graph_d.h:79
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
internal::GraphObjectContainer< AdjElement > adjEntries
The container containing all entries in the adjacency list of this node.
Definition: Graph_d.h:200