Hi! Please give me a hand

I have this c/c++ program that is an implementation of disjunct sets using trees. The problem is that when i use the test values it works, but when i try to use generated values i doesn't anymore. When the make_set function is called, it won't link the addresses and I couldn't figure why. The problem is at make_set call from evaluation function.

Hope you could tell me. PS: I used profiler.h, to generate random values and generate charts. Here's the header file: https://docs.google.com/uc?authuser=0&id=0B1HP8DU_DI00c0NVOGt1ay1zNVE&export=download

Thanks in advance.

Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// ConsoleApplication7.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "profiler.h"
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
#include<iostream>
#include<conio.h>

using namespace std;

Profiler profiler("demo");

struct node
{
	int nr;
	int dim;
	struct node * par;
};
//node*ar[10001];
int n = 0, x, y;
int a[2][60001];

long nrIntrari;

void make_set(node *x, int n)
{
	nrIntrari++;
	x->par = x;
	x->dim = 1;
}
node* find_set(node*x, int n)
{
	nrIntrari++;
	node* p = x;
	profiler.countOperation("CompareDisj", n);
	if (p->par != p)
	{
		x->par = find_set(x->par, n);
		p = x->par;
	}
	return p;
}

void link(node *x, node *y, int n)
{
	profiler.countOperation("CompareDisj", n);
	if (x->dim>y->dim)
		y->par = x;
	else
	{
		x->par = y;
		if (x->dim == y->dim)
			y->dim++;
	}
}

void set_union(node *x, node*y, int n)
{
	node* x1 = find_set(x, n);
	node* y1 = find_set(y, n);
	profiler.countOperation("CompareDisj", n);
	if (x1 != y1)
	{
		link(x1, y1, n);
	}
}


void conex_comp(int n, node *ar[])
{
	int comp[10];
	for (int i = 1; i <= 9; i++)
	{
		profiler.countOperation("AssignDisj", n);
		comp[i] = i;
	}
	for (int i = 1; i <= 9; i++)
	{
		profiler.countOperation("AssignDisj", n);
		comp[i] = find_set(ar[i],n)->nr;
	}

	for (int i = 1; i <= 9; i++)
	{
		cout << i << " " << comp[i] << endl;
	}
}

void test()
{
	int nr = 9;
	int mk = 7;
	int i;
	node*ar[10001];
	a[0][0] = 1;
	a[1][0] = 2;
	a[0][1] = 2;
	a[1][1] = 3;
	a[0][2] = 3;
	a[1][2] = 4;
	a[0][3] = 4;
	a[1][3] = 1;
	a[0][4] = 5;
	a[1][4] = 6;
	a[0][5] = 6;
	a[1][5] = 7;
	a[0][6] = 8;
	a[1][6] = 9;

	for (i = 1; i <= nr; i++)
	{
		ar[i] = new node;
		ar[i]->nr = i;
		make_set(ar[i], nr);
	}
	for (i = 0; i < mk; i++)
	{
		set_union(ar[a[0][i]], ar[a[1][i]], nr);
	}
}

void evaluation()
{
	int n=30000, i, nr = 10000;
	node*ar[10001];
	ar[10001] = (struct node*)malloc(sizeof(struct node[10001]));
	for (n = 10000; n < 60000; n = n + 5000)
	{
		FillRandomArray(a[0], nr);
		FillRandomArray(a[1], nr);
		for (i = 1; i <= nr; i++)
		{
			ar[i] = new node;
			ar[i]->nr = i;
			make_set(ar[i], nr);
		}
		for (i = 0; i < n; i++)
		{
			set_union(ar[a[0][i]], ar[a[1][i]], n);
		}

		conex_comp(n, ar);

		profiler.addSeries("Disjoint", "CompareDisj", "AssignDisj");
		profiler.createGroup("Disjoint_chart", "Disjoin");
		profiler.createGroup("comparison", "CompareDisj");
		profiler.createGroup("assignment", "AssignDisj");
	}

	profiler.showReport();
}

int _tmain(int argc, _TCHAR* argv[])
{
	test();
	//evaluation();
	_getch();
	return 0;
}

Last edited on
Topic archived. No new replies allowed.