Esempio n. 1
0
int main(){
    int i,j,k,start,end,count,root,des;
    int Vnum;
    unsigned long max=MAX_LENGTH;
    char *str;
    str = (char *)malloc(max*sizeof(char));
    char p[100];
    char tmp[100];
    Node *array=NULL;
    Node *tra=NULL;
loop: while(getline(&str,&max,stdin)!=EOF)
    {
        if(str[0]=='V')
        {
            array=cleargraph(array);
            memset(tmp,'\0',sizeof(tmp));
            i=2;
            count = 0;
            while((*(str+i)>=48&&*(str+i)<=57)||*(str+i)=='-')
            {
                tmp[count]= *(str+i);
                i++;
                count++;
            }
            Vnum = getint(tmp);
            if(Vnum<0)
            {
                printf("Error: invalid input about the number of V.\n");
                continue;
            }
            array = (Node *)malloc(Vnum*sizeof(Node));
            if(array==NULL)
            {
                printf("Error: there is not enough room for storing the graph.\n");
                return false;
            }
            //printf("Vnum=%d\n",Vnum);
            for(i=0;i<Vnum;i++)       //initialize
            {
                array[i].ID=i;
                array[i].next=NULL;
                //printf("%d\n",array[i].ID);
            }
            
            
        }
        else if(str[0]=='E')
        {
            if(array==NULL)
            {
                printf("Error: the graph does not exist.\n");
                array=cleargraph(array);
                continue;
            }
            edgenum=0;
            for(i=0;*(str+i)!='\0';i++)
            {
                if(*(str+i)=='<')
                {
                    memset(tmp,'\0',sizeof(tmp));
                    i++;
                    count = 0;
                    while((*(str+i)>=48&&*(str+i)<=57)||*(str+i)=='-')
                    {
                        tmp[count]= *(str+i);
                        i++;
                        count++;
                    }
                    start = atoi(tmp);
                    if(start>Vnum-1||start<0)
                    {
                        printf("Error: a vertex specified does not exist.\n");
                        array=cleargraph(array);
                        goto loop;
                    }
                    i++;    //omit ','
                    count = 0;
                    memset(tmp,'\0',sizeof(tmp)); //initialize tmp array
                    while((*(str+i)>=48&&*(str+i)<=57)||*(str+i)=='-')
                    {
                        tmp[count]= *(str+i);
                        i++;
                        count++;
                    }
                    end = atoi(tmp);
                    if(end>Vnum-1||end<0)
                    {
                        printf("Error: a vertex specified does not exist.\n");
                        array=cleargraph(array);
                        goto loop;
                    }
                    if(start != end)
                    {
                        //store a edge
                        if(insertedge(array,start,end)==false)
                        {
                            array=cleargraph(array);
                            goto loop;
                        }
                        edgenum++;
                    }
                    
                    long long vv = (long)Vnum * (long)Vnum;
                    if(edgenum>vv)
                    {
                        printf("Error: the number of edges is beyond the limit\n");
                        array=cleargraph(array);
                        goto loop;
                    }
                }
            }
            if(edgenum==0)
            {
                goto loop;
            }
            int n = Vnum;
            int **arr;
            SAT_Manager mgr; 
            int *clause1, *clause2, *clause3, *clause4;
            for(k=1;k<=Vnum;k++)        //vertex cover of size k
            {  
                int multiply = n*k;     //Vnum*k atoms
                mgr = SAT_InitManager();
                SAT_SetNumVariables(mgr, multiply);
                //dynamic assign two-dimention array
                arr = (int **)malloc(sizeof(int *)*n);
                if(!arr)
                {
                    printf("Error: there is not enough room.\n");
                    SAT_ReleaseManager(mgr);
                    return false;
                }
                memset(arr, 0, sizeof(int *)*n);
                while(n--)
                {
                    arr[n] = (int *)malloc(sizeof(int)*k);
                }
                n = Vnum;
                int index = 1;
                //initialize the VarIndex
                for(i=0;i<n;i++)
                {
                    for(j=0;j<k;j++)
                        arr[i][j]= index++;
                }
                //create clause
                
                //test
                // for(i=0;i<n;i++)
                // {
                //     for(j=0;j<k;j++)
                //         printf("arr[%d][%d]=%d\n",i,j,arr[i][j]);
                // }

                clause1 = (int *)malloc(sizeof(int)*n);
                for(i=0;i<k;i++)
                {
                    for(j=0;j<n;j++)
                    {
                        clause1[j]= arr[j][i]<<1;
                    }
                    SAT_AddClause(mgr, clause1, n);
                    
                }
                
                //free(clause);
                clause2 = (int *)malloc(sizeof(int)*2);
                int r;
                for(i=0;i<n;i++)
                {
                    for(j=0;j<k;j++)
                    {
                        for(r=j+1;r<k;r++)
                        {

                            clause2[0] = (arr[i][j]<<1)+1;
                            clause2[1] = (arr[i][r]<<1)+1;
                            SAT_AddClause(mgr, clause2, 2);
                        }                     
                    }
                }
                //free(clause);
                clause3 = (int *)malloc(sizeof(int)*2);
                for(i=0;i<k;i++)
                {
                    for(j=0;j<n;j++)
                    {
                        for(r=j+1;r<n;r++)
                        {
                            clause3[0] = (arr[j][i]<<1)+1;
                            //printf("%d ",clause3[0]);
                            clause3[1] = (arr[r][i]<<1)+1;
                            //printf("%d\n",clause3[1]);
                            SAT_AddClause(mgr,clause3,2);
                        }
                        
                    }
                }

                //free(clause);
                clause4 = (int *)malloc(sizeof(int)*2*k);
                Node * point;
                int m;
                for(i=0;i<n;i++)
                {
                    point = array+i;
                    while(point->next)
                    {
                        point = point->next;
                        j = point->ID;
                        m=0;
                        //printf("j=%d\n",j);
                        for(r=0;r<k;r++)
                        {
                            clause4[m++] = arr[i][r]<<1;
                            //printf("arr[%d][%d]=%d\n",i,r,arr[i][r]<<1);
                            clause4[m++] = arr[j][r]<<1;
                            //printf("arr[%d][%d]=%d\n",j,r,arr[j][r]<<1);
                        }
                        SAT_AddClause(mgr, clause4, 2*k);
                        // printf("i=%d k=%d\n",i,k);
                         // printf("clause4\n");
                         // for(int bbb=0;bbb<2*k;bbb++)printf("%d ",clause4[bbb]);
                         // printf("\n");
                    }
                }

                //free(clause);
                //finish adding clause
                int pp[2], bak, idx[n];
                pipe(pp);
                bak = dup(STDOUT_FILENO);
                dup2(pp[1],STDOUT_FILENO);
                int result = SAT_Solve(mgr);
                dup2(bak, STDOUT_FILENO);
                int pre = -1;
                
                if(result == SATISFIABLE)
                {

                    int idx_num = SAT_NumVariables(mgr);
                    //printf("idx_num=%d\n",idx_num);
                    for(i=1;i<=idx_num;i++)
                    {
                        //printf("i=%d\n",i);
                        int a = SAT_GetVarAsgnment(mgr, i);
                        //printf("%d\n",a);
                        if(a==1)
                        {
                            if(pre!= -1)
                            {
                                printf("%d ",pre);
                                fflush(stdout);
                            }
                            pre = (i-1)/k;
                            //printf("i=%d,pre=%d\n",i,pre);
                        }
                        else if(a == -1){
                            printf("Error: not get the right result\n");
                            fflush(stdout);
                            goto loop;
                        }
                    }
                    printf("%d\n",pre);
                    fflush(stdout);
                    for(i=0;i<n;i++)
                        free(arr[i]);
                    free(arr);
                    free(clause1);
                    free(clause2);
                    free(clause3);
                    free(clause4);
                    SAT_ReleaseManager(mgr);
                    goto loop;
                }
            }
            for(i=0;i<n;i++)
                free(arr[i]);
            free(arr);
            SAT_ReleaseManager(mgr);
            printf("unsat\n");
            fflush(stdout);
            goto loop;
        }     
        else if(str[0]=='s')
        {

            i=0;
            if(array==NULL)
            {
                printf("Error: there is no graph existing, please input a new graph firstly\n");
                goto loop;
            }
            //for(i=0;*(str+i)!='\0';i++)printf("i=%d %c\n",i,*(str+i));
            i+=2;
            if(*(str+i)!='\0')
            {
                //while(*(str+i)<48||*(str+i)>57)i++;
                memset(tmp,'\0',sizeof(tmp));
                count = 0;
                while((*(str+i)>=48&&*(str+i)<=57)||*(str+i)=='-')
                {
                    tmp[count]= *(str+i);
                    i++;
                    count++;
                }
                //printf("%s\n",tmp);
                root = atoi(tmp);
                //printf("root=%d\n",root);
                if(root<0 || root> Vnum-1)
                {
                    printf("Error: a vertex specified does not exist.\n");
                    goto loop;
                }
                //while(*(str+i)<48||*(str+i)>57)i++;
                i++;
                memset(tmp,'\0',sizeof(tmp));
                count = 0;
                while((*(str+i)>=48&&*(str+i)<=57)||*(str+i)=='-')
                {
                    tmp[count]= *(str+i);
                    i++;
                    count++;
                }
                
                des = atoi(tmp);
                //printf("des=%d\n",des);
                if(des<0 || des> Vnum-1)
                {
                    printf("Error: a vertex specified does not exist.\n");
                    goto loop;
                }    
            }
            shortestpath(array,Vnum,root,des);
            printf("\n");
        }
        
    }
    free(array);
    return true;
}
Esempio n. 2
0
void initgraph(int larg, int haut)
{
    char *window_name[2] = {"Fenetre graphique",NULL};
    char *icon_name[2] = {"graph", NULL};
    XTextProperty windowName, iconName;
    unsigned long wattrmask;
    XSetWindowAttributes wattr;
    unsigned long gcattrmask;
    XGCValues gcattr;
    XEvent report;

    /* déja initialisée ? */
    if (init) {
        fprintf(stderr, "%s: graphique deja initialise\n", NAME);
        return;
    }

    /* connection au serveur X */
    if ((display = XOpenDisplay(NULL)) == NULL) {
        fprintf(stderr, "%s: impossible de se connecter au serveur X %s\n",
                NAME, XDisplayName(NULL));
        abort();
    }

    screen = DefaultScreen(display);
    black = BlackPixel(display, screen);
    white = WhitePixel(display, screen);

    /* on crée la fenêtre */
    window = XCreateSimpleWindow(display, RootWindow(display, screen),
                                 0, 0, larg, haut, BORDER_WIDTH,
                                 black, white);
    wattrmask = CWEventMask | CWSaveUnder | CWBackingStore;
    wattr.event_mask = ExposureMask;
    wattr.backing_store = Always;
    wattr.save_under = True;
    XChangeWindowAttributes(display, window, wattrmask, &wattr);

    /* Création de l'icone */
    /*
    XpmCreatePixmapFromData(display, window, icon_xpm, &icon_pixmap, 
                            NULL, NULL);
    */
//    icon_pixmap = XCreateBitmapFromData(display, window, icon_bits, 
//                                        icon_width, icon_height);
    
    /* allocation mémoire */
    if (!(size_hints = XAllocSizeHints())) {
        fprintf(stderr, "%s: pb allocation SizeHints\n", NAME);
        abort();
    }
    if (!(wm_hints = XAllocWMHints())) {
        fprintf(stderr, "%s: pb allocation WMHints\n", NAME);
        abort();
    }
    /* la taille minimale demandée */
    size_hints->flags = PMinSize | PMaxSize;
    size_hints->min_width = larg;
    size_hints->min_height = haut;
    size_hints->max_width = larg;
    size_hints->max_height = haut;

    /* les noms de la fenetre et de l'icone */
    if (XStringListToTextProperty(window_name, 1, &windowName) == 0) {
        fprintf(stderr, "%s: pb allocation windowName\n", NAME);
        abort();
    }
    if (XStringListToTextProperty(icon_name, 1, &iconName) == 0) {
        fprintf(stderr, "%s: pb allocation iconName\n", NAME);
        abort();
    }    
    
    /* quelques conseils pour le WindowManager */
    wm_hints->initial_state = NormalState;
    wm_hints->input = True;
//    wm_hints->icon_pixmap = icon_pixmap;
//    wm_hints->flags = StateHint | IconPixmapHint | InputHint;
    wm_hints->flags = StateHint | InputHint;
    XSetWMProperties(display, window, &windowName, &iconName, NULL, 0,
                     size_hints, wm_hints, NULL);
    XFree(windowName.value);
    XFree(iconName.value);
    
    /* les attributs de dessin */
//    font = XLoadFont(display, fontname);
    gcattr.foreground = black;
    gcattr.background = white;
//    gcattr.font = font;
    gcattrmask = GCForeground | GCBackground;
//    gcattrmask = GCForeground | GCBackground | GCFont;
    gc = XCreateGC(display, window, gcattrmask, &gcattr);

    /* affiche la fenêtre */
    XMapRaised(display, window);

    /* il faut attendre un Expose avant de dessiner ... */
    XWindowEvent(display, window, ExposureMask, &report);
    
	shadow = XCreatePixmap(display, window, larg, haut, DefaultDepth(display, screen));
	init = 1;

	cleargraph();
    return;
}