创建不包含在 BFS 中的简单路径边

发布于 2024-10-19 06:04:29 字数 415 浏览 5 评论 0原文

首先...这是问题...

给出一个有向图 G = (V, E) 的示例,V 中的源顶点 s 以及 E 中包含的一组树边 F,使得对于包含的每个顶点在 V 中,图中从 s 到 v 的唯一简单路径 (V, F) 是 G 中的最短路径,但边集 F 不能通过在 G 上运行 BFS 来生成,无论每个顶点的排序方式如何邻接表。

现在......因为这是家庭作业,我不需要一个直接的答案,而是更多关于要查看/尝试的想法。但是,这是我到目前为止所想到的......

我基本上认为我可以创建一个具有到特定顶点的几条最短路径的图。当然,这些路径之一可以在 BFS 中找到。但是,我可以使用其中一条替代路线来适应 F。但是,让我失望的部分是“无论顶点如何排序”......我不知道如何将其包含到我的过程。我尝试过循环、各种不同的方向流,但还没有任何结果。

还有其他想法吗?

First off...here's the problem...

Give an example of a directed graph G = (V, E), a source vertex s in V, and a set of tree edges F contained in E, such that for each vertex contained in V, the unique simple path in the graph (V, F) from s to v is a shortest path in G, yet the set of edges F cannot be produced by running BFS on G, no matter how the vertices are ordered in each adjacency list.

Now...since this IS homework, I don't want a straight-out answer, but more of ideas for things to look at/try. But, here's what I've thought of so far...

I've basically figured that I can create a graph that has several shortest paths to a particular vertex. Of course, one of those paths would be found in the BFS. But, I can use one of the alternate routes to fit into F. However, the part that is throwing me off is the "no matter how the vertices are ordered"... I'm not sure how to include that in to my process. I've tried loops, various different directional flows, and nothing yet.

Any other ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梅窗月明清似水 2024-10-26 06:04:29
    3
 s ---- x
 |     /  
1|    / 1
 |   /
 |  /
 y

如果上图没有显示

假设

E = { ((s, x) : 3), ((s, y) : 1), ((y, x) : 1) }

其中二元组是带有权重的有序对。

边集F = { (s, y), (y, x) } 包含图的所有顶点。此外,它包含的从 s 到 x 的唯一简单路径是图中从 s 到 x 的最短路径。然而,BFS 永远不会找到 F。

从s开始,x和y将被发现并标记为灰色。
然后,当探索 y 时,它只会找到另一个灰色顶点,即 x,并且不会将其作为后代添加到生成的广度优先树中。

    3
 s ---- x
 |     /  
1|    / 1
 |   /
 |  /
 y

In case the above picture doesn't show

Assume

E = { ((s, x) : 3), ((s, y) : 1), ((y, x) : 1) }

where the two-tuples are ordered pairs with weights attached.

The edge set F = { (s, y), (y, x) } contains all the vertices of the graph. Further, the unique simple path it contains from s to x is the shortest path in the graph from s to x. However, F will never be found by a BFS.

Starting from s, x and y will be discovered and marked gray.
Then, when y is explored, it will only find one other gray vertex, i.e. x, and not add it as a descendant in the resulting breadth-first tree.

十年不长 2024-10-26 06:04:29

我不确定你是否只是为了找到一个反例,或者找到一个在任何给定图上运行的算法。找到一个反例并不是很难,比如

  0
 / \
1   2
|\ /|
| x |
|/ \|
3   4

源为0,边集F包含(0,1)、(0,2)、(1,3)、(2,4)。 F 不能由 BFS 产生。

我还没有找到任何图的通用算法,但也许上面的示例可以提供线索。

I am not sure whether you are just to find a counter-example, or find an algorithm running on any given graph. Finding a counter-example is not very difficult, for example,

  0
 / \
1   2
|\ /|
| x |
|/ \|
3   4

The source is 0. The edge set F contains (0,1) (0,2), (1,3), (2,4). F cannot be produced by BFS.

I haven't found a general algorithm for any graph, but maybe the above sample can provide a clue.

梦回旧景 2024-10-26 06:04:29

这个问题很旧,但我发布了另一个答案,因为它已经被查看了很多次。这是 Cormen/Leiserson/Rivest/Stein 的《算法》书中的问题 22-2.6,其他人很可能会遇到。

上述响应假设该图已加权。如果图被视为未加权,BFS 可以在任一响应中生成边集。问题并没有说图是加权的,并且在 Cormen 中它位于有关未加权图的部分中。

这里是一个解决方案未加权的情况。步骤是:

  1. 绘制一个从源 s 到 2 个顶点 x 和 y 的图。 s 的出度为 2。x 和 y 分别出向 a 和 b,而不出向其他地方。
  2. BFS 无法访问的树是从 s 到 x,从 x 到 a,然后从 s 到 y,再从 y 到 b。

它之所以有效,是因为 a 和 b 到 x 和 y 的距离相同。如果您先将 x 入队,那么您通过 BFS 的路径将从 x 到 a 和 b。如果你先将 y 入队,那么你通过 BFS 的路径将是从 y 到 a 和 b。在 BFS 中,您不能像解决方案那样混合和匹配。

The question is old, but I am posting another answer because it has been viewed many times. This is question 22-2.6 in Cormen/Leiserson/Rivest/Stein's Algorithms book and is likely to be encountered by others.

The responses above assume that the graph is weighted. BFS can produce the edge set in either response if the graph is taken as unweighted. The problem does not say that the graph is weighted, and in Cormen it is in a section about unweighted graphs.

Here is a solution in the unweighted case. The steps are:

  1. Take a graph that goes out from the source s to 2 vertices x and y. s has out-degree 2. x and y each go out to a and b and nowhere else.
  2. Your tree that is unreachable by BFS is from s to x, from x to a, then from s to y, and y to b.

It works because a and b have the same distance from x and y. If you enqueue x first, then your path via BFS will be from x to both a and b. If you enqueue y first, then your path via BFS will be from y to both a and b. In BFS you cannot mix and match the way the solution does.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文