graph_matrix.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*!
  2. Temelia - Graph interface implementation with adjacency matrix
  3. Copyright (C) 2008, 2009 Ceata (http://cod.ceata.org/proiecte/temelia)
  4. @author Dascalu Laurentiu
  5. This program is free software; you can redistribute it and
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 3
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. */
  17. #ifndef GRAPH_MATRIX_H_
  18. #define GRAPH_MATRIX_H_
  19. #include "platform.h"
  20. #include "vertex.h"
  21. #include "edge.h"
  22. /*!
  23. * @brief Singleton containing structure filled with corresponding
  24. * pointers to functions.
  25. */
  26. DECLSPEC graph_implementation_t graph_matrix_get_implementation();
  27. /*!
  28. * @brief Returns new empty graph represented with adjacency matrix.
  29. * @param Number of vertices
  30. */
  31. DECLSPEC void *graph_matrix_new(int size);
  32. /*!
  33. * @brief Frees the memory occupied by this graph representation.
  34. * Complexity O(1)
  35. *
  36. * @param Graph representation reference
  37. */
  38. DECLSPEC void graph_matrix_delete(void *graph);
  39. /*!
  40. * @brief Resizes current graph representation to new size.
  41. * Complexity O(V * V)
  42. *
  43. * @param Graph representation reference
  44. * @param New size - should be greater than current size
  45. */
  46. DECLSPEC void graph_matrix_resize(void *graph, int size);
  47. /*!
  48. * @brief Adds new edge to adjacency matrix; edge is identified
  49. * by vertices and attributes.
  50. * Complexity O(1)
  51. *
  52. * @param Graph representation reference
  53. * @param Edge
  54. */
  55. DECLSPEC void graph_matrix_add_edge(void *graph, unsigned int src, unsigned int dest,
  56. edge_t edge);
  57. /*!
  58. * @brief Removes an edge identified by vertices and returns
  59. * the reference.
  60. * Complexity O(1)
  61. *
  62. * @param Graph representation reference
  63. * @param Vertex 1
  64. * @param Vertex 2
  65. * @return Edge between vertex 1 and vertex 2
  66. */
  67. DECLSPEC edge_t graph_matrix_remove_edge(void *graph, unsigned int vertex1,
  68. unsigned int vertex2);
  69. /*
  70. * @see graph_matrix_remove_edge
  71. */
  72. DECLSPEC edge_t graph_matrix_get_edge(void *graph, unsigned int vertex1,
  73. unsigned int vertex2);
  74. /*!
  75. * @brief Returns the first neighbor vertex of given vertex.
  76. * Context is set up because of the iteration property.
  77. * Complexity O(V)
  78. *
  79. * @param Graph representation reference
  80. * @param Vertex
  81. * @param Context reference, where iteration information is stored.
  82. * In this implementation, you must pass a valid (unsigned int *)
  83. */
  84. DECLSPEC vertex_t graph_matrix_first_vertex(void *graph, unsigned int vertex,
  85. void **context);
  86. /*!
  87. * @brief Deletes allocated context for iterating, by graph_matrix_first_vertex.
  88. * Notice that it is automatically called when the iterating process is over
  89. *
  90. * @param Context
  91. */
  92. DECLSPEC void graph_matrix_delete_vertex_context(void **context);
  93. /*!
  94. * @brief Returns the next neighbor vertex of given vertex.
  95. * Context is used to find out the last visited neighbor.
  96. * Complexity O(V)
  97. *
  98. * @param Graph representation reference
  99. * @param Vertex
  100. * @param Context reference, where iteration information is stored.
  101. */
  102. DECLSPEC vertex_t graph_matrix_next_vertex(void *graph, unsigned int vertex,
  103. void **context);
  104. /*!
  105. * @brief Returns the first edge with a neighbor vertex of given vertex.
  106. * Context is set up because of the iteration property.
  107. * Complexity O(V)
  108. *
  109. * @param Graph representation reference
  110. * @param Vertex
  111. * @param Context reference, where iteration information is stored.
  112. * In this implementation, you must pass a valid (unsigned int *)
  113. */
  114. DECLSPEC edge_t graph_matrix_first_edge(void *graph, unsigned int vertex, void **context);
  115. /*!
  116. * @brief Deletes allocated context for iterating, by graph_matrix_first_edge.
  117. * Notice that it is automatically called when the iterating process is over
  118. *
  119. * @param Context
  120. */
  121. DECLSPEC void graph_matrix_delete_edge_context(void **context);
  122. /*!
  123. * @brief Returns the edge to the next neighbor.
  124. * Context is used to find out the last visited edge.
  125. * Complexity O(V)
  126. *
  127. * @param Graph representation reference
  128. * @param Vertex
  129. * @param Context reference, where iteration information is stored.
  130. */
  131. DECLSPEC edge_t graph_matrix_next_edge(void *graph, unsigned int vertex,
  132. void **context);
  133. /*!
  134. * @brief Iterates over the edges of current graph and calls
  135. * the callback function for-each record.
  136. *
  137. * Complexity O(V*V)
  138. *
  139. * If you want to pretty debug the graph, then see the examples
  140. * in temelia_samples. Basically, it uses a static variable to
  141. * count current edge id and when id is equal to the number of vertices
  142. * it emits a newline and resets id to 0.
  143. *
  144. * @param Graph
  145. * @param Iterating callback function
  146. */
  147. DECLSPEC void graph_matrix_iterate(void *graph, void(*iterate)(vertex_t src,
  148. vertex_t dest, double cost, char *label, void *key, char type));
  149. /*!
  150. * @brief Iterates over the edge of graph.
  151. * Complexity O(V*V)
  152. *
  153. * @see graph_matrix_iterate
  154. */
  155. DECLSPEC void graph_matrix_iterate_edges(void *graph, void edge_handler(edge_t e,
  156. void *context), void *context);
  157. /*!
  158. * @brief Transposes graph.
  159. * Complexity O(V*V)
  160. *
  161. * @param Graph
  162. */
  163. DECLSPEC void graph_matrix_transpose(void *graph);
  164. /*!
  165. * @brief Computes degree, in degree and out degree of graph.
  166. * Complexity O(V)
  167. *
  168. * @param Graph
  169. * @param Vertex identifier
  170. */
  171. DECLSPEC unsigned int graph_matrix_vertex_degree(void *graph, unsigned int vertex);
  172. DECLSPEC unsigned int graph_matrix_vertex_in_degree(void *graph, unsigned int vertex);
  173. DECLSPEC unsigned int graph_matrix_vertex_out_degree(void *graph, unsigned int vertex);
  174. /*!
  175. * @brief Computes graph's dimension - number of edges.
  176. * Complexity O(V*V)
  177. *
  178. * @param Graph
  179. */
  180. DECLSPEC unsigned int graph_matrix_dimension(void *graph);
  181. /*!
  182. * @brief Removes and deletes the edges from/in vertex.
  183. * Complexity O(V)
  184. *
  185. * @param Graph
  186. * @param Vertex identifier
  187. * @param Edge destructor
  188. */
  189. DECLSPEC void graph_matrix_delete_vertex_edges(void *graph, unsigned int vertex,
  190. void(*edge_delete)(edge_t edge));
  191. #endif /* GRAPH_MATRIX_H_ */