rawgraph.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at https://graphviz.org
  9. *************************************************************************/
  10. #include <cgraph/list.h>
  11. #include <ortho/rawgraph.h>
  12. #include <stdbool.h>
  13. #include <stddef.h>
  14. #include <util/alloc.h>
  15. #define UNSCANNED 0
  16. #define SCANNING 1
  17. #define SCANNED 2
  18. rawgraph *make_graph(size_t n) {
  19. rawgraph* g = gv_alloc(sizeof(rawgraph));
  20. g->nvs = n;
  21. g->vertices = gv_calloc(n, sizeof(vertex));
  22. for(size_t i = 0; i < n; ++i) {
  23. g->vertices[i].color = UNSCANNED;
  24. }
  25. return g;
  26. }
  27. void
  28. free_graph(rawgraph* g)
  29. {
  30. for(size_t i = 0; i < g->nvs; ++i)
  31. adj_list_free(&g->vertices[i].adj_list);
  32. free (g->vertices);
  33. free (g);
  34. }
  35. void insert_edge(rawgraph *g, size_t v1, size_t v2) {
  36. if (!edge_exists(g, v1, v2)) {
  37. adj_list_append(&g->vertices[v1].adj_list, v2);
  38. }
  39. }
  40. void remove_redge(rawgraph *g, size_t v1, size_t v2) {
  41. adj_list_remove(&g->vertices[v1].adj_list, v2);
  42. adj_list_remove(&g->vertices[v2].adj_list, v1);
  43. }
  44. static bool zeq(size_t a, size_t b) {
  45. return a == b;
  46. }
  47. bool edge_exists(rawgraph *g, size_t v1, size_t v2) {
  48. return adj_list_contains(&g->vertices[v1].adj_list, v2, zeq);
  49. }
  50. DEFINE_LIST(int_stack, size_t)
  51. static int DFS_visit(rawgraph *g, size_t v, int time, int_stack_t *sp) {
  52. vertex* vp;
  53. vp = g->vertices + v;
  54. vp->color = SCANNING;
  55. const adj_list_t adj = vp->adj_list;
  56. time = time + 1;
  57. for (size_t i = 0; i < adj_list_size(&adj); ++i) {
  58. const size_t id = adj_list_get(&adj, i);
  59. if(g->vertices[id].color == UNSCANNED)
  60. time = DFS_visit(g, id, time, sp);
  61. }
  62. vp->color = SCANNED;
  63. int_stack_push_back(sp, v);
  64. return time + 1;
  65. }
  66. void
  67. top_sort(rawgraph* g)
  68. {
  69. int time = 0;
  70. int count = 0;
  71. if (g->nvs == 0) return;
  72. if (g->nvs == 1) {
  73. g->vertices[0].topsort_order = count;
  74. return;
  75. }
  76. int_stack_t sp = {0};
  77. int_stack_reserve(&sp, g->nvs);
  78. for(size_t i = 0; i < g->nvs; ++i) {
  79. if(g->vertices[i].color == UNSCANNED)
  80. time = DFS_visit(g, i, time, &sp);
  81. }
  82. while (!int_stack_is_empty(&sp)) {
  83. const size_t v = int_stack_pop_back(&sp);
  84. g->vertices[v].topsort_order = count;
  85. count++;
  86. }
  87. int_stack_free(&sp);
  88. }