1. 程式人生 > >第十一週 【專案2-操作用鄰接表儲存的圖】

第十一週 【專案2-操作用鄰接表儲存的圖】

/*  

*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 100
typedef 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;
}
程式測試: