Advertisement
Guest User

SO

a guest
Nov 4th, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.09 KB | None | 0 0
  1. #include <glib.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <limits.h>
  7. #include <time.h>
  8.  
  9. #define min(a, b) (((a) < (b)) ? (a) : (b));
  10.  
  11.  
  12. typedef struct
  13. {
  14.     int weight;
  15.     char *city_a;
  16.     char *city_b;
  17. } Edge;
  18.  
  19. char *getcwd(char *buf, size_t size);
  20. int compare_edges(const void *city_a, const  void *city_b);
  21. void print_edge(char *city);
  22. void parse(Edge edges[]);
  23. void run_kruskal(Edge edges[], GPtrArray *result);
  24. char *subString (const char *input, int offset, int len, char *dest);
  25. void sort(Edge list[], int length);
  26. GHashTable *is_in_group(GPtrArray *groups, char *city, int elements_in_groups);
  27. void hash_table_contains(gpointer data, gpointer user_data);
  28.  
  29.  
  30.  
  31. int main(int argc, char **argv)
  32. {
  33.     Edge *edges = malloc(8130 * sizeof (Edge)); // 8257 - 128 = 8129 edges
  34.     parse(edges);
  35.     sort(edges, 8128);
  36.     GPtrArray *min_tree = g_ptr_array_new();
  37.     run_kruskal(edges, min_tree);
  38.  
  39.  
  40.     /*for (int l = 0; l < 8128; l++)
  41.     {
  42.         puts(edges[l].city_a);
  43.         puts(edges[l].city_b);
  44.         printf("Weight: %d \n", edges[l].weight);
  45.  
  46.     }*/
  47.  
  48.     /*GList *list = NULL;
  49.     list = g_list_append(list, "Hello world!");
  50.     printf("The first item is '%s'\n", g_list_first(list)->data);
  51.     */
  52.     return 0;
  53. }
  54.  
  55. gboolean compare_strings(gconstpointer a, gconstpointer b){
  56.     gboolean true = true;
  57.     gboolean false = false;
  58.     if(strcmp(a,b) == 0){
  59.         return true;
  60.     } else {
  61.         return false;
  62.     }
  63. }
  64.  
  65.  
  66. /*runs kruskal's algorithm on @param edges and stores
  67.  a minimal spanning tree in @param min_tree*/
  68. void run_kruskal(Edge edges[], GPtrArray *result)
  69. {
  70.     int elements_in_groups = 0;
  71.     GPtrArray *groups = g_ptr_array_new();
  72.  
  73.     for (int i = 0; i < 1; i++)
  74.     {
  75.         char *city_a = edges[0].city_a;
  76.         char *city_b = edges[1000].city_b;
  77.         puts("Added:");
  78.         puts(city_a);
  79.         puts(city_b);
  80.         puts("----");
  81.  
  82.         // Check if city A and B are already in a group
  83.         GHashTable *t1 = g_hash_table_new (NULL, compare_strings);
  84.         g_hash_table_add(t1, city_a);
  85.         g_ptr_array_add (groups, t1);
  86.         elements_in_groups++;
  87.         GHashTable *t2 = g_hash_table_new (NULL, compare_strings);
  88.         g_hash_table_add(t2, city_b);
  89.  
  90.         g_ptr_array_add (groups, t2);
  91.  
  92.         elements_in_groups++;
  93.         GHashTable *group_A = is_in_group(groups, city_a, elements_in_groups);
  94.  
  95.         GHashTable *group_B = is_in_group(groups, city_b, elements_in_groups);
  96.  
  97.        
  98.  
  99.         //group_A
  100.        
  101.     }
  102.  
  103.  
  104.  
  105.  
  106. }
  107.  
  108.  
  109. GHashTable *is_in_group(GPtrArray *groups, char *city, int elements_in_groups)
  110. {
  111.     puts("is_in_group called");
  112.     GHashTable *temp_set = NULL;
  113.  
  114.     for (int i = 0; i < elements_in_groups; i++)
  115.     {
  116.  
  117.         temp_set = g_ptr_array_index(groups, i);
  118.         if(g_hash_table_contains(temp_set, city)){
  119.             printf("Found: %s\n",city );
  120.             return temp_set;
  121.  
  122.         }
  123.     }
  124.     printf("City: %s not found\n", city);
  125.     return temp_set;
  126. }
  127.  
  128.  
  129.  
  130.  
  131. int compare_edges(const void *a, const void *b)
  132. {
  133.     Edge *e1 = (Edge *)a;
  134.     Edge *e2 = (Edge *)b;
  135.     return e1 -> weight > e2->weight ? 1 : -1;
  136. }
  137. void sort(Edge list[], int length)
  138. {
  139.     qsort(list, length, sizeof(Edge), &compare_edges);
  140. }
  141. // Syracuse--"Springfield, MO" [1114]
  142. void parse(Edge edges[])
  143. {
  144.     char working_directory[1024];
  145.     if (getcwd(working_directory, sizeof(working_directory)) != NULL)
  146.     {
  147.         //fprintf(stdout, "Current working dir: %s\n", working_directory);
  148.     }
  149.     else
  150.     {
  151.         //perror("getcwd() error");
  152.     }
  153.  
  154.     static const char filename[] = "/USA-highway-miles.in.txt";
  155.  
  156.     char *full_path;
  157.     full_path = malloc(strlen(working_directory) + strlen(filename) + 1);
  158.     full_path[0] = '\0';
  159.     strcat(full_path, working_directory);
  160.     strcat(full_path, filename);
  161.     //printf("%s\n", full_path);
  162.  
  163.     FILE *file = fopen(full_path, "r");
  164.     if (file != NULL)
  165.     {
  166.  
  167.         char line[110]; /* or other suitable maximum line size */
  168.  
  169.         /*Skip until we see flag "TOKEN"*/
  170.         while (fgets(line, sizeof line, file) != NULL
  171.                 && strstr(line, "TOKEN") == NULL)
  172.         {
  173.  
  174.  
  175.         }
  176.         /* read the input */
  177.         int index = 0;
  178.         int letter_index = 0;
  179.         char *temp1; //City A
  180.         char *temp2; //Weight
  181.         char *temp3; // City B
  182.  
  183.         while (fgets(line, sizeof line, file) != NULL
  184.                 && strstr(line, "EOF") == NULL)
  185.         {
  186.  
  187.             line[strlen(line) - 2] = 0; // remove ' ] '
  188.             temp1 = strtok(line, "[");
  189.             temp2 = strtok(NULL, "["); //temp2 = weight
  190.             if (line[strlen(line) - 2] == '"') //if "City" remove last ' " '
  191.             {
  192.                 line[strlen(line) - 2] = '\0';
  193.                 temp1 = strtok(line, "--");
  194.                 temp3 = strtok(NULL, "--");
  195.                 temp3++;
  196.             }
  197.             else
  198.             {
  199.                 line[strlen(line) - 1] = '\0';
  200.                 temp1 = strtok(line, "--");
  201.                 temp3 = strtok(NULL, "--");
  202.  
  203.             }
  204.             if (temp1[0] == '"')
  205.             {
  206.                 temp1++; //Increase pointer one, will point to the next char after ' " '
  207.                 line[strlen(line) - 1] = '\0'; // Changes second " ' " to \0
  208.             }
  209.  
  210.             //We now need to make new copies of the pointers,
  211.             //since those pointers all point to the same array, which will be modified next loop
  212.             char *temp_copy;
  213.             temp_copy = malloc(sizeof(char) * strlen(temp2));
  214.             strcpy(temp_copy, temp2);
  215.             edges[index].weight = atoi(temp_copy);
  216.             temp_copy = malloc(sizeof(char) * strlen(temp1));
  217.             strcpy(temp_copy, temp1);
  218.             edges[index].city_a = temp_copy;
  219.             temp_copy = malloc(sizeof(char) * strlen(temp3));
  220.             strcpy(temp_copy, temp3);
  221.             edges[index].city_b = temp_copy;
  222.             index ++;
  223.  
  224.  
  225.         }
  226.         fclose(file);
  227.  
  228.  
  229.     }
  230.     else
  231.     {
  232.         perror(filename);
  233.     }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement