检查图是否为二分图
check if the graph is bipartite or not
我正在学习图,在我写完一段关于BFS的代码后,我脑子里确实有一个问题,我如何改进我的代码,使其同时检查这个图是否是二分图?使用相同的功能。我想将代码访问的节点涂成这样
int颜色//-1(未访问节点未着色)、1(父节点为红色)、0(子节点为蓝色)
有人能帮我吗:)?
struct node {
int child_count;
int child[max];
int color;
};
void BFS(node*G[], int s){
int w, v;
queue <int> q;
bool visited[max];
for (int i = 0; i < s; i++){
visited[i] = false;
}
for (int i = 0; i < s; i++){
if (!visited[i])
q.push(i);
while (!q.empty())
{
v = q.front();
if (!visited[v])
{
visited[v] = true;
for (int i = 0; i < G[v]->child_count; i++)
{
w = G[v]->child[i];
q.push(w);
}
}
q.pop();
}
}
}
java 中的代码
void bfs(int adjacency_matrix[][], int source){
Queue<Integer> queue = new LinkedList<>();
int numNodes = adjacency_matrix[source].length - 1;
boolean [] visited = new boolean[numNodes + 1];
int element;
HashMap<Integer, Integer> colors = new HashMap<>();
visited[source] = true; // set source vertex/node as visited so we start from it
queue.add(source); // add source to the queue
colors.put(source, 0); // assign color 0
while(!queue.isEmpty()){
element = queue.remove(); // remove head node.. this is parent
for(int i = 0; i < numNodes; i++){
if(adjacency_matrix[element][i] == 1 && Objects.equals(colors.get(i), colors.get(element))){ // same set with different number
System.out.println("not bipartite here "+i+" and "+element); // this will give duplicate.. use set
}
if(adjacency_matrix[element][i] == 1 && visited[i] == false){
queue.add(i); // child
visited[i] = true;
colors.put(i, 1 - colors.get(element)); // this will make it alternate
}
}
}
System.out.println();
}
这是C++中的代码。
#include <iostream>
#include <vector>
#include <list>
#include <queue>
struct Edge{
int source;
int destination;
};
void addEdge(std::vector<Edge> edges, std::vector<std::list<int>> &adjList){
for(int i = 0; i < edges.size(); i++){
int source = edges[i].source;
int destination = edges[i].destination;
adjList[source].push_back(destination);
// And because it's an undirected grapgh
adjList[destination].push_back(source);
}
}
void printGraph(int nodes, std::vector<std::list<int>> adjList){
for(int i = 0; i < nodes; i++){
std::cout << i << ":";
std::list<int>::iterator it;
for(it = adjList[i].begin(); it != adjList[i].end(); it++)
std::cout << *it << "->";
std::cout << "NULLn";
}
}
bool check_bipartite(int nodes, int start,
std::vector<std::list<int>> adjList){
std::vector<bool> visited(nodes, false);
std::queue<int> q;
visited[start] = true;
// -1 means no colour is assigned. 0 means red and 1 means blue
std::vector<int> colours(nodes, -1);
q.push(start);
// Assign it red colour
colours[start] = 0;
while(!q.empty()){
int a = q.front();
q.pop();
std::list<int>::iterator it;
for(it = adjList[a].begin(); it != adjList[a].end(); it++){
if(!visited[*it]){
q.push(*it);
// It won't have any colour assigned yet
// Assign a colour opposite to the parent
colours[*it] = ((colours[a] == 0) ? 1 : 0);
std::cout << "Colour of " << *it << " = " << colours[*it] << std::endl;
// Mark it as visited
visited[*it] = true;
}
// If this vertex has already been visited
// Check if the colour of the parent is different from this vertix
else if(visited[*it]){
if(colours[a] == colours[*it])
return false;
}
}
}
return true;
}
int main(){
// Un-directed graph
// Vertex 0 is an un-connected node
int nodes1 = 10;
std::vector<Edge> edges1 = {
{1, 2},
{2, 3},
{2, 8},
// Add this edge to make it non-bipartite
//{2, 4},
{3, 4},
{4, 6},
{5, 7},
{5, 9},
{8, 9}
};
// Create an adjacency graph
std::vector<std::list<int>> adjList(nodes1);
// Add all nodes
addEdge(edges1, adjList);
// Print the adjacency graph
printGraph(nodes1, adjList);
if(check_bipartite(nodes1, 1, adjList))
std::cout << "It's a bipartite grapgh.n";
else
std::cout << "It's NOT a bipartite graph.n";
return 0;
}
相关文章:
- 擦除是否删除 stl 无序列图元素使用的堆内存
- 检查两个图是否为补码的功能
- 图问题:找出两个节点是否在每个节点的O(1)时间和O(2)存储中共享同一分支
- 是否有可能让 c++ dll 在后台运行 python 程序并让它填充向量图?如果是这样,如何?
- 组合图:C 是否有TensorFlow Import_graph_def当量
- 是否可以在邻接列表中删除提升图中的顶点?
- 使用提升图 [BGL] 检查add_edge之前顶点是否已存在
- 如何使用广度优先搜索确定是否可以在有向图中到达顶点
- 在图算法中,确定是否访问节点的最佳方法是什么?
- 是否可以使用 lower_bound() 对结构的普通数组进行二分搜索
- 检查 Gdiplus::Bitmap::FromFile 是否返回了有效的位图
- 计算二分图中的路径数(长度 n)
- 检查密集图中是否存在汉密尔顿循环
- 如何确定整个图是否是一个强连接的组件
- GDI - 是否可以在WM_PAINT调用之间将位图保存在内存中
- c或c++中是否有头文件来实现图、树等数据结构
- 检查图是否为二分图
- 比较两个(非STL)贴图是否相等
- 测试一个图是否是树
- 从映射中取出Int值并将其迭代到二分图中