如何遍历邻接矩阵?
假设我生成了以下邻接矩阵,那么
A B C D E F G H I
A 0 1 0 1 0 0 0 0 0
B 1 0 0 0 0 0 0 0 0
C 0 0 0 1 0 0 0 0 0
D 1 0 1 0 0 0 1 0 0
E 0 0 0 0 0 1 0 0 0
F 0 0 0 0 1 0 0 0 0
G 0 0 0 1 0 0 0 0 0
H 0 0 0 0 0 0 0 0 1
I 0 0 0 0 0 0 0 1 0
遍历以确认我可以从 G 到 B 的最佳方法是什么? 因为
[G][D] = true
[A][D] = true
[A][B] = true
G-->D-->A-->B
我知道 BFS/DFS,但对我可以用这个矩阵做什么以便我可以为其实现 BFS/DFS 感到困惑。
任何帮助表示感谢,谢谢!
Say I have the following adjacency matrix produced
A B C D E F G H I
A 0 1 0 1 0 0 0 0 0
B 1 0 0 0 0 0 0 0 0
C 0 0 0 1 0 0 0 0 0
D 1 0 1 0 0 0 1 0 0
E 0 0 0 0 0 1 0 0 0
F 0 0 0 0 1 0 0 0 0
G 0 0 0 1 0 0 0 0 0
H 0 0 0 0 0 0 0 0 1
I 0 0 0 0 0 0 0 1 0
Whats the best way to traverse through to confirm that I can go from G to B?
since
[G][D] = true
[A][D] = true
[A][B] = true
G-->D-->A-->B
I am aware of BFS/DFS but stumped as to what I can do with this matrix so that I can implement BFS/DFS for it.
Any help is appreciated thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您只需要查看是否可以到达某个节点,请使用 BFS 或 DFS。
If you only need to see if you can reach some node use BFS or DFS.
使用任何旧的图形搜索,例如:
Use any old graph search, for example:
如果将邻接矩阵与其本身相乘,您将得到一个包含长度为 2 的所有路径的矩阵,依此类推。
矩阵的 n 次方将显示所有节点之间长度为 n 的路径数。
当然,如果您只需要两个节点之间的距离,则不必进行完整的矩阵乘法。
If you multiply the adjacency matrix by itself, you'll get a matrix that contains all paths with a length of 2 and so on.
Your matrix raised to the power of n will show you the number of paths of length n between all the nodes.
Of course if you only need the distance between two nodes, you don't have to do the full matrix multiplication.