コード例 #1
0
ファイル: a4-ece650.c プロジェクト: EuphieLiu/Assignment1
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;
}
コード例 #2
0
ファイル: graph.c プロジェクト: curiousguy13/ds_and_algos
int main()
{
    int n,ans,a,b;
    node* start=NULL;
    do
    {
        printf("\n---------Directed Graph------------");
        printf("\n1.Insertnode");
        printf("\n2.Insertedge");
        printf("\n3.Deletenode");
        printf("\n4.Deleteedge");
        printf("\n5.Display-nodes");
        printf("\n6.Exit\n");
        printf("\nEnter your choice:");
        scanf("%d",&ans);
        fflush(stdin);
        switch(ans)
        {
        case 1:
            printf("\nEnter the value to be inserted:");
            fflush(stdin);
            scanf("%c",&n);
            fflush(stdin);
            insertnode(&start,n);
            break;
        case 2:
            printf("\nEnter the start point of edge:");
            fflush(stdin);
            scanf("%c",&a);
            fflush(stdin);
            printf("\nEnter the end point of edge:");
            scanf("%c",&b);
            fflush(stdin);
            insertedge(start,a,b);
            break;

        case 3:
            printf("Enter the value to be deleted-->");
            fflush(stdin);
            scanf("%c",&n);
            fflush(stdin);
            delete_node(&start,n);
            break;
        case 4:
            printf("\nEnter the start point of edge:");
            fflush(stdin);
            scanf("%c",&a);
            fflush(stdin);
            printf("\nEnter the end point of edge:");
            scanf("%c",&b);
            fflush(stdin);
            delete_edge(start,a,b);
            break;
        case 5:
            printf("\nDisplaying node of the graph using breadth first traversal: \n");
            bfs_disp(start);
            break;
        case 6:
            break;
        }

    }
    while(ans!=6);
    return 0;
}