• 图的存储之邻接矩阵


    邻接矩阵存图:

    c++

    复制代码
     1 #include
     2 using namespace std;
     3 
     4 class AM//邻接矩阵存图 用法 AM 图的名称 ={规模长(int),规模宽(int),是否为无向图,是为true,不是为false(bool)} 
     5 {
     6     private:
     7         int sn, sm;
     8         bool is_undirected_graph = true;//是否为无向图,是为true,不是为false
     9         bool arry[sn][sm];
    10     public:
    11         void AM(int input_n, int input_m,bool input_is_undirected_graph)//构造函数 input_n是规模长,input_m是规模宽,input_is_undirected_graph是 是否为无向图
    12         {
    13             this->sn = input_n; this->sm = input_m;//赋值
    14             this->is_undirected_graph = input_is_undirected_graph;//赋值
    15             return;//写不写都行
    16         }
    17         void ~AM(){}//析构函数,这里没什么用,写上更好
    18         void link(int _A_, int _B_)//从_A_到_B_连边 
    19         {
    20             if (is_undirected_garph == true)//如果是无向图
    21             {
    22                 this->arry[_A_][_B_] = this->arry[_B_][_A_] = true;//双向边连上
    23                 return;//写不写都行
    24             }
    25             else//如果不是
    26             {
    27                 this->arry[_A_][_B_] = true;//只连一个
    28                 return;//写不写都行
    29             }
    30             return//写不写都行
    31         }
    32         bool unlink(int _A_, int _B_)//去掉_A_到_B_这条边,成功返回true,失败返回false
    33         {
    34             if (this->arry[_A_][_B_] == false)//如果没边 return false;
    35             {
    36                 return false;
    37             }
    38             if (is_undirected_garph == true)//如果是无向图
    39             {
    40                 this->arry[_A_][_B_] = this->arry[_B_][_A_] = false;//两边都删
    41                 return true;//成功
    42             }
    43             else//如果不是
    44             {
    45                 this->arry[_A_][_B_] = false;//只删一边
    46                 return true;//成功
    47             }
    48         }
    49         boll ask(int _A_, int _B_)//_A_到_B_是否有边
    50         {
    51             return this->arry[_A_][_B_] == true ? true : false;
    52             /*三目运算符,如果有边,返回true,没变返回false
    53             * 格式为 a ? b : c
    54             * 意为如果a成立那么表达式为b,如果a不成立,那么返回c
    55             * 可以译作C++代码:
    56             * if (a) //也可写作if (a==true)
    57             * {
    58             *    return b;
    59             * }
    60             * else
    61             * {
    62             *    return c;
    63             * }
    64             */
    65         }
    66 };    
    复制代码

     

  • 相关阅读:
    1533_AURIX_TriCore内核架构_指令集信息
    Linux目录和文件管理
    基于Apache组件,分析对象池原理
    分布式文件存储 - - - MinIO从入门到飞翔
    vscode settings
    向勒索病毒说不,是时候重塑数据保护策略
    MATLAB 不同的surface图需要一个统一的colorbar
    Python DCM转NRRD及NRRD转NII
    快速查看Oracle数据库告警日志的存储位置
    告别BeanUtils,Mapstruct从入门到精通
  • 原文地址:https://www.cnblogs.com/algorithm-weaver/p/juzhen.html