- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我曾尝试用 C 语言搜索 BFS 实现,但它们似乎都期待邻接矩阵形式的图形,据我了解,它能够立即找出所有相邻节点。
输入示例
{
Nodes:
0
1
2
3
Edges:
0 2
1 3
2 3
0 1
}
伪代码
BFS (G, s) //Where G is the graph and s is the source node
let Q be queue.
Q.enqueue( s )
mark s as visited.
while ( Q is not empty)
v = Q.dequeue( )
for all neighbours w of v in Graph G /* <---------------------------------------- HOW? */
if w is not visited
Q.enqueue( w )
mark w as visited.
来自 https://www.hackerearth.com/practice/algorithms/graphs/breadth-first-search/tutorial/
最佳答案
I have tried to google BFS implementations in C but all of them seem to be expecting graph in form of adjacency matrix, and from what I have understood it enables to find out all adjacent nodes in no time.
嗯,不。邻接矩阵允许您在与节点数无关的非零时间内找到相邻节点的集。但是仍然需要与节点数成正比的时间来确定该集合的所有元素是什么。其他表示,例如邻接列表,可以允许在相同的常数时间内找到集合,并及时找到与其数量成正比的元素(这可能比节点总数少得多) .
- But what am I supposed to do to find out adjacent nodes if the input is in form of pair of nodes?
如何构建邻接矩阵或邻接列表或替代表示,然后使用它?
- Am i supposed to store the edges and loop through them each time I am looking for neighbours? But that sounds slow and like something dummy like me would do :( .
平面的边列表是一种可能的表示。有一些方法可以使这样的列表更有效地使用(例如,通过对其进行排序和/或对其进行索引),但这是否真的成功取决于问题。
- Or am I somehow supposed to convert the input to adjacency matrix?
如果您确实想创建一个邻接矩阵,那么首先创建一个表示所有节点且没有边的矩阵。阅读边列表,并为其中的每条边填写适当的条目,或者如果图是无向的,则填写适当的两个条目。
关于c - 如果图不是邻接矩阵的形式,如何找到节点的所有邻居?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52173701/
我从 SpatialPolygonsDataFrame 开始,它包含用于创建加纳各地区 map 的数据(可在 http://www.diva-gis.org/datadown 获取)。我正在尝试创建一
我遇到了一个问题,我需要根据存储在前一个元素中的信息修改容器的元素。示例: 如果前一个 vector 元素可被 2 整除,则将当前元素乘以 10 vector -> [12, 11, 33, 10]
总的来说,我对脚本编写还很陌生。我正在编写一个 expect 脚本,它通过 ssh 进入 Cisco 交换机,并运行“show cdp neighbors”命令来获取连接到交换机的所有设备的列表。然后
我正在尝试比较节点的值。使用 flood-fill 算法,我能够垂直和水平检查网格的每个节点。现在我必须更新我的代码以检查位于对 Angular 线上的单元格,如下图所示: 红色是当前节点,黄色是需要
我使用预先计算的指标使用 Scikit-Learn 的最近邻/半径分类。这意味着,我将成对距离的 n_samples_train x n_samples_train 矩阵传递给分类器的拟合方法。 现在
我有一个大的稀疏图,我将其表示为邻接矩阵(100k x 100k 或更大),存储为边数组。具有(非稀疏)4 x 4 矩阵的示例: 0 7 4 0 example_array = [ [7,1,2],
从有向图中并给出两个顶点 (v, u) 我需要找到:共同的“出”邻居和共同的“入”邻居。 例如: import networkx as nx ghybrid = nx.DiGraph() ghybri
我正在使用 JavaScript 进行图像处理,我想知道是否有任何通用公式可以确定像素的 x 邻居。 我知道对于 3*3 的正方形,可以使用特定的 x 和 y 像素确定 8 个邻居。 (x-1,y-1
在 CentOS 6.4(内核 2.6.32)上,为什么下面的第二个 arping 调用会创建一个新的 ARP 表条目,而第一个不会?网络行为是相同的,我感到困惑的是,在我看来,系统调用实际上是等同的
我是一名优秀的程序员,十分优秀!