void * taskGreedySearch(void *arg_in)
{
    Node **nodeArray = (Node **)arg_in;
    int last_state, last_type;
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &last_state);
    pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &last_type);
    DEBUG("Task three");

    DEBUG("Trying a simple greedy algorithm, no 3-clique optimization.");
    // Find the node with the highest vertex count and mark it RED
    int candidate = 0;
    for (unsigned int cntr = 1; cntr < g_count; cntr++)
    {
        if (nodeArray[candidate]->getVertexCount() < nodeArray[cntr]->getVertexCount())
        {
            candidate = cntr;
        }
    }
    // Find the node next to it with the highest vertex cound and mark it GREEN
    set<int>::iterator iter = nodeArray[candidate]->getVertices();
    int second = -1;
    while (iter != nodeArray[candidate]->getVerticesEnd())
    {
        int test = *iter;
        iter++;
        if (second == -1 || nodeArray[second]->getVertexCount() < nodeArray[test]->getVertexCount())
        {
            second = test;
				}
    }
    // Start marking all the nodes with the appropriate colors.
    Color colors[g_count];
    for (unsigned int cntr = 0; cntr < g_count; cntr++)
    {
        colors[cntr] = UNKNOWN;
    }
    int colored = 0;
    try
    {
       colored += setNodeColor(nodeArray, colors, nodeArray[candidate], RED);
       colored += setNodeColor(nodeArray, colors, nodeArray[second], GREEN);
    }
    catch (colorexception e)
    {
        DEBUG("Got an exception coloring the graph, must be unsolveable.");
        setSolution(NOT_COLORABLE, NULL);

        return ((void *) &SUCCESS);
    }
    
    // Could not do a straight compute of the answer - Time to get greedy
    Color colorTest[g_count];
    for (unsigned int cntr = 0; cntr < g_count; cntr++)
    {
        colorTest[cntr] = colors[cntr];
    }
    Solution greedySolution = greedySearch(nodeArray, colorTest);
    if (greedySolution == SOLVEABLE)
    {
        DEBUG("Greedy search found the problem to be solveable.");
        setSolution(SOLVEABLE, colorTest);
    }
    else
    {
        DEBUG("Greedy search returned %d.", greedySolution);
        setSolution(NOT_COLORABLE, NULL);
    }

    return ((void *) &SUCCESS);
}
示例#2
0
int main(int argc, char const *argv[]) {
	int running = 1, cost = 0, i, j;
	char input[BUFFER_SIZE], *aft;
	char vertex, dest, option;
	Graph *g = NULL;
	createGraph(&g);

	#ifdef DEV

	insertVertex(g, 'a');
	insertVertex(g, 'b');
	insertVertex(g, 'c');
	insertVertex(g, 'd');
	insertVertex(g, 'e');
	insertVertex(g, 'f');
	insertEdge(g, 'a', 'b', 100);
	insertEdge(g, 'a', 'c', 300);
	insertEdge(g, 'a', 'a', 100);
	insertEdge(g, 'd', 'b', 70);
	insertEdge(g, 'f', 'c', 10);
	insertEdge(g, 'a', 'f', 10);
	insertEdge(g, 'a', 'f', 20);
	insertEdge(g, 'a', 'a', 100);
	displayGraph(g);
	greedySearch(g, 'a');
	destroyGraph(g);
	#else

        while (running) {
                memset(input, 0, BUFFER_SIZE);
                printf("\n1 - inserir vertice\n");
                printf("2 - inserir aresta\n");
                printf("3 - remover vertice\n");
                printf("4 - remover aresta\n");
                printf("5 - imprimir grafo\n");
                printf("6 - Busca Gulosa\n");
                printf("7 - sair\n");
                printf("\n$");

                scanf(" %c", &option);
                switch (option) {
                case '1':
                        printf("Digite um id para o novo no (apenas um caracter)\n");
                        printf(">>");
                        scanf(" %c", &vertex);
                        if (!insertVertex(g, vertex)) {
                                printf("Vertice inserido!\n");
                                printf("Digite os vizinhos (exemplo:b10 c20 d30) obs: caso o valor nao seja indicado o default sera 0\n");
                                getchar();
                                fgets(input, BUFFER_SIZE, stdin);
                                for(i = 0; i < strlen(input); i++) {
                                        if(isalpha(input[i])) {
                                                dest = input[i];
                                                for(j = i+1; j <= strlen(input); j++) {
                                                        if(isdigit(input[j])) {
                                                                cost = strtod(&input[j], &aft);
                                                                i = (aft - input) -1;
                                                                if(!insertEdge(g, vertex, dest, cost))
                                                                        printf("Aresta inserida [%c] - [%c] com custo %d!\n", vertex, dest, cost);
                                                                break;
                                                        }
                                                        if(isalpha(input[j]) || input[j] == '\0') {
                                                                if(!insertEdge(g, vertex, dest, 0))
                                                                        printf("Aresta inserida [%c] - [%c] com custo 0!\n", vertex, dest);
                                                                break;
                                                        }
                                                }
                                        }
                                }
                        }

                        break;
                case '2':
                        printf("Digite um par de vertices e um custo para a nova aresta (exemplo: A B 100)\n");
                        printf(">>");
                        scanf(" %c %c %d", &vertex, &dest, &cost);
                        if(!insertEdge(g, vertex, dest, cost))
                                printf("Aresta inserida!\n");
                        break;
                case '3':
                        printf("Digite o id do vertice a ser deletado\n");
                        printf(">>");
                        scanf(" %c", &vertex);
                        if(!removeVertex(g, vertex))
                                printf("vertice removido\n");
                        break;
                case '4':
                        printf("Digite o par de vertices e o custo da aresta (exemplo: A B 100)\n");
                        printf(">>");
                        scanf(" %c %c %d", &vertex, &dest, &cost);
                        if(!removeEdge(g, vertex, dest, cost))
                                printf("aresta removida!\n");
                        break;
                case '5':
                        displayGraph(g);
                        break;
                case '6':
                        printf("Digite um vertice para inicio da busca\n");
                        printf(">>");
                        scanf(" %c", &vertex);
                        greedySearch(g, vertex);
                        break;

                case '7':
                        printf("Ate logo\n");
                        running = 0;
                        destroyGraph(g);
                        break;
                default:
                        printf("Opcao invalida\n");
                        break;
                }
        }
	#endif
	return 0;
}
void * taskGreedyThreeClique(void *arg_in)
{
    Node **nodeArray = (Node **)arg_in;
    int last_state, last_type;
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &last_state);
    pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &last_type);
    vector<ThreeClique> threeCliqueVector;
    DEBUG("Task two");

    // Determine if we have any 3 cliques
    // If we do start with the three clique with the highest vertex count.
    // Otherwise pick the single node with the highest vertex count.
    for (unsigned int aindex = 0; aindex + 3 < g_count; aindex++)
    {
        pthread_testcancel();
        if (nodeArray[aindex]->getVertexCount() < 3)
        {
            // This node does not have enough vertices to be a 4-clique
            continue;
        }
        set<int>::iterator b = nodeArray[aindex]->getVerticesAtOrAfter(aindex + 1);
        while (b != nodeArray[aindex]->getVerticesEnd())
        {
            int bindex = *b;
            b++;
            // Would be nice to skip the last 2 elements but the iterator has no "remaining" access.
            set<int>::iterator c = nodeArray[bindex]->getVerticesAtOrAfter(bindex + 1);
            while (c != nodeArray[bindex]->getVerticesEnd())
            {
                int cindex = *c;
                c++;
                if (! nodeArray[aindex]->hasVertex(cindex))
                {
                    continue;
                }
                // We have found a 3-clique add it to the list.
                threeCliqueVector.push_back(ThreeClique(nodeArray[aindex], nodeArray[bindex], nodeArray[cindex]));
                DEBUG(threeCliqueVector[threeCliqueVector.size() - 1].print().c_str());
            }
        }
    }

    if (threeCliqueVector.size() > 0)
    {
        DEBUG("We have %ld three-cliques, going greedy that way.", threeCliqueVector.size());
        // Determine the 3-count with the highest vertex count and start there.
        ThreeClique greediestClique = threeCliqueVector[0];
        unsigned int cntr;
        for (cntr = 1; cntr < threeCliqueVector.size(); cntr++)
        {
            pthread_testcancel();
            ThreeClique testClique = threeCliqueVector[cntr];
            if (greediestClique.getVertexCount() < testClique.getVertexCount())
            {
                DEBUG(testClique.print().c_str());
                greediestClique = testClique;
            }
        }
        DEBUG(greediestClique.print().c_str());

        // Start marking all the nodes with the appropriate colors.
        Color colors[g_count];
        for (cntr = 0; cntr < g_count; cntr++)
        {
            colors[cntr] = UNKNOWN;
        }
        unsigned int colored = 0;
        try
        {
           colored += setNodeColor(nodeArray, colors, greediestClique.getNodeA(), RED);
           colored += setNodeColor(nodeArray, colors, greediestClique.getNodeB(), GREEN);
           colored += setNodeColor(nodeArray, colors, greediestClique.getNodeC(), BLUE);
        }
        catch (colorexception e)
        {
            DEBUG("Got an exception coloring the graph, must be unsolveable.");
            setSolution(NOT_COLORABLE, NULL);

            return ((void *) &SUCCESS);
        }
        if (colored >= g_count)
        {
            DEBUG("We have colored %d nodes.", colored);

            // We were able to do a straight compute of the colors - return the result.
            setSolution(SOLVEABLE, colors);

            return ((void *) &SUCCESS);
        }

        // Could not do a straight compute of the answer - Time to get greedy
        Color colorTest[g_count];
        for (cntr = 0; cntr < g_count; cntr++)
        {
            colorTest[cntr] = colors[cntr];
        }
        Solution greedySolution = greedySearch(nodeArray, colorTest);
        if (greedySolution == SOLVEABLE)
        {
            DEBUG("Greedy search found the problem to be solveable.");
            setSolution(SOLVEABLE, colorTest);
        }
        else
        {
            DEBUG("Greedy search returned %d.", greedySolution);
            setSolution(NOT_COLORABLE, NULL);
        }
    }

    // If we found no 3-cliques that proves nothing.
    return ((void *) &SUCCESS);
}
示例#4
0
文件: main.c 项目: karahiyo/ipvs
/*===============================================================
  routine    : main
  return val : int
  input  par : int argc
               char *argv[]
  function   : main function
 ===============================================================*/
int main(int argc, char *argv[])
{
  double psnr;
  int headframe, tailframe, skipframe;
  char *srcname, c, *dd, filename[255];
  register int i, j;

  clock_t start, end;

  fprintf(stderr, "\nstarting program...\n");
  system("date");
  start = clock();

  /* パラメータの初期化 */
  srcname   = SRC_NAME;
  headframe = HEAD_FRAME;
  tailframe = TAIL_FRAME;
  skipframe = SKIP_FRAME;

  /* パラメータの設定 */
  if (argc < 1) {
    fprintf(stderr, "usage : %s [option]\n", argv[0]);
    fprintf(stderr, "  -s <string>  source image filename\n");
    fprintf(stderr, "  -h           help\n");
    exit(1);
  }  
  while (--argc > 0) {
    c = (*++argv)[0];
    if (!(c == '-')) fprintf(stderr, "\nunknown option...\n");
    else {
      c = (*argv)[1];  dd = &(*argv)[1];
      switch (c) {
      case 'h':
        fprintf(stderr, "\nusage : command [option]\n");
        fprintf(stderr, "  -s <string>  source image filename\n");
        fprintf(stderr, "  -h           help\n");
        exit(1);
        break;
      case 's':
        if (--argc > 0) {
          c = (*++argv)[0];
          if (c == '-') fprintf(stderr, "\noption error...\n");
          else srcname = (*argv);
        } else {
          fprintf(stderr, "\noption error \"%s\"...\n", (*argv));
          exit(1);
        }
        break;
      default:
        fprintf(stderr, "\nunknown option...\n");
        break;
      }
    }
  }
  fprintf(stderr, "image size = %dx%d pixel\n", SRC_X_SIZE, SRC_Y_SIZE);
  fprintf(stderr, "macro block size = %dx%d pixel\n", MB_SIZE, MB_SIZE);
  fprintf(stderr, "search window  x = [-%d:+%d], y = [-%d:+%d]\n",
          SW_SIZE, SW_SIZE, SW_SIZE, SW_SIZE);
  fprintf(stderr, "head frame number = %d, tail frame number = %d (skip:%d)\n",
          headframe, tailframe, skipframe);

  /* 先頭フレーム名の作成 */
  makeFileName(filename, srcname, headframe, ".cif");
  fprintf(stderr, "\n[frame no.%d]\n", headframe);
  fprintf(stderr, "now loading frame data from \"%s\"...\n", filename);
  /* 先頭フレームの読み込み */
  loadRawFloat(filename, premap);

  for (j = headframe + skipframe; j <= tailframe; j += skipframe) {  
    /* 動きベクトルの初期化 */
    for (i = 0; i < MB_X_NUM*MB_Y_NUM; i++) 
      vecx[i] = vecy[i] = 0.0;
    /* フレーム番号情報の表示 */
    fprintf(stderr, "\n[frame no.%d]-[frame no.%d]\n", j - skipframe, j);
    /* 次フレーム名の作成 */
    makeFileName(filename, srcname, j, ".cif");
    fprintf(stderr, "now loading frame data from \"%s\"...\n", filename);
    /* 次フレームの読み込み */
    loadRawFloat(filename, crtmap);
    /* Three Step Search */
    greedySearch(premap, crtmap, vecy, vecx);
    /* 予測画像の作成 */
    makePrediction(premap, mcmap, vecy, vecx);
    /* PSNR の算出 */
    psnr = getPsnrFloat(crtmap, mcmap);
    fprintf(stderr, "psnr = %f [dB]\n", psnr);
    /* フレームの複写 */
    for (i = 0; i < SRC_Y_SIZE*SRC_X_SIZE; i++)
      premap[i] = crtmap[i];
  }

  fprintf(stderr, "\ndone!!\n");
  system("date");
  end = clock();

  fprintf(stderr, "TIME=%d[ms]\n",end-start);
  return 0;
}