如何从邻接矩阵创建无向图?
你好,到处都有一个通过绘图热来创建图形的解释。矩阵。但是,我需要简单的伪代码或算法......我知道如何从 adj 中绘制它。矩阵,不知道为什么没有人在哪里解释如何实际将其放入代码中。我不是指实际的代码,而是至少指算法...很多人说.. 1 是如果有一个我知道的边缘.. 我已经创建了 adj。矩阵,不知道如何将其转换为图形。我的顶点没有名称,它们只是矩阵的索引。例如 1-9 是“我的矩阵的名称”
1 2 3 4 5 6 7 8 9
1 0 1 0 0 1 0 0 0 0
2 1 0 1 0 0 0 0 0 0
3 0 1 0 1 0 0 0 0 0
4 0 0 1 0 0 1 0 0 0
5 1 0 0 0 0 0 1 0 0
6 0 0 0 1 0 0 0 0 1
7 0 0 0 0 1 0 0 1 0
8 0 0 0 0 0 0 1 0 0
9 0 0 0 0 0 1 0 0 0
,最初是一个迷宫...必须将 row1 col4 标记为开始,row7 col8 结束...
没有人告诉我如何从矩阵中实现图形(没有笔) :
谢谢
Hello everywhere there is an explanation by drawings hot to create graph out of adj. matrix. However, i need simple pseudo code or algorithym for that .... I know how to draw it out of adj. matrix and dont know why nobody no where explains how to actually put it in code. I dont mean actual code but at least algorithm ... Many say .. 1 is if there is an edge i know that.. I have created the adj. matrix and dont know how to transfer it to graph. My vertices dont have names they are just indexes of the matrix. for example 1-9 are the "names of my matrix"
1 2 3 4 5 6 7 8 9
1 0 1 0 0 1 0 0 0 0
2 1 0 1 0 0 0 0 0 0
3 0 1 0 1 0 0 0 0 0
4 0 0 1 0 0 1 0 0 0
5 1 0 0 0 0 0 1 0 0
6 0 0 0 1 0 0 0 0 1
7 0 0 0 0 1 0 0 1 0
8 0 0 0 0 0 0 1 0 0
9 0 0 0 0 0 1 0 0 0
that was originaly a maze ... have to mark row1 col4 as start and row7 col8 end ...
Nobody ever told me how to implement graph out of matrix (without pen) :Pp
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对称性
邻接矩阵是图的表示。对于无向图,其矩阵是对称的。例如,如果存在从
顶点 i
到顶点 j
的边,则也必须存在从顶点 j
到顶点的边我。这实际上是同一个边缘。
算法
注意到这种性质,您可以像下面这样简单地实现您的算法:
Nature of symmetry
Adjancency matrix is a representation of a graph. For undirected graph, its matrix is symmetrical. For instance, if there is an edge from
vertex i
tovertex j
, there must also be an edge fromvertex j
tovertex i
. That is the same edge actually.Algorithm
Noticing this nature, you can implement your algorithm as simple as:
您可以将图从邻接矩阵表示形式转换为基于节点的表示形式,如下所示:
这里,图表示为节点数组,其中包含指向可到达的相邻节点的指针。设置节点及其邻居指针后,您可以使用此数据结构来执行所需的图形算法,在此(简单的)示例中打印出每个节点具有的出站有向边的数量。
You can convert a graph from an adjacency matrix representation to a node-based representation like this:
Here, the graph is represented as an array of nodes with pointers to reachable neighbouring nodes. After setting up the nodes and their neighbour pointers you use this data structure to perform the graph algorithms you want, in this (trivial) example print out the number of outbound directed edges each node has.