图的顺序存储结构

对于图的存储,只要理解其中一个,对于其他的理解自然也就理解了。

有向图与无向图:无向图相对于有向图,就是将有向图矩阵关于主对称轴对称。在数据存储时,多进行一次,横纵坐标交换存储的过程。

有向网和无向网同有向图和无向图:多出的仅仅是权值的村储,也就是将位置关系判断的变量存储权值。(实际上,我们表示图的方式,可以理解为权值为1

 

 

#include <iostream>

#define MAX_SIZE 20

using namespace std;

typedef struct A {
    int Adj;//用来表示有向图无向图顶点之间的关系;表示有向网和无向网顶点之间的关系以及权值大小
    //int* InforPointer;
}TT[MAX_SIZE][MAX_SIZE];

struct Graph {
    int Vertex[MAX_SIZE];//顶点数据集合
    int vertex, arcnum;//顶点和边的数量
    TT RT;
};

int checkfunc(Graph* G, int n) {//检查我们输入的弧头弧尾(实际上就是顶点),在我们存储的顶点集合中存在
    for (int i = 0; i < G->vertex; ++i)
        if (G->Vertex[i] == n)
            return i;

    return -1;
}

void Digraph(Graph * G)//有向图
{
    cout << "Please input number of vertices and number of arcs:" << endl;
    cin >> G->vertex >> G->arcnum;//顶点数以及边数
    cout << "Please input the vertices data:" << endl;
    for (int i = 0; i < G->vertex; ++i)
        cin >> G->Vertex[i];//顶点集合的数据(就是顶点的序列号:1号顶点,2号顶点...)
    for (int i = 0; i < G->vertex; ++i)
        for (int j = 0; j < G->vertex; ++j) {
            G->RT[i][j].Adj = 0;//将矩阵置空(全部为0)
            //G->RT[i][j].InforPointer = NULL;
        }
    for (int i = 0; i < G->arcnum; ++i) {
        int head, tail;//弧头和弧尾
        cout << "Please input arcs head and arcs tail:" << endl;
        cin >> head >> tail;
        int m = checkfunc(G, head);//检查顶点是否存在
        int n = checkfunc(G, tail);
        if (m == -1 || n == -1) {
            cerr << "arcs head or arcs tail error!!!";
            return;
        }
        G->RT[m][n].Adj = 1;//检查顶点无误之后,确定m顶点到n顶点之间存在有向关系
    }

}

void Undigraph(Graph * G)
{
    cout << "Please input number of vertices and number of arcs:" << endl;
    cin >> G->vertex >> G->arcnum;
    cout << "Please input the vertices data:" << endl;
    for (int i = 0; i < G->vertex; ++i)
        cin >> G->Vertex[i];
    for (int i = 0; i < G->vertex; ++i)
        for (int j = 0; j < G->vertex; ++j) {
            G->RT[i][j].Adj = 0;
            //G->RT[i][j].InforPointer = NULL;
        }
    for (int i = 0; i < G->arcnum; ++i) {
        int head, tail;
        cout << "Please input arcs head and arcs tail:" << endl;
        cin >> head >> tail;
        int m = checkfunc(G, head);
        int n = checkfunc(G, tail);
        if (m == -1 || n == -1) {
            cerr << "arcs head or arcs tail error!!!";
            return;
        }
        G->RT[m][n].Adj = 1;
        G->RT[n][m].Adj = 1;//无向图关于主对称轴对称
    }

}

void DigraphNetwork(Graph * G) {
    cout << "Please input number of vertices and number of arcs:" << endl;
    cin >> G->vertex >> G->arcnum;
    cout << "Please input the vertices data:" << endl;
    for (int i = 0; i < G->vertex; ++i) {
        cin >> G->Vertex[i];
    }
    for (int i = 0; i < G->vertex; ++i) {
        for (int j = 0; j < G->vertex; ++j) {
            G->RT[i][j].Adj = 0;
            //G->RT[i][j].InforPointer = NULL;
        }
    }
    for (int i = 0; i < G->arcnum; ++i) {
        int v, a, w;
        cout << "Please input arcs head , arcs tail and weight:" << endl;
        cin >> v >> a >> w;
        int m = checkfunc(G, v);
        int n = checkfunc(G, a);
        if (m == -1 || n == -1) {
            cerr << "arcs head or arcs tail error!!!";
            return;
        }
        G->RT[m][n].Adj = w;
    }
}

void UndigraphNetwork(Graph * G) {
    cout << "Please input number of vertices and number of arcs:" << endl;
    cin >> G->vertex >> G->arcnum;
    cout << "Please input the vertices data:" << endl;
    for (int i = 0; i < G->vertex; ++i) {
        cin >> G->Vertex[i];
    }
    for (int i = 0; i < G->vertex; ++i) {
        for (int j = 0; j < G->vertex; ++j) {
            G->RT[i][j].Adj = 0;
            //G->RT[i][j].InforPointer = NULL;
        }
    }
    for (int i = 0; i < G->arcnum; ++i) {
        int v, a, w;
        cout << "Please input arcs head , arcs tail and weight:" << endl;
        cin >> v >> a >> w;
        int m = checkfunc(G, v);
        int n = checkfunc(G, a);
        if (m == -1 || n == -1) {
            cerr << "arcs head or arcs tail error!!!";
            return;
        }
        G->RT[m][n].Adj = w;
        G->RT[n][m].Adj = w;
    }
}

void PrintGraph(Graph * G) {
    for (int i = 0; i < G->vertex; ++i) {
        for (int j = 0; j < G->vertex; ++j) {
            cout << G->RT[i][j].Adj << " ";
        }
        cout << endl;
    }
}

int main()
{
    int choice;
    Graph* G = new Graph[sizeof(Graph)];
    cout << "Please choose the type of graph:" << endl;
    cin >> choice;
    switch (choice) {
    case 1:
        Digraph(G);
        break;
    case 2:
        Undigraph(G);
        break;
    case 3:
        DigraphNetwork(G);
        break;
    case 4:
        UndigraphNetwork(G);
        break;
    default:
        cerr << "Input error!!!" << endl;
    }

    PrintGraph(G);

    system("PAUSE");
    return 0;
}

 

 

posted @ 2019-03-19 23:56  Hk_Mayfly  阅读(294)  评论(0)    收藏  举报