简体   繁体   English

用邻接矩阵表示图

[英]Representing a graph with an adjacency matrix

# Creating the graph as an adjacency matrix
vertices = [[0, 1, 1, 0],
            [0, 0, 1, 0],
            [0, 0, 0, 1],
            [0, 0, 0, 0]]
edges =  [[0, 3, 4, 0],
          [0, 0, 0.5, 0],
          [0, 0, 0, 1],
          [0, 0, 0, 0]]

So I am trying to understand Dijkstra's algorithm in python but I don't understand the graph clearly since I don't understand the real meaning of each sublist, I understand that certain numbers like 3, 4, 1 and 0.5 are the distances of certain vericles but thats really it.因此,我试图了解 python 中的 Dijkstra 算法,但由于我不了解每个子列表的真正含义,所以我不清楚该图,我了解某些数字(例如 3、4、1 和 0.5)是某些距离vericles,但就是这样。 I am attaching the image of what the graph looks like visually.我附上了图表在视觉上的样子。

在此处输入图像描述

Adjacency matrices show information between pairs of vertices.邻接矩阵显示顶点对之间的信息。 In this example, you should interpret the lists like a matrix where the elements in each row and column corresponds to a pair of vertices:在此示例中,您应该将列表解释为矩阵,其中每行和每列中的元素对应于一对顶点:

  a b c d
a 0 1 1 0
b 0 0 1 0
c 0 0 0 1
d 0 0 0 0

Here, a 0 in the a column and a row, or at the position (0, 0) signifies that there is no edge from the a node to the a node and a 1 in the b column and the a row or position (0, 1) signifies that there is an edge from the a node to the b node.这里, a列和a中的0,或者position(0, 0)表示从a节点到a节点没有边并且b列和a行中的1或者position(0 , 1) 表示从a节点到b节点有一条边。 In other words, anywhere there is a 1, it signifies that there is an edge starting at the node corresponding to the row and ending at the node corresponding to the column, whereas a 0 signifies that there isn't an edge.换句话说,任何有 1 的地方,都表示有一条边从行对应的节点开始,到列对应的节点结束,而 0 表示没有边。

Apply similar logic to the edges list, which instead marks the weight of the edge rather than just marking whether an edge exists or not.将类似的逻辑应用于edges列表,它标记边缘的权重,而不仅仅是标记边缘是否存在。 As @misha mentioned, this list is not enough to represent the interconnectedness of nodes, because edges between nodes may have a weight of 0, which would be indistinguishable from an edge not existing at all, so both the edges and the vertices matrices are necessary正如@misha 提到的,这个列表不足以表示节点的互连性,因为节点之间的边的权重可能为 0,这与根本不存在的边无法区分,因此edgesvertices矩阵都是必要的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM