dglib.dox 12 KB

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