【数据结构与算法】带权有向图

MyGraph.h

#pragma once
#include 
#include 
#include 
using namespace std;

// 邻接矩阵
// 带权有向图

const int MAXSIZE = 20;
const int INFINITE = 100;

template 
class CMyGraph
{
public:
    CMyGraph();
    ~CMyGraph();

private:
    T vertexArray[MAXSIZE] = {0};               // 顶点集合
    int edge[MAXSIZE][MAXSIZE];                 // 边集合
    int numVertex = 0;                          // 顶点的数量
    int numEdge = 0;                            // 边的数量

private:
    bool isGraphEmpty(void) const;      // 图是否为空
    bool isGraphFull(void) const;       // 图是否满了
    int getVertexPos(const T& vertex);  // 得到顶点的位置,若不存在则返回-1
    void errorMessage(int i);           // 错误信息

public:
    int GetNumOfVertices(void) const;       // 获得顶点的数量
    int GetNumOfEdge(void) const;           // 边的数量
    int GetWeight(const T& vertex1, const T& vertex2);  //得到边的权重
    void InsertVertex(const T& vertex);                                         // 插入顶点
    void InsertEdge(const T& vertex1, const T& vertex2, const int weight);      // 插入边
    void DeleteVertex(const T& vertex);     // 删除顶点
    void DeleteEdge(const T& vertex1, const T& vertex2); // 删除边
    void Display(void);         // 显示图

    T* DepthFirstSearch(const T& beginVertex);  //深度优先搜索
    T* BreathFirestSearch(const T& beginVertex);//广度优先搜索

    bool PathConnect(const T& vertex1, const T& vertex2);   //两顶点之间是否联通

};

MyGraph.cpp

#include "MyGraph.h"

template<class T>
CMyGraph::CMyGraph()
{
    for (int i = 0; i < MAXSIZE; i++) {
        for (int j = 0; j < MAXSIZE; j++) {
            if (i == j) {
                edge[i][j] = 0;
            }
            else {
                edge[i][j] = INFINITE;
            }
        }
    }
}

template<class T>
CMyGraph::~CMyGraph()
{
}


template<class T>
bool CMyGraph::isGraphEmpty(void) const
{
    if (0 == numVertex) {
        return true;
    }
    return false;
}

template<class T>
bool CMyGraph::isGraphFull(void) const
{
    if (MAXSIZE == numVertex) {
        return true;
    }
    return false;
}



template<class T>
int CMyGraph::getVertexPos(const T & vertex)
{
    for (int i = 0; i < numVertex; i++) {
        if (vertex == vertexArray[i]) {
            return i;
        }
    }
    return -1;
}

template<class T>
void CMyGraph::errorMessage(int i)
{
    switch (i)
    {
    case 1:
        cout << "图已满" << endl; break;
    case 2:
        cout << "图为空" << endl; break;
    case 3:
        cout << "顶点已存在" << endl; break;
    case 4:
        cout << "顶点不存在" << endl; break;
    case 5:
        cout << "权值超最大值" << endl; break;
    case 6:
        cout << "不允许两个顶点相同" << endl; break;
    default:
        break;
    }
}

template<class T>
int CMyGraph::GetNumOfVertices(void) const
{
    return numVertex;
}

template<class T>
int CMyGraph::GetNumOfEdge(void) const
{
    return numEdge;
}

template<class T>
int CMyGraph::GetWeight(const T & vertex1, const T & vertex2)
{
    if (vertex1 == vertex2) {
        errorMessage(6);
        return 0;
    }
    int pos1 = getVertexPos(vertex1);
    int pos2 = getVertexPos(vertex2);
    if (-1 == pos1 || -1 == pos2) {
        errorMessage(4);
        return INFINITE;
    }
    return edge[pos1][pos2];
}

template<class T>
void CMyGraph::InsertVertex(const T & vertex)
{
    if (isGraphFull()) {
        errorMessage(1);
        return;
    }
    if (-1 != getVertexPos(vertex)) {
        errorMessage(3);
        return;
    }
    vertexArray[numVertex] = vertex;
    numVertex++;
}

template<class T>
void CMyGraph::InsertEdge(const T & vertex1, const T & vertex2, const int weight)
{
    if (vertex1 == vertex2) {
        errorMessage(6);
        return;
    }
    int pos1 = getVertexPos(vertex1);
    int pos2 = getVertexPos(vertex2);
    if (-1 == pos1 || -1 == pos2) {
        errorMessage(4);
        return;
    }
    if (weight >= INFINITE) {
        errorMessage(5);
        return;
    }
    edge[pos1][pos2] = weight;
    numEdge++;
}

template<class T>
void CMyGraph::DeleteVertex(const T & vertex)
{
    if (isGraphEmpty()) {
        errorMessage(2);
        return;
    }
    int pos = getVertexPos(vertex);
    if (-1 == pos) {
        errorMessage(4);
        return;
    }
    if (pos == MAXSIZE - 1) {
        for (int i = 0; i < MAXSIZE - 1; i++) {
            edge[i][pos] = INFINITE;
            edge[pos][i] = INFINITE;
        }
    }
    else {
        for (int i = pos; i < MAXSIZE - 1; i++) {
            vertexArray[i] = vertexArray[i + 1];
        }
        for (int i = 0; i < MAXSIZE - 1; i++) {
            edge[i][pos] = edge[i][pos + 1];
            edge[pos][i] = edge[pos + 1][i];
        }
    }

    numVertex--;
}

template<class T>
void CMyGraph::DeleteEdge(const T & vertex1, const T & vertex2)
{
    if (vertex1 == vertex2) {
        errorMessage(6);
        return;
    }
    int pos1 = getVertexPos(vertex1);
    int pos2 = getVertexPos(vertex2);
    if (-1 == pos1 || -1 == pos2) {
        errorMessage(4);
        return;
    }
    edge[pos1][pos2] = INFINITE;
    numEdge--;
}

template<class T>
void CMyGraph::Display(void)
{
    cout << "顶点集合:" << endl;;
    for (int i = 0; i < numVertex; i++) {
        cout << vertexArray[i] << "  ";
    }
    cout << endl << "--------" << endl;
    cout << "边集合:" << endl;;
    for (int i = 0; i < numVertex; i++) {
        for (int j = 0; j < numVertex; j++) {
            cout << edge[i][j] << "  ";
        }
        cout << endl;
    }
}
template<class T>
T* CMyGraph::DepthFirstSearch(const T& beginVertex) {
        T* DFS_visitList = new T[MAXSIZE]{};
        stack visitStack;
        int length = 0;
        T temp = beginVertex;
        while (1) {
            int k = getVertexPos(temp);
            if (-1 == k) {
                errorMessage(4);
                return nullptr;
            }


            int i = 0;
            for (; i < length; i++) {
                if (temp == DFS_visitList[i]) {
                    break;
                }
            }
            if (i == length)
            {
                DFS_visitList[length] = temp;
                length++;

                for (int j = 0; j < numVertex; j++) {
                    if (k == j) {
                        continue;
                    }
                    else if (INFINITE != edge[k][j]) {
                        visitStack.push(vertexArray[j]);
                    }
                }
            }
            if (visitStack.empty()) {
                return DFS_visitList;
            }
            temp = visitStack.top();
            visitStack.pop();
        }
}

template<class T>
T * CMyGraph::BreathFirestSearch(const T & beginVertex)
{
    queue visitQueue;
    int length = 0;
    T* BFS_visitList = new T[MAXSIZE]{};
    T Temp = beginVertex;
    while (1) {
        int k = getVertexPos(Temp);
        if (-1 == k) {
            errorMessage(4);
            return nullptr;
        }

        int i = 0;
        for (; i < length; i++) {
            if (Temp == BFS_visitList[i]) {
                break;
            }
        }
        if (i == length) {
            BFS_visitList[length] = Temp;
            length++;

            for (int i = 0; i < numVertex; i++) {
                if (k == i) {
                    continue;
                }
                else if (INFINITE != edge[k][i]) {
                    visitQueue.push(vertexArray[i]);
                }
            }
        }
        if (visitQueue.empty()) {
            return BFS_visitList;
        }
        Temp = visitQueue.front();
        visitQueue.pop();
    }
}

template<class T>
bool CMyGraph::PathConnect(const T & vertex1, const T & vertex2)
{
    T* list = DepthFirstSearch(vertex1);
    if (nullptr == list) {
        return false;
    }
    while (NULL != *list) {
        if (vertex2 == *list) {
            return true;
        }
        list++;
    }
    return false;
}

源.cpp

#include "MyGraph.h"
#include "MyGraph.cpp"


void main() {
    CMyGraph<char> mg;
    mg.InsertVertex('a');
    mg.InsertVertex('b');
    mg.InsertVertex('c');
    mg.InsertVertex('d');
    mg.InsertVertex('e');
    mg.DeleteVertex('a');
    mg.InsertEdge('a', 'b', 10);
    mg.InsertEdge('b', 'c', 20);
    mg.InsertEdge('b', 'd', 12);
    mg.InsertEdge('d', 'e', 65);
    mg.Display();
    cout << "---" << endl;
    cout << "深度优先遍历:";
    char* DFS_List = mg.DepthFirstSearch('a');
    if (nullptr != DFS_List) {
        cout << DFS_List << endl;
    }
    cout << "---" << endl;
    cout << "广优先遍历:";
    char* BFS_List = mg.BreathFirestSearch('a');
    if (nullptr != BFS_List) {
        cout << BFS_List << endl;
    }
    cout << "---" << endl;
    cout << "a,e是否连通: "<< mg.PathConnect('a','e') << endl;
    cout << "e,a是否连通: " << mg.PathConnect('e', 'a') << endl;
    system("pause");
}

你可能感兴趣的:(C++数据结构)