请查看我的算法以检测二部图

问题描述

二部图是一个图,其顶点可以分为两个独立的集合U和V,这样每个边(u,v)要么连接从U到V的顶点,要么连接从V到U的顶点。 ,对于每个边(u,v),u属于U,v属于V,或者u属于V,v属于U。我们也可以说没有边连接相同集合的顶点。

有关更多信息和bfs方法,请参见:https://www.geeksforgeeks.org/bipartite-graph/

================================================ =========================

我知道有一种用于检测二分图的著名的BFS方法,但是我想知道下面的我是否也可以工作。

Remove a random node (let's call it A) from graph.

Using recursion,divide other nodes into 2 independent sets.

Add back A to one of the sets. If that is not possible,return "Not Bipartite"

Base case: Empty graph is bipartite since it is divided into two empty sets. 

解决方法

如果您有二部图

*---*---A---*---*

然后随机选择A,您会得到

*---*       *---*

作为一个子问题,并且没有足够的信息来分组匿名端点,以确保可以重新添加A