body, table{font-family: 微软雅黑; font-size: 13.5pt}
table{border-collapse: collapse; border: solid gray; border-width: 2px 0 2px 0;}
th{border: 1px solid gray; padding: 4px; background-color: #DDD;}
td{border: 1px solid gray; padding: 4px;}
tr:nth-child(2n){background-color: #f8f8f8;}

/* Graph.h */
#ifndef __GRAPH_H__
#define __GRAPH_H__
#include<iostream>
#include<string>
#include<fstream>
#define value_type char   //图顶点的数据类型
#define weight_vaule_type int
#define max_weight_value_type 0x7FFFFFFF   //默认int类型是signed int
//图中邻接矩阵都是动态申请空间
using namespace std;
namespace meihao
{
        static bool graph_error = false;
        class Graph
        {
        public:
                Graph(const string& fileName):_vertexNum(0)
                        ,_vertex(nullptr)
                        ,_arc(nullptr)
                {
                        readGraphFromFile(fileName);
                }
                ~Graph();
                void printVertexMatrix()const;
                void printGraphEdgeMatrix()const;
                int getGraphVertexNumber()const;
                weight_vaule_type getGraphEdgeWeight(int vi,int vj)const;
                value_type getGraphVertexData(int vi)const;  //获取第vi个顶点的值
                int setGraphEdgeWeight(int vi,int vj,int weight);  //修改边的权值
                int getOutDegree(int vi)const;  //获取某一个顶点的出度
                int getInputDegree(int vi)const;  //获取某一个顶点的入度
        protected:
                void readGraphFromFile(const string& fileName);
                void writeGraphFromFile(const string& fileName);
        private:
                int _vertexNum;  //图的顶点数
                value_type* _vertex;   //顶点数组
                weight_vaule_type** _arc;      //邻接矩阵,边数组
        };
};
#endif

/* mainfunc.cpp */
#include"Graph.h"
int main()
{
        meihao::Graph g("data.txt");
        cout<<"图顶点数:"<<g.getGraphVertexNumber()<<endl;
        cout<<"图顶点矩阵:"<<endl;
        g.printVertexMatrix();
        g.printGraphEdgeMatrix();
        cout<<"点A的入度:"<<g.getInputDegree(0)<<endl;
        cout<<"点A的出度:"<<g.getOutDegree(0)<<endl;
        cout<<"(A,C)="<<g.getGraphEdgeWeight(1,2)<<endl;
        cout<<"set <C,D> =5"<<endl;
        g.setGraphEdgeWeight(2,3,5);
        cout<<"(C,D) ="<<g.getGraphEdgeWeight(2,3)<<endl;
        system("pause");
}

/* data.txt */
4
A B C D
0 1 1 1
1 0 1 0
1 1 0 1
1 0 1 0

运行结果:

//文件目录结构
/* Graph.cpp */
#include"Graph.h"
namespace meihao
{
        void Graph::readGraphFromFile(const string& filename)
        {
                ifstream ifs(filename.c_str(),ios::in|ios::out);  //打开文件,可读可写
                if(ifs.bad())
                {
                        cout<<"read file data init Graph failure!"<<endl;
                        return ;
                }
                ifs>>_vertexNum;
                _vertex = new value_type[_vertexNum]();
                for(int idx=0;idx!=_vertexNum;++idx)  //初始化顶点数组
                        ifs>>_vertex[idx];
                //动态开辟二维数组的内存空间
                _arc = new weight_vaule_type*[_vertexNum]();
                for(int idx=0;idx!=_vertexNum;++idx)
                {
                        _arc[idx] = new weight_vaule_type[_vertexNum]();
                }
                //初始化边数组,邻接矩阵
                for(int idx=0;idx!=_vertexNum;++idx) 
                {
                        for(int iidx=0;iidx!=_vertexNum;++iidx)
                        {
                                ifs>>_arc[idx][iidx];
                        }
                }
                ifs.close();
        }
        void Graph::writeGraphFromFile(const string& fileName)
        {
                ofstream ofs(fileName.c_str(),ios::out);
                if(ofs.bad())
                {
                        cout<<"write file Graph failure!"<<endl;
                        return ;
                }
                //写顶点数
                ofs<<_vertexNum<<endl;
                //写顶点数组
                for(int idx=0;idx!=_vertexNum;++idx)
                {
                        ofs<<_vertex[idx]<<" ";
                }
                ofs<<endl;
                //写邻接矩阵
                for(int idx=0;idx!=_vertexNum;++idx)
                {
                        for(int iidx=0;iidx!=_vertexNum;++iidx)
                        {
                                ofs<<_arc[idx][iidx]<<" ";
                        }
                        ofs<<endl;
                }
                ofs<<endl;
        }
        void Graph::printVertexMatrix()const
        {
                for(int idx=0;idx!=_vertexNum;++idx)
                {
                        cout<<_vertex[idx]<<" ";
                }
                cout<<endl;
        }
        void Graph::printGraphEdgeMatrix()const
        {
                for(int idx=0;idx!=_vertexNum;++idx)
                {
                        for(int iidx=0;iidx!=_vertexNum;++iidx)
                        {
                                cout<<_arc[idx][iidx]<<" ";
                        }
                        cout<<endl;
                }
                cout<<endl;
        }
        int Graph::getGraphVertexNumber()const
        {
                return _vertexNum;
        }
        value_type Graph::getGraphVertexData(int vi)const
        {
                return _vertex[vi];
        }

        weight_vaule_type Graph::getGraphEdgeWeight(int vi,int vj)const
        {
                if(vi<0||vj<0||vi==vj||vi>=_vertexNum||vi>=_vertexNum)
                {
                        graph_error = true;
                        return -1;   //因为图的权值可以是负数,为了区分,就拿一个bool变量来标记什么情况下-1表示出错了
                }
                return _arc[vi][vj];
        }
        int Graph::setGraphEdgeWeight(int vi,int vj,weight_vaule_type weight)
        {
                if(vi<0||vj<0||vi==vj||vi>=_vertexNum||vi>=_vertexNum||weight>max_weight_value_type)
                {
                        graph_error = true;
                        return -1;  
                }
                _arc[vi][vj] = weight;
                writeGraphFromFile("data.txt");
        }
        int Graph::getOutDegree(int vi)const
        {
                if(vi<0||vi>=_vertexNum)
                {
                        graph_error = true;
                        return -1;
                }
                int OD = 0;
                for(int idx=0;idx!=_vertexNum;++idx)
                        OD += _arc[vi][idx];
                return OD;
        }
        int Graph::getInputDegree(int vi)const
        {
                if(vi<0||vi>=_vertexNum)
                {
                        graph_error = true;
                        return -1;
                }
                int ID = 0;
                for(int idx=0;idx!=_vertexNum;++idx)
                        ID += _arc[idx][vi];
                return ID;
        }
        Graph::~Graph()
        {
                if(nullptr!=_vertex)
                {
                        delete []_vertex;
                        _vertex = nullptr;
                }       
                if(nullptr!=_arc)
                {
                        for(int idx=0;idx!=_vertexNum;++idx)
                                delete []_arc[idx];
                        delete []_arc;
                        _arc = nullptr;
                }       
        }
};

图的邻接矩阵存储实现,C++描述的更多相关文章

  1. _DataStructure_C_Impl:图的邻接矩阵存储

    //_DataStructure_C_Impl:邻接矩阵 #include<stdio.h> #include<stdlib.h> #include<string.h&g ...

  2. java 图的邻接矩阵

    有向图 在有向图中,结点对<x ,y>是有序的,结点对<x,y>称为从结点x到结点y的一条有向边,因此,<x,y>与<y,x>是两条不同的边.有向图中的 ...

  3. PTA 邻接矩阵存储图的深度优先遍历

    6-1 邻接矩阵存储图的深度优先遍历(20 分) 试实现邻接矩阵存储图的深度优先遍历. 函数接口定义: void DFS( MGraph Graph, Vertex V, void (*Visit)( ...

  4. 数据结构C++实现邻接矩阵存储图

    定义邻接矩阵存储的图类.[实验要求] 1. 创建一个邻接矩阵存储的图: 2. 返回图中指定边的权值: 3. 查找图中某顶点的第一个邻接顶点.某顶点关于另一个顶点的下一个邻接顶点序号: 4. 图的深度优 ...

  5. 算法与数据结构(四) 图的物理存储结构与深搜、广搜(Swift版)

    开门见山,本篇博客就介绍图相关的东西.图其实就是树结构的升级版.上篇博客我们聊了树的一种,在后边的博客中我们还会介绍其他类型的树,比如红黑树,B树等等,以及这些树结构的应用.本篇博客我们就讲图的存储结 ...

  6. 数据结构(12) -- 图的邻接矩阵的DFS和BFS

    //////////////////////////////////////////////////////// //图的邻接矩阵的DFS和BFS ////////////////////////// ...

  7. Java图的邻接矩阵实现

    /** * * 图的邻接矩阵实现 * @author John * * @param <T> */ class AMWGraph<T> { private ArrayList& ...

  8. E1_1 用邻接矩阵存储有向图,并输出各顶点的出度和入度

    参考书:图论算法理论.实现及应用(北京大学出版社) 输入数据:(test.txt) 程序: /* 邻接矩阵存储有向图 */ #include <cstring> #include < ...

  9. 图的邻接矩阵实现(c)

    参考:算法:c语言实现 一书 图的邻接矩阵实现 #ifndef GRAPH #define GRAPH /* 图的邻接矩阵实现 */ #include<stdio.h> #include& ...

随机推荐

  1. Python全栈开发-执行字符串形式的语句和字符串形式的表达式方法(即exec和eval方法)

    Python有时需要动态的创造Python代码,然后将其作为语句执行  或  作为表达式计算. exec用于执行存储在字符串中的Python代码.   1. 语句与表达式的区别:表达式是 某事,语句是 ...

  2. 免费的剪贴板工具Ditto安装与使用

    下载地址:https://sourceforge.net/projects/ditto-cp 直接安装,选择好安装的位置,一直选择默认的选项就行

  3. PHP数组合并和去重的函数有哪些

    PHP数组合并和去重的函数有哪些 一.总结 一句话总结:合并:array_merge() array_merge_recursive() +号:去重:array_flip() array_unique ...

  4. Redis入门指南之二(安装及配置)

    本节主要内容 1. 前言2. redis安装3. 启动和停止Redis 1. 前言 安装Redis需要知道自己需要哪个版本,有针对性的安装,比如如果需要redis GEO这个地理集合的特性,那么red ...

  5. Transcranial magnetic stimulation (TMS)

    Transcranial magnetic stimulation (TMS) Effect of Transcranial Magnetic Stimulation on Free Will Tra ...

  6. shiro中JSP标签

    Shiro提供了JSTL标签用于在JSP/GSP页面进行权限控制,如根据登录用户显示相应的页面按钮. 导入标签库 <%@taglib prefix="shiro" uri=& ...

  7. nginx-exporter安装使用

    一.没有vts的启动方式   #nginx_exporter -telemetry.address=:9113 -nginx.scrape_uri="http://127.0.0.1:100 ...

  8. php 求余

    一,异号 1.函数值符号规律(余数的符号) mod(负,正)=正 mod(正,负)=负 结论:两个整数求余时,其值的符号为除数的符号. 2.取值规律 先将两个整数看作是正数,再作除法运算 ①能整除时, ...

  9. Grafana安装配置介绍

    一.Grafana介绍 Grafana是一个可视化面板(Dashboard),有着非常漂亮的图表和布局展示,功能齐全的度量仪表盘和图形编辑器,支持Graphite.zabbix.InfluxDB.Pr ...

  10. AWVS和AppScan使用代理进行扫描教程

    一.说明 扫描网站时,一是可能某些网站网络不能直接访问,二是可能不想曝露本机IP.这时要进行处理. 第一个方法是如果有vpn直接登vpn,vpn的话由于是直接修改操作系统层次的网络,扫描器不需要额外做 ...