Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <glib.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <limits.h>
- #include <time.h>
- #define min(a, b) (((a) < (b)) ? (a) : (b));
- typedef struct
- {
- int weight;
- char *city_a;
- char *city_b;
- } Edge;
- char *getcwd(char *buf, size_t size);
- int compare_edges(const void *city_a, const void *city_b);
- void print_edge(char *city);
- void parse(Edge edges[]);
- void run_kruskal(Edge edges[], GPtrArray *result);
- char *subString (const char *input, int offset, int len, char *dest);
- void sort(Edge list[], int length);
- GHashTable *is_in_group(GPtrArray *groups, char *city, int elements_in_groups);
- void hash_table_contains(gpointer data, gpointer user_data);
- int main(int argc, char **argv)
- {
- Edge *edges = malloc(8130 * sizeof (Edge)); // 8257 - 128 = 8129 edges
- parse(edges);
- sort(edges, 8128);
- GPtrArray *min_tree = g_ptr_array_new();
- run_kruskal(edges, min_tree);
- /*for (int l = 0; l < 8128; l++)
- {
- puts(edges[l].city_a);
- puts(edges[l].city_b);
- printf("Weight: %d \n", edges[l].weight);
- }*/
- /*GList *list = NULL;
- list = g_list_append(list, "Hello world!");
- printf("The first item is '%s'\n", g_list_first(list)->data);
- */
- return 0;
- }
- gboolean compare_strings(gconstpointer a, gconstpointer b){
- gboolean true = true;
- gboolean false = false;
- if(strcmp(a,b) == 0){
- return true;
- } else {
- return false;
- }
- }
- /*runs kruskal's algorithm on @param edges and stores
- a minimal spanning tree in @param min_tree*/
- void run_kruskal(Edge edges[], GPtrArray *result)
- {
- int elements_in_groups = 0;
- GPtrArray *groups = g_ptr_array_new();
- for (int i = 0; i < 1; i++)
- {
- char *city_a = edges[0].city_a;
- char *city_b = edges[1000].city_b;
- puts("Added:");
- puts(city_a);
- puts(city_b);
- puts("----");
- // Check if city A and B are already in a group
- GHashTable *t1 = g_hash_table_new (NULL, compare_strings);
- g_hash_table_add(t1, city_a);
- g_ptr_array_add (groups, t1);
- elements_in_groups++;
- GHashTable *t2 = g_hash_table_new (NULL, compare_strings);
- g_hash_table_add(t2, city_b);
- g_ptr_array_add (groups, t2);
- elements_in_groups++;
- GHashTable *group_A = is_in_group(groups, city_a, elements_in_groups);
- GHashTable *group_B = is_in_group(groups, city_b, elements_in_groups);
- //group_A
- }
- }
- GHashTable *is_in_group(GPtrArray *groups, char *city, int elements_in_groups)
- {
- puts("is_in_group called");
- GHashTable *temp_set = NULL;
- for (int i = 0; i < elements_in_groups; i++)
- {
- temp_set = g_ptr_array_index(groups, i);
- if(g_hash_table_contains(temp_set, city)){
- printf("Found: %s\n",city );
- return temp_set;
- }
- }
- printf("City: %s not found\n", city);
- return temp_set;
- }
- int compare_edges(const void *a, const void *b)
- {
- Edge *e1 = (Edge *)a;
- Edge *e2 = (Edge *)b;
- return e1 -> weight > e2->weight ? 1 : -1;
- }
- void sort(Edge list[], int length)
- {
- qsort(list, length, sizeof(Edge), &compare_edges);
- }
- // Syracuse--"Springfield, MO" [1114]
- void parse(Edge edges[])
- {
- char working_directory[1024];
- if (getcwd(working_directory, sizeof(working_directory)) != NULL)
- {
- //fprintf(stdout, "Current working dir: %s\n", working_directory);
- }
- else
- {
- //perror("getcwd() error");
- }
- static const char filename[] = "/USA-highway-miles.in.txt";
- char *full_path;
- full_path = malloc(strlen(working_directory) + strlen(filename) + 1);
- full_path[0] = '\0';
- strcat(full_path, working_directory);
- strcat(full_path, filename);
- //printf("%s\n", full_path);
- FILE *file = fopen(full_path, "r");
- if (file != NULL)
- {
- char line[110]; /* or other suitable maximum line size */
- /*Skip until we see flag "TOKEN"*/
- while (fgets(line, sizeof line, file) != NULL
- && strstr(line, "TOKEN") == NULL)
- {
- }
- /* read the input */
- int index = 0;
- int letter_index = 0;
- char *temp1; //City A
- char *temp2; //Weight
- char *temp3; // City B
- while (fgets(line, sizeof line, file) != NULL
- && strstr(line, "EOF") == NULL)
- {
- line[strlen(line) - 2] = 0; // remove ' ] '
- temp1 = strtok(line, "[");
- temp2 = strtok(NULL, "["); //temp2 = weight
- if (line[strlen(line) - 2] == '"') //if "City" remove last ' " '
- {
- line[strlen(line) - 2] = '\0';
- temp1 = strtok(line, "--");
- temp3 = strtok(NULL, "--");
- temp3++;
- }
- else
- {
- line[strlen(line) - 1] = '\0';
- temp1 = strtok(line, "--");
- temp3 = strtok(NULL, "--");
- }
- if (temp1[0] == '"')
- {
- temp1++; //Increase pointer one, will point to the next char after ' " '
- line[strlen(line) - 1] = '\0'; // Changes second " ' " to \0
- }
- //We now need to make new copies of the pointers,
- //since those pointers all point to the same array, which will be modified next loop
- char *temp_copy;
- temp_copy = malloc(sizeof(char) * strlen(temp2));
- strcpy(temp_copy, temp2);
- edges[index].weight = atoi(temp_copy);
- temp_copy = malloc(sizeof(char) * strlen(temp1));
- strcpy(temp_copy, temp1);
- edges[index].city_a = temp_copy;
- temp_copy = malloc(sizeof(char) * strlen(temp3));
- strcpy(temp_copy, temp3);
- edges[index].city_b = temp_copy;
- index ++;
- }
- fclose(file);
- }
- else
- {
- perror(filename);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement