數據結構之圖 Data Structure — graph

Adjacency Matrix用C實現如下:

#include<stdio.h>
#include<stdlib.h>
#include<curses.h>

typedef char VertexType;
typedef int EdgeType;

#define MAXVEX 100
#define INFINITY 11111
//#define DEBUG

typedef struct
{
    VertexType vexs[MAXVEX];
    EdgeType arc[MAXVEX][MAXVEX];
    int numVertexes, numEdges; //定義頂點數,邊數
}Graph;

//Locate the vertex you need
int locates(Graph *g,char ch)
{
    int i=0;
    for(i=0; i<g->numVertexes;i++){
        if(g->vexs[i]==ch)
            break;
    }

    if(i>=g->numVertexes)  //如果超過頂點數量了,返回-1
        return -1;

    return i; //return the index of the vertex you are looking
}

//建立一個無方向網圖的鄰接矩陣表示
void CreateGraph(Graph *g)
{
    int i,j,k,w;
    printf("Input the number of vertex and arc:\n");
    scanf("%d %d", &(g->numVertexes), &(g->numEdges));
    printf("#\n");
#ifdef DEBUG
    printf("The number is %d and %d.\n",g->numVertexes,g->numEdges);
#endif

    printf("The total amount of vertexes is %d. Assign to each.\n",g->numVertexes);

    //assign a value to each vertex
    for(i=0;i<g->numVertexes;i++){
        g->vexs[i]=getchar();
        while(g->vexs[i] == '\n'){
            g->vexs[i]=getchar();
        }
    }

#ifdef DEBUG
    for(i=0;i<g->numVertexes;i++){
        printf("Vexs[i] is %c ",g->vexs[i]);
    }
    printf("\n");
#endif

    //initialize each arc/edge as infinity
    for(i=0;i<g->numEdges;i++){
        for(j=0;j<g->numEdges;j++){
            g->arc[i][j]=INFINITY; //initialize
        }
    }

    for (int i = 0; i < g->numEdges; ++i){
        char p,q;
        printf("input the i and j of (Vi,Vj), and the weight:\n"); //一條邊的兩個結點,和這條邊的權重
        p=getchar();
        while(p=='\n'){
            p=getchar();
        }
        q=getchar();
        while(q=='\n'){
            q=getchar();
        }
        scanf("%d",&w);

        int m=-1;
        int n=-1;
        m=locates(g,p);
        n=locates(g,q);
        if(n==-1 || m==-1){
            fprintf(stderr,"No such vertex\n");
            return;
        }
        g->arc[m][n]=w;
        g->arc[n][m]=g->arc[m][n];
    }
}

void printGraph(Graph g){
    int i,j;
    for(i=0;i<g.numVertexes;i++){
        for(j=0;j<g.numVertexes;j++){
            printf("%d\t",g.arc[i][j] );
        }
        printf("\n\n\n");
    }
}

int main(int argc, char const *argv[])
{
    Graph g;

    CreateGraph(&g);
    printGraph(g);

    return 0;
}

Adjacency List的表示,用C實現

#include<stdio.h>
#include<stdlib.h>

#define DEBUG
#define MAXVEX 1000

typedef char VertexType;
typedef int EdgeType;

typedef struct EdgeNode //邊表結構
{
    int adjvex; //鄰接點域
    EdgeType weight;
    struct EdgeNode *next;
}EdgeNode;

typedef struct VertexNode  //頂點表結構
{
    VertexType data;
    EdgeNode *firstedge;    //
}VertexNode,AdjList[MAXVEX];

typedef struct
{
    AdjList adjList;
    int numVertexes,numEdges;
}GraphList;

int Locate(GraphList *g,char ch)
{
    int i;
    for(i=0;i<MAXVEX;i++){
        if(ch==g->adjList[i].data){
            break;
        }
    }
    if(i>MAXVEX){
        fprintf(stderr,"there is no such vertex.\n");
        return -1;
    }
    return i;
}

void CreateGraph(GraphList *g)
{
    int i,j,k;
    EdgeNode *e;
    EdgeNode *f;
    printf("input the amount of matrixs and arcs:\n");
    scanf("%d %d",&g->numVertexes,&g->numEdges);

#ifdef DEBUG
    printf("numVertexes is %d. numEdges is %d.\n",g->numVertexes,g->numEdges);
#endif

for(i=0;i<g->numVertexes;i++){
    printf("輸入頂點%d:\n",i);
    g->adjList[i].data=getchar();
    g->adjList[i].firstedge=NULL;
    while(g->adjList[i].data=='\n'){
        g->adjList[i].data=getchar();
    }
}

//build 邊表
    for(k=0;k<g->numEdges;k++){
        printf("輸入邊(vi,vj)上的頂點序號:\n");
        char p,q;
        p=getchar();
        while(p=='\n'){
            p=getchar();
        }
        q=getchar();
        while(q=='\n'){
            q=getchar();
        }
        int m,n;
        m=Locate(g,p);
        n=Locate(g,q);
        if(m==-1 || n==-1){
            return;
        }
#ifdef DEBUG
    printf("p=%c\n",p);
    printf("q=%c\n",q);
    printf("m=%d\n",m);
    printf("n=%d\n",n);
#endif

        //申請內存空間生成邊表結點
        e=(EdgeNode *)malloc(sizeof(EdgeNode));
        if(e==NULL){
            fprintf(stderr,"malloc() error.\n");
            return;
        }

        e->adjvex=n;
        e->next=g->adjList[m].firstedge;
        g->adjList[m].firstedge=e;


        f=(EdgeNode *)malloc(sizeof(EdgeNode));
        if(f==NULL){
            fprintf(stderr,"malloc() error.\n");
            return;
        }

        f->adjvex=m;
        f->next=g->adjList[n].firstedge;
        g->adjList[n].firstedge=f;
    }
}

void printGraph(GraphList *g)
{
    int i=0;
#ifdef DEBUG
    printf("printGraph() starts.\n");
#endif

    while(g->adjList[i].firstedge!=NULL && i<MAXVEX){
        printf("頂點%c與這些點連接:",g->adjList[i].data);
        EdgeNode *e=NULL;
        e=g->adjList[i].firstedge;
        while(e!=NULL){
            printf("%d ",e->adjvex);    //打印邊表的節點
            e=e->next;
        }
        i++;
        printf("\n");
    }
}


int main()
{
    GraphList g;
    CreateGraph(&g);
    printGraph(&g);
    return 0;
}

Minimum Cost Spanning Tree

1.Prim Algorithm

用C++實現,代碼如下:

/*
 * prim.cpp
 *
 *  Created on: May 3, 2017
 *      Author: yiyue
 */

#include<iostream>

using namespace std;
class prims
{
private:
    int no_of_edges,no_of_nodes;//number of edges and nodes
    int graph[10][10],visited[10],mindist[10]; //minimum distance
public:
    void input();
    void output();
    void spanningtree();

    prims()//constructor -- same name of class
    {
        no_of_edges=no_of_nodes=0;
        for(int i=0;i<10;i++){
            visited[i]=mindist[i]=0;
            for(int j=0;j<10;j++){
                graph[i][j]=0;
            }
        }
    }
};

void prims::input()
{
    int vertex1,vertex2,cost;
    cout<<"Enter no_of_nodes    ";
    cin>>no_of_nodes;
    cout<<"Enter the no_of_edges    ";
    cin>>no_of_edges;
    for(int i=0;i<no_of_edges;i++){
        cout<<"Enter Vertex1    ";
        cin>>vertex1;
        cout<<"Enter vertex2    ";
        cin>>vertex2;
        cout<<"Enter the cost of "<<vertex1<< " and "<<vertex2<<" ";
        cin>>cost;
        graph[vertex1][vertex2]=graph[vertex2][vertex1]=cost;
    }
}

void prims::output()
{
    for(int i=0;i<no_of_nodes;i++){
        cout<<endl;
        for(int j=0;j<no_of_nodes;j++){
            cout.width(4);
            cout<<graph[i][j];
        }
    }
}


void prims::spanningtree(){
    int min=9999,row,col,index=0;
    for(int i=0;i<no_of_nodes;i++){
        for(int j=i;j<no_of_nodes;j++){
            if(graph[i][j]<min&&graph[i][j]!=0){
                min=graph[i][j];
                row=i;  //set it to vertex1
                col=j;  //vertex2
            }
        }
    }
    visited[row]=visited[col]=1;
    mindist[index++]=min;

    for(int i=0;i<no_of_nodes-2;i++){
        min=9999;
        for(int j=0;j<no_of_nodes;j++){
            if(visited[j]==1){
                for(int k=i;k<no_of_nodes;k++){
                    if(graph[j][k]<min&&graph[j][k]!=0&&visited[k]==0){//visited[k]==0 makes sure it's not self-loop
                        min=graph[j][k];
                        row=j;  //set it to vertex1
                        col=k;  //vertex2
                    }
                }
            }
        }
        mindist[index++]=min;
        visited[row]=visited[col]=1;
    }
    int total=0;
    cout<<endl;
    cout<<"Minimum distance path is ";
    for(int i=0;i<no_of_nodes-1;i++){
        cout<<" "<<mindist[i]<<" ";
        total=total+mindist[i];
    }
    cout<<endl<<"Total path cost is "<<total;
}

int main()
{
    prims obj;
    obj.input();
    obj.output();
    obj.spanningtree();
    return 0;
}

另一種實現: (參見數據結構(嚴蔚敏))

#include <iostream>
using namespace std;

typedef char VerTexType;
typedef int ArcType;
#define MVNum 100
#define MaxInt 32767                        //表示極大值,即∞

//輔助數組的定義,用來記錄從頂點集U到V-U的權值最小的邊
struct{
    VerTexType adjvex;                      //最小邊在U中的那個頂點
    ArcType lowcost;                        //最小邊上的權值
}closedge[MVNum];

//- - - - -圖的鄰接表存儲表示- - - - -                       
typedef char VerTexType;                    //假設頂點的數據類型爲字符型 
typedef int ArcType;                        //假設邊的權值類型爲整型 
typedef struct{ 
    VerTexType vexs[MVNum];                 //頂點表 
    ArcType arcs[MVNum][MVNum];             //鄰接矩陣 
    int vexnum,arcnum;                      //圖的當前點數和邊數 
}AMGraph;

int LocateVex(AMGraph G , VerTexType v){
    //確定點v在G中的位置
    for(int i = 0; i < G.vexnum; ++i)
        if(G.vexs[i] == v)
            return i;
        return -1;
}//LocateVex

void CreateUDN(AMGraph &G){ 
    //採用鄰接矩陣表示法,創建無向網G 
    int i , j , k;
    cout <<"請輸入總頂點數,總邊數,以空格隔開:";
    cin >> G.vexnum >> G.arcnum;                            //輸入總頂點數,總邊數
    cout << endl;

    cout << "輸入點的名稱,如a" << endl;

    for(i = 0; i < G.vexnum; ++i){   
        cout << "請輸入第" << (i+1) << "個點的名稱:";
        cin >> G.vexs[i];                                   //依次輸入點的信息 
    }
    cout << endl;
    for(i = 0; i < G.vexnum; ++i)                           //初始化鄰接矩陣,邊的權值均置爲極大值MaxInt 
        for(j = 0; j < G.vexnum; ++j)   
            G.arcs[i][j] = MaxInt;
    cout << "輸入邊依附的頂點及權值,如a b 5" << endl;
    for(k = 0; k < G.arcnum;++k){                           //構造鄰接矩陣 
        VerTexType v1 , v2;
        ArcType w;
        cout << "請輸入第" << (k + 1) << "條邊依附的頂點及權值:";
        cin >> v1 >> v2 >> w;                               //輸入一條邊依附的頂點及權值
        i = LocateVex(G, v1);  j = LocateVex(G, v2);        //確定v1和v2在G中的位置,即頂點數組的下標 
        G.arcs[i][j] = w;                                   //邊<v1, v2>的權值置爲w 
        G.arcs[j][i] = G.arcs[i][j];                        //置<v1, v2>的對稱邊<v2, v1>的權值爲w 
    }//for
}//CreateUDN 

int Min(AMGraph G){
    //返回權值最小的點
    int i;
    int index = -1;
    int min = MaxInt;
    for(i = 0 ; i < G.vexnum ; ++i){
        if(min > closedge[i].lowcost && closedge[i].lowcost != 0){
            min = closedge[i].lowcost;
            index = i;
        }
    }//for
    return index;
}//Min

void MiniSpanTree_Prim(AMGraph G, VerTexType u){ 
    //無向網G以鄰接矩陣形式存儲,從頂點u出發構造G的最小生成樹T,輸出T的各條邊  
    int k , j , i;
    VerTexType u0 , v0;
    k =LocateVex(G, u);                                                 //k爲頂點u的下標 
    for(j = 0; j < G.vexnum; ++j){                                      //對V-U的每一個頂點vi,初始化closedge[i] 
        if(j != k){  
            closedge[j].adjvex = u;
            closedge[j].lowcost = G.arcs[k][j];                         //{adjvex, lowcost}
        }//if
    }//for
    closedge[k].lowcost = 0;                                            //初始,U = {u}
    for(i = 1; i < G.vexnum; ++i){                                      //選擇其餘n-1個頂點,生成n-1條邊(n= G.vexnum) 
        k = Min(G);  
        //求出T的下一個結點:第k個頂點,closedge[k]中存有當前最小邊 
        u0 = closedge[k].adjvex;                                        //u0爲最小邊的一個頂點,u0∈U 
        v0 = G.vexs[k];                                                 //v0爲最小邊的另一個頂點,v0∈V-U 
        cout << "邊  " <<u0 << "--->" << v0 << endl;                     //輸出當前的最小邊(u0, v0) 
        closedge[k].lowcost = 0;        //第k個頂點併入U集 
        for(j = 0; j < G.vexnum; ++j) 
            if(G.arcs[k][j] < closedge[j].lowcost){                     //新頂點併入U後重新選擇最小邊 
                closedge[j].adjvex = G.vexs[k];
                closedge[j].lowcost = G.arcs[k][j];
            }//if 
    }//for 
}//MiniSpanTree_Prim 

int main(){
    cout << "************算法6.8 普里姆算法**************" << endl << endl;
    AMGraph G;
    CreateUDN(G);
    cout << endl;
    cout << "無向圖G創建完成!" << endl;
    cout <<endl;

    cout << "******利用普里姆算法構造最小生成樹結果:******" << endl;
    MiniSpanTree_Prim(G , 'a');
    cout <<endl;
    return 0;
}//main
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章