Пример #1
0
int m_coloring(int i)
{
	int color;
		
	#ifdef DEBUG
	printf("%s, %d\n",__FUNCTION__, i);
	#endif
	if(promising(i))
	{
		if(i == num)
		{
			result = 1;
			#ifdef DEBUG
			printf("BICOLORABLE case\n");
			#endif
			return result;
		}
		else
		{
			
			#ifdef DEBUG
			printf("promising %d \n", i);
			#endif
			for(color = 1; color <= 2; color++)
			{
				vcolor[i+1] = color;
				m_coloring(i+1);
				if(result == 1 || result == -1)
					return result;
			}
			result = -1;
			return result;
		}
	}
	
			#ifdef DEBUG
			printf("not promising %d \n", i);
			#endif
}
Пример #2
0
void hamiltonian ( int node_index )
{
  int j, k;
  if( promising(node_index) )  // current node is valid in a Hamiltonian cycles
  {
     if( node_index == (node_amount) )  // at latest node
     {  hamiltonian_cycle_amount = 1;  // increase hamiltonian_cycle_amount
        printf(" v%d", result_array[1]);  // print out the result
        for( k = 2;k <= node_amount;k ++ )
          printf(" ¡÷ v%d", result_array[k]);
        printf("\n\n");
     }
     else  // not latest node
     {
        for( j = 2;j <= node_amount;j ++ )  // recursively scan from second node to latest node
        {
           result_array[node_index+1] = j; // store result
           hamiltonian( node_index+1 );  // call function recursively
        }
     }
  }

}
/***************************************************************************************
 *	@function : Implementation of 0/1 Knapsack Backtracking method. 
 *	@param: w - weights of n items 
 * 	@param: p - profits of n items
 *	@param: i - Counter (Initially 0)
 *	@param: profit - Profit of 1st item 
 *	@param: weight - Weight of 1st item
 *	@return: void
 ***************************************************************************************/
void backTracking(int *w, int *p, int i, int profit, int weight)
{

	if(weight<=capacity && profit>maxProfit)
	{
		int i;
		maxWeight=weight;
		maxProfit=profit;
		for(i=0;i<50;i++)			
			{	
				bestSet[i] = include[i];
				//printf("%d",include[i] );
			}
	}
	bool isPromising=promising(w,p,i,profit,weight);	
	
	if(isPromising)
	{
		include[cnt++] = 1;
		backTracking(w,p,i+1,profit+p[i+1],weight+w[i+1]); 
		include[cnt++] = 0;
		backTracking(w,p,i+1,profit,weight);
	}
}