//遗传算法求解图的最小连通支配集
int findMCDSWithGeneticAlgorithm() {
    
    //计算出度最大的节点,把它设为生成初始种群的起始节点
    calculateDegree();
    int startNode = 0;
    for (int i = 1; i < countOfNodes; i++) {
        if (degreeArr[i] > degreeArr[startNode]) {
            startNode = i;
        }
        nodesArr[i] = i;
    }
    //生成支配集
    for (int i = 0; i < countOfDominatingSets; i++) {
        createConnectedDominatingSet(countOfNodes, adjacencyMatrix, dominatingSetsArr[i], startNode);
        calculateCountOfBlackNodes(i);
        if (isThisGraphCDS(i) == 0) {
            printf("下标为%2d的个体不是支配集\n", i);
        }
        randomArrangeNodesOfCDS(i);
    }
    printf("生成的支配集是:\n");
    printDominatingSets(countOfNodes, countOfDominatingSets, dominatingSetsArr);//打印所有支配集
    findTheBestDominatingSetThisTurn();
    best = bestOfThisTurn;
    for (int i = 0; i < countOfNodes; i++) {
        bestDominatingSet[i] = dominatingSetsArr[bestOfThisTurnIndex][i];
    }
    
    int iteration = 1;//迭代次数
    //开始迭代
    while (iteration <= maxIteration) {
        
        printf("--------------------------------------第%d次迭代--------------------------------------\n", iteration);

        selection();        //选择
        crossover();        //交叉
        mutation();        //变异
        localOptimization();        //局部优化

        printResult();      //计算并打印本次迭代的结果

        for (int i = 0; i < countOfDominatingSets; i++) {
            int notCDS = 0;
            if (isThisGraphCDS(i) == 0) {
                printf("下标为%2d的个体不是支配集\n", i);
                notCDS++;
            }
            if (notCDS > 0) {
                exit(0);
            }
        }
        iteration++;
    }
    return best;
}
Example #2
0
File: aes.c Project: prabhaks/AES
static void specialDiv(unsigned char *rem1, unsigned char *rem2,
		unsigned char *quot, int remDegree, int quotDegree) {
	int tempQD = quotDegree;
	int rem1D = calculateDegree(rem1);
	unsigned char x = rem1[3 - rem1D];
	unsigned char inv;
	while (rem1D > remDegree) {
		inv = INV[(int) (rem2[3] / 16)][(int) (rem2[3] % 16)];
		quot[3 - tempQD] = bigDotProduct(inv, x);
		rem1[3 - rem1D] = rem1[3 - rem1D]
				^ (bigDotProduct(rem2[3], quot[3 - tempQD]));
		tempQD--;
		rem1D = calculateDegree(rem1);
		x = rem1[3 - rem1D];
	}
	quot[3 - tempQD] = bigDotProduct(inv, (x ^ 0x01));
	rem1[3 - rem1D] = rem1[3 - rem1D]
			^ (bigDotProduct(rem2[3], quot[3 - tempQD]));

}
Example #3
0
File: aes.c Project: prabhaks/AES
static void calLongHangDiv2(unsigned char *rem1, unsigned char *rem2,
		unsigned char *quot, int remDegree, int quotDegree) {
	int tempQD = quotDegree;
	int tempRD = remDegree;
	int rem1D = calculateDegree(rem1);
	unsigned char x = rem1[3 - rem1D];
	while (rem1D >= remDegree) {
		unsigned char inv = INV[(int) (rem2[3 - remDegree] / 16)][(int) (rem2[3
				- remDegree] % 16)];
		quot[3 - tempQD] = bigDotProduct(inv, x);
		int i = 0;
		while (tempRD >= 0) {
			rem1[3 - rem1D + i] = rem1[3 - rem1D + i]
					^ (bigDotProduct(rem2[3 - tempRD], quot[3 - tempQD]));
			i++;
			tempRD--;
		}
		tempQD--;
		rem1D = calculateDegree(rem1);
		x = rem1[3 - rem1D];
		tempRD = remDegree;
	}
}
Example #4
0
File: aes.c Project: prabhaks/AES
void ProcessInverse(char* poly) {
	table_check = 0;
	int i, k;
	if (poly == NULL || strlen(poly) != 8) {
		fprintf(stderr,
				"Error : polynomial length should be exactly equal to 4 bytes (8 hex string chars)\n");
		exit(1);
	}
	unsigned char *rem1 = (unsigned char *) malloc(sizeof(unsigned char *) * 4);
	for (i = 0; i < 3; i++) {
		rem1[i] = 0x00;
	}
	rem1[i] = 0x01;
	unsigned char *rem2 = (unsigned char *) malloc(sizeof(unsigned char *) * 4);
	for (i = 0, k = 0; i < 8; i = i + 2) {
		rem2[k++] = ((convertInHex("input polynomial", poly[i]) << 4)
				| (convertInHex("input polynomial", poly[i + 1]) & 0x0f))
				& 0xff;
	}
	unsigned char *orig_poly = (unsigned char *) malloc(
			sizeof(unsigned char *) * 4);
	memcpy(orig_poly, rem2, 4);
	unsigned char *quot = (unsigned char *) malloc(sizeof(unsigned char *) * 4);
	initQuot(quot);
	unsigned char *aux1 = (unsigned char *) malloc(sizeof(unsigned char *) * 4);
	for (i = 0; i < 4; i++) {
		aux1[i] = 0x00;
	}
	unsigned char *aux2 = (unsigned char *) malloc(sizeof(unsigned char *) * 4);
	for (i = 0; i < 3; i++) {
		aux2[i] = 0x00;
	}
	aux2[i] = 0x01;
	printInvOut(rem1, quot, aux1, 1);
	printInvOut(rem2, quot, aux2, 2);
	int remDegree, quotDegree, prevDegree = 4;
	remDegree = calculateDegree(rem2);
	quotDegree = prevDegree - remDegree;
	initQuot(quot);
	if (remDegree == 0) {
		specialDiv(rem1, rem2, quot, remDegree, quotDegree);
		calAux(quot, aux1, aux2);
		printInvOut(rem1, quot, aux1, 3);
		printOutInv(orig_poly, aux1);
		return;
	}
	calLongHangDiv(rem1, rem2, quot, remDegree, quotDegree);
	calAux(quot, aux1, aux2);
	printInvOut(rem1, quot, aux1, 3);
	if (checkEmpty(rem1) == 1) {
		printNoInv(orig_poly);
		return;
	}
	prevDegree = remDegree;
	remDegree = calculateDegree(rem1);
	quotDegree = prevDegree - remDegree;
	initQuot(quot);
	if (remDegree == 0) {
		specialDiv(rem2, rem1, quot, remDegree, quotDegree);
		calAux(quot, aux2, aux1);
		printInvOut(rem2, quot, aux2, 4);
		printOutInv(orig_poly, aux2);
		return;
	}
	calLongHangDiv2(rem2, rem1, quot, remDegree, quotDegree);
	calAux(quot, aux2, aux1);
	printInvOut(rem2, quot, aux2, 4);
	if (checkEmpty(rem2) == 1) {
		printNoInv(orig_poly);
		return;
	}

	prevDegree = remDegree;
	remDegree = calculateDegree(rem2);
	quotDegree = prevDegree - remDegree;
	initQuot(quot);
	if (remDegree == 0) {
		specialDiv(rem1, rem2, quot, remDegree, quotDegree);
		calAux(quot, aux1, aux2);
		printInvOut(rem1, quot, aux1, 5);
		printOutInv(orig_poly, aux1);
		return;
	}
	calLongHangDiv2(rem1, rem2, quot, remDegree, quotDegree);
	calAux(quot, aux1, aux2);
	printInvOut(rem1, quot, aux1, 5);
	if (checkEmpty(rem1) == 1) {
		printNoInv(orig_poly);
		return;
	}
	prevDegree = remDegree;
	remDegree = calculateDegree(rem1);
	quotDegree = prevDegree - remDegree;
	initQuot(quot);
	if (remDegree == 0) {
		specialDiv(rem2, rem1, quot, remDegree, quotDegree);
		calAux(quot, aux2, aux1);
		printInvOut(rem2, quot, aux2, 6);
		printOutInv(orig_poly, aux2);
		return;
	}

}