Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 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. printf("%d %d\n",x,y );
  33. if (parentID[y][x] == -1)
  34. return get_pixel_id(x,y,w);
  35. return up_trees_find(parentID, w, parentID[y][x]);
  36. }
  37.  
  38. /* Merge the two groups to which pixel p1 and pixel p2 belong. */
  39. void up_trees_union(pixelID parentID[ROWS][COLS], unsigned int w, pixelID p1, pixelID p2) {
  40. pixelID f1 = min(p1,p2);
  41. pixelID f2 = max(p1,p2);
  42. f1 = up_trees_find(parentID, w, f1);
  43. f2 = up_trees_find(parentID, w, f2);
  44. int x = get_x_coord(f2,w);
  45. int y = get_y_coord(f2,w);
  46. parentID[y][x] = f1;
  47. }
  48.  
  49. /* Store forest of up-trees in the array parentID, given the graph G. */
  50. void up_trees_new(graph G, pixelID parentID[ROWS][COLS]) {
  51. for (unsigned y =0; y<(*G).image_height; y++){
  52. for (unsigned x =0; x<(*G).image_width; x++) {
  53. //parentID[y][x] = get_pixel_id(x,y,(*G).image_width);
  54. parentID[y][x] = -1;
  55. }
  56. }
  57. }
  58.  
  59. /*
  60. * Run UNION-FIND, and store the final forest of up-trees in array parentID,
  61. * where count is a boolean flag indicating whether to print out the count.
  62. */
  63. void run_union_find(graph G, pixelID parentID[ROWS][COLS], bool count) {
  64. int calls = 0;
  65. //test and debug up trees find
  66. for (unsigned y = 0; y < (*G).image_width; y++) {
  67. for (unsigned x = 0; x < (*G).image_width; x++) {
  68. if (((x+1<(*G).image_width) && (equal_colors((*G).pixels[y][x],(*G).pixels[y][x+1])))) {
  69. if (up_trees_find(parentID, (*G).image_width , get_pixel_id(x,y,(*G).image_width)) != up_trees_find(parentID, (*G).image_width, get_pixel_id(x+1,y,(*G).image_width)) ) {
  70. up_trees_union(parentID, (*G).image_width, parentID[y][x], parentID[y][x+1]);
  71. calls++;
  72. }
  73. }
  74. if ( (y+1<(*G).image_height) && (equal_colors((*G).pixels[y][x],(*G).pixels[y+1][x]))) {
  75. if (up_trees_find(parentID, (*G).image_width ,get_pixel_id(x,y,(*G).image_width)) != up_trees_find(parentID, (*G).image_width, get_pixel_id(x,y+1,(*G).image_width) ) ){
  76. up_trees_union(parentID, (*G).image_width, parentID[y][x], parentID[y+1][x]);
  77. calls++;
  78. }
  79. }
  80. }
  81. }
  82. if (count == true)
  83. printf("The number of times that the method UNION was called for this image is: %d\n",calls );
  84. return;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement