spacepaste

  1.  
  2. /*graph.h*/
  3. typedef enum {UNDIRECTED=0,DIRECTED} graph_type_e;
  4. /* Adjacency list node*/
  5. typedef struct adjlist_node
  6. {
  7. int vertex; /*Index to adjacency list array*/
  8. struct adjlist_node *next; /*Pointer to the next node*/
  9. }adjlist_node_t, *adjlist_node_p;
  10. /* Adjacency list */
  11. typedef struct adjlist
  12. {
  13. int num_members; /*number of members in the list (for future use)*/
  14. adjlist_node_t *head; /*head of the adjacency linked list*/
  15. }adjlist_t, *adjlist_p;
  16. /* Graph structure. A graph is an array of adjacency lists.
  17. Size of array will be number of vertices in graph*/
  18. typedef struct graph
  19. {
  20. graph_type_e type; /*Directed or undirected graph */
  21. int num_vertices; /*Number of vertices*/
  22. adjlist_p adjListArr; /*Adjacency lists' array*/
  23. }graph_t, *graph_p;
  24. /* Exit function to handle fatal errors*/
  25. void err_exit(char* msg)
  26. {
  27. printf("[Fatal Error]: %s \nExiting...\n", msg);
  28. exit(1);
  29. }
  30. /*graph.c*/
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. // #include "graph.h"
  34. /* Function to create an adjacency list node*/
  35. adjlist_node_p createNode(int v)
  36. {
  37. adjlist_node_p newNode = (adjlist_node_p)malloc(sizeof(adjlist_node_t));
  38. if(!newNode)
  39. err_exit("Unable to allocate memory for new node");
  40. newNode->vertex = v;
  41. newNode->next = NULL;
  42. return newNode;
  43. }
  44. /* Function to create a graph with n vertices; Creates both directed and undirected graphs*/
  45. graph_p createGraph(int n, graph_type_e type)
  46. {
  47. int i;
  48. graph_p graph = (graph_p)malloc(sizeof(graph_t));
  49. if(!graph)
  50. err_exit("Unable to allocate memory for graph");
  51. graph->num_vertices = n;
  52. graph->type = type;
  53. /* Create an array of adjacency lists*/
  54. graph->adjListArr = (adjlist_p)malloc(n * sizeof(adjlist_t));
  55. if(!graph->adjListArr)
  56. err_exit("Unable to allocate memory for adjacency list array");
  57. for(i = 0; i < n; i++)
  58. {
  59. graph->adjListArr[i].head = NULL;
  60. graph->adjListArr[i].num_members = 0;
  61. }
  62. return graph;
  63. }
  64. /*Destroys the graph*/
  65. void destroyGraph(graph_p graph)
  66. {
  67. if(graph)
  68. {
  69. if(graph->adjListArr)
  70. {
  71. int v;
  72. /*Free up the nodes*/
  73. for (v = 0; v < graph->num_vertices; v++)
  74. {
  75. adjlist_node_p adjListPtr = graph->adjListArr[v].head;
  76. while (adjListPtr)
  77. {
  78. adjlist_node_p tmp = adjListPtr;
  79. adjListPtr = adjListPtr->next;
  80. free(tmp);
  81. }
  82. }
  83. /*Free the adjacency list array*/
  84. free(graph->adjListArr);
  85. }
  86. /*Free the graph*/
  87. free(graph);
  88. }
  89. }
  90. /* Adds an edge to a graph*/
  91. void addEdge(graph_t *graph, int src, int dest)
  92. {
  93. /* Add an edge from src to dst in the adjacency list*/
  94. adjlist_node_p newNode = createNode(dest);
  95. newNode->next = graph->adjListArr[src].head;
  96. graph->adjListArr[src].head = newNode;
  97. graph->adjListArr[src].num_members++;
  98. if(graph->type == UNDIRECTED)
  99. {
  100. /* Add an edge from dest to src also*/
  101. newNode = createNode(src);
  102. newNode->next = graph->adjListArr[dest].head;
  103. graph->adjListArr[dest].head = newNode;
  104. graph->adjListArr[dest].num_members++;
  105. }
  106. }
  107. /* Function to print the adjacency list of graph*/
  108. void displayGraph(graph_p graph)
  109. {
  110. int i;
  111. for (i = 0; i < graph->num_vertices; i++)
  112. {
  113. adjlist_node_p adjListPtr = graph->adjListArr[i].head;
  114. printf("\n%d: ", i);
  115. while (adjListPtr)
  116. {
  117. printf("%d->", adjListPtr->vertex);
  118. adjListPtr = adjListPtr->next;
  119. }
  120. printf("NULL\n");
  121. }
  122. }
  123. int main()
  124. {
  125. graph_p undir_graph = createGraph(5, UNDIRECTED);
  126. graph_p dir_graph = createGraph(5, DIRECTED);
  127. addEdge(undir_graph, 0, 1);
  128. addEdge(undir_graph, 0, 4);
  129. addEdge(undir_graph, 1, 2);
  130. addEdge(undir_graph, 1, 3);
  131. addEdge(undir_graph, 1, 4);
  132. addEdge(undir_graph, 2, 3);
  133. addEdge(undir_graph, 3, 4);
  134. addEdge(dir_graph, 0, 1);
  135. addEdge(dir_graph, 0, 4);
  136. addEdge(dir_graph, 1, 2);
  137. addEdge(dir_graph, 1, 3);
  138. addEdge(dir_graph, 1, 4);
  139. addEdge(dir_graph, 2, 3);
  140. addEdge(dir_graph, 3, 4);
  141. printf("\nUNDIRECTED GRAPH");
  142. displayGraph(undir_graph);
  143. destroyGraph(undir_graph);
  144. printf("\nDIRECTED GRAPH");
  145. displayGraph(dir_graph);
  146. destroyGraph(dir_graph);
  147. return 0;
  148. }
  149.