Ejemplo n.º 1
0
void CrossingsMatrix::init(Level &L)
{
	for (int i = 0; i < L.size(); i++)
	{
		map[i] = i;
		for (int j = 0; j < L.size(); j++)
			matrix(i,j) = 0;
	}

	for (int i = 0; i < L.size(); i++)
	{
		node v = L[i];
		const Array<node> &L_adj_i = L.adjNodes(v);

		for(auto pos_adj_k : L_adj_i)
		{
			for (int j = i + 1; j < L.size(); j++)
			{
				const Array<node> &L_adj_j = L.adjNodes(L[j]);

				for (auto pos_adj_l : L_adj_j)
				{
					matrix(i,j) += (pos_adj_k > pos_adj_l);
					matrix(j,i) += (pos_adj_l > pos_adj_k);
				}
			}
		}
	}
}
Ejemplo n.º 2
0
void BarycenterHeuristic::call (Level &L)
{
	const HierarchyLevels &levels = L.levels();

	for (int i = 0; i <= L.high(); ++i) {
		node v = L[i];
		long sumpos = 0L;

		const Array<node> &adjNodes = L.adjNodes(v);
		for(int j = 0; j <= adjNodes.high(); ++j)
			sumpos += levels.pos(adjNodes[j]);

		m_weight[v] = (adjNodes.high() < 0) ? 0.0 :
			double(sumpos) / double(adjNodes.size());
	}

	L.sort(m_weight);
}
Ejemplo n.º 3
0
void MedianHeuristic::call (Level &L)
{
	const HierarchyLevels &levels = L.levels();

	for (int i = 0; i <= L.high(); ++i) {
		node v = L[i];

		const Array<node> &adjNodes = L.adjNodes(v);
		const int high = adjNodes.high();

		if (high < 0) m_weight[v] = 0;
		else if (high & 1)
			m_weight[v] = levels.pos(adjNodes[high/2]) + levels.pos(adjNodes[1+high/2]);
		else
			m_weight[v] = 2*levels.pos(adjNodes[high/2]);
	}

	L.sort(m_weight,0,2*levels.adjLevel(L.index()).high());
}
Ejemplo n.º 4
0
void SiftingHeuristic::call(Level &L)
{
	List<node> vertices;
	int i;

	const int n = L.size();

	m_crossingMatrix->init(L); // initialize crossing matrix

	if (m_strategy == left_to_right || m_strategy == random) {
		for (i = 0; i < n; i++) {
			vertices.pushBack(L[i]);
		}

		if (m_strategy == random) {
			vertices.permute();
		}

	} else { // m_strategy == desc_degree
		int max_deg = 0;

		for (i = 0; i < n; i++) {
			int deg = L.adjNodes(L[i]).size();
			if (deg > max_deg) max_deg = deg;
		}

		Array<List<node>, int> bucket(0, max_deg);
		for (i = 0; i < n; i++) {
			bucket[L.adjNodes(L[i]).size()].pushBack(L[i]);
		}

		for (i = max_deg; i >= 0; i--) {
			while(!bucket[i].empty()) {
				vertices.pushBack(bucket[i].popFrontRet());
			}
		}
	}

	for(i = 0; i< vertices.size(); i++) {
		int dev = 0;

		// sifting left
		for(; i > 0; --i) {
			dev = dev - (*m_crossingMatrix)(i-1,i) + (*m_crossingMatrix)(i,i-1);
			L.swap(i-1,i);
			m_crossingMatrix->swap(i-1,i);
		}

		// sifting right and searching optimal position
		int opt = dev, opt_pos = 0;
		for (; i < n-1; ++i) {
			dev = dev - (*m_crossingMatrix)(i,i+1) + (*m_crossingMatrix)(i+1,i);
			L.swap(i,i+1);
			m_crossingMatrix->swap(i,i+1);
			if (dev <= opt) {
				opt = dev; opt_pos = i+1;
			}
		}

		// set optimal position
		for (; i > opt_pos; --i) {
			L.swap(i-1,i);
			m_crossingMatrix->swap(i-1,i);
		}
	}
}