Ejemplo n.º 1
0
void shuffle2(char *t) {
	vertex *u, *v;
	int i, j;

	/* exact copy case */
	if (k_ >= l_) {
		strncpy(t, s_, l_);
		return;
	}

	/* simple permutation case */
	if (k_ <= 1) {
		strncpy(t, s_, l_);
		permutec(t, l_);
		return;
	}

	/* the Wilson algorithm for random arborescence */
	for (i = 0; i < n_vertices; i++)
		vertices[i].intree = 0;
	vertices[root].intree = 1;
	for (i = 0; i < n_vertices; i++) {
		u = &vertices[i];
		while (!u->intree) {
			u->next = (*randfunc)() % u->n_indices;
			u = &vertices[u->indices[u->next]];
		}
		u = &vertices[i];
		while (!u->intree) {
			u->intree = 1;
			u = &vertices[u->indices[u->next]];
		}
	}

	/* shuffle indices to prepare for walk */
	for (i = 0; i < n_vertices; i++) {
		u = &vertices[i];
		if (i != root) {
			j = u->indices[u->n_indices - 1];	/* swap the last one */
			u->indices[u->n_indices - 1] = u->indices[u->next];
			u->indices[u->next] = j;
			permutei(u->indices, u->n_indices - 1);	/* permute the rest */
		} else
			permutei(u->indices, u->n_indices);
		u->i_indices = 0;	/* reset to zero before walk */
	}

	/* walk the graph */
	strncpy(t, s_, k_ - 1);	/* the first let remains the same */
	u = &vertices[0];
	i = k_ - 1;
	while (u->i_indices < u->n_indices) {
		v = &vertices[u->indices[u->i_indices]];
		j = v->i_sequence + k_ - 2;
		t[i++] = s_[j];
		u->i_indices++;
		u = v;
	}
}
Ejemplo n.º 2
0
static void test_discrete_choice_structure() {
	time_t seed;
	time(&seed);
	MTwist rng(seed);
	// Range of discrete distribution
	const int N = TEST_SIZE;
	// N * M = number of samples taken
	const int M = TEST_SAMPLES;

	// Test the average results of applying our distribution algorithm for entity connection relationships.
	T tree(N*10);
	int j = 17;
	for (int i = 0; i < N; i++) {
		tree.insert(j, j);
		j = permutei(j, N);
	}

	std::vector<int> pick_count(N, 0);
	for (int i = 0; i < N * M; i++) {
		int p = tree.random_select(rng);
		pick_count.at(p)++;
	}

	ASSERT(pick_count[0] == 0, "'0' should not be picked!");
	// for i>0, normalize and output:
	for (int i = 1; i < N; i += INCREMENT) {
		printf("For %d: %.4f\n", i,pick_count[i] * SCALE_FACTOR / double(i));
	}
	StatCalc stats;
	for (int i = 1; i < N; i++) {
		stats.add_element(pick_count[i] * SCALE_FACTOR / double(i));
	}
	stats.print_summary();
}
Ejemplo n.º 3
0
static void measure_heap() {
	PERF_TIMER2("measure_heap");
	T tree;
	const int N = TEST_SIZE * TEST_SAMPLES;
	int j = N/2;
	for (int i = 0; i < N; i++) {
		tree.push(j);
		j = permutei(j, N);
		if ((j+i) % 52 == 0) {
			tree.pop();
		}
		if (tree.size() > 100) {
			while (!tree.empty()) {
				tree.pop();
			}
		}
	}
	while (!tree.empty()) {
		tree.pop();
	}
}