【发布时间】:2016-01-14 16:03:39
【问题描述】:
我的作业是使用 c 语言创建从 S 到有向图中任何其他顶点的最短路径数
图表显示为 txt 文件,如下所示:
3 // number of vertex in G
{2,3},{1},{} // in the first {} we can see the neighbors for V1 , in the second for V2 and so on
我必须为 s 打印一个最短路径数的数组
我使用的算法类似于 BFS,并添加了一些内容: numOfShortest(G,S)
for vertex x which belongs to gropu V-S
do color[x]=white, d[x]=0, F[x]=0
color[s]=gray,d[s]=0,F[s]=1
while Q is not empty //= let Q be a queue
do u=dequeue(Q)
for each vertex v = N(u) // = for every neighbor of u
do if color[v] = white
then color[v]= gray, d[v]=d[u]+1
F[v]=f[v]+f[u] // = v must have atleast the same number of paths as u
enqueue(Q,v)
else if color[v]=gray
then if d[u] < d[v]
then f[v]=f[v]+f[u]
color[u]=black // = when finished with every N(u)
现在我必须考虑一些事情(如果我错了,请纠正我)
- 使用链表实现入队
- 为每个包含邻居的 v 创建一个名为 vertex 的结构 (使用动态数组)
- 我需要以某种方式将写入文件的邻居扫描到 结构顶点上的邻居
也许我在准备方面做得太过分了,有一种更简单的方法可以做到这一点,我脑子里有些混乱。 感谢谁能帮忙
【问题讨论】:
标签: c directed-graph breadth-first-search