Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. /*
  2. * The Union Find implementation using up-trees.
  3. */
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <stdbool.h>
  8. #include "lib/unionfind.h"
  9. #include "lib/graph.h"
  10. #include "lib/pixel.h"
  11. #include <math.h>
  12.  
  13. int min(int p1, int p2) {
  14. if (p1>p2)
  15. return p2;
  16. return p1;
  17. }
  18.  
  19. int max(int p1, int p2) {
  20. if (p1>p2)
  21. return p1;
  22. return p2;
  23. }
  24. /*pixelID root(int x, int y, pixelID parentID[ROWS][COLS]) {
  25. while ()
  26. } */
  27.  
  28. /* Find and return the index of the root of the pixel with pixelID idx. */
  29. pixelID up_trees_find(pixelID parentID[ROWS][COLS], unsigned int w, pixelID idx) {
  30. int x = get_x_coord(idx,w);
  31. int y = get_y_coord(idx,w);
  32. if (parentID[y][x] == -1)
  33. return get_pixel_id(x,y,idx);
  34. printf("%d\n",parentID[y][x] );
  35. int cx = get_x_coord(parentID[y][x],w);
  36. int cy = get_y_coord(parentID[y][x],w);
  37. return up_trees_find(parentID, w, get_pixel_id(cx,cy,w));
  38. }
  39.  
  40. /* Merge the two groups to which pixel p1 and pixel p2 belong. */
  41. void up_trees_union(pixelID parentID[ROWS][COLS], unsigned int w, pixelID p1, pixelID p2) {
  42. pixelID f1 = min(p1,p2);
  43. pixelID f2 = max(p1,p2);
  44. f1 = up_trees_find(parentID, w, f1);
  45. f2 = up_trees_find(parentID, w, f2);
  46. int x = get_x_coord(f2,w);
  47. int y = get_y_coord(f2,w);
  48. parentID[y][x] = f1;
  49. }
  50.  
  51. /* Store forest of up-trees in the array parentID, given the graph G. */
  52. void up_trees_new(graph G, pixelID parentID[ROWS][COLS]) {
  53. for (unsigned y =0; y<(*G).image_height; y++){
  54. for (unsigned x =0; x<(*G).image_width; x++) {
  55. parentID[y][x] = -1;
  56. }
  57. }
  58. }
  59.  
  60. /*
  61. * Run UNION-FIND, and store the final forest of up-trees in array parentID,
  62. * where count is a boolean flag indicating whether to print out the count.
  63. */
  64. void run_union_find(graph G, pixelID parentID[ROWS][COLS], bool count) {
  65. int calls = 0;
  66. //test and debug up trees find
  67. int g = (*G).image_height;
  68. for (unsigned y = 0; y < (*G).image_height; y++) {
  69. for (unsigned x = 0; x < (*G).image_width; x++) {
  70. if (((x+1<(*G).image_width) && (equal_colors((*G).pixels[y][x],(*G).pixels[y][x+1])))) {
  71. if (up_trees_find(parentID, (*G).image_width , get_pixel_id(x,y,g)) != up_trees_find(parentID, (*G).image_width, get_pixel_id(x+1,y,g) )) {
  72. up_trees_union(parentID, (*G).image_width, get_pixel_id(x,y,g), get_pixel_id(x+1,y,g));
  73. calls++;
  74. }
  75. }
  76. if ( (y+1<(*G).image_height) && (equal_colors((*G).pixels[y][x],(*G).pixels[y+1][x]))) {
  77. if (up_trees_find(parentID, (*G).image_width ,get_pixel_id(x,y,g)) != up_trees_find(parentID, (*G).image_width, get_pixel_id(x,y+1,g)) ) {
  78. up_trees_union(parentID, (*G).image_width, get_pixel_id(x,y,g), get_pixel_id(x,y+1,g));
  79. calls++;
  80. }
  81. }
  82. }
  83. }
  84. if (count == true)
  85. printf("The number of times that the method UNION was called for this image is: %d\n",calls );
  86. return;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement