Advertisement
Guest User

Untitled

a guest
Apr 2nd, 2017
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.82 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4. #include <string.h>
  5.  
  6. #include "graph.h"
  7.  
  8. /* helper function prototypes */
  9.  
  10. // create a new vertex with a specific label
  11. Vertex* new_vertex(const char* name);
  12.  
  13. // create a new w-weighted edge from vertex id u to vertex id v
  14. Edge* new_edge(int u, int v, int w);
  15.  
  16. // destroy a vertex, including its label and all of its edges
  17. void free_vertex(Vertex* vertex);
  18.  
  19.  
  20. /* function definitions */
  21.  
  22. // create a new, empty graph, with space for n vertices
  23. Graph* new_graph(int n) {
  24.     Graph* graph = malloc(sizeof (*graph));
  25.     assert(graph);
  26.    
  27.     graph->n = 0;
  28.     graph->maxn = n;
  29.    
  30.     graph->vertices = malloc(n * sizeof (Vertex*));
  31.     assert(graph->vertices);
  32.    
  33.     return graph;
  34. }
  35.  
  36. // create a new vertex with a specific label
  37. Vertex* new_vertex(const char* label) {
  38.     assert(label);
  39.  
  40.     Vertex* vertex = malloc(sizeof (*vertex));
  41.     assert(vertex);
  42.  
  43.     // make sure to copy the label across
  44.     vertex->label = malloc((1 + strlen(label)) * sizeof (char));
  45.     assert(vertex->label);
  46.     strcpy(vertex->label, label);
  47.  
  48.     vertex->first_edge = NULL;
  49.  
  50.     return vertex;
  51. }
  52.  
  53. // create a new w-weighted edge from vertex id u to vertex id v
  54. Edge* new_edge(int u, int v, int w) {
  55.     Edge* edge = malloc(sizeof (*edge));
  56.     assert(edge);
  57.  
  58.     edge->u = u;
  59.     edge->v = v;
  60.     edge->weight = w;
  61.  
  62.     edge->next_edge = NULL;
  63.  
  64.     return edge;
  65. }
  66.  
  67. // destroy a graph, its vertices, and their edges
  68. void free_graph(Graph* graph) {
  69.     if (graph) {
  70.         int i;
  71.         for (i = 0; i < graph->n; i++) {
  72.             free_vertex(graph->vertices[i]);
  73.         }
  74.  
  75.         free(graph->vertices);
  76.         free(graph);   
  77.     }
  78. }
  79.  
  80. // destroy a vertex, including its label and all of its edges
  81. void free_vertex(Vertex* vertex) {
  82.     if (vertex) {
  83.         while (vertex->first_edge) {
  84.             Edge* edge = vertex->first_edge;
  85.             vertex->first_edge = vertex->first_edge->next_edge;
  86.             free(edge);
  87.         }
  88.         free(vertex->label);
  89.         free(vertex);  
  90.     }
  91. }
  92.  
  93. // add a new vertex with label 'name' to a graph
  94. void graph_add_vertex(Graph* graph, const char* name) {
  95.     if (graph->n < graph->maxn) {
  96.         graph->vertices[graph->n] = new_vertex(name);  
  97.         graph->n++;
  98.     } else {
  99.         fprintf(stderr, "hey! adding new vertex to full graph\n");
  100.     }
  101. }
  102.  
  103. // add an undirected edge between u and v with weight w to graph
  104. void graph_add_u_edge(Graph* graph, int u, int v, int w) {
  105.     // an undirected edge is just two directed edges
  106.     graph_add_d_edge(graph, u, v, w);
  107.     graph_add_d_edge(graph, v, u, w);
  108. }
  109.  
  110. // add a directed edge from u to v with weight w to a graph
  111. void graph_add_d_edge(Graph* graph, int u, int v, int w) {
  112.     if(u < graph->n && u >= 0 && v < graph->n && v >= 0) {
  113.         Edge* edge = new_edge(u, v, w);
  114.         edge->next_edge = graph->vertices[u]->first_edge;
  115.         graph->vertices[u]->first_edge = edge;
  116.     } else {
  117.         fprintf(stderr, "hey! adding edge between non-existant vertices\n");
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement