• 可达矩阵-邻接矩阵-以及有向图的python绘制


    参考1
    自定义输入矩阵来绘制

    根据参考代码,
    自定义
    代码如下:

    # 编程实现有向图连通性的判断
    from pylab import mpl
    
    mpl.rcParams['font.sans-serif'] = ['SimHei']
    mpl.rcParams['axes.unicode_minus'] = False
    import numpy as np
    import networkx as nx
    import matplotlib.pyplot as plt
    import pylab
    
    
    #定义x三阶矩阵
    x = np.array([[1, 0, 0], [1, 1, 0], [1, 1, 1]])
    
    #随机生成x为五阶矩阵
    # x = np.random.randint(0, 2, (5, 5))
    n = len(x)
    
    value_1 = value_2 = sum_1 = sum_2 = sum_3 = sum_4 = y = final = x
    y = x + x.T
    
    # 计算可达矩阵
    for i in range(1, n):
        value_1 = np.matmul(value_1, x)
        sum_1 = sum_1 + value_1
    sum_2 = sum_1 + np.identity(n)
    
    reachability_matrix = sum_2 > 0.5
    
    print("此有向图的可达矩阵为:")
    print(reachability_matrix.astype(int))
    
    final = reachability_matrix + reachability_matrix.T
    
    for i in range(1, n):
        value_2 = np.matmul(value_2, y)
        sum_3 = sum_3 + value_2
    sum_4 = sum_3 + np.identity(n)
    reachability_matrix_1 = sum_4 > 0.5
    
    # 给出判断结果
    if ((reachability_matrix.astype(int) == np.ones((n, n)).astype(int)).all()):
        print("此有向线图G为强连通图或其为无向连通图")
    elif ((final.astype(int) == np.ones((n, n)).astype(int)).all()):
        print("此有向线图G是单向连通图")
    elif ((reachability_matrix_1.astype(int) == np.ones((n, n)).astype(int)).all()):
        print("此有向线图G是弱连通图")
    else:
        print("此有向图不连通")
    
    # 下面展示图形化输出有向图G
    G = nx.DiGraph()
    for i in range(0, n):
        j=i+1
        G.add_node(i, desc='p' + str(j))
    
    for p in range(0, n):
        for q in range(0, n):
            if x[p, q] == 1:
                G.add_edges_from([(p, q)], weight='1')
    
    edge_labels = dict([((u, v), d['weight']) for u, v, d in G.edges(data=True)])
    edge_colors = ['black']
    pos = nx.spring_layout(G)
    node_labels = nx.get_node_attributes(G, 'desc')
    nx.draw_networkx_labels(G, pos, labels=node_labels)
    nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels)
    nx.draw(G, pos, node_size=1500, edge_color=edge_colors, edge_cmap=plt.cm.Reds)
    plt.title('Directed Graph', fontsize=10)
    pylab.show()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71

    第二版

    增大了字体
    可以自定义字体大小

    # 编程实现有向图连通性的判断
    from pylab import mpl
    
    mpl.rcParams['font.sans-serif'] = ['SimHei']
    mpl.rcParams['axes.unicode_minus'] = False
    import numpy as np
    import networkx as nx
    import matplotlib.pyplot as plt
    import pylab
    
    
    #定义x三阶矩阵
    x = np.array([[1, 0, 0], [1, 1, 0], [1, 1, 1]])
    
    #随机生成x为五阶矩阵
    # x = np.random.randint(0, 2, (5, 5))
    n = len(x)
    
    value_1 = value_2 = sum_1 = sum_2 = sum_3 = sum_4 = y = final = x
    y = x + x.T
    
    # 计算可达矩阵
    for i in range(1, n):
        value_1 = np.matmul(value_1, x)
        sum_1 = sum_1 + value_1
    sum_2 = sum_1 + np.identity(n)
    
    reachability_matrix = sum_2 > 0.5
    
    print("此有向图的可达矩阵为:")
    print(reachability_matrix.astype(int))
    
    final = reachability_matrix + reachability_matrix.T
    
    for i in range(1, n):
        value_2 = np.matmul(value_2, y)
        sum_3 = sum_3 + value_2
    sum_4 = sum_3 + np.identity(n)
    reachability_matrix_1 = sum_4 > 0.5
    
    # 给出判断结果
    if ((reachability_matrix.astype(int) == np.ones((n, n)).astype(int)).all()):
        print("此有向线图G为强连通图或其为无向连通图")
    elif ((final.astype(int) == np.ones((n, n)).astype(int)).all()):
        print("此有向线图G是单向连通图")
    elif ((reachability_matrix_1.astype(int) == np.ones((n, n)).astype(int)).all()):
        print("此有向线图G是弱连通图")
    else:
        print("此有向图不连通")
    
    # 下面展示图形化输出有向图G
    G = nx.DiGraph()
    for i in range(0, n):
        j = i + 1
        G.add_node(i, desc='p' + str(j))
    
    for p in range(0, n):
        for q in range(0, n):
            if x[p, q] == 1:
                G.add_edges_from([(p, q)], weight='1')
    
    edge_labels = dict([((u, v), d['weight']) for u, v, d in G.edges(data=True)])
    edge_colors = ['black']
    pos = nx.spring_layout(G)
    node_labels = nx.get_node_attributes(G, 'desc')
    nx.draw_networkx_labels(G, pos, labels=node_labels, font_size=16)  # 设置字体大小为16
    
    nx.draw_networkx_edge_labels(G, pos, edge_labels=edge_labels, font_size=12)
    
    nx.draw(G, pos, node_size=1500, edge_color=edge_colors, edge_cmap=plt.cm.Reds)
    plt.title('Directed Graph', fontsize=10)
    pylab.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
  • 相关阅读:
    线性代数学习笔记8-3:二次型、合同矩阵、标准型、规范型
    uniapp - 倒计时组件-优化循环时间倒计时
    Java的JDBC编程
    【单片机基础】单片机的时序概念
    TypeScript学习第二天:认识ts的数据类型
    ubuntu20 install ros
    Javascript中简单数据类型的转换
    HTTP vs RPC:理解两种通信协议的区别
    前端时间分片渲染
    [附源码]Python计算机毕业设计Django和vue的茶文化交流平台的设计与实现
  • 原文地址:https://blog.csdn.net/qq_41517071/article/details/134192732