I have implemented the Kruskal algorithm in C using an adjacency matrix graph representation, the problem is, it keeps popping up segmentation fault error, I've been trying to figure out what is wrong for quite a while and I can't seem to find the problem, could anyone else take a look please?
Thanks.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#define MAXVERT 10
#define MAXEDGES 20
#define INF 100000
/*graph representation using an Adjacency matrix*/
typedef struct AdjMatrix
{
int nodes;
int adjMat[MAXVERT][MAXVERT];
} graph;
/*function prototypes*/
int find(int node, int *trees);
void merge(int i, int j, int *trees);
void printminimal(int min[][3], int n);
/*main algorithm*/
void kruskal(graph *g)
{
int EDGES[MAXEDGES][3]; /*graph edges*/
int MINEDGES[MAXVERT-1][3]; /*edges already in the minimal spanning tree*/
int nextedge=0;
int numedges=0;
int trees[MAXVERT]; /*tree subsets*/
int i, j, k;
int temp;
for(i=0;i<g->nodes;i++)
trees[i]=i;
k=0;
for(i=0; i<g->nodes; i++)
for(j=0; j<g->nodes; j++)
{
if(i<j)
{
EDGES[k][0]=i;
EDGES[k][1]=j;
EDGES[k][2]=g->adjMat[i][j];
k++;
}
else
break;
}
/*Bubblesort*/
for(i=0; i<g->nodes; i++)
for(j=0; j<i; j++)
{
if(EDGES[j][2] > EDGES[j+1][2])
{
temp=EDGES[j][0];
EDGES[j][0]=EDGES[j+1][0];
EDGES[j+1][0]=temp;
temp=EDGES[j][1];
EDGES[j][1]=EDGES[j+1][1];
EDGES[j+1][1]=temp;
temp=EDGES[j][2];
EDGES[j][2]=EDGES[j+1][2];
EDGES[j+1][2]=temp;
}
}
while(numedges < (g->nodes-1))
{
i=find(EDGES[nextedge][0], trees);
j=find(EDGES[nextedge][1], trees);
if((i!=j)&&(EDGES[nextedge][2]!=-1)) /*check if the nodes belong to the same subtree*/
{
merge(i,j,trees);
MINEDGES[numedges][0]=EDGES[nextedge][0];
MINEDGES[numedges][1]=EDGES[nextedge][1];
MINEDGES[numedges][2]=EDGES[nextedge][2];
numedges++;
}
nextedge++;
}
}
int find(int node, int *trees)
{
if(trees[node]!=node)
return trees[node];
else
return node;
}
void merge(int i, int j, int *trees)
{
if(i<j)
trees[j]=i;
else
trees[i]=j;
}
void printminimal(int min[][3], int n)
{
int i, weight=0;
printf("Minimal tree:\n(");
for(i=0;i<n;i++)
{
printf("(V%d,V%d), ", min[i][0],min[i][1]);
weight+=min[i][2];
}
printf(")\n Total weight sum of the minimal tree is: %d", weight);
}
int main(void)
{
int i,j;
graph *g=(graph *)malloc(sizeof(graph));
/*int adjMat[8][8] = {0,INF,INF,11,INF,1,7,
INF,0,INF,3,INF,4,8,INF,
INF,INF,0,INF,INF,INF,12,INF,
INF,3,INF,0,15,INF,INF,INF,
11,INF,INF,INF,0,20,INF,INF,
INF,4,INF,INF,20,0,INF,INF,
1,8,12,INF,INF,INF,0,5,
7,INF,INF,INF,INF,INF,5,0};*/
for(i=0;i<4;i++)
for(j=0;j<i;j++)
{
if(i==j)
{
g->adjMat[i][j]=0;
continue;
}
printf("%d-%d= ", i, j);
scanf("%d", &(g->adjMat[i][j]));
g->adjMat[j][i]=g->adjMat[i][j];
}
g->nodes=4;
kruskal(g);
}
In the kruskal function, where you intend to populate the EDGES array, you don't:
for(i=0; i<g->nodes; i++)
for(j=0; j<g->nodes; j++)
{
if(i<j)
{
EDGES[k][0]=i;
EDGES[k][1]=j;
EDGES[k][2]=g->adjMat[i][j];
k++;
}
else
break;
}
For j == 0, i is never < j, so you immediately break out of the inner loop. I suspect it should be i > j in the condition.
Since EDGES is uninitialised, find tries to access an unspecified element of trees.
I had to add the following to get this to kruskal to get it to compile from gcc:
int *dvra = trees;
You can then compile it with debug information:
gcc -g -o kruskal kruskal.c
and run it through gdb:
gdb kruskal
You can then type run and enter to start the program. I entered 1,2,3,... when prompted for values.
This then gives:
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400a92 in find (node=32767, trees=0x7fffffffe110) at test.c:86
86 if(trees[node]!=node)
Hmm, that's curious. Trees holds only 10 items (value of the MAXVERT define), so accessing node 32767 goes out of bounds. If you enter 32767 in the calculator program and go to the programming (hexadecimal) mode, you will find it is 7FFF (or MAX_SHORT, the maximum 16-bit signed integer value). That's also interesting.
NOTE: You can investigate variable values by using the print command (e.g. print node) and the backtrace using the bt command.
These are coming from the while loop in kruskal (the only place that is calling find), so we need to investigate where that value is coming from. Lets quit out of gdb (press 'q' and enter then confirm with 'y' and enter).
Add the following to the while loop and run the resulting program:
printf("%d: nextedge=%d EDGES[nextedge][0]=%d EDGES[nextedge][1]=%d\n", numedges, nextedge, EDGES[nextedge][0], EDGES[nextedge][1]);
which gives:
0: nextedge=0 EDGES[nextedge][0]=-557487152 EDGES[nextedge][1]=32767
So it looks like EDGES[0] is not being initialized, which points to the if(i<j) condition in the initialization loop above the bubblesort. OK, so lets trace what is happening in the initialization loop by adding the following inside the if loop:
printf("EDGES[%d]: 0=%d 1=%d\n", k, i, j);
Rerunning this, we see that there are no lines associated with this statement, so it is not getting executed.
Changing the if condition to:
if(i<=j)
causes the statement to be executed and the segment fault to go away.
Related
I should mention that I am in my 1st 2 weeks of an intro to programming class before people get too crazy with answers.
Using this array as an example,
int scores[30] = {90,85,100,50,50,85,60,70,55,55,80,95,70,60,95,
80,100,75,70,95,90,90,70,95,50,65,85,95,100,65}
I am trying to parse through it to create 2 new parallel arrays to use later. The idea is to make one array that holds the "scores" and one that holds the "occurrences" of each score. I end up compiling with no errors however during run time it crashes.
void frequency(int scores[], int max){
int i, x=0, temp=0, count=0, sum=0, mode=0;
int score[sum]; //unknown length of array, sum gets added after the while loop
int freq[sum];
printf("score\tfrequency\n");
printf("-----\t---------\n");
fprintf(fp, "score\tfrequency\n");
fprintf(fp, "-----\t---------\n");
for (i = 0; i < max; ++i){
while (scores[i]==scores[x]){
x++;
count++;
sum++;
temp = x-1;
if(scores[i] != scores[x]){
//printf(" %d\t %d\n",scores[i], count);
freq[i] = count;
score[i] = scores[i];
count=0;
i=temp;
x=temp+1;
sum++;
printf("%d\t%d", score[i], freq[i]);
fprintf(fp, "%d\t%d", score[i], freq[i]);
}
}
}
}
This part:
int i, x=0, temp=0, count=0, sum=0, mode=0;
int score[sum];
int freq[sum];
looks wrong.
You set sumto zero and then use it for the array dimension. Did you mean to do:
sum = max;
I end up compiling with no errors however during run time it crashes.
Reason:
The reason why your program crashes is because you have not allocated sufficient memory to the arrays that you use int the frequency() function
void frequency(int scores[], int max){
int i, x=0, temp=0, count=0, sum=0, mode=0;
int score[sum];
int freq[sum];
Solution:
So, is there a way to provide memory during run time according to requirements or change memory size of blocks during compile time?
Yes, that's the very reason why Dynamic memory allocation is used.... though you send a fixed array to the frequency() function in your code, the function I've provided works for any integer array you send..
Here I've provided code in which
one array stores all the unique scores
and other array stores number of occurrences of each score
I've done this using dynamic memory allocation.. I think it's easy to understand if you have basic understanding of dynamic memory allocation functions.. if you have any doubts, ask me through the comments :) and by the way I've assumed your main function to be :
int main()
{
int scores[30] = {90,85,100,50,50,85,60,70,55,55,80,95,70,60,95,
80,100,75,70,95,90,90,70,95,50,65,85,95,100,65};
frequency(scores,30);
return 0;
}
Code:
#include <stdio.h>
#include <stdlib.h>
void frequency(int scores[], int max);
int main()
{
int scores[30] = {90,85,100,50,50,85,60,70,55,55,80,95,70,60,95,
80,100,75,70,95,90,90,70,95,50,65,85,95,100,65};
frequency(scores,30);
return 0;
}
void frequency(int scores[], int max)
{
int i,j,count=0,flag=0,occur=0;
int *score=malloc(sizeof(int));
if(malloc==NULL)
{
printf("memory allocation failed");
exit(1);
//it's good to check if memory allocated was successful or not
//I've avoided it for further allocations,to decrease the size of post :)
}
int *freq=malloc(sizeof(int));
printf("score\tfrequency\n");
printf("-----\t---------\n");
//building array which has only scores
for(i=0;i<max;i++)
{
if(count==0) //first time
{
score=realloc(score,(count+1)*sizeof(int));
//increasing size of array by 1*sizeof(int)
score[count]=scores[i];
count++;
}//first one requires no checking whether it's repeated or not
else
{
flag=0; //resetting flag value
for(j=0;j<count;j++)
{
if(scores[i]==score[j])
{
flag=1; //
break;
}
}
if(flag==0) // if not repeated need to add new element
{
score=realloc(score,(count+1)*sizeof(int));
score[count]=scores[i];
count++;
}
}
}
//allocating memory for frequency array
freq=realloc(freq,count*sizeof(int));
//building array which has frequency of each score
for(i=0;i<count;i++)
{
occur=0;
for(j=0;j<max;j++)
{
if(score[i]==scores[j])
occur++;
}
freq[i]=occur;
}
for(i=0;i<count;i++) //printing output
printf("\n %d\t %d\n",score[i],freq[i]);
free(score); //freeing the blocks
free(freq);
}
My approach is quite simple to understand
first I create array score which creates extra memory whenever it encounters unique elements and stores in it
and then I check occurrences for each of the element of score array in the scores array and store them in freq array.
Output:
score frequency
----- ---------
90 3
85 3
100 3
50 3
60 2
70 4
55 2
80 2
95 5
75 1
65 2
I hope this is what you were trying to achieve :)
I hope you have spent beautiful Christmas holidays. I am studying for an exam and I have a problem with my project in ANSI C. My code works but not always, it's strange because for some input values it works for other not. I have two arrays, A and B, that must be different in size and I have to write a function that do the mathematical union of the two arrays in another array. If there are elements of the same value I have to insert in the new array only one. I write all the code (I also post a question here because I had some problems with the union) but it does not work always. Gcc compile and I execute but it's not correct. I debugged with gdb and it said
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400d1c in unionearraycrescente (a=0x7fffffffdd50, indice_m=4,
b=0x7fff00000005, indice_n=5, minimo=6, indiceMinimo=22)
at array.c:152
152 if(b[i]==c[j])
And this is the code near the problem
int arrayun(int a[], int index_m, int b[], int index_n, int minimum, int indexMinimum)
{
int i=0;
int j=0;
int found;
int lenc=0;
int c[lenc];
for(i=0;i<index_m;i++){
found = 0;
for(j=0; j<i && !found;j++)
if(a[i]==c[j])
found = 1;
if(!found)
c[lenc++] = a[i];
}
for(i=0;i<index_n;i++){
found=0;
for(j=0;j<i && !found;j++)
{
if(b[i]==c[j]) //debug gbd problem - segfault
found = 1;
}
if(!found)
c[lenc++] = b[i];
}
I am Italian so the comments are in my language, if you have any problems I will translate the comments. I want only to resolve this memory error. Thank you.
I follow some of your advices and in that part of code it works, I changed all the variables with index_m and I don't receive segfault but after the union I use the selection sort to sort in ascending order and it return me not the right values but in the first position negative values.
int arrayun (int a[], int index_m, int b[], int index_n, int minimum, int indexMinimum)
{
int i=0;
int j=0;
int found;
int lenc;
int c[index_m];
for(i=0;i<index_m;i++){
found = 0;
for(j=0; j<i && !found;j++)
if(a[i]==c[j])
found = 1; //setta trovato = 1
if(!found)
c[index_m++] = a[i];
}
for(i=0;i<index_n;i++){ //index_m or index_n?
found=0;
for(j=0;j<i && !found;j++)
{
if(b[i]==c[j]) //debug gbd problem - segfault - SOLVED but
found = 1;
}
if(!found)
c[index_m++] = b[i];
}
for (i=0; i<index_m-1;i++)
{
minimum=c[i];
indexMinimum=i;
for (j=i+1;j<index_m; j++)
{
if (c[j]<minimum)
{
minimum=c[j];
indiexMinimum=j;
}
}
c[indexMinimum]=c[i];
c[i]=minimum;
}
for(i=0;i<index_m;i++)
printf("Element %d\n",c[i]);
return c[index_m]; //I think here it's wrong
}
int c[lenc]; means in your program it is c[0]
and you are allocating ZERO Memory for the array.
And if you try for b[i]==c[i] where i>=0 means its a segmentation fault only.
Instead you can initialize like,
c[index_m];
int lenc=0;
int c[lenc];
this is array of 0 length.and in loop you are trying to access c[1],c[2]... etc.
To cure this problem you can pass length of the bigger array
int unionearraycrescente (int a[], int index_m, int b[], int index_n,int len, int minimum, int indexMinimum)
and you can then initialize like
int c[len];
So all I'm trying to do is take an input from the user of how many cards to use and then randomly assign each card to a different index in an array. I'm having extensive issues getting the rand function to work properly. I've done enough reading to find multiple different ways of shuffling elements in an array to find this one to be the easiest in regards to avoiding duplicates. I'm using GCC and after I input the amount of cards I never get the values from the array back and if I do they're all obscenely large numbers. Any help would be appreciated.
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
void main(){
srand(time(NULL));
int d, c, i, z, l, r;
printf("Enter the deck length: ");
scanf("%d\n ", &c);
int deck[c];
int swap[c];
z = c;
for(l=0; l<c; l++){
swap[l] = l;
}
for(i=z; i=0; i--){
r = rand() / i
deck[i] = swap[r];
for(r; r=(c-1); r++){
swap[r] = swap[(r+1)];
}
}
for(d = 0; d < c; d++){
printf("%d ", deck[d]);
}
return;
}
I can spot one major problem here:
for(i=z; i=0; i--)
^^^
This loop will never execute since you are using assignment(=) and setting i to 0 therefore the condition will always be false, although using equality(==) will still be false in this case, you probably want:
for(i=z; i!=0; i--)
This means you will be using deck unitialized which is undefined behavior. Once you fix that you have a similar problems here:
for(r; r=(c-1); r++){
main has to return int and your return at the end needs to provide a value.
Turning on warning should have allowed you to find most of these issues, for example using -Wall with gcc gives me the following warning for both for loops:
warning: suggest parentheses around assignment used as truth value [-Wparentheses]
Note, see How can I get random integers in a certain range? for guidelines on how to use rand properly.
You basically need to be able to generate 52 numbers pseudo-randomly, without repeating. Here is a way to do that...
First, loop a random number generator 52 times, with a method to ensure none of the random numbers repeat. Two functions in addition to the main() will help to do this:
#include <ansi_c.h>
int NotUsedRecently (int number);
int randomGenerator(int min, int max);
int main(void)
{
int i;
for(i=0;i<52;i++)
{
printf("Card %d :%d\n",i+1, randomGenerator(1, 52));
}
getchar();
return 0;
}
int randomGenerator(int min, int max)
{
int random=0, trying=0;
trying = 1;
while(trying)
{
srand(clock());
random = (rand()/32767.0)*(max+1);
((random >= min)&&(NotUsedRecently(random))) ? (trying = 0) : (trying = 1);
}
return random;
}
int NotUsedRecently (int number)
{
static int recent[1000];//make sure this index is at least > the number of values in array you are trying to fill
int i,j;
int notUsed = 1;
for(i=0;i<(sizeof(recent)/sizeof(recent[0]));i++) (number != recent[i]) ? (notUsed==notUsed) : (notUsed=0, i=(sizeof(recent)/sizeof(recent[0])));
if(notUsed)
{
for(j=(sizeof(recent)/sizeof(recent[0]));j>1;j--)
{
recent[j-1] = recent[j-2];
}
recent[j-1] = number;
}
return notUsed;
}
I can't find any errors here. The purpose of the program is to thing all possible combinations of array elements and their sums. I'm trying to write a program which will return me an array of elements where every next one does not equal any previous one or the sum of any previous combinations of elements. I started like this and encountered an error: it says that program has stopped working...
#include <stdio.h>
int m[20];
void initm(int x[]) {
for(int i=0; i<20; i++) {
m[i]=i;
}
}
void sorter(int x[]) {
for(int i=0; i<20; i++) {
for(int j=0; j<20; j++) {
/* nested for loop to get all possible combinations */
printf("%d===%d===%d", x[i], x[j], x[i]+x[j]);
}
}
}
int main() {
initm(m[20]);
sorter(m[20]);
return 0;
}
m[20] reads an int one element beyond the end of your array so
initm(m[20]);
sorter(m[20]);
should be
initm(m);
sorter(m);
Here's what I'm trying to do.
There are 3 arrays, cost[] node1[] and node2[].
These entires correspond to edges of a graph with node1[i],node2[i] and cost[i] specifying that there is an edge going from vertex node1[i] to node2[i] with an edge weight of cost[i].
I'm trying to sort these edges with respect to their weights, i.e sort the cost[] array using merge-sort. However whenever I'm, changing an entry in the cost[] array I also want to change the corresponding entries in the node1 and node2 array since even the nodes of the graph have to be modified. Ie if node1[]=1,2,3 and node2[]=2,3,1 cost[]={7 4 8} then after sorting the cost array the node1 and node2 should look like node1[]=2,1,3 node2[]=3,2,1. and cost[]=4,7,8
Here's my code.
#include<stdio.h>
#include<stdlib.h>
int merge_sort(int arr[],int low,int high,int node1[],int node2[])
{
int mid;
if(low<high) {
mid=(low+high)/2;
// Divide and Conquer
merge_sort(arr,low,mid,node1,node2);
merge_sort(arr,mid+1,high,node1,node2);
// Combine
merge(arr,low,mid,high,node1,node2);
}
return 0;
}
int merge(int arr[],int l,int m,int h,int node1[],int node2[])
{
int arr1[80000],arr2[80000]; // Two temporary arrays to
int arr3[70000],arr4[70000];
int arr5[70000],arr6[70000];
int n1,n2,i,j,k;
n1=m-l+1;
n2=h-m;
for(i=0; i<n1; i++)
{
arr1[i]=arr[l+i];
arr3[i]=node1[l+i];
arr5[i]=node2[l+i];
}
for(j=0; j<n2; j++)
{
arr2[j]=arr[m+j+1];
arr4[i]=node1[m+j+1];
arr6[i]=node2[m+j+1];
}
arr1[i]=99999; // To mark the end of each temporary array
arr2[j]=99999;
arr3[i]=99999;
arr4[j]=99999;
arr5[i]=99999;
arr6[j]=99999;
i=0;
j=0;
for(k=l; k<=h; k++) { //process of combining two sorted arrays
if(arr1[i]<=arr2[j])
{
arr[k]=arr1[i++];
//node1[k]=arr3[i++]; COMMENTED LINES!!!!!!!!!!!
//node2[k]=arr5[i++];
}
else
{
arr[k]=arr2[j++];
//node1[k]=arr4[j++]; COMMENTED LINES!!!!!!!!~!
//node2[k]=arr6[j++];
}
}
return(0);
}
int main(void)
{
int i,j,n,vert1,vert2,weight;
scanf("%d",&n);
int adjmat[n+1][n+1],cluster[n+1][n+1];
int *cost,*node1,*node2;
node1=malloc(sizeof(int)*1000000);
node2=malloc(sizeof(int)*1000000);
cost=malloc(sizeof(int)*1000000);
for(i=0;i<n+1;i++)
for(j=0;j<n+1;j++)
{
adjmat[i][j]=0;
cluster[i][j]=0;
}
for(i=1;i<n+1;i++)
cluster[i][0]=i;
for(i=1;i<(n+1)*(n+1);i++)
{
scanf("%d %d %d",&vert1,&vert2,&weight);
node1[i]=vert1;
node2[i]=vert2;
cost[i]=weight;
if(node1[i]==node1[i-1] && node2[i]==node2[i-1] && cost[i]==cost[i-1])
break;
// printf("%d %d %d\n",node1[i],node2[i],cost[i]);
adjmat[vert1][vert2]=weight;
adjmat[vert2][vert1]=weight;
}
printf("\n%d\n",i);
merge_sort(cost,1,124751,node1,node2);
for(j=1;j<i;j++)
printf("%d %d %d\n",node1[j],node2[j],cost[j]);
return(0);
}
Whenever I comment the lines in the merge function the code manages to sort the cost array. However whenever I un comment these lines somehow everything gets equated to 0. i.e all entires of the node1 node2 and cost arrays are 0. Could anyone tell me why this is happening? Thanks!
You probably have forgotten to take care of the side effect of the i++ operation. There is no need at all at that place to work with side effects, don't do that.