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. Codeforces 994 C - Two Squares

    C - Two Squares 思路: 点积叉积应用 代码: #include<bits/stdc++.h> using namespace std; #define fi first # ...

  2. ASP.NET 4.x Web Api Odata v4 backend modify query 修改查询

    有时候我们会想给予权限添加 filter 到查询上. 比如 会员和管理员都使用了 /api/products 作为 product 查询 但是会员不应该可以看见还没有上架的货品 /api/produc ...

  3. 使用C#读取网站相对路径文件夹下所有图片

    public JsonResult GetCourseInitCover() { //设置相对路径 string imgurl = Server.MapPath("~/Content/ima ...

  4. pytorch变量

    下文中所使用的pytorch版本为1.0.1 在python,如果全局变量在函数中没有提前用global申明,就修改其值,结果是这个全局变量不会被修改,会在这个函数中另外产生一个局部变量(名字相同). ...

  5. 『算法设计_伪代码』贪心算法_最短路径Dijkstra算法

    Dijkstra算法实际上是一个贪婪算法(Greedy algorithm).因为该算法总是试图优先访问每一步循环中距离起始点最近的下一个结点.Dijkstra算法的过程如下图所示. 初始化 给定图中 ...

  6. java多线程wait()方法必须放在while循环里面的原因探析

    1.写一个包子生产消费案例:一次生产或消费一个包子,有包子就消费,没有就生产.(部分代码参考传智播客刘意2015Java基础视频讲义) 1.1 写一个Baozi.class,包含main()方法,用来 ...

  7. JS代码判断IE6,IE7,IE8,IE9

    做网页有时候会用到JS检测IE的版本,下面是检测Microsoft Internet Explorer版本的三种代码! 有一种代码: <script type="text/javasc ...

  8. Ubuntu下忘记MySQL root密码解决方法

    1.忘了mysql密码,从网上找到的解决方案记录在这里. 编辑mysql的配置文件/etc/mysql/my.cnf,在[mysqld]段下加入一行“skip-grant-tables”. 2.重启服 ...

  9. MIR7预制发票扣除已经预制的数量(每月多次预制,未即时过账)

    业务场景见抬头,有没有标准的解决方案就不说了,也没去考虑... 这个增强还是SAP老表提供的,感谢,省了不少时间. INCLUDE:LMR1MF6S 最后的位置 ENHANCEMENT ZMIR7_0 ...

  10. Alibaba Java Coding Guidelines

    阿里巴巴于10月14日在杭州云栖大会上,正式发布众所期待的<阿里巴巴Java开发规约>扫描插件!该插件由阿里巴巴P3C项目组研发.P3C是世界知名的反潜机,专门对付水下潜水艇,寓意是扫描出 ...