|
| 1 | + |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <stdbool.h> |
| 5 | +#include <string.h> |
| 6 | +#include <assert.h> |
| 7 | +#include "../src/graph.h" |
| 8 | + |
| 9 | + |
| 10 | +typedef struct city { |
| 11 | + char* name; |
| 12 | + double latitude; |
| 13 | + double longitude; |
| 14 | +} city; |
| 15 | + |
| 16 | +typedef struct highway { |
| 17 | + int miles; |
| 18 | +} highway; |
| 19 | + |
| 20 | + |
| 21 | +/* Private Functions */ |
| 22 | +static void __add_city_as_vertex(graph_t g, char* name, double latitude, double longitude); /* we get to always make N and W.*/ |
| 23 | +static void __print_city(city* c); |
| 24 | +static void __free_city(city* c); |
| 25 | +static void __add_highway_as_edge(graph_t g, int src, int dest, int miles); |
| 26 | +static char* __str_duplicate(const char* s); |
| 27 | + |
| 28 | +unsigned int* __breadth_first_search(graph_t g, vertex_t start); |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | +int main(int argc, char const *argv[]) { |
| 33 | + bool verbose = false; |
| 34 | + |
| 35 | + if (argc == 2 && strcmp(argv[1], "-v") == 0) { |
| 36 | + verbose = true; |
| 37 | + } |
| 38 | + |
| 39 | + graph_t g = g_init(); |
| 40 | + vertex_t v; |
| 41 | + unsigned int i; |
| 42 | + |
| 43 | + /* add the vertices */ |
| 44 | + __add_city_as_vertex(g, "New York", 40.7128, 74.0060); |
| 45 | + __add_city_as_vertex(g, "Washington D.C.", 38.9072, 77.0369); |
| 46 | + __add_city_as_vertex(g, "Cleveland", 41.4993, 81.6944); |
| 47 | + __add_city_as_vertex(g, "Detroit", 42.3314, 83.0458); |
| 48 | + __add_city_as_vertex(g, "Atlanta", 33.7490, 84.3880); |
| 49 | + __add_city_as_vertex(g, "St. Louis", 38.6270, 90.1994); |
| 50 | + __add_city_as_vertex(g, "Dallas", 32.7767, 96.7970); |
| 51 | + __add_city_as_vertex(g, "Salt Lake", 40.7608, 111.8910); |
| 52 | + __add_city_as_vertex(g, "Pheonix", 33.4484, 112.0740); |
| 53 | + __add_city_as_vertex(g, "Las Vegas", 36.1699, 115.1398); |
| 54 | + __add_city_as_vertex(g, "Los Angeles", 34.0522, 118.2437); |
| 55 | + __add_city_as_vertex(g, "San Fransisco", 37.7749, 122.4194); |
| 56 | + |
| 57 | + /* add edges between the vertices */ |
| 58 | + __add_highway_as_edge(g, 0, 1, 227); /* NY - DC */ |
| 59 | + __add_highway_as_edge(g, 1, 0, 227); /* DC - NY */ |
| 60 | + __add_highway_as_edge(g, 1, 2, 371); /* DC - Cleveland */ |
| 61 | + __add_highway_as_edge(g, 1, 4, 639); /* DC - Atlanta */ |
| 62 | + __add_highway_as_edge(g, 4, 1, 639); /* Atlanta - DC */ |
| 63 | + __add_highway_as_edge(g, 2, 3, 168); /* Cleveland - Detroit */ |
| 64 | + __add_highway_as_edge(g, 2, 4, 557); /* Cleveland - St. Louis */ |
| 65 | + __add_highway_as_edge(g, 4, 5, 630); /* St. Louis - Dallas */ |
| 66 | + __add_highway_as_edge(g, 5, 7, 1064); /* Dallas - Pheonix */ |
| 67 | + __add_highway_as_edge(g, 7, 8, 300); /* Pheonix - LV */ |
| 68 | + __add_highway_as_edge(g, 7, 9, 372); /* Pheonix - LA */ |
| 69 | + __add_highway_as_edge(g, 9, 7, 372); /* LA - Pheonix */ |
| 70 | + __add_highway_as_edge(g, 9, 10, 381); /* LA - SF */ |
| 71 | + __add_highway_as_edge(g, 8, 6, 420); /* LV - SLC */ |
| 72 | + __add_highway_as_edge(g, 6, 10, 735); /* SLC - SF */ |
| 73 | + |
| 74 | + printf("Breadth First Search; starting at New York: \n"); |
| 75 | + unsigned int* bfs = __breadth_first_search(g, g_vertex_get(g, 0)); |
| 76 | + for (i = 0; i < g_vertices_inserted(g); i++) { |
| 77 | + printf("%d, ", bfs[i]); |
| 78 | + } |
| 79 | + printf("\n"); |
| 80 | + |
| 81 | + for (i = 0; i < g_vertices_inserted(g); i++) { |
| 82 | + v = g_vertex_get(g, bfs[i]); |
| 83 | + city* c = g_vertex_metadata(v); |
| 84 | + __print_city(c); |
| 85 | + } |
| 86 | + |
| 87 | + /* free things */ |
| 88 | + g_iterate_vertices(g, v, i) { |
| 89 | + city* c = g_vertex_metadata(v); |
| 90 | + /*__print_city(c); */ |
| 91 | + __free_city(c); |
| 92 | + g_vertex_metadata_update(v, NULL); |
| 93 | + } |
| 94 | + g_free(g); |
| 95 | + free(bfs); |
| 96 | + |
| 97 | + if (verbose == true) |
| 98 | + printf("Completed!\n"); |
| 99 | + |
| 100 | + return 0; |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +/* PRIVATE FUNCTIONS */ |
| 105 | +static void __add_city_as_vertex(graph_t g, char* name, double latitude, double longitude) { |
| 106 | + city* c = calloc(1, sizeof(city)); |
| 107 | + |
| 108 | + c->name = __str_duplicate(name); |
| 109 | + c->latitude = latitude; |
| 110 | + c->longitude = longitude; |
| 111 | + g_vertex_add(g, c); |
| 112 | +} |
| 113 | + |
| 114 | +static void __print_city(city* c) { |
| 115 | + printf("City Name: %s\tLatitude: %f°N\tLongitude: %f°W\n", c->name, c->latitude, c->longitude); |
| 116 | +} |
| 117 | + |
| 118 | +static void __free_city(city* c) { |
| 119 | + free(c->name); |
| 120 | + c->latitude = 0.0; |
| 121 | + c->longitude = 0.0; |
| 122 | + free(c); |
| 123 | +} |
| 124 | + |
| 125 | +static void __add_highway_as_edge(graph_t g, int src, int dest, int miles) { |
| 126 | + highway* h = calloc(1, sizeof(highway)); |
| 127 | + h->miles = miles; |
| 128 | + g_edge_add(g, src, dest, h); |
| 129 | +} |
| 130 | + |
| 131 | +static char* __str_duplicate(const char* s) { |
| 132 | + size_t len = strlen(s); |
| 133 | + char* buf = malloc((len + 1) * sizeof(char)); |
| 134 | + if (buf == NULL) |
| 135 | + return NULL; |
| 136 | + strcpy(buf, s); |
| 137 | + buf[len] = '\0'; |
| 138 | + return buf; |
| 139 | +} |
| 140 | + |
| 141 | + |
| 142 | +#define SET_BIT(A,k) (A[((k) / 8)] |= (1 << ((k) % 8))) |
| 143 | +#define CHECK_BIT(A, k) (A[((k) / 8)] & (1 << ((k) % 8))) |
| 144 | + |
| 145 | +unsigned int* __breadth_first_search(graph_t g, vertex_t start) { |
| 146 | + unsigned int* ret = calloc(g_vertices_inserted(g), sizeof(unsigned int)); |
| 147 | + |
| 148 | + /* Use a bitarray to track which nodes we have visited |
| 149 | + NOTE: this is not always correct, but it will work since it is larger! */ |
| 150 | + char* bitarray = calloc(g_vertices_inserted(g) / 8 + 1, sizeof(char)); |
| 151 | + unsigned int id = g_vertex_id(start); |
| 152 | + SET_BIT(bitarray, id); |
| 153 | + |
| 154 | + int cur_pos = 0; |
| 155 | + int pos = 0; |
| 156 | + ret[pos++] = id; |
| 157 | + |
| 158 | + edge_t e; |
| 159 | + unsigned int i; |
| 160 | + while (cur_pos != pos) { |
| 161 | + vertex_t v = g_vertex_get(g, ret[cur_pos]); |
| 162 | + g_iterate_edges(v, e, i) { |
| 163 | + id = g_edge_dest(e); |
| 164 | + if (CHECK_BIT(bitarray, id) != 0) |
| 165 | + continue; /* already visited */ |
| 166 | + SET_BIT(bitarray, id); |
| 167 | + ret[pos++] = id; |
| 168 | + } |
| 169 | + cur_pos++; |
| 170 | + } |
| 171 | + |
| 172 | + free(bitarray); |
| 173 | + return ret; |
| 174 | +} |
0 commit comments