1. 程式人生 > >2015年大二上-資料結構-圖-1-(1)圖基本演算法庫

2015年大二上-資料結構-圖-1-(1)圖基本演算法庫

圖的儲存結構主要包括鄰接矩陣和鄰接表,本演算法庫提供儲存結構的定義,以及用於構造圖儲存結構、不同結構的轉換及顯示的程式碼。演算法庫採用程式的多檔案組織形式,包括兩個檔案:

  

  1.標頭檔案:Graph.h,包含定義圖資料結構的程式碼、巨集定義、要實現演算法的函式的宣告;

/*  
*Copyright (c) 2014,煙臺大學計算機學院  
*All rights reserved.  
*檔名稱:Annpion.cpp  
*作者:王耀鵬  
*完成日期:2016年1月23日  
*版本號:v1.0  
*  
*問題描述:圖基本演算法庫
*輸入描述:無  
*輸出描述:圖的基本操作
*/  
#ifndef GRAPH_H_INCLUDED
#define GRAPH_H_INCLUDED

#define MAXV 100                //最大頂點個數
#define INF 32767       //INF表示∞
typedef int InfoType;

//以下定義鄰接矩陣型別
typedef struct
{
    int no;                     //頂點編號
    InfoType info;              //頂點其他資訊,在此存放帶權圖權值
} VertexType;                   //頂點型別

typedef struct                  //圖的定義
{
    int edges[MAXV][MAXV];      //鄰接矩陣
    int n,e;                    //頂點數,弧數
    VertexType vexs[MAXV];      //存放頂點資訊
} MGraph;                       //圖的鄰接矩陣型別

//以下定義鄰接表型別
typedef struct ANode            //弧的結點結構型別
{
    int adjvex;                 //該弧的終點位置
    struct ANode *nextarc;      //指向下一條弧的指標
    InfoType info;              //該弧的相關資訊,這裡用於存放權值
} ArcNode;

typedef int Vertex;

typedef struct Vnode            //鄰接表頭結點的型別
{
    Vertex data;                //頂點資訊
    int count;                  //存放頂點入度,只在拓撲排序中用
    ArcNode *firstarc;          //指向第一條弧
} VNode;

typedef VNode AdjList[MAXV];    //AdjList是鄰接表型別

typedef struct
{
    AdjList adjlist;            //鄰接表
    int n,e;                    //圖中頂點數n和邊數e
} ALGraph;                      //圖的鄰接表型別

//功能:由一個反映圖中頂點鄰接關係的二維陣列,構造出用鄰接矩陣儲存的圖
//引數:Arr - 陣列名,由於形式引數為二維陣列時必須給出每行的元素個數,在此將引數Arr宣告為一維陣列名(指向int的指標)
//      n - 矩陣的階數
//      g - 要構造出來的鄰接矩陣資料結構
void ArrayToMat(int *Arr, int n, MGraph &g); //用普通陣列構造圖的鄰接矩陣
void ArrayToList(int *Arr, int n, ALGraph *&G); //用普通陣列構造圖的鄰接表
void MatToList(MGraph g,ALGraph *&G);//將鄰接矩陣g轉換成鄰接表G
void ListToMat(ALGraph *G,MGraph &g);//將鄰接表G轉換成鄰接矩陣g
void DispMat(MGraph g);//輸出鄰接矩陣g
void DispAdj(ALGraph *G);//輸出鄰接表G

#endif // GRAPH_H_INCLUDED

  2.原始檔:Graph.cpp,包含實現各種演算法的函式的定義

/*  
*Copyright (c) 2014,煙臺大學計算機學院  
*All rights reserved.  
*檔名稱:Annpion.cpp  
*作者:王耀鵬  
*完成日期:2016年1月23日  
*版本號:v1.0  
*  
*問題描述:圖基本演算法庫
*輸入描述:無  
*輸出描述:圖的基本操作
*/  
#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<n; ++i)
        for(j=0; j<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)
            {
                p=(ArcNode *)malloc(sizeof(ArcNode));
                p->adjvex=j;
                p->info=Arr[i*n+j];
                p->nextarc=G->adjlist[i].firstarc;
                G->adjlist[i].firstarc=p;
                ++count;
            }
    G->e=count;
}
void MatToList(MGraph g,ALGraph *&G)//將鄰接矩陣g轉換成鄰接表G
{
    int i,j;
    G=(ALGraph *)malloc(sizeof(ALGraph));
    ArcNode *p;
    G->n=g.n;
    G->e=g.e;
    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->adjvex=j;
                p->info=g.edges[i][j];
                p->nextarc=G->adjlist[i].firstarc;
                G->adjlist[i].firstarc=p;
            }
}
void ListToMat(ALGraph *G,MGraph &g)//將鄰接表G轉換成鄰接矩陣g
{
    int i,j;
    ArcNode *p;
    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");
    }
}

3.在同一專案(project)中建立一個原始檔(如main.cpp),編制main函式,完成相關的測試工作。 例:

/*  
*Copyright (c) 2014,煙臺大學計算機學院  
*All rights reserved.  
*檔名稱:Annpion.cpp  
*作者:王耀鵬  
*完成日期:2016年1月23日  
*版本號:v1.0  
*  
*問題描述:圖基本演算法庫
*輸入描述:無  
*輸出描述:圖的基本操作
*/  
#include <stdio.h>
#include <malloc.h>
#include "graph.h"

int main()
{
    MGraph g1,g2;
    ALGraph *G1,*G2;
    int A[6][6]=
    {
        {0,5,0,7,0,0},
        {0,0,4,0,0,0},
        {8,0,0,0,0,9},
        {0,0,5,0,0,6},
        {0,0,0,5,0,0},
        {3,0,0,0,1,0}
    };

    ArrayToMat(A[0], 6, g1);  //取二維陣列的起始地址作實參,用A[0],因其實質為一維陣列地址,與形參匹配
    printf(" 有向圖g1的鄰接矩陣:\n");
    DispMat(g1);

    ArrayToList(A[0], 6, G1);
    printf(" 有向圖G1的鄰接表:\n");
    DispAdj(G1);

    MatToList(g1,G2);
    printf(" 圖g1的鄰接矩陣轉換成鄰接表G2:\n");
    DispAdj(G2);

    ListToMat(G1,g2);
    printf(" 圖G1的鄰接錶轉換成鄰接鄰陣g2:\n");
    DispMat(g2);
    printf("\n");
    return 0;
}

執行結果: