邻接矩阵:无权图删除边

文章描述了一个使用C++模板设计的图的邻接矩阵抽象数据类型,包括无权图中删除两个顶点关联边的算法。类定义包含了图的创建、打印邻接矩阵、检查图是否为空等方法。输入和输出格式被详细规定,包括图的类型、顶点数、边数等信息。示例展示了如何在有向图中删除边并更新邻接矩阵。
摘要由CSDN通过智能技术生成

问题描述 :

目的:使用C++模板设计并逐步完善图的邻接矩阵抽象数据类型(ADT)。

内容:(1)请参照图的邻接矩阵模板类原型,设计并逐步完善图的邻接矩阵ADT。(由于该环境目前仅支持单文件的编译,故将所有内容都集中在一个源文件内。在实际的设计中,推荐将抽象类及对应的派生类分别放在单独的头文件中。)

(2)设计并实现一个算法,在已存在的无权图中删除两个顶点的关联边。删除成功,返回true;否则返回false。图的存储结构采用邻接矩阵。将其加入到ADT中。

函数原型:

//图删除边 

template<class TypeOfVer, class TypeOfEdge>

bool adjmatrix_graph<TypeOfVer, TypeOfEdge>::Delete_Edge( int u, int v ); //有向图时,u为弧尾,v为弧头

注意:DG(有向图), DN(有向网), UDG(无向图), UDN(无向网)

输入说明 :

建图的输入数据格式参见建图的算法说明。

第一行:图的类型

第二行:结点数

第三行:结点集

第四行:边数

第五行:边集

第六行:邻接顶点1

第七行:邻接顶点2

输出说明 :

第一行:顶点集

第二行:删除边前的边数

第三行:删除边前的邻接矩阵

空行

第四行:true(false)

第五行:删除边后的边数

第六行:删除边后的邻接矩阵

输入范例 :

DG
6
A B C D E F
7
0 1
0 2
0 3
1 4
2 4
3 0
3 5
0
3

输出范例 :

A B C D E F
7
0 1 1 1 0 0 
0 0 0 0 1 0 
0 0 0 0 1 0 
1 0 0 0 0 1 
0 0 0 0 0 0 
0 0 0 0 0 0 

true
6
0 1 1 0 0 0 
0 0 0 0 1 0 
0 0 0 0 1 0 
1 0 0 0 0 1 
0 0 0 0 0 0 
0 0 0 0 0 0 

#include <iostream>
#include <vector>
#include <string>
using namespace std;
template <class TypeOfVer, class TypeOfEdge>
class adjmatrix_graph {
public:
    int Vers;
    int Edges;
    vector<vector<TypeOfEdge>> edge;
    vector<TypeOfVer>  ver;
    TypeOfEdge noEdge;
    string GraphKind;
    bool DIRECTION = false, WEIGHT = false;

    adjmatrix_graph()
    {
        Vers = 0;
        Edges = 0;
        edge.clear();
        ver.clear();
        noEdge = 0;
    }
    bool PrintMatrix() {

        for (int i = 0; i < Vers; i++)
        {
            for (int j = 0; j < Vers - 1; j++)
                cout << edge[i][j] << " ";
            cout << edge[i][Vers - 1] << " ";
            cout << endl;
        }
        return 1;
    }
    bool GraphisEmpty() { return Vers == 0; }
    string GetGraphKind() { return GraphKind; }
    TypeOfVer GetVer(int u) {
        return ver[u];
    }
    int GetFirstAdjVex(int u) {
        for (auto i : edge[u])if (i != noEdge)return i;
        return -1;

    } //返回G中指定顶点u的第一个邻接顶点的位序(顶点集)。若顶点在G中没有邻接顶点,则返回-1 


    int GetNextAdjVex(int u, int v) {
        for (int i = v + 1; i < Vers; ++i)
            if (edge[u][i] != noEdge)return edge[u][i];
        return -1;
    }//返回G中指定顶点u的下一个邻接顶点(相对于v)的位序(顶点集)。若顶点在G中没有邻接顶点,则返回-1
    //bool PutVer(int u, TypeOfVer data); //对G中指定顶点赋值 onst TypeOfVer& data); //往G中添加一个顶点 
    //int LocateVer(TypeOfVer data); //返回G中指定顶点的位置 

    void CreateGraph(bool need_emp)
    {
        cin >> GraphKind;//图的类型 
        cin >> Vers;//结点数
        ver.resize(Vers);
        for (int i = 0; i < Vers; i++)//结点集
            cin >> ver[i];

        if (need_emp)
            cin >> noEdge;//无边标记

        vector<TypeOfEdge> line;//邻接矩阵初始化
        for (int j = 0; j < Vers; j++)
        {
            for (int i = 0; i < Vers; i++)
                line.push_back(noEdge);
            edge.push_back(line);
        }

        cin >> Edges;//边数
        vector<int> x_p, y_p, w_p;
        for (int i = 0; i < Edges; i++)
        {
            int c_x, c_y;
            cin >> c_x >> c_y;
            x_p.push_back(c_x);
            y_p.push_back(c_y);
        }

        if (GraphKind == "DG")//DG(有向图)
            DIRECTION = true, WEIGHT = false;
        if (GraphKind == "DN")//DN(有向网)
            DIRECTION = true, WEIGHT = true;
        if (GraphKind == "UDG")//UDG(无向图)
            DIRECTION = false, WEIGHT = false;
        if (GraphKind == "UDN")//UDN(无向网)
            DIRECTION = false, WEIGHT = true;

        if (WEIGHT)
            for (int i = 0; i < Edges; i++)
            {
                int c_w;
                cin >> c_w;
                w_p.push_back(c_w);
            }

        for (int i = 0; i < Edges; i++)
        {
            if (DIRECTION == false)//无向图操作
            {
                if (WEIGHT == true)//带权值的网的操作
                    edge[x_p[i]][y_p[i]] = edge[y_p[i]][x_p[i]] = w_p[i];
                else//无权值操作
                    edge[x_p[i]][y_p[i]] = edge[y_p[i]][x_p[i]] = 1;
            }
            else
            {
                if (WEIGHT == true)//带权值的网的操作
                    edge[x_p[i]][y_p[i]] = w_p[i];
                else//无权值操作
                    edge[x_p[i]][y_p[i]] = 1;
            }
        }
    }
    int GetVerNum() { return Vers; }    //取得当前顶点数 
    int GetEdgeNum() { return Edges; }  //取得当前边数 
    void Insert_Edge(int u, int v) {
        edge[u][v] = 1;
    }
    void Insert_Edge(int u, int v, TypeOfEdge w) {
        edge[u][v] = w;
    }
    //bool DeleteVer(const TypeOfVer& data); //往G中删除一个顶点
    bool Delete_Edge(int u, int v) {
        if (DIRECTION) {
            if (edge[u][v] == noEdge)return false;
            edge[u][v] = noEdge;
            --Edges;
            return true;
        }
        else {
            if (edge[u][v] == noEdge|| edge[v][u] == noEdge)return false;
            edge[u][v] = edge[v][u] = noEdge;
            --Edges;
            return true;
        }
    }
    bool Delete_Edge(int u, int v, TypeOfEdge w) {
        if (edge[u][v] != w)return false;
        edge[u][v] = noEdge;
        --Edges;
        return true;
    }
    //void DFS_Traverse(int u); //DFS遍历(外壳部分)
    //void BFS_Traverse(int u); //BFS遍历
    //~adjmatrix_graph(); //析构函数 
    void Print_Format(bool Type) {
        if (Type)cout << GetGraphKind() << endl;
        for (auto i : ver) {
            cout << i << " \n"[i == ver.back()];
        }
        cout << endl;
        PrintMatrix();
    }
};

int main()
{
    adjmatrix_graph<char, int>g;
    g.CreateGraph(0);
    int u, v;
    cin >> u >> v;
    
    for (auto i : g.ver) {
        cout << i << " \n"[i == g.ver.back()];
    }
    cout << g.GetEdgeNum() << endl;
    g.PrintMatrix();
    cout << endl;
    cout << (g.Delete_Edge(u, v) ? "true" : "false") << endl;
    cout << g.GetEdgeNum() << endl;
    g.PrintMatrix();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值