473,396 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Segmentation fault in determining closest pair

13
I'm trying to code the closest pair algorithm...but when the distribution of points is too close, it is giving segmentation fault. My algorithm is right as for points sparsely placed it is giving same answer with bruteforce...the problem is most probably when the line dividing points into two halves contain more than one point(not sure though) whose probability inc. with more and more points in picture. Here's my code
http://pastebin.com/ZbDtC9dq

the segmentation fault is in line
double d1 = closestPair(ppx, ll, lenL);
//checked with debugger

here's quickSortClosestPair.c
http://pastebin.com/jCYd8dr8

help !!
Jul 6 '10 #1
6 3111
Banfa
9,065 Expert Mod 8TB
If you want help with your code you are going to have to post your code.
Jul 6 '10 #2
noob15
13
here's the code
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<math.h>
  4. #include<assert.h>
  5. #include<string.h>
  6.  
  7.  
  8. #define POINTS 1000 
  9.  
  10. struct point{
  11.     double x;
  12.     double y;
  13. };
  14.  
  15. #include"quickSortClosestPair.c"
  16.  
  17. double gaussrand();
  18. void generatePoints(struct point *pp[]);
  19. double closestPair(struct point *ppx[], struct point *ppy[], int n);
  20. double bruteForceClosest(struct point *ppx[], struct point *ppy[], int n );
  21. double distance(struct point *p1, struct point *p2);
  22. void print(struct point *pp[]);
  23. int isLeft(struct point *p, struct point *bisect);
  24.  
  25.  
  26. int main()
  27. {
  28.     //array of points whose min. closest pair we have to find
  29.     struct point *pp1[POINTS];
  30.     generatePoints(pp1);
  31.  
  32.     //copy of pp1
  33.     struct point *pp2[POINTS];
  34.     int i;
  35.     for(i=0; i<POINTS; i++){
  36.         pp2[i] = malloc(sizeof(struct point));
  37.         assert(pp2[i] != NULL);
  38.         pp2[i]->x = pp1[i]->x;
  39.         pp2[i]->y = pp1[i]->y;
  40.     }
  41. //    print(pp1);
  42. //    printf("hiiiii\n");
  43.     QuickSort(pp1, 0, POINTS-1, 'x');
  44.     QuickSort(pp2, 0, POINTS-1, 'y');
  45.     print (pp1);
  46.     //d gives the closest distance
  47.     double d = closestPair(pp1, pp2, POINTS);
  48.  
  49.     printf("the distance between closest pair of point is %f\n", d);
  50.  
  51.     d = bruteForceClosest(pp1, pp2, POINTS);
  52.     printf("the distance between closest pair of point by brute force is %f\n", d);
  53.  
  54.  
  55.     return 0;
  56. }
  57.  
  58.  
  59. void print(struct point *pp[])
  60. {
  61.     int i=0;
  62.     while(i< POINTS){
  63.         printf("%f %f\n",pp[i]->x, pp[i]->y);
  64.         i++;
  65.     }
  66. }
  67.  
  68.  
  69. void generatePoints(struct point *pp[])
  70. {
  71.     int i;
  72.     srand(time(0));
  73.     for(i=0; i<POINTS; i++){
  74.         pp[i] = malloc(sizeof(struct point));
  75.         assert(pp[i] != NULL);
  76.         pp[i]->x = rand()%100 + gaussrand();
  77.         pp[i]->y = rand()%100 + gaussrand();
  78.  
  79.     }
  80. }
  81.  
  82.  
  83. double closestPair(struct point *ppx[], struct point *ppy[], int n)
  84. {
  85.     if(n <= 3){
  86.         return bruteForceClosest(ppx, ppy, n);
  87.     }
  88.  
  89.     //compute separation line L such that half points are
  90.     //on one side nd half on other side
  91.     struct point *bisect = ppx[n/2];
  92.  
  93.     struct point **ll = malloc(sizeof(struct point*)*n);
  94.     struct point **rr = malloc(sizeof(struct point*)*n);
  95.  
  96.     int lenL = 0;
  97.     int lenR = 0;
  98.  
  99.     int i,j;
  100.     for(i=0; i<n; i++){
  101.         if(isLeft(ppy[i], bisect)){
  102.             ll[lenL] = ppy[i];
  103.             lenL++;
  104.         }else if(!(isLeft(ppy[i], bisect))){
  105.             rr[lenR] = ppy[i];
  106.             lenR++;
  107.         }
  108.     }
  109.  
  110.     double d1 = closestPair(ppx, ll, lenL);
  111.     double d2 = closestPair(ppx+n-lenR, rr, lenR);
  112.     double min1 = (d1 <= d2) ? d1 : d2;
  113.  
  114.     //delete all points further than min1 from separation line L
  115.     int stripLen = 0;
  116.     struct point *striped[POINTS];
  117.     for(i=0; i<n; i++){
  118.         if((ppy[i])->x >= (bisect->x - min1) || (ppy[i])->x <= (bisect->x + min1)){
  119.             striped[stripLen] = ppy[i];
  120.             stripLen++;
  121.         }
  122.     }
  123.  
  124.     //check the next 7 points till the end 
  125.     for(i=0; i<stripLen; i++){
  126.         for(j=i+1; j-i<8 && j<stripLen; j++){
  127.             if(distance(striped[i], striped[j]) < min1){
  128.                 min1 = distance(striped[i],striped[j]);
  129.             }
  130.         }
  131.     }
  132.  
  133.     return min1;
  134. }
  135.  
  136.  
  137. int isLeft(struct point *p, struct point *bisect)
  138. {
  139.     if(p->x < bisect->x){
  140.         return 1;
  141.     }else if(p->x == bisect->x && p->y < bisect->y){
  142.         return 1;
  143.     }
  144.     return 0;
  145.  
  146. }
  147.  
  148.  
  149. double distance(struct point *p1, struct point  *p2)    
  150. {
  151.     double distance1 = sqrt((p1->x - p2->x) * (p1->x - p2->x) + (p1->y - p2->y) * (p1->y - p2->y));
  152. //    assert(distance1 != 0);
  153.     return distance1;
  154. }
  155.  
  156.  
  157. double bruteForceClosest(struct point *ppx[], struct point *ppy[], int n )
  158. {
  159.     double min1 = INFINITY;
  160.     double distance1;
  161.     int i,j;
  162.     for(i=0; i<n-1; i++){
  163.         for(j=i+1; j<n; j++){
  164.             distance1 = distance(ppx[i],ppx[j]);
  165.             if(min1 > distance1){
  166.                 min1 = distance1;
  167.             }
  168.         }
  169.     }
  170.     return min1;
  171. }    
  172.  
  173.  
  174. double gaussrand()
  175. {
  176.     static double V1, V2, S;
  177.     static int phase = 0;
  178.     double X;
  179.     if(phase == 0) {
  180.         do{
  181.             double U1 = (double)rand() / RAND_MAX;
  182.             double U2 = (double)rand() / RAND_MAX;
  183.             V1 = 2 * U1 - 1;
  184.             V2 = 2 * U2 - 1;
  185.             S = V1 * V1 + V2 * V2;
  186.         }while(S>=1 || S==0);
  187.  
  188.         X = V1 * sqrt(-2 * log(S) / S);
  189.  
  190.     }else{
  191.         X = V2 * sqrt(-2 * log(S) / S);
  192.     }
  193.  
  194.     phase = 1 - phase;
  195.  
  196.     return X;
  197. }
  198.  
  199.  
  200.  
Jul 6 '10 #3
noob15
13
@noob15
quickSortClosestPair.c
Expand|Select|Wrap|Line Numbers
  1. // quicksort, if char==c then it sorts points acc. to x coordinate otherwise it sorts according to y coordinate
  2. void Swap(struct point **a, struct point **b);
  3. void QuickSort(struct point *aa[], int p, int r, char c);
  4. int Partition1(struct point *aa[], int p, int r);
  5. int Partition2(struct point *aa[], int p, int r);
  6.  
  7. //sorts array of struct point according to index given by c
  8. void QuickSort(struct point *aa[], int p, int r, char c)
  9. {
  10.     if(p<r && c=='x'){
  11.         int q = Partition1(aa, p, r);
  12.         QuickSort(aa, p, q-1, 'x');
  13.         QuickSort(aa, q+1, r, 'x');
  14.     }
  15.  
  16.     if(p<r && c=='y'){
  17.         int q = Partition2(aa, p, r);
  18.         QuickSort(aa, p, q-1, 'y');
  19.         QuickSort(aa, q+1, r, 'y');
  20.     }
  21. }
  22.  
  23. int Partition1(struct point *aa[], int p, int r)
  24. {
  25.     int x=aa[r]->x;
  26.     int y=aa[r]->y;
  27.     int i=p-1;
  28.     int j;
  29.     for(j=p; j<r; j++){
  30.         if(aa[j]->x < x){
  31.             i++;
  32.             Swap(&aa[i], &aa[j]);
  33.         }else if(aa[j]->x == x && aa[j]->y < y){
  34.             i++;
  35.             Swap(&aa[i], &aa[j]);
  36.         }
  37.     }
  38.     Swap(&aa[i+1], &aa[r]);
  39.     return (i+1);
  40. }
  41.  
  42.  
  43. int Partition2(struct point *aa[], int p, int r)
  44. {
  45.     int x=aa[r]->y;
  46.     int y=aa[r]->x;
  47.     int i=p-1;
  48.     int j;
  49.     for(j=p; j<r; j++){
  50.         if(aa[j]->y < x){
  51.             i++;
  52.             Swap(&aa[i], &aa[j]);
  53.         }else if(aa[j]->y == x && aa[j]->x < y){
  54.             i++;
  55.             Swap(&aa[i], &aa[j]);
  56.         }
  57.  
  58.     }
  59.     Swap(&aa[i+1], &aa[r]);
  60.     return (i+1);
  61. }
  62.  
  63. void Swap(struct point **a, struct point **b)
  64. {
  65.     struct point *tmp = *a;
  66.     *a = *b;
  67.     *b = tmp;
  68. }
  69.  
Jul 6 '10 #4
Banfa
9,065 Expert Mod 8TB
Line 15, you should never, NEVER include code files. If its meant to be included then it should be a header file (.h) if it is a code file then you should compile it separately and link it into your program.


Its tricky, closestPair has recursed over 500 times by the time the segmentation fault occurs. On my machine it seems to happen inside malloc suggesting a memory allocation issue but I estimate it wont have more than 4 Mbytes of data on the heap and rather less on the stack which should be ok.
Jul 6 '10 #5
noob15
13
@Banfa
yupp....both heap and stack memory should not be a problem as the same program is running fine with larger number of points but points being distributed relatively apart from each other(changing %100 of rand to bigger value) !!

Btw thanks for pointing out line 15 thing...'ll avoid such a thing in future.
Jul 6 '10 #6
Banfa
9,065 Expert Mod 8TB
I found that changing the %100 to a larger value made no difference.

I'm not sure however you are also using over 1Mbyte of stack space. I suspect if you re-wrote this routine to run iteratively rather than recursively and avoided the recursion into closestPair>500 times it might work better.
Jul 7 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
2
by: tikcireviva | last post by:
Hi Guys, I am not sure what's wrong with my program. When I ran it under the GDB, it shows the following messages , which said that the issue is from stl_list.h. Does anyone know what's...
0
by: muggy440 | last post by:
Hello; The code below is one of solving the closest pair problem but some parts missing can any one help me out. #include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h>
0
by: fantasticamir | last post by:
Guys, I have a problem with "map". Imap<int,myObj> test; ... myObj obj; test.insert(pair<int,myObj> (100,obj));
4
by: fantasticamir | last post by:
here is my code!! #include <pthread.h> #include <iostream> #include <unistd.h> #include <map> #define MET_TIMEOUT_INTERVAL 10;
61
by: arnuld | last post by:
I have created a program which creates and renames files. I have described everything in comments. All I have is the cod-duplication. function like fopen, sprint and fwrite are being called again...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.