Exemplo n.º 1
0
int main(){
    int size = 6;
    int arr[size];
    int arr1[6]={1,2,3,4,5,6};
    int arr2[6]={-11,-12,-13,-14,-15,-16};
    int res[6];
    int i;
    fillRand3(arr, size);

    for (i = 0; i < size; i++)
        printf("%d\t", arr[i]);
    printf("\nrez = %d\n",checkRand3(arr, size));
    printf("sred = %.3f\n",meanValue(arr, size));
    printf("min = %d\n", minValue(arr, size));
    printf("index= %i\n", meanIndex(arr, size));
    printf("index = %d\n", minIndex(arr, size));
    printf("elem = %d\n", maxOccurance(arr, size));
    printf("res = %d\n", diff(arr1, arr2, res, size));
    sub(arr1, arr2, res, size);
    for (i=0;i<size;i++)
        printf("%d\t", res[i]);
    printf("\neq= %d\n", eq(arr1, arr2, size));
    land(arr1, arr2, res, size);
    for (i=0;i<size;i++)
        printf("%d\t", res[i]);
    return 0;
}
Exemplo n.º 2
0
int main(){
    int size = 8;
    int arr[size];
    int arr1[8]={1,2,3,4,5,6,7,8};
    int arr2[8]={11,12,13,14,15,16,17,18};
    int res[8];

    int index;
    int i;
    srand(time(NULL));
        fillRand1(arr, size);
    for (i=0;i<size;i++)
        printf("%d\t", arr[i]);
        checkRand1(arr, size);
    printf("\n CheckRand = %d\n",checkRand1(arr, size));
        meanValue(arr, size);
    printf("Average value = %.3f\n",meanValue(arr, size));
        minValue(arr, size);
    printf("Min value = %d\n", minValue(arr, size));
        meanIndex(arr, size);
    printf("Index values near the average value = %d\n", meanIndex(arr, size));
        minIndex(arr, size);
    printf("Min index = %i \n ", minIndex(arr,size));
        maxOccurance(arr, size);
    printf("Value, which is most common in the array = %d\n", maxOccurance(arr, size));
        diff(arr1, arr2, res, size);
    printf("Diff = %d\n", diff(arr1, arr2, res, size));
       mult(arr1,arr2,res,size);
    printf("Mult\n");
    for (i=0;i<size;i++)
        printf("%d\t", res[i]);
        printf("\n");
        lt(arr1,arr2,size);
    printf("Less than = %d\n", lt(arr1, arr2, size));
        land(arr1,arr2,res,size);
    for (i=0;i<size;i++)
        printf("%d\t", res[i]);

    return 0;

}
Exemplo n.º 3
0
int main(void)
{
	int j[] = {4,10,19,42,15,53,36}, capacity = 7;
	
	printf("The smallest value in the vector is: %d\n", minValue(j, capacity));
	printf("The product of all the values in the vector is: %d\n", productOfAll(j, capacity));
	printf("The maximum value in the vector is: %d\n", maxValue(j, capacity));
	printf("The index of the minimum value of the vector is: %d\n", minIndex(j, capacity));
	printf("The sum of the vectors is: %d\n", sumAll(j, capacity));
	printf("The max value index is: %d\n", maxIndex(j, capacity));
	


}
Event Scheduler::getNextEvent ()
{
    size_t minIndex(0);
    double minTime(HUGE_VAL);
    for(size_t i(0); i < masterEL.size(); ++i)
    {
        if(masterEL[i].getCollisionTime() < minTime)
        {
            minTime = masterEL[i].getCollisionTime();
            minIndex = i;
        }
    }

    return masterEL[minIndex];
}
Exemplo n.º 5
0
	template <class T> vec2ul Grid2<T>::getMinIndex() const
	{
		vec2ul minIndex(0, 0);
		const T *minValue = &m_data[0];
		for (size_t y = 0; y < m_dimY; y++)
			for (size_t x = 0; x < m_dimX; x++)
			{
				const T *curValue = &m_data[y * m_dimX + x];
				if (*curValue < *minValue)
				{
					minIndex = vec2ul(x, y);
					minValue = curValue;
				}
			}
		return minIndex;
	}
Exemplo n.º 6
0
bool isParallelFreeUndirected(const Graph &G)
{
    if (G.numberOfEdges() <= 1) return true;

    SListPure<edge> edges;
    EdgeArray<int> minIndex(G), maxIndex(G);
    parallelFreeSortUndirected(G,edges,minIndex,maxIndex);

    SListConstIterator<edge> it = edges.begin();
    edge ePrev = *it, e;
    for(it = ++it; it.valid(); ++it, ePrev = e) {
        e = *it;
        if (minIndex[ePrev] == minIndex[e] && maxIndex[ePrev] == maxIndex[e])
            return false;
    }

    return true;
}
Exemplo n.º 7
0
template <class T> std::pair<UINT, UINT> Grid2D<T>::minIndex() const
{
	std::pair<UINT, UINT> minIndex(0, 0);
	const T *minValue = &m_data[0];
	for(UINT rowIndex = 0; rowIndex < m_rows; rowIndex++)
	{
		for(UINT colIndex = 0; colIndex < m_cols; colIndex++)
		{
			const T *curValue = &m_data[rowIndex * m_cols + colIndex];
			if(*curValue < *minValue)
			{
				minIndex = std::make_pair(rowIndex, colIndex);
				minValue = curValue;
			}
		}
	}
	return minIndex;
}
Exemplo n.º 8
0
template <class T> std::pair<size_t, size_t> Grid2<T>::minIndex() const
{
	std::pair<size_t, size_t> minIndex(0, 0);
	const T *minValue = &m_data[0];
	for(size_t rowIndex = 0; rowIndex < m_dimX; rowIndex++)
	{
		for(size_t colIndex = 0; colIndex < m_dimY; colIndex++)
		{
			const T *curValue = &m_data[rowIndex * m_dimY + colIndex];
			if(*curValue < *minValue)
			{
				minIndex = std::make_pair(rowIndex, colIndex);
				minValue = curValue;
			}
		}
	}
	return minIndex;
}
Exemplo n.º 9
0
int numSquares(int n)
{
    if(n == 1){return 1;}
    if(n == 2){return 2;}
    vector<int> num = {1};
    for(int i=2;i<=n;i++)
    {
        if(square(i))
        {
            num.push_back(1);
        }
        else
        {
            num.push_back(num[minIndex(num)]+1); 
            //cout << num[minIndex(num)]+1;
        }
    }
    return  num[n-1];
}
Exemplo n.º 10
0
void OptimalRanking::call (const Graph& G, NodeArray<int> &rank)
{
	List<edge> R;

	m_subgraph.get().call(G,R);

	EdgeArray<bool> reversed(G,false);
	for (edge e : R)
		reversed[e] = true;
	R.clear();

	EdgeArray<int> length(G,1);

	if(m_separateMultiEdges) {
		SListPure<edge> edges;
		EdgeArray<int> minIndex(G), maxIndex(G);
		parallelFreeSortUndirected(G, edges, minIndex, maxIndex);

		SListConstIterator<edge> it = edges.begin();
		if(it.valid())
		{
			int prevSrc = minIndex[*it];
			int prevTgt = maxIndex[*it];

			for(it = it.succ(); it.valid(); ++it) {
				edge e = *it;
				if (minIndex[e] == prevSrc && maxIndex[e] == prevTgt)
					length[e] = 2;
				else {
					prevSrc = minIndex[e];
					prevTgt = maxIndex[e];
				}
			}
		}
	}

	EdgeArray<int> cost(G,1);
	doCall(G, rank, reversed, length, cost);
}
Exemplo n.º 11
0
int main(){
int i, x, min, ind, ind2, chas, pr, pr2, riz, riz2;
float ser;
int v=4;

int myMatrix[4] =
{
1,2,3,4
};

int myMatrix2[4] =
{
1,2,3,4
};

int myMatrix3[4] =
{
1,2,3,4
};

int myMatrix4[4] =
{
0,0,0,0
};

int log1[4] =
{
0,1,0,1
};

int log2[4] =
{
1,1,0,0
};

puts("Proizvolnaia matriza");
srand(time(NULL));
fillRand2(myMatrix, v);
for(i = 0; i<4; i++){
            printf("%d\n",myMatrix[i]);
    }
puts(" \n");

x = checkRand2(myMatrix, v);
printf("Vse elementi ot -255 do 255?\n %d\n", x);
puts(" \n");

ser = meanValue(myMatrix, v);
printf("Srednee arifmeticheskoe\n %f\n", ser);
puts(" \n");

min = minValue(myMatrix, v);
printf("Minimalnii element masiva\n %d\n", min);
puts(" \n");

ind = meanIndex(myMatrix, v);
printf("index pervogo element, nablizhenii do ser ar\n %d\n", ind);
puts(" \n");

ind2 = minIndex(myMatrix, v);
printf("index pervogo minimalnogo\n %d\n", ind2);
puts(" \n");

chas = maxOccurance(myMatrix, v);
printf("samii chastii element (bolshii)\n %d\n", chas);
puts(" \n");

pr = diff(myMatrix, myMatrix2 , myMatrix3, v);
printf("vse raznizi = 0?\n %d\n", pr);
puts(" \n");

pr2 = diff(myMatrix, myMatrix, myMatrix3, v);
printf("vse raznizi = 0? (sdelano narochno)\n %d\n", pr2);
puts(" \n");

puts("Raznica 2-x matriz");
sub(myMatrix, myMatrix2, myMatrix3, v);
for(i = 0; i<4; i++){
            printf("%d\n",myMatrix3[i]);
    }
puts(" \n");

riz = lt(myMatrix, myMatrix2, v);
printf("vse menshe?\n %d\n", riz);
puts(" \n");

riz2 = lt(myMatrix4, myMatrix2, v);
printf("vse menshe?(narochno)\n %d\n", riz2);
puts(" \n");

puts("logicheskii AND");
land(log1, log2, myMatrix3, v);
for(i = 0; i<4; i++){
            printf("%d\n",myMatrix3[i]);
    }
puts(" \n");
return 0;
}
Exemplo n.º 12
0
//辅助函数:选出最小的两个节点,返回的是节点在的位置
//因为这里是用数组模拟节点的关系
void SelectMin(HuffmanTree HT, int k, int &min1, int &min2)
{
	min1 = minIndex(HT, k);
	min2 = minIndex(HT, k);
}
Exemplo n.º 13
0
void planarCNBGraph(Graph &G, int n, int m,	int b)	
{
	G.clear();
	if (b <= 0) b = 1;
	if (n <= 0) n = 1;
	if ((m <= 0) || (m > 3*n-6)) m = 3*n-6;
	
	node cutv;
	G.newNode();
	
	for (int nB=1; nB<=b; nB++){
		cutv = G.chooseNode();
		// set number of nodes for the current created block
		int actN = randomNumber(1, n);
		
		node v1 = G.newNode();
		
		if (actN <= 1){
			G.newEdge(v1, cutv);
		}
		else
			if (actN == 2){
				node v2 = G.newNode();
				G.newEdge(v1, v2);
				
				int rnd = randomNumber(1, 2);
				edge newE;
				int rnd2 = randomNumber(1, 2);
				if (rnd == 1){
					newE = G.newEdge(v1, cutv);
				}
				else{
					newE = G.newEdge(v2, cutv);
				}
				if (rnd2 == 1){
					G.contract(newE);
				}
			}
			else{
				// set number of edges for the current created block
				int actM;
				if (m > 3*actN-6)
					actM = randomNumber(1, 3*actN-6);
				else
					actM = randomNumber(1, m);
				if (actM < actN)
					actM = actN;
				
				int ke = actN-3, kf = actM-actN;
				
				Array<node> nodes(actN);
				Array<edge> edges(actM);
				Array<face> bigFaces(actM);
					
				// we start with a triangle
				node v2 = G.newNode(), v3 = G.newNode();
				nodes[0] = v1;
				nodes[1] = v2;
				nodes[2] = v3;
				edges[0] = G.newEdge(v1,v2);
				edges[1] = G.newEdge(v2,v3);
				edges[2] = G.newEdge(v3,v1);
				
				int actInsertedNodes = 3;
				
				CombinatorialEmbedding E(G);
				FaceArray<int> posBigFaces(E);
				int nBigFaces = 0, nEdges = 3;
				
				while(ke+kf > 0) {
					int p = randomNumber(1,ke+kf);
			
					if (nBigFaces == 0 || p <= ke) {
						int eNr = randomNumber(0,nEdges-1);
						edge e  = edges[eNr];
						face f  = E.rightFace(e->adjSource());
						face fr = E.rightFace(e->adjTarget());
						
						node u = e->source();
						node v = e->target();
						
						edges[nEdges++] = E.split(e);
						
						if (e->source() != v && e->source() != u)
							nodes[actInsertedNodes++] = e->source();
						else
							nodes[actInsertedNodes++] = e->target();

						if (f->size() == 4) {
							posBigFaces[f] = nBigFaces;
							bigFaces[nBigFaces++] = f;
						}
						if (fr->size() == 4) {
							posBigFaces[fr] = nBigFaces;
							bigFaces[nBigFaces++] = fr;
						}
			
						ke--;
					}
					else {						
						int pos = randomNumber(0,nBigFaces-1);
						face f = bigFaces[pos];
						int df = f->size();
						int i = randomNumber(0,df-1), j = randomNumber(2,df-2);
			
						adjEntry adj1;
						for (adj1 = f->firstAdj(); i > 0; adj1 = adj1->faceCycleSucc())
							i--;
			
						adjEntry adj2;
						for (adj2 = adj1; j > 0; adj2 = adj2->faceCycleSucc())
							j--;
			
						edge e = E.splitFace(adj1,adj2);
						edges[nEdges++] = e;
						
						face f1 = E.rightFace(e->adjSource());
						face f2 = E.rightFace(e->adjTarget());
			
						bigFaces[pos] = f1;
						posBigFaces[f1] = pos;
						if (f2->size() >= 4) {
							posBigFaces[f2] = nBigFaces;
							bigFaces[nBigFaces++] = f2;
						}
						if (f1->size() == 3) {
							bigFaces[pos] = bigFaces[--nBigFaces];
						}
						
						kf--;
					}
				}
						
				// delete multi edges
				SListPure<edge> allEdges;
				EdgeArray<int> minIndex(G), maxIndex(G);
		
				parallelFreeSortUndirected(G,allEdges,minIndex,maxIndex);
		
				SListConstIterator<edge> it = allEdges.begin();
				edge ePrev = *it, e;
				for(it = ++it; it.valid(); ++it, ePrev = e) {
					e = *it;
					if (minIndex[ePrev] == minIndex[e] &&
						maxIndex[ePrev] == maxIndex[e])
					{
						G.move(e,
							e->adjTarget()->faceCycleSucc()->twin(), ogdf::before,
							e->adjSource()->faceCycleSucc()->twin(), ogdf::before);
					}
				}
				
				node cutv2 = nodes[randomNumber(0,actN-1)];
				
				int rnd = randomNumber(1,2);
				edge newE = G.newEdge(cutv2, cutv);
				if (rnd == 1){
					G.contract(newE);
				}
			}
	}
};
Exemplo n.º 14
0
template <class T> const T& Grid2<T>::minValue() const
{
	std::pair<size_t, size_t> index = minIndex();
	return m_data[index.first * m_dimY + index.second];
}
Exemplo n.º 15
0
int main()
{
	initialize();
	double minDistance = DBL_MAX; // The very largest allowed value for double type.
	for (int i = 0; i < iteration; ++i)
	{
		iterationInitialize();
		printf("Iterating %d ...\n", i + 1);

		for (int j = 0; j < nrOfAnts; ++j)
		{
			for (int iIndex = 0; iIndex < nrOfCities; ++iIndex)
			{
				routeIndex[iIndex].cumP = 0;
				routeIndex[iIndex].visited = 0;
			}

			int temp = rand() % nrOfCities; // Randomly choose a city as the very starting point for the ant j.
			route[j][0] = temp;
			routeIndex[temp].visited = 1;

			// Determine the next city given the current city, and a complete route covering all cities is built for the ant j afterwards.
			for (int k = 1; k < nrOfCities - 1; ++k)
			{
				route[j][k] = transit(routeIndex, route[j][k - 1]);
			}

			// Set the only unvisited city as the last city.
			for (int iIndex = 0; iIndex < nrOfCities; ++iIndex)
			{
				if (!routeIndex[iIndex].visited)
				{
					route[j][nrOfCities - 1] = iIndex;
				}
			}

			distance[j] = getDistance(route[j], nrOfCities);
		}

		// The following 2 double-loop update the pheromone matrix.
		for (int j = 0; j < nrOfAnts; ++j)
		{
			for (int k = 0; k < nrOfCities - 1; ++k)
			{
				deltaTau[route[j][k]][route[j][k + 1]] = Q / distance[j];
			}
		}

		for (int j = 0; j < nrOfCities; ++j)
		{
			for (int k = 0; k < nrOfCities; ++k)
			{
				tau[j][k] += (1 - rho) * tau[j][k] + deltaTau[j][k];
			}
		}

		int minIdx = minIndex(distance, nrOfAnts);
		int minRoute[nrOfCities];
		if (distance[minIdx] < minDistance)
		{
			minDistance = distance[minIdx];
			copyArrayElement(route[minIdx], minRoute, nrOfCities);
		}

		printf("The minimum distance: %lf\n", minDistance);
		printf("Corresponding route:\n");
		for (int i = 0; i < nrOfCities - 1; ++i)
		{
			printf("%d->", route[minIdx][i]+1);
		}
		printf("%d\n", route[minIdx][nrOfCities - 1]+1);
	}

	return 0;
}
Exemplo n.º 16
0
template <class T> const T& Grid2D<T>::minValue() const
{
	std::pair<UINT, UINT> index = minIndex();
	return m_data[index.first * m_cols + index.second];
}
Exemplo n.º 17
0
void planarBiconnectedGraph(Graph &G, int n, int m, bool multiEdges)
{
	if (n < 3) n = 3;
	if (m < n) m = n;
	if (m > 3*n-6) m = 3*n-6;

	int ke = n-3, kf = m-n;

	G.clear();
	
	Array<edge> edges(m);
	Array<face> bigFaces(m);
	//random_source S;

	// we start with a triangle
	node v1 = G.newNode(), v2 = G.newNode(), v3 = G.newNode();
	edges[0] = G.newEdge(v1,v2);
	edges[1] = G.newEdge(v2,v3);
	edges[2] = G.newEdge(v3,v1);

	CombinatorialEmbedding E(G);
	FaceArray<int> posBigFaces(E);
	int nBigFaces = 0, nEdges = 3;

	while(ke+kf > 0) {
		int p = randomNumber(1,ke+kf);

		if (nBigFaces == 0 || p <= ke) {
			edge e  = edges[randomNumber(0,nEdges-1)];
			face f  = E.rightFace(e->adjSource());
			face fr = E.rightFace(e->adjTarget());

			edges[nEdges++] = E.split(e);

			if (f->size() == 4) {
				posBigFaces[f] = nBigFaces;
				bigFaces[nBigFaces++] = f;
			}
			if (fr->size() == 4) {
				posBigFaces[fr] = nBigFaces;
				bigFaces[nBigFaces++] = fr;
			}

			ke--;

		} else {
			int pos = randomNumber(0,nBigFaces-1);
			face f = bigFaces[pos];
			int df = f->size();
			int i = randomNumber(0,df-1), j = randomNumber(2,df-2);

			adjEntry adj1;
			for (adj1 = f->firstAdj(); i > 0; adj1 = adj1->faceCycleSucc())
				i--;

			adjEntry adj2;
			for (adj2 = adj1; j > 0; adj2 = adj2->faceCycleSucc())
				j--;

			edge e = E.splitFace(adj1,adj2);
			edges[nEdges++] = e;

			face f1 = E.rightFace(e->adjSource());
			face f2 = E.rightFace(e->adjTarget());

			bigFaces[pos] = f1;
			posBigFaces[f1] = pos;
			if (f2->size() >= 4) {
				posBigFaces[f2] = nBigFaces;
				bigFaces[nBigFaces++] = f2;
			}
			if (f1->size() == 3) {
				bigFaces[pos] = bigFaces[--nBigFaces];
			}

			kf--;
		}
	}

	if (multiEdges == false) {
		SListPure<edge> allEdges;
		EdgeArray<int> minIndex(G), maxIndex(G);

		parallelFreeSortUndirected(G,allEdges,minIndex,maxIndex);

		SListConstIterator<edge> it = allEdges.begin();
		edge ePrev = *it, e;
		for(it = ++it; it.valid(); ++it, ePrev = e) {
			e = *it;
			if (minIndex[ePrev] == minIndex[e] &&
				maxIndex[ePrev] == maxIndex[e])
			{
				G.move(e,
					e->adjTarget()->faceCycleSucc()->twin(), ogdf::before,
					e->adjSource()->faceCycleSucc()->twin(), ogdf::before);
			}
		}

	}
}