LGraph.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /** \file
  2. * \brief routines
  3. *
  4. * \par
  5. * Copyright (C)<br>
  6. * See README.md in the root directory for details.
  7. *
  8. * \par
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * \par
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * \par
  21. * You should have received a copy of the GNU General Public
  22. * License along with this program; if not, see
  23. * http://www.gnu.org/copyleft/gpl.html
  24. *
  25. * SPDX-License-Identifier: GPL-3.0+
  26. * License-Filename: LICENSE
  27. */
  28. /**
  29. * @file: LGraph.h
  30. */
  31. #ifndef LAYOUT_GRAPH_H
  32. #define LAYOUT_GRAPH_H
  33. /**
  34. * node positions of connected nodes and used for positioning
  35. * upper_left = 0
  36. * lower_left = 1
  37. * upper_right = 2
  38. * lower_right = 3
  39. */
  40. enum orient { upper_left, lower_left, upper_right, lower_right };
  41. /**
  42. * Graph with Layout methods
  43. * Includes ranking, median and transposition heuristic
  44. */
  45. class LGraph {
  46. /**
  47. * list of all nodes including dummy nodes
  48. */
  49. list< pLNode > m_nodes_list;
  50. /**
  51. * list of all edges without self edges
  52. */
  53. list< pLEdge > m_edges_list;
  54. /**
  55. * list of all self-edges
  56. */
  57. list< pLEdge > m_selfedges_list;
  58. /**
  59. * total number of nodes including dummy nodes
  60. */
  61. int m_total_nodes_num;
  62. /**
  63. * total number of dummy nodes
  64. */
  65. int m_total_dummynodes_num;
  66. /**
  67. * total number of edges without self-edges
  68. */
  69. int m_total_edges_num;
  70. /**
  71. * total number of horizontal edges without self-edges
  72. */
  73. int m_total_horedges_num;
  74. /**
  75. * total number of self-edges
  76. */
  77. int m_total_selfedges_num;
  78. /**
  79. * total number of reversed edges without self-edges
  80. */
  81. int m_total_reversededges_num;
  82. /**
  83. * serial number for uniq node id
  84. */
  85. int next_node_id;
  86. /**
  87. * serial number for uniq edge id
  88. */
  89. int next_edge_id;
  90. /**
  91. * uniq graph number if set by user or 0 if not set
  92. */
  93. int m_id;
  94. /**
  95. * x spacing in the graph
  96. */
  97. int m_xspacing_num;
  98. /**
  99. * y spacing in the graph
  100. */
  101. int m_yspacing_num;
  102. /**
  103. * max y level, number of levels is (maxrank+1)
  104. */
  105. unsigned int maxrank;
  106. /**
  107. * if >0 graph is already layouted
  108. */
  109. unsigned int layouted;
  110. /**
  111. * number of starter nodes
  112. */
  113. unsigned int m_nstarter_num;
  114. /**
  115. * horizontal node ordering
  116. */
  117. Ordering* order;
  118. public:
  119. /**
  120. * 4 arrays with node pointers for every node as start of block where node belongs to
  121. */
  122. std::array< vector < pLNode > , 4 > root;
  123. /**
  124. * 4 arrays with node pointers for every node as node to align with for node
  125. */
  126. std::array< vector < pLNode > , 4 > align;
  127. /**
  128. * 4 arrays with node pointers
  129. */
  130. std::array< vector < pLNode > , 4 > sink;
  131. /**
  132. * 4 arrays with node pointers
  133. */
  134. std::array< vector < pLNode > , 4 > medians;
  135. /**
  136. * 4 arrays with node shift
  137. */
  138. std::array< vector < double > , 4 > shift;
  139. /**
  140. * splay with node id and node
  141. */
  142. splay_tree nodesplay = (splay_tree)0;
  143. /**
  144. * splay with edge id and edge
  145. */
  146. splay_tree edgesplay = (splay_tree)0;
  147. /**
  148. * create graph
  149. */
  150. LGraph();
  151. /**
  152. * delete graph
  153. */
  154. ~LGraph();
  155. /**
  156. * Replacing revert edges from the ReverseEdges list.
  157. */
  158. void ReverseReverseEdges(list<pLEdge>& ReverseEdges);
  159. /**
  160. * get number of vertical y levels
  161. */
  162. int getMaxrank() { return maxrank; }
  163. /**
  164. * This main Layout function do whole procedure of LAYOUT:
  165. * 1. Reversing "Reverse" edges
  166. * 2. Initing Rank for all nodes
  167. * 3. Find and breaks "long" edges
  168. * 4. Makes ordering (horizontal placement)
  169. * 5. Place node at absolute positions
  170. */
  171. void Layout(unsigned long int number_of_iterations = 3,
  172. bool do_transpose = true,
  173. int transpose_range = -1,
  174. bool verbose = false,
  175. bool usebary = false
  176. );
  177. /**
  178. * Init Rank value for each node.
  179. *
  180. * rank = 0 for "head" nodes;
  181. * rank = max(rank(adj_nodes)) for others.
  182. */
  183. virtual void InitRank();
  184. /**
  185. * Breaks all "long" edges with dummy nodes
  186. */
  187. void AddDummyNodes(list<pLEdge>& LongEdges);
  188. /**
  189. * Finds all "long" edges in graph and puts it to the LondEdges list.
  190. */
  191. void FindLongEdges(list<pLEdge>& LongEdges);
  192. /**
  193. * Init pos value for each node using order.
  194. */
  195. void InitPos(Ordering* order);
  196. /**
  197. * Check node in order data
  198. */
  199. void CheckOrder(Ordering* order);
  200. /**
  201. * Init coordiates for each node
  202. */
  203. void InitCoordinates(Ordering* order,
  204. int normalwide = 15,
  205. int dummywide = 5,
  206. int vertical_size = 40);
  207. void FinalCoordinates_vertical_align(Ordering* order, int dir);
  208. /**
  209. * set nodes at final (x,y) position in the graph
  210. */
  211. void FinalCoordinates(Ordering* order);
  212. /**
  213. * The weighted median heuristic for reducing edge crossings.
  214. * At each rank a vertex is assigned a median based on the adjacent
  215. * vertices on the previous rank. Then, the vertices in the rank are
  216. * sorted by their medians. returns true if did not changed.
  217. */
  218. bool WeightedMedianHeuristic(int iter, bool verbose, bool usebary);
  219. /**
  220. * The transposition heuristic for reducing edge crossings.
  221. * Transpose repeatedly exchanges adjacent vertices on the
  222. * same rank if this decreases the number of crossings.
  223. */
  224. void Transpose(unsigned long int maxtry, bool verbose);
  225. /**
  226. * Calculate all edges crossings in the whole graph.
  227. */
  228. int countCrossing(Ordering* order);
  229. /**
  230. * Calculate all crossings between rank an rank+1.
  231. */
  232. int countCrossingOnRank(Ordering* order, int rank);
  233. /**
  234. * Calculate all edges crossings in the whole graph.
  235. */
  236. int FinalcountCrossing(Ordering* order);
  237. /**
  238. * Calculate all crossings between rank an rank+1.
  239. */
  240. int FinalcountCrossingOnRank(Ordering* order, int rank);
  241. /**
  242. * The initial (horizontal)ordering.
  243. * Should be called after InitRank().
  244. */
  245. vector<vector<pLNode>> InitOrder();
  246. /**
  247. * init edge crossing info
  248. */
  249. vector<int> InitOrder2();
  250. /**
  251. * find node with id
  252. */
  253. virtual pLNode FindNode (int num);
  254. /**
  255. * find edge with id
  256. */
  257. virtual pLEdge FindEdge (int num);
  258. virtual void FinalCoordinates_init_medians(Ordering* order);
  259. /**
  260. * get list of all nodes in graph including dummy nodes
  261. */
  262. list<pLNode>* nodes_list()
  263. {
  264. return &m_nodes_list;
  265. }
  266. /**
  267. * get list of edges in graph without self-edges
  268. */
  269. list<pLEdge>* edges_list()
  270. {
  271. return &m_edges_list;
  272. }
  273. /**
  274. * get list of self-edges in graph
  275. */
  276. list<pLEdge>* selfedges_list()
  277. {
  278. return &m_selfedges_list;
  279. }
  280. /**
  281. * get uniq graph id if set by user or 0
  282. */
  283. int id() { return m_id; }
  284. /**
  285. * get max. node id possible
  286. */
  287. int maxnodeid () { return next_node_id; }
  288. /**
  289. * get max. edge id possible
  290. */
  291. int maxedgeid () { return next_edge_id; }
  292. /**
  293. * get number of real nodes in graph without dummy nodes
  294. */
  295. int nrealnodes() { return (m_total_nodes_num - m_total_dummynodes_num); }
  296. /**
  297. * get number of nodes in graph including dummy nodes
  298. */
  299. int nnodes() { return m_total_nodes_num; }
  300. /**
  301. * get number of dummy nodes in graph
  302. */
  303. int ndummynodes() { return m_total_dummynodes_num; }
  304. /**
  305. * get number of edges in graph without self-edges
  306. */
  307. int nedges() { return m_total_edges_num; }
  308. /**
  309. * get number of horizontal edges in graph without self-edges
  310. */
  311. int nhoredges() { return m_total_horedges_num; }
  312. /**
  313. * set number of horizontal edges in graph without self-edges
  314. */
  315. void Set_Nhoredges(int value) { m_total_horedges_num = value; }
  316. /**
  317. * get number of reversed edges in graph without self-edges
  318. */
  319. int nreversededges() { return m_total_reversededges_num; }
  320. /**
  321. * get number of self-edges in graph
  322. */
  323. int nselfedges() { return m_total_selfedges_num; }
  324. /**
  325. * get x spacing
  326. */
  327. int xspacing() { return m_xspacing_num; }
  328. /**
  329. * set x spacing
  330. */
  331. void Setxspacing(int value ) { m_xspacing_num = value; }
  332. /**
  333. * get y spacing
  334. */
  335. int yspacing() { return m_yspacing_num; }
  336. /**
  337. * set y spacing
  338. */
  339. void Setyspacing(int value ) { m_yspacing_num = value; }
  340. /**
  341. * Checks the integrity of data of the graph
  342. * \return tru if data is oke
  343. */
  344. virtual bool Verify();
  345. /**
  346. * run dfs to assing node dfs number
  347. */
  348. void DFS(pLNode node,
  349. map<pLNode, bool>* isused,
  350. map<pLNode, int>* dfs,
  351. int* num);
  352. /**
  353. * Search reverse edges and puts them into ReverseEdges list.
  354. * returns true if reversed edges increased
  355. */
  356. virtual bool FindReverseEdges(list<pLEdge>& ReverseEdges);
  357. /**
  358. * Add a new node to the graph
  359. * Allocate memory, create new node and insert it to nodes list of the graph
  360. * \sa AddEdge, DeleteNode, DeleteEdge
  361. */
  362. pLNode AddNode();
  363. /**
  364. * add node as dummy node
  365. */
  366. virtual void AddDummyNode();
  367. /**
  368. * Connect nodes from and to
  369. * Allocate needed memory, create new edge and insert it to
  370. * edges lists of nodes from and to, and to edges list of graph
  371. * \param from - start node of the edge
  372. * \param to - end node of the edge
  373. * \sa AddNode, DeleteNode, DeleteEdge
  374. */
  375. virtual pLEdge AddEdge(pLNode from, pLNode to, void *e);
  376. /**
  377. * Delete node from the graph and all its incoming and outgoing edges,
  378. * and free memory, allocated for node
  379. * \param node - node to be deleted
  380. * \sa AddNode, AddEdge, DeleteEdge
  381. */
  382. virtual void DeleteNode(pLNode node);
  383. /**
  384. * Delete edge between nodes from and to, and free allocated memory
  385. * \param from - node from
  386. * \param to - node to
  387. * \sa AddNode, AddEdge, DeleteNode
  388. * \return True if edge from->to had existed, False otherwise.
  389. */
  390. virtual bool DeleteEdge(pLNode from, pLNode to);
  391. /**
  392. * Free memory used for Node p
  393. * \param pNode pointer to free
  394. */
  395. virtual void FreeNode(pLNode p);
  396. /**
  397. * Free memory used for Edge p
  398. * \param pEdge pointer to free
  399. */
  400. virtual void FreeEdge(pLEdge p);
  401. /**
  402. * set uniq id number of graph
  403. * \param int graph number
  404. */
  405. virtual void Setid (int id);
  406. /**
  407. * Dump all nodes and edges
  408. */
  409. virtual void Dump();
  410. /**
  411. * Delete all nodes and edges
  412. */
  413. virtual void Destroy();
  414. friend class LEdge;
  415. friend class LNode;
  416. friend class Ordering;
  417. };
  418. #endif
  419. /* end. */