dglib.dox 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*! \page dglib GRASS Directed Graph Library
  2. by GRASS Development Team (https://grass.osgeo.org)
  3. \tableofcontents
  4. \section vlibDglibShortIntro Short introduction
  5. The Directed Graph Library or DGLib (Micarelli 2002)
  6. provides functionality for vector network analysis. This library
  7. released under GPL is hosted by the GRASS project (within the GRASS
  8. source code). As a stand-alone library it may also be used by other
  9. software projects.
  10. The Directed Graph Library library provides functionality to assign
  11. costs to lines and/or nodes. That means that costs can be accumulated
  12. while traveling along polylines. The user can assign individual costs
  13. to all lines and/or nodes of a vector map and later calculate least costly
  14. path connections based on the accumulated costs. Applications are
  15. transport analysis, connectivity and more. Implemented applications
  16. cover shortest/fastest path, traveling salesman (round trip), allocation of
  17. sources (creation of subnetworks), minimum Steiner trees (star-like
  18. connections), and iso-distances (from centers).
  19. For details, please read Blazek et al. 2002 (see below).
  20. Related vector functions are:
  21. Vect_graph_add_edge(),
  22. Vect_graph_init(),
  23. Vect_graph_set_node_costs(),
  24. Vect_graph_shortest_path(),
  25. Vect_net_build_graph(),
  26. Vect_net_nearest_nodes(),
  27. Vect_net_shortest_path(), and
  28. Vect_net_shortest_path_coor().
  29. \section intro Introduction
  30. The Directed Graph Library or DGLib (Micarelli 2002) provides
  31. functionality for vector network analysis. This library released under
  32. GPL is hosted by the GRASS project (in the CVS server). As stand-alone
  33. library it may also be used by other software project.
  34. A graph is a system of logical connections between a collection of
  35. objects called vertices. Graphs are usually represented by a picture,
  36. so that each vertex is shown as a point, with the connections shown as
  37. line segments. These vertices are also commonly referred to as nodes,
  38. edges referred to as arcs. A directed graph (digraph) consists of a
  39. finite set of vertices, and a finite set of edges, where an edge is an
  40. ordered pair of vertices. A directed graph has the property that edges
  41. have a direction, this is the reason for defining an edge as an
  42. ordered pair of vertices often referred to as the head and the tail of
  43. the edge.
  44. The original design idea behind DGLib was to support middle sized
  45. graphs in RAM with a near-static structure that doesn't need to be
  46. dynamically modified by the user program; ability to read graphs from
  47. input streams and process them with no needle to rebuild internal
  48. trees. A representation has been defined, where graph data is stored
  49. in 32bit word arrays and each element pointer is converted to a
  50. relative offset. This representation is serializable from/to
  51. input/output streams and allows fast load-and-use processing. Graphs
  52. need to be de-serialized in order to be edited. In further
  53. refactorings the library has evolved to support dynamic changes and
  54. state-independent algorithm (algorithms can be run on both
  55. serializable or editable graphs).
  56. DGLib defines a serializable graph as being in FLAT state and a
  57. editable graph as being in TREE state. The implementation makes
  58. intensive use of libavl (http://www.msu.edu/~pfaffben/avl/) AVL data
  59. structures to support TREE state.
  60. So far DGLib defines three different graph versions, version 1
  61. supports directed graph with a weak concept of the edge, it can
  62. support many applications where one doesn't need to know about the
  63. input edges of a node (in-degree) and where there is no requirement to
  64. directly retrieve edges by their identifier but only by head/tail
  65. combinations. Version 2 adds in-degree support and a true edge
  66. addressing, yet supporting directed graph. Version 3 uses the same
  67. internal representation of version 2 but activates code branches to
  68. support undirected graphs.
  69. The DGLib user can control a number of static features and can attach
  70. a arbitrary amount of data to each node (node-attributes) and each
  71. edge (edge-attributes). Attributes are not considered to be part of
  72. the graph structure and can be edited also when the graph is in FLAT
  73. state.
  74. Graph traversal in neither recursive nor hook (callback) based, but
  75. built on the use of traversers for nodes and edges. By default,
  76. traversal is ordered by node and edge identifiers but can optionally
  77. be ordered by other means. For example, it is often useful to visit
  78. edges on a weight order} basis (greedy algorithm), this is possible
  79. via prioritizers that are activated by setting specific graph options.
  80. Both preemptive (blocking) and non-preemptive
  81. (non-blocking/multiplexed) I/O is supported, although GRASS does not
  82. actually use graph storage it may be easily required by any other
  83. library user. Thread safety is so far ensured by a data separation
  84. design that keeps all application context states into stack
  85. containers, whose life cycle is controlled by the user program. Each
  86. graph is a separate container and two or more graphs never
  87. conflict. In addition algorithms (ie. shortest path) can safely share
  88. the same graph, while concurrent editing on the same graph is unsafe.
  89. As DGLib is under development, only a bunch of polynomial time
  90. algorithms have been implemented, and the basic structure is being
  91. stressed to be a mature core to possibly time wasting
  92. computations. Current algorithms are: shortest path, depth spanning,
  93. and minimum spanning. Spanning algorithms silently behave as
  94. arborescenses when applied to directed graphs. A clip callback
  95. function, optionally supplied by the user, comes called by the library
  96. while traversing the graph in order to alter default algorithm
  97. behavior (i.e. user can control access to specific graph segments
  98. while computing shortest path).
  99. The Directed Graph Library library provides functionality to assign
  100. costs to lines and/or nodes. That means that costs can be accumulated
  101. while traveling along polylines. The user can assign individual costs
  102. to all lines and/or nodes of a vector map and later calculate shortest
  103. path connections based on the accumulated costs. Applications are
  104. transport analysis, connectivity and more.
  105. Text based on:<br>
  106. R. Blazek, M. Neteler, and R. Micarelli. The new GRASS 5.1 vector
  107. architecture. In Open source GIS - GRASS users conference 2002,
  108. Trento, Italy, 11-13 September 2002. University of Trento, Italy,
  109. 2002.
  110. <a href="http://www.ing.unitn.it/%7Egrass/conferences/GRASS2002/proceedings/proceedings/pdfs/Blazek_Radim.pdf">http://www.ing.unitn.it/~grass/conferences/GRASS2002/proceedings/proceedings/pdfs/Blazek_Radim.pdf</a>
  111. \section codeWalkTrough Code Walk Through explanation
  112. Directed graph library is responsible for network analysis operations
  113. done on vector maps. For this purpose directed graph library converts
  114. the vector attributes into form of a graph. This graph structure and
  115. related structures are defined in dglib/graph.h This header file
  116. contains defination of all structers required for storing components
  117. of graph, transversing, accessing and modifying it.
  118. Map Structure Map_info contains a instance of dglGraph_s as one of its
  119. elements named 'graph'. This element is populated call to api
  120. Vect_net_build_graph(). This API takes several arguments as defined in
  121. lib/vector/Vlib/net.c (see \ref vectorlib).
  122. Following structure declares dglGraph_s, main datastructure for storing graph.
  123. \verbatim
  124. typedef struct _dglGraph
  125. {
  126. int iErrno; // used to report what kind of error while operating on it lately.
  127. dglByte_t Version; //graph version 1 2 or 3
  128. dglByte_t Endian; // big or small
  129. dglInt32_t NodeAttrSize; //node attr sze
  130. dglInt32_t EdgeAttrSize; //edge attribute size
  131. dglInt32_t aOpaqueSet[ 16 ];
  132. dglInt32_t cNode; // count of nodes
  133. dglInt32_t cHead; // count of head nodes ( nodes from those edge orignates)
  134. dglInt32_t cTail; // count of tail nodes (nodes at whom edge terimate )
  135. dglInt32_t cAlone; // component nodes ( which are not connected to anyone )
  136. dglInt32_t cEdge; //count of edges
  137. dglInt64_t nnCost; //total cost of edges
  138. dglInt32_t Flags; // flags for graph . This conveys whether graph is of what kind.
  139. dglInt32_t nFamily;
  140. dglInt32_t nOptions;
  141. void * pNodeTree; // pointer to tree of nodes
  142. void * pEdgeTree; //pointer to tree of edges
  143. dglByte_t * pNodeBuffer; //pointer to node buffer. this points contiguous memory location containing pointers to nodes of graph.
  144. dglInt32_t iNodeBuffer; //current index of node being accessed. This is used as offset index from pNodeBuffer
  145. dglByte_t * pEdgeBuffer; //pointer to edge buffer. this points to contiguous memory ,containing pointer to edgesets passing through each node. Here as per i remember means orignating.
  146. dglInt32_t iEdgeBuffer; // offset index counter for edge buffer.
  147. //rest i have not studied yet (NKD).
  148. dglEdgePrioritizer_s edgePrioritizer;
  149. dglNodePrioritizer_s nodePrioritizer;
  150. //so far statistics are only computed by dglAddEdge()
  151. #ifdef DGL_STATS
  152. clock_t clkAddEdge; // cycles spent during the last addedge execution
  153. int cAddEdge; // # of calls to dglAddEdge()
  154. clock_t clkNodeTree; // cycles spent in accessing the node binary tree
  155. int cNodeTree; // # of probes in the node tree
  156. #endif
  157. }
  158. \endverbatim
  159. As mentioned earlier, graphs are stored as FLAT form and for all
  160. operations they are first converted to TREE. In FLAT form,
  161. pnodeBuffer contains all information about nodes. They are stored in
  162. ascending order as per nodeid. pnodeBuffer points to a contiguous
  163. memory area contains pointer to node type structure. This node type
  164. structure is a memory area indexed as
  165. \verbatim
  166. // Node macros - addresses in a flat node ( as defined in dglib/graph_v1.h ,
  167. // similar for version 2)
  168. #define DGL_IN_NODEID_v1 0
  169. #define DGL_IN_STATUS_v1 1
  170. #define DGL_IN_TAIL_OFFSET_v1 2// this gives where edgeset pointer is in pnode
  171. #define DGL_IN_ATTR_v1 3
  172. #define DGL_IN_SIZE_v1 DGL_IN_ATTR_v1
  173. \endverbatim
  174. Similarly pEdgeBuffer is a contiguous memory locations contains
  175. pointer to edgesets passing through each node. These edgesets can be
  176. transversed using dglEdgesetTraverser_s structure defined in
  177. dglib/graph.h .
  178. \verbatim
  179. // Edge macros - addresses in a flat edge (as defined in dglib/graph_v1.h and similar for version 2)
  180. #define DGL_IL_HEAD_OFFSET_v1 0
  181. #define DGL_IL_TAIL_OFFSET_v1 1
  182. #define DGL_IL_COST_v1 2
  183. #define DGL_IL_ID_v1 3
  184. #define DGL_IL_ATTR_v1 4
  185. #define DGL_IL_SIZE_v1 DGL_IL_ATTR_v1
  186. \endverbatim
  187. For any operations on graph, these first need to be converted to TREE
  188. from XXX. Dglib first convert FLAT to AVL trees using libavl. This
  189. operation is called unflattening.
  190. This has been explained earlier that graph may be one of three
  191. versions depending on which operations will be supported. APIs for
  192. these has been diffrentiated from each other through a suffix _v1 and
  193. _v2 . All generic graph operations are defined in dglib/graph.h
  194. . These APIs depending on graph version call respective version
  195. APIs. For version 1 they resolve into *_v1() ones declared and defined
  196. in dglib/graph_v1.h and dglib/graph_v1.c . For version 2, they are is
  197. in dglib/graph_v2.h and dglib/graph_v2.c . For version 3, DOCUMENT
  198. THIS. Operations on various permitives are defined in template files.
  199. <ul>
  200. <li> Node operations : nodemgmt-template.c ->This contains operations like getting, setting a node, finding in and out edgeset from node.
  201. <li> Edge operations: edgemgmt-template.c -> add edge, delete edge, get edge etc.
  202. <li> Spanning tree : span-template.c -> Build the depth-first spanning tree, minimum spanning tree using Prims algorithm
  203. <li> Shortest path : sp-template.c -> it implement Dijkastra's algo using min heap. Algorithm needs to be studied more.
  204. <li> misc operations: misc-template.c -> operations like flattening or unflattening of graph, edgeset transversing, node set transversing etc.
  205. </ul>
  206. All these files contains defination using/for macros.These macro
  207. definations are there to make graph version transparent and provide
  208. uniform interface. Macro declaration happens in v1-defs.h for version 1
  209. and v2-defs.h for version 2 graphs. If you are looking for any API,
  210. if it is all capital, it is a macro. You need to look for its
  211. expansion. This expansion will be true function for that
  212. operation. Just a piece of advice , always keep programmers manual
  213. with you. Just search in it for what you want otherwise one may spend
  214. quite substantial time in finding desired code.
  215. Vlib/level-two.c defines various APIs used for operation done on nodes
  216. and edges . For example finding node coordinates, edges coming from
  217. head nodes. etc.
  218. dglib/example folder contains some good examples which work only on
  219. graph (I mean graph is required to be input). They are easy and almost
  220. self explanatory once you know what API are for.
  221. \section functions Functions
  222. (yet incomplete, all functions need to be doxygenized...)
  223. dglNodeGet_Id()
  224. dglGet_NodeAttrSize()
  225. dglNodeGet_Attr()
  226. dglNodeSet_Attr()
  227. dglInitialize()
  228. dglFlatten()
  229. dglAddEdge()
  230. dglGetNode()
  231. dglShortestPath()
  232. dglShortestDistance()
  233. dglEdgeGet_Id()
  234. dglEdgeGet_Cost()
  235. dglFreeSPReport()
  236. \section vlibReferences References
  237. R. Blazek, M. Neteler, and R. Micarelli. The new GRASS 5.1
  238. vector architecture. In Open source GIS - GRASS users conference 2002,
  239. Trento, Italy, 11-13 September 2002. University of Trento, Italy, 2002.
  240. <a href="http://www.ing.unitn.it/~grass/conferences/GRASS2002/proceedings/proceedings/pdfs/Blazek_Radim.pdf">http://www.ing.unitn.it/~grass/conferences/GRASS2002/proceedings/proceedings/pdfs/Blazek_Radim.pdf</a>
  241. \section contacts Contacts
  242. Roberto Micarelli, Italy<br>
  243. GRASS Development Team<br>
  244. Nitin K Dhiman, AINN group, CAIR <nitinkdhiman@gmail.com> (code walkthrough documentation)
  245. \section seealso See Also
  246. GRASS 6 Vector Architecture \ref vectorlib
  247. Last change: $Date$
  248. */