修改后的BFS/DFS在友谊网络上的应用

发布于 2025-01-25 18:40:19 字数 755 浏览 2 评论 0原文

关于图形和友谊网络,我有一个非常有趣的问题。如下:

老师想确保他的学生不要通过确保没有一对彼此认识的人获得相同的作业来作弊。他认为他只能制作两种不同版本的作业来做到这一点。设计算法(伪代码),以测试给定的学生及其连接图是否可能。该算法应基于DFS或BFS。

我使用BFS解决了问题,并通过与已经访问的节点进行比较,

Modified BFS(v)
  
  Mark v as visited
  Enqueue v
  
  While queue is not empty
    Dequeue v
    Assign either homework to v
    
    For all unvisited neighbors a of v
      Give them the opposite homework
      Mark them as visited
      Add them to queue
 
    For all visited neighbors b of v
      Check if their homework is the same as v, terminate if it is
    

该算法似乎适用于小型手工图形,我可以在其中写出每个步骤。它注定要适合所有这些图形吗?如果算法是正确的,那么伪代码也可以接受吗?我对排队/脱水的顺序以及发生的顺序有些不确定,以及我是否可以在没有临时变量的情况下对最后一行进行比较,以跟踪“当前”节点的作业。

我感谢任何帮助/输入,如果您对另一种算法有想法(例如使用DFS),我也很感谢

I have a very intresting problem regarding graphs and friendship networks. It is as follows:

A teacher wants to make sure that his students aren't cheating by making sure no pair of people who know each other get the same homework. He believes he can do this by making only two different versions of the homework. Design an algorithm (pseudo code) to test whether this is possible or not for a given graph of students and their connections. The algorithm should be based on DFS or BFS.

I approached the problem using BFS and modifying it by comparing to already visited nodes

Modified BFS(v)
  
  Mark v as visited
  Enqueue v
  
  While queue is not empty
    Dequeue v
    Assign either homework to v
    
    For all unvisited neighbors a of v
      Give them the opposite homework
      Mark them as visited
      Add them to queue
 
    For all visited neighbors b of v
      Check if their homework is the same as v, terminate if it is
    

This algorith seems to work for small handmade graphs where i can write out every step. Is it bound to work for all such graphs? If the algorithm is correct, is the pseudo code also acceptable? I am a little uncertain regarding the order of queueing/dequeuing and when it happens as well as whether or not i can make the comparison on the last row without maybe a temporary variable to keep track of the homework for the "current" node.

I wold appreciate any help/input and if you have ideas for another algorithm (for example using DFS), i would appreciate that as well

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

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

发布评论

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

评论(1

梦归所梦 2025-02-01 18:40:19

您犯了一个错误 - 将任何作业分配给V在循环之前进行。

那么,队列中的每个顶点都将已经分配了家庭作业,并且从未选择要分配给邻居的作业。这显然适用于所有连接的图形。这是一个很好的算法。

您还需要处理未连接的图,并在未访问v时,在所有运行bfs(v)的顶点上有一个循环。

You made a mistake -- the assign either homework to v goes before the loop.

Then every vertex in the queue will already have homework assigned, and there is never any choice as to which homework to assign to neighbors. This obviously works for all connected graphs. It's a good algorithm.

You need to handle unconnected graphs as well, with a loop over all vertices that runs BFS(v) when v is unvisited.

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