graph_sparse_matrix.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*!
  2. Temelia - Sparse matrix implementation structure graph
  3. Copyright (C) 2008 Ceata (http://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. #include "include/graph.h"
  18. #include "include/graph_sparse_matrix.h"
  19. #include "include/sparse_matrix.h"
  20. #include <stdlib.h>
  21. struct _graph_sparse_matrix_t
  22. {
  23. int size;
  24. sparse_matrix_t sparse_matrix;
  25. };
  26. typedef struct _graph_sparse_matrix_t *graph_sparse_matrix_t;
  27. void *graph_sparse_matrix_new(int size)
  28. {
  29. graph_sparse_matrix_t graph;
  30. LOGGER("[graph sparse matrix new] size %d\n", size);
  31. _ASSERT(size, <=, 0, NULL_POINTER, NULL);
  32. graph = (graph_sparse_matrix_t) _new(sizeof(struct _graph_sparse_matrix_t));
  33. _ASSERT(graph, ==, NULL, NULL_POINTER, NULL);
  34. graph->size = size;
  35. graph->sparse_matrix = sparse_matrix_new(size, size, size * size,
  36. SPARSE_MATRIX_COLUMN_DENSE | SPARSE_MATRIX_ROW_DENSE);
  37. if (graph->sparse_matrix == NULL)
  38. {
  39. _delete(graph);
  40. return NULL;
  41. }
  42. return graph;
  43. }
  44. void graph_sparse_matrix_delete(void *graph)
  45. {
  46. graph_sparse_matrix_t m_graph = (graph_sparse_matrix_t) graph;
  47. _ASSERT(m_graph, ==, NULL, NULL_POINTER, );
  48. LOGGER("[graph sparse matrix delete] graph %p, matrix %p\n", graph,
  49. m_graph->sparse_matrix);
  50. sparse_matrix_delete(m_graph->sparse_matrix);
  51. _delete(m_graph);
  52. }
  53. void graph_sparse_matrix_resize(void *graph, int size)
  54. {
  55. graph_sparse_matrix_t m_graph = (graph_sparse_matrix_t) graph;
  56. LOGGER("[graph sparse matrix resize] graph %p, new size %d\n", graph, size);
  57. _ASSERT(m_graph, ==, NULL, NULL_POINTER,);
  58. sparse_matrix_resize(m_graph->sparse_matrix, size, size, size * size);
  59. }
  60. void graph_sparse_matrix_add_edge(void *graph, unsigned int src,
  61. unsigned int dest, edge_t edge)
  62. {
  63. graph_sparse_matrix_t m_graph = (graph_sparse_matrix_t) graph;
  64. LOGGER(
  65. "[graph sparse matrix add edge] graph %p, source %u, destination %u, edge %p\n",
  66. graph, src, dest, edge);
  67. _ASSERT(graph, ==, NULL, NULL_POINTER,);
  68. _ASSERT(edge, ==, NULL, NULL_POINTER,);
  69. _ASSERT(src, >=, (unsigned int) m_graph->size, INVALID_INPUT,);
  70. _ASSERT(dest, >=, (unsigned int) m_graph->size, INVALID_INPUT,);
  71. sparse_matrix_set_key_at(m_graph->sparse_matrix, src, dest, edge);
  72. }
  73. edge_t graph_sparse_matrix_remove_edge(void *graph, unsigned int vertex1,
  74. unsigned int vertex2)
  75. {
  76. graph_sparse_matrix_t m_graph = (graph_sparse_matrix_t) graph;
  77. edge_t edge;
  78. LOGGER(
  79. "[graph sparse matrix remove edge] graph %p, source %u, destination %u\n",
  80. graph, vertex1, vertex2);
  81. _ASSERT(graph, ==, NULL, NULL_POINTER, NULL);
  82. _ASSERT(vertex1, >=, (unsigned int) m_graph->size, INVALID_INPUT, NULL);
  83. _ASSERT(vertex2, >=, (unsigned int) m_graph->size, INVALID_INPUT, NULL);
  84. edge
  85. = sparse_matrix_get_key_at(m_graph->sparse_matrix, vertex1,
  86. vertex2, 0);
  87. sparse_matrix_set_key_at(m_graph->sparse_matrix, vertex1, vertex2, NULL);
  88. return edge;
  89. }
  90. edge_t graph_sparse_matrix_get_edge(void *graph, unsigned int vertex1,
  91. unsigned int vertex2)
  92. {
  93. graph_sparse_matrix_t m_graph;
  94. LOGGER(
  95. "[graph sparse matrix get edge] graph %p, source %u, destination %u\n",
  96. graph, vertex1, vertex2);
  97. m_graph = (graph_sparse_matrix_t) graph;
  98. _ASSERT(graph, ==, NULL, NULL_POINTER, NULL);
  99. _ASSERT(vertex1, >=, (unsigned int) m_graph->size, INVALID_INPUT, NULL);
  100. _ASSERT(vertex2, >=, (unsigned int) m_graph->size, INVALID_INPUT, NULL);
  101. return sparse_matrix_get_key_at(m_graph->sparse_matrix, vertex1, vertex2, 0);
  102. }
  103. vertex_t graph_sparse_matrix_first_vertex(void *graph, unsigned int vertex,
  104. void **context)
  105. {
  106. graph_sparse_matrix_t m_graph = (graph_sparse_matrix_t) graph;
  107. sparse_matrix_t m_matrix;
  108. unsigned int i, size, *init;
  109. edge_t edge;
  110. LOGGER(
  111. "[graph sparse matrix first vertex] graph %p, vertex %u, context %p\n",
  112. graph, vertex, context);
  113. _ASSERT(graph, ==, NULL, INVALID_INPUT, NULL);
  114. m_matrix = (sparse_matrix_t) m_graph->sparse_matrix;
  115. size = m_graph->size;
  116. init = _new(sizeof(unsigned int));
  117. for (i = 0; i < size; i++)
  118. {
  119. edge = sparse_matrix_get_key_at(m_matrix, vertex, i, 1);
  120. if (edge)
  121. {
  122. *init = i;
  123. *((unsigned int **) context) = init;
  124. return edge_get_vertex2(edge);
  125. }
  126. }
  127. _delete(init);
  128. return NULL;
  129. }
  130. void graph_sparse_matrix_delete_vertex_context(void **context)
  131. {
  132. unsigned int **m_context = (unsigned int **) context;
  133. _ASSERT(context, ==, NULL, NULL_POINTER, );
  134. _delete(*m_context);
  135. }
  136. vertex_t graph_sparse_matrix_next_vertex(void *graph, unsigned int vertex,
  137. void **context)
  138. {
  139. graph_sparse_matrix_t m_graph = (graph_sparse_matrix_t) graph;
  140. edge_t m_edge;
  141. unsigned int it;
  142. LOGGER(
  143. "[graph sparse matrix next vertex] graph %p, vertex %u, context %p\n",
  144. graph, vertex, context);
  145. _ASSERT(graph, ==, NULL, NULL_POINTER, NULL);
  146. _ASSERT(vertex, >=, (unsigned int) m_graph->size, NULL_POINTER, NULL);
  147. _ASSERT(context, ==, NULL, NULL_POINTER, NULL);
  148. it = **(unsigned int **) context;
  149. _ASSERT(it, >= , (unsigned int) m_graph->size, INVALID_INPUT, NULL);
  150. while (++it < (unsigned int) m_graph->size)
  151. {
  152. m_edge
  153. = sparse_matrix_get_key_at(m_graph->sparse_matrix, vertex, it,
  154. 1);
  155. if (m_edge)
  156. {
  157. **(unsigned int **) context = it;
  158. return edge_get_vertex2(m_edge);
  159. }
  160. }
  161. _delete(*(unsigned int **) context);
  162. return NULL;
  163. }
  164. edge_t graph_sparse_matrix_first_edge(void *graph, unsigned int vertex,
  165. void **context)
  166. {
  167. graph_sparse_matrix_t m_graph = (graph_sparse_matrix_t) graph;
  168. sparse_matrix_t m_matrix;
  169. edge_t edge;
  170. unsigned int i, size, *init;
  171. LOGGER(
  172. "[graph sparse matrix first edge] graph %p, vertex %u, context %p\n",
  173. graph, vertex, context);
  174. _ASSERT(graph, ==, NULL, INVALID_INPUT, NULL);
  175. m_matrix = (sparse_matrix_t) m_graph->sparse_matrix;
  176. size = m_graph->size;
  177. init = _new(sizeof(unsigned int));
  178. for (i = 0; i < size; i++)
  179. {
  180. edge = sparse_matrix_get_key_at(m_matrix, vertex, i, 1);
  181. if (edge)
  182. {
  183. *init = i;
  184. *((unsigned int **) context) = init;
  185. return edge;
  186. }
  187. }
  188. _delete(init);
  189. return NULL;
  190. }
  191. void graph_sparse_matrix_delete_edge_context(void **context)
  192. {
  193. unsigned int **m_context = (unsigned int **) context;
  194. _ASSERT(context, ==, NULL, NULL_POINTER, );
  195. _delete(*m_context);
  196. }
  197. edge_t graph_sparse_matrix_next_edge(void *graph, unsigned int vertex,
  198. void **context)
  199. {
  200. graph_sparse_matrix_t m_graph = (graph_sparse_matrix_t) graph;
  201. edge_t m_edge;
  202. unsigned int it;
  203. LOGGER("[graph sparse matrix next edge] graph %p, vertex %u, context %p\n",
  204. graph, vertex, context);
  205. _ASSERT(graph, ==, NULL, NULL_POINTER, NULL);
  206. _ASSERT(vertex, >=, (unsigned int) m_graph->size, NULL_POINTER, NULL);
  207. _ASSERT(context, ==, NULL, NULL_POINTER, NULL);
  208. it = **(unsigned int **) context;
  209. _ASSERT(it, >= , (unsigned int) m_graph->size, INVALID_INPUT, NULL);
  210. while (++it < (unsigned int) m_graph->size)
  211. {
  212. m_edge
  213. = sparse_matrix_get_key_at(m_graph->sparse_matrix, vertex, it,
  214. 1);
  215. if (m_edge)
  216. {
  217. **(unsigned int **) context = it;
  218. return m_edge;
  219. }
  220. }
  221. _delete(*(unsigned int **) context);
  222. return NULL;
  223. }
  224. void graph_sparse_matrix_iterate(void *graph, void(*iterate)(vertex_t src,
  225. vertex_t dest, double cost, char *label, void *key, int type))
  226. {
  227. int i, j, n;
  228. graph_sparse_matrix_t m_graph = (graph_sparse_matrix_t) graph;
  229. edge_t m_edge;
  230. LOGGER("[graph sparse matrix iterate] graph %p, iterating function %p\n",
  231. graph, iterate);
  232. _ASSERT(graph, ==, NULL, NULL_POINTER,);
  233. n = m_graph->size;
  234. for (i = 0; i < n; i++)
  235. {
  236. for (j = 0; j < n; j++)
  237. {
  238. m_edge = (edge_t) sparse_matrix_get_key_at(m_graph->sparse_matrix,
  239. i, j, 0);
  240. if (m_edge)
  241. {
  242. iterate(edge_get_vertex1(m_edge), edge_get_vertex2(m_edge),
  243. edge_get_cost(m_edge), edge_get_label(m_edge),
  244. edge_get_key(m_edge), edge_get_type(m_edge));
  245. }
  246. }
  247. }
  248. }
  249. void graph_sparse_matrix_iterate_edges(void *graph, void edge_handler(edge_t e,
  250. void *context), void *context)
  251. {
  252. int i, j, n;
  253. graph_sparse_matrix_t m_graph = (graph_sparse_matrix_t) graph;
  254. edge_t m_edge;
  255. LOGGER(
  256. "[graph sparse matrix iterate edges] graph %p, iterating function %p, context %p\n",
  257. graph, edge_handler, context);
  258. _ASSERT(graph, ==, NULL, NULL_POINTER,);
  259. n = m_graph->size;
  260. for (i = 0; i < n; i++)
  261. {
  262. for (j = 0; j < n; j++)
  263. {
  264. m_edge = (edge_t) sparse_matrix_get_key_at(m_graph->sparse_matrix,
  265. i, j, 0);
  266. if (m_edge)
  267. edge_handler(m_edge, context);
  268. }
  269. }
  270. }
  271. void graph_sparse_matrix_transpose(void *graph)
  272. {
  273. int i, j, n;
  274. graph_sparse_matrix_t m_graph = (graph_sparse_matrix_t) graph;
  275. edge_t m_edge1, m_edge2;
  276. LOGGER("[graph sparse matrix transpose] graph %p\n", graph);
  277. _ASSERT(graph, ==, NULL, NULL_POINTER,);
  278. n = m_graph->size;
  279. for (i = 0; i < n; i++)
  280. {
  281. for (j = i + 1; j < n; j++)
  282. {
  283. // First edge
  284. m_edge1 = (edge_t) sparse_matrix_get_key_at(m_graph->sparse_matrix,
  285. i, j, 0);
  286. edge_switch_vertices(m_edge1);
  287. // Second edge
  288. m_edge2 = (edge_t) sparse_matrix_get_key_at(m_graph->sparse_matrix,
  289. j, i, 0);
  290. edge_switch_vertices(m_edge2);
  291. // Swap operation
  292. sparse_matrix_set_key_at(m_graph->sparse_matrix, i, j, m_edge2);
  293. sparse_matrix_set_key_at(m_graph->sparse_matrix, j, i, m_edge1);
  294. }
  295. }
  296. }
  297. unsigned int graph_sparse_matrix_vertex_degree(void *graph, unsigned int vertex)
  298. {
  299. int i, n, degree;
  300. graph_sparse_matrix_t m_graph = (graph_sparse_matrix_t) graph;
  301. LOGGER("[graph sparse matrix vertex degree] graph %p, vertex %u\n", graph,
  302. vertex);
  303. _ASSERT(graph, ==, NULL, NULL_POINTER, -1);
  304. n = m_graph->size;
  305. degree = 0;
  306. for (i = 0; i < n; i++)
  307. {
  308. if (sparse_matrix_get_key_at(m_graph->sparse_matrix, vertex, i, 0))
  309. degree++;
  310. if (sparse_matrix_get_key_at(m_graph->sparse_matrix, i, vertex, 0))
  311. degree++;
  312. }
  313. return degree;
  314. }
  315. unsigned int graph_sparse_matrix_vertex_in_degree(void *graph,
  316. unsigned int vertex)
  317. {
  318. int i, n, in_degree;
  319. graph_sparse_matrix_t m_graph = (graph_sparse_matrix_t) graph;
  320. _ASSERT(graph, ==, NULL, NULL_POINTER, -1);
  321. n = m_graph->size;
  322. in_degree = 0;
  323. for (i = 0; i < n; i++)
  324. {
  325. if (sparse_matrix_get_key_at(m_graph->sparse_matrix, i, vertex, 0))
  326. in_degree++;
  327. }
  328. return in_degree;
  329. }
  330. unsigned int graph_sparse_matrix_vertex_out_degree(void *graph,
  331. unsigned int vertex)
  332. {
  333. int i, n, out_degree;
  334. graph_sparse_matrix_t m_graph = (graph_sparse_matrix_t) graph;
  335. _ASSERT(graph, ==, NULL, NULL_POINTER, -1);
  336. n = m_graph->size;
  337. out_degree = 0;
  338. for (i = 0; i < n; i++)
  339. {
  340. if (sparse_matrix_get_key_at(m_graph->sparse_matrix, vertex, i, 0))
  341. out_degree++;
  342. }
  343. return out_degree;
  344. }
  345. unsigned int graph_sparse_matrix_dimension(void *graph)
  346. {
  347. int i, j, n, dimension;
  348. graph_sparse_matrix_t m_graph = (graph_sparse_matrix_t) graph;
  349. _ASSERT(graph, ==, NULL, NULL_POINTER, -1);
  350. n = m_graph->size;
  351. dimension = 0;
  352. for (i = 0; i < n; i++)
  353. {
  354. for (j = 0; j < n; j++)
  355. {
  356. if (sparse_matrix_get_key_at(m_graph->sparse_matrix, i, j, 0))
  357. dimension++;
  358. }
  359. }
  360. return dimension;
  361. }
  362. void graph_sparse_matrix_delete_vertex_edges(void *graph, unsigned int vertex,
  363. void(*edge_delete)(edge_t edge))
  364. {
  365. int i, n;
  366. graph_sparse_matrix_t m_graph = (graph_sparse_matrix_t) graph;
  367. _ASSERT(graph, ==, NULL, NULL_POINTER,);
  368. _ASSERT(vertex, >=, (unsigned int) m_graph->size, NULL_POINTER,);
  369. n = m_graph->size;
  370. for (i = 0; i < n; i++)
  371. {
  372. edge_delete(sparse_matrix_get_key_at(m_graph->sparse_matrix, i, vertex,
  373. 0));
  374. sparse_matrix_set_key_at(m_graph->sparse_matrix, i, vertex, NULL);
  375. edge_delete(sparse_matrix_get_key_at(m_graph->sparse_matrix, vertex, i,
  376. 0));
  377. sparse_matrix_set_key_at(m_graph->sparse_matrix, vertex, i, NULL);
  378. }
  379. }
  380. void graph_sparse_matrix_load(void *graph_src, void *graph_dest)
  381. {
  382. graph_sparse_matrix_t m_graph_src = (graph_sparse_matrix_t) graph_src;
  383. graph_sparse_matrix_t m_graph_dest = (graph_sparse_matrix_t) graph_dest;
  384. int i, j, size;
  385. _ASSERT(graph_src, ==, NULL, NULL_POINTER,);
  386. _ASSERT(graph_dest, ==, NULL, NULL_POINTER,);
  387. _ASSERT(m_graph_src->size, != , m_graph_dest->size, INVALID_INPUT,);
  388. size = m_graph_src->size;
  389. for (i = 0; i < size; i++)
  390. {
  391. for (j = 0; j < size; j++)
  392. sparse_matrix_set_key_at(m_graph_dest->sparse_matrix, i, j,
  393. sparse_matrix_get_key_at(m_graph_src->sparse_matrix, i, j,
  394. 0));
  395. }
  396. }
  397. void *graph_sparse_matrix_store(void *graph_src)
  398. {
  399. graph_sparse_matrix_t m_graph = (graph_sparse_matrix_t) graph_src;
  400. graph_sparse_matrix_t m_clone;
  401. _ASSERT(graph_src, ==, NULL, NULL_POINTER, NULL);
  402. m_clone = (graph_sparse_matrix_t) _new(
  403. sizeof(struct _graph_sparse_matrix_t));
  404. m_clone->size = m_graph->size;
  405. m_clone->sparse_matrix = sparse_matrix_clone(m_graph->sparse_matrix);
  406. return m_clone;
  407. }
  408. void graph_sparse_matrix_debug(void *graph)
  409. {
  410. }
  411. graph_implementation_t graph_sparse_matrix_get_implementation()
  412. {
  413. static struct _graph_implementation_t
  414. _graph_adjacency_sparse_matrix_implementation;
  415. _graph_adjacency_sparse_matrix_implementation.new_g = graph_sparse_matrix_new;
  416. _graph_adjacency_sparse_matrix_implementation.delete_g
  417. = graph_sparse_matrix_delete;
  418. _graph_adjacency_sparse_matrix_implementation.resize
  419. = graph_sparse_matrix_resize;
  420. _graph_adjacency_sparse_matrix_implementation.add_edge
  421. = graph_sparse_matrix_add_edge;
  422. _graph_adjacency_sparse_matrix_implementation.remove_edge
  423. = graph_sparse_matrix_remove_edge;
  424. _graph_adjacency_sparse_matrix_implementation.get_edge
  425. = graph_sparse_matrix_get_edge;
  426. _graph_adjacency_sparse_matrix_implementation.first_vertex
  427. = graph_sparse_matrix_first_vertex;
  428. _graph_adjacency_sparse_matrix_implementation.delete_vertex_context
  429. = graph_sparse_matrix_delete_vertex_context;
  430. _graph_adjacency_sparse_matrix_implementation.next_vertex
  431. = graph_sparse_matrix_next_vertex;
  432. _graph_adjacency_sparse_matrix_implementation.first_edge
  433. = graph_sparse_matrix_first_edge;
  434. _graph_adjacency_sparse_matrix_implementation.delete_edge_context
  435. = graph_sparse_matrix_delete_edge_context;
  436. _graph_adjacency_sparse_matrix_implementation.next_edge
  437. = graph_sparse_matrix_next_edge;
  438. _graph_adjacency_sparse_matrix_implementation.iterate_edges
  439. = graph_sparse_matrix_iterate_edges;
  440. _graph_adjacency_sparse_matrix_implementation.transpose
  441. = graph_sparse_matrix_transpose;
  442. _graph_adjacency_sparse_matrix_implementation.vertex_degree
  443. = graph_sparse_matrix_vertex_degree;
  444. _graph_adjacency_sparse_matrix_implementation.vertex_in_degree
  445. = graph_sparse_matrix_vertex_in_degree;
  446. _graph_adjacency_sparse_matrix_implementation.vertex_out_degree
  447. = graph_sparse_matrix_vertex_out_degree;
  448. _graph_adjacency_sparse_matrix_implementation.dimension
  449. = graph_sparse_matrix_dimension;
  450. _graph_adjacency_sparse_matrix_implementation.delete_vertex_edges
  451. = graph_sparse_matrix_delete_vertex_edges;
  452. _graph_adjacency_sparse_matrix_implementation.load
  453. = graph_sparse_matrix_load;
  454. _graph_adjacency_sparse_matrix_implementation.store
  455. = graph_sparse_matrix_store;
  456. _graph_adjacency_sparse_matrix_implementation.debug
  457. = graph_sparse_matrix_debug;
  458. return &_graph_adjacency_sparse_matrix_implementation;
  459. }