第十一周 【项目2-操作用邻接表存储的图】

来源:互联网 发布:星际老男孩淘宝店倒闭 编辑:程序博客网 时间:2024/06/02 05:48
/*  

*Copyright  (c)2017,烟台大学计算机与控制工程学院      

*All rights reservrd.            

*作者:赵楷文 

*完成时间:2017年11月09日      

*版本号:v1.0      

*问题描述:假设图G采用邻接表存储,分别设计实现以下要求的算法: 
(1)输出出图G中每个顶点的出度; 
(2)求出图G中出度最大的一个顶点,输出该顶点编号; 
(3)计算图G中出度为0的顶点数; 
(4)判断图G中是否存在边<i,j>。 
利用下图作为测试用图,输出结果。 
这里写图片描述 

一、graph.h

#include <stdio.h>#include <malloc.h>#define MaxSize 100typedef char ElemType;typedef struct node{    ElemType data;    int ltag,rtag;      //Ôö¼ÓµÄÏßË÷±ê¼Ç    struct node *lchild;    struct node *rchild;} TBTNode;void CreateTBTNode(TBTNode * &b,char *str){    TBTNode *St[MaxSize],*p=NULL;    int top=-1,k,j=0;    char ch;    b=NULL;             //½¨Á¢µÄ¶þ²æÊ÷³õʼʱΪ¿Õ    ch=str[j];    while (ch!='\0')    //strδɨÃèÍêʱѭ»·    {        switch(ch)        {        case '(':            top++;            St[top]=p;            k=1;            break;      //Ϊ×ó½áµã        case ')':            top--;            break;        case ',':            k=2;            break;                          //ΪÓÒ½áµã        default:            p=(TBTNode *)malloc(sizeof(TBTNode));            p->data=ch;            p->lchild=p->rchild=NULL;            if (b==NULL)                    //*pΪ¶þ²æÊ÷µÄ¸ù½áµã                b=p;            else                            //Òѽ¨Á¢¶þ²æÊ÷¸ù½áµã            {                switch(k)                {                case 1:                    St[top]->lchild=p;                    break;                case 2:                    St[top]->rchild=p;                    break;                }            }        }        j++;        ch=str[j];    }}void DispTBTNode(TBTNode *b){    if (b!=NULL)    {        printf("%c",b->data);        if (b->lchild!=NULL || b->rchild!=NULL)        {            printf("(");            DispTBTNode(b->lchild);            if (b->rchild!=NULL) printf(",");            DispTBTNode(b->rchild);            printf(")");        }    }}TBTNode *pre;                       //È«¾Ö±äÁ¿void Thread(TBTNode *&p){    if (p!=NULL)    {        Thread(p->lchild);          //×ó×ÓÊ÷ÏßË÷»¯        if (p->lchild==NULL)        //Ç°ÇýÏßË÷        {            p->lchild=pre;          //½¨Á¢µ±Ç°½áµãµÄÇ°ÇýÏßË÷            p->ltag=1;        }        else p->ltag=0;        if (pre->rchild==NULL)      //ºó¼ÌÏßË÷        {            pre->rchild=p;          //½¨Á¢Ç°Çý½áµãµÄºó¼ÌÏßË÷            pre->rtag=1;        }        else pre->rtag=0;        pre=p;        Thread(p->rchild);          //ÓÒ×ÓÊ÷ÏßË÷»¯    }}TBTNode *CreaThread(TBTNode *b)     //ÖÐÐòÏßË÷»¯¶þ²æÊ÷{    TBTNode *root;    root=(TBTNode *)malloc(sizeof(TBTNode));  //´´½¨¸ù½áµã    root->ltag=0;    root->rtag=1;    root->rchild=b;    if (b==NULL)                //¿Õ¶þ²æÊ÷        root->lchild=root;    else    {        root->lchild=b;        pre=root;               //preÊÇ*pµÄÇ°Çý½áµã,¹©¼ÓÏßË÷Óà        Thread(b);              //ÖÐÐò±éÀúÏßË÷»¯¶þ²æÊ÷        pre->rchild=root;       //×îºó´¦Àí,¼ÓÈëÖ¸Ïò¸ù½áµãµÄÏßË÷        pre->rtag=1;        root->rchild=pre;       //¸ù½áµãÓÒÏßË÷»¯    }    return root;}void ThInOrder(TBTNode *tb){    TBTNode *p=tb->lchild;      //Ö¸Ïò¸ù½áµã    while (p!=tb)    {        while (p->ltag==0) p=p->lchild;        printf("%c ",p->data);        while (p->rtag==1 && p->rchild!=tb)        {            p=p->rchild;            printf("%c ",p->data);        }        p=p->rchild;    }}int main(){    TBTNode *b,*tb;    CreateTBTNode(b,"A(B(D(,G)),C(E,F))");    printf(" ¶þ²æÊ÷:");    DispTBTNode(b);    printf("\n");    tb=CreaThread(b);    printf(" ÏßË÷ÖÐÐòÐòÁÐ:");    ThInOrder(tb);    printf("\n");    return 0;}

二、graph.cpp

#include <stdio.h>#include <malloc.h>#include "graph.h"//¹¦ÄÜ£ºÓÉÒ»¸ö·´Ó³Í¼Öж¥µãÁÚ½Ó¹ØϵµÄ¶þάÊý×飬¹¹Ôì³öÓÃÁÚ½Ó¾ØÕó´æ´¢µÄͼ//²ÎÊý£ºArr - Êý×éÃû£¬ÓÉÓÚÐÎʽ²ÎÊýΪ¶þάÊý×éʱ±ØÐë¸ø³öÿÐеÄÔªËظöÊý£¬Ôڴ˽«²ÎÊýArrÉùÃ÷ΪһάÊý×éÃû£¨Ö¸ÏòintµÄÖ¸Õ룩//      n - ¾ØÕóµÄ½×Êý//      g - Òª¹¹Ôì³öÀ´µÄÁÚ½Ó¾ØÕóÊý¾Ý½á¹¹void ArrayToMat(int *Arr, int n, MGraph &g){    int i,j,count=0;  //countÓÃÓÚͳ¼Æ±ßÊý£¬¼´¾ØÕóÖзÇ0ÔªËظöÊý    g.n=n;    for (i=0; i<g.n; i++)        for (j=0; j<g.n; j++)        {            g.edges[i][j]=Arr[i*n+j]; //½«Arr¿´×÷n¡ÁnµÄ¶þάÊý×飬Arr[i*n+j]¼´ÊÇArr[i][j]£¬¼ÆËã´æ´¢Î»ÖõŦ·òÔÚ´ËÓ¦Óà            if(g.edges[i][j]!=0 && g.edges[i][j]!=INF)                count++;        }    g.e=count;}void ArrayToList(int *Arr, int n, ALGraph *&G){    int i,j,count=0;  //countÓÃÓÚͳ¼Æ±ßÊý£¬¼´¾ØÕóÖзÇ0ÔªËظöÊý    ArcNode *p;    G=(ALGraph *)malloc(sizeof(ALGraph));    G->n=n;    for (i=0; i<n; i++)                 //¸øÁÚ½Ó±íÖÐËùÓÐÍ·½ÚµãµÄÖ¸ÕëÓòÖóõÖµ        G->adjlist[i].firstarc=NULL;    for (i=0; i<n; i++)                 //¼ì²éÁÚ½Ó¾ØÕóÖÐÿ¸öÔªËØ        for (j=n-1; j>=0; j--)            if (Arr[i*n+j]!=0)      //´æÔÚÒ»Ìõ±ß£¬½«Arr¿´×÷n¡ÁnµÄ¶þάÊý×飬Arr[i*n+j]¼´ÊÇArr[i][j]            {                p=(ArcNode *)malloc(sizeof(ArcNode));   //´´½¨Ò»¸ö½Úµã*p                p->adjvex=j;                p->info=Arr[i*n+j];                p->nextarc=G->adjlist[i].firstarc;      //²ÉÓÃÍ·²å·¨²åÈë*p                G->adjlist[i].firstarc=p;            }    G->e=count;}void MatToList(MGraph g, ALGraph *&G)//½«ÁÚ½Ó¾ØÕógת»»³ÉÁÚ½Ó±íG{    int i,j;    ArcNode *p;    G=(ALGraph *)malloc(sizeof(ALGraph));    for (i=0; i<g.n; i++)                   //¸øÁÚ½Ó±íÖÐËùÓÐÍ·½ÚµãµÄÖ¸ÕëÓòÖóõÖµ        G->adjlist[i].firstarc=NULL;    for (i=0; i<g.n; i++)                   //¼ì²éÁÚ½Ó¾ØÕóÖÐÿ¸öÔªËØ        for (j=g.n-1; j>=0; j--)            if (g.edges[i][j]!=0)       //´æÔÚÒ»Ìõ±ß            {                p=(ArcNode *)malloc(sizeof(ArcNode));   //´´½¨Ò»¸ö½Úµã*p                p->adjvex=j;                p->info=g.edges[i][j];                p->nextarc=G->adjlist[i].firstarc;      //²ÉÓÃÍ·²å·¨²åÈë*p                G->adjlist[i].firstarc=p;            }    G->n=g.n;    G->e=g.e;}void ListToMat(ALGraph *G,MGraph &g)//½«ÁÚ½Ó±íGת»»³ÉÁÚ½Ó¾ØÕóg{    int i,j;    ArcNode *p;    g.n=G->n;   //¸ù¾Ýһ¥ͬѧ¡°¾Ù±¨¡±¸ÄµÄ¡£g.nδ¸³Öµ£¬ÏÂÃæµÄ³õʼ»¯²»Æð×÷Óà    g.e=G->e;    for (i=0; i<g.n; i++)   //Ïȳõʼ»¯ÁÚ½Ó¾ØÕó        for (j=0; j<g.n; j++)            g.edges[i][j]=0;    for (i=0; i<G->n; i++)  //¸ù¾ÝÁÚ½Ó±í£¬ÎªÁÚ½Ó¾ØÕó¸³Öµ    {        p=G->adjlist[i].firstarc;        while (p!=NULL)        {            g.edges[i][p->adjvex]=p->info;            p=p->nextarc;        }    }}void DispMat(MGraph g)//Êä³öÁÚ½Ó¾ØÕóg{    int i,j;    for (i=0; i<g.n; i++)    {        for (j=0; j<g.n; j++)            if (g.edges[i][j]==INF)                printf("%3s","¡Þ");            else                printf("%3d",g.edges[i][j]);        printf("\n");    }}void DispAdj(ALGraph *G)//Êä³öÁÚ½Ó±íG{    int i;    ArcNode *p;    for (i=0; i<G->n; i++)    {        p=G->adjlist[i].firstarc;        printf("%3d: ",i);        while (p!=NULL)        {            printf("-->%d/%d ",p->adjvex,p->info);            p=p->nextarc;        }        printf("\n");    }}
三、main.cpp

#include <stdio.h>#include <malloc.h>#include "graph.h"//返回图G中编号为v的顶点的出度int OutDegree(ALGraph *G,int v){    ArcNode *p;    int n=0;    p=G->adjlist[v].firstarc;    while (p!=NULL)    {        n++;        p=p->nextarc;    }    return n;}//输出图G中每个顶点的出度void OutDs(ALGraph *G){    int i;    for (i=0; i<G->n; i++)        printf("  顶点%d:%d\n",i,OutDegree(G,i));}//输出图G中出度最大的一个顶点void OutMaxDs(ALGraph *G){    int maxv=0,maxds=0,i,x;    for (i=0; i<G->n; i++)    {        x=OutDegree(G,i);        if (x>maxds)        {            maxds=x;            maxv=i;        }    }    printf("顶点%d,出度=%d\n",maxv,maxds);}//输出图G中出度为0的顶点数void ZeroDs(ALGraph *G){    int i,x;    for (i=0; i<G->n; i++)    {        x=OutDegree(G,i);        if (x==0)            printf("%2d",i);    }    printf("\n");}//返回图G中是否存在边<i,j>bool Arc(ALGraph *G, int i,int j){    ArcNode *p;    bool found = false;    p=G->adjlist[i].firstarc;    while (p!=NULL)    {        if(p->adjvex==j)        {            found = true;            break;        }        p=p->nextarc;    }    return found;}int main(){    ALGraph *G;    int A[7][7]=    {        {0,1,1,1,0,0,0},        {0,0,0,0,1,0,0},        {0,0,0,0,1,1,0},        {0,0,0,0,0,0,1},        {0,0,0,0,0,0,0},        {0,0,0,1,1,0,1},        {0,1,0,0,0,0,0}    };    ArrayToList(A[0], 7, G);    printf("(1)各顶点出度:\n");    OutDs(G);    printf("(2)最大出度的顶点信息:");    OutMaxDs(G);    printf("(3)出度为0的顶点:");    ZeroDs(G);    printf("(4)边<2,6>存在吗?");    if(Arc(G,2,6))        printf("是\n");    else        printf("否\n");    printf("\n");    return 0;}
程序测试:





阅读全文
0 0