Ghost689 2023-03-16 16:03 采纳率: 92.6%
浏览 135
已结题

修改算法,检测无向图是否为二部图,可能进行着色

设计并实现了一种改进的宽度优先搜索算法,以检测图是否为二部图。如果一个无向图的节点可以被染成红色和蓝色,即每条边的一端的节点是红色的,另一端是蓝色的,则称为二部图。如果可能的话,该算法也应该进行着色。原始代码在下。请注意,您可能必须使用多个连接的组件为图形着色,因此,您应该确保处理了所有的顶点。将您的程序应用到下面的图表中(每个只有一个连接组件)(在第一个,着色色应该是可能的,在第二个不是)

img

bfsgraph.h,不能修改

#ifndef BFSGRAPH_H_INCLUDED
#define BFSGRAPH_H_INCLUDED

// Header file for breadth-first search
#include <limits.h>

// Maximum number of vertices
// define according to your needs
#define MAXVERTS 1000

// Nodes are numbered as integers from 1

// Colors for color table
#define WHITE 0
#define GREY 1
#define BLACK 2

// Maximum value in the distance table
#define INF INT_MAX

// Defines edge in the adjacency list
typedef struct bfsedgnd {
    int nodenum;
    struct bfsedgnd* next;
} bfsedgenode, *pbfsedgenode;

// Defines the graph
typedef struct bfsg {
    pbfsedgenode adj_list[MAXVERTS+1];
    int pred[MAXVERTS+1];
    int dist[MAXVERTS+1];
    int color[MAXVERTS+1];
    int nVertices;
} bfsgraph;

// Initializes graph for breadth-first search
void init_graph(bfsgraph* g, int vertices);

// Adds new edge (x,y)
void add_edge(bfsgraph* g, int x, int y);

// Actual breadth-first search from node s
void bfsearch(bfsgraph* g, int s);

// Frees allocated memory
void delete_graph(bfsgraph* g, int vertices);

#endif

bfsmain.c,想要着色时可以修改

// DSA Programming task 4.3 - Bicoloring
// Do changes here once you want to try your bi coloring.
// Base file using breadth-first search

#include <stdio.h>
#include "bfsgraph.h"

int main(){

    // Define and initialize graph with 8 nodes
    // Nodes are assumed to be integers 1..8
    bfsgraph g;
    init_graph(&g,8);

    // Add some edges:
    add_edge(&g,1,2);
    add_edge(&g,1,4);
    add_edge(&g,1,5);

    add_edge(&g,2,3);
    add_edge(&g,3,4);
    add_edge(&g,5,6);

    add_edge(&g,6,7);
    add_edge(&g,6,8);

    // Do breadth-first search
    bfsearch(&g,1);

//Uncomment when ready    
/*    // The other graph - add this one to see that coloring is not possible
    // add_edge(&g,2,4);

    if (bicolor(&g,colors) == 0) {
    printf("Coloring is not possible!\n");
    }
    else {
        int i;
        printf("Coloring is possible:\n");
        for (i=1; i <= 8; i++) {
            printf("Node[%d] = ",i);
            colors[i]==RED?printf("RED\n"):printf("BLUE\n");
        }
    }
*/
    // Delete graph
    delete_graph(&g,8);

    return 0;
}

bfsgraph.c,对TODO所在区域(bfsearch函数)的算法修改,具体方式详情见原始代码部分的注释,按照TODO注释部分进行修改

// DSA Programming task 4.3 - Bicoloring
// You work on this files where the TODO is

#include <stdio.h>
#include <stdlib.h>
#include "bfsgraph.h"
// Code file for breadth-first search

// Initializes graph for breadth-first search
void init_graph(bfsgraph* g, int vertices){
    g->nVertices = vertices;
    int i;
    for(i=1; i <= vertices; i++) {
        g->adj_list[i] = 0;
        g->pred[i] = 0;
        g->dist[i] = INF;
        g->color[i] = WHITE;
    }
}

// Adds new edge (x,y)
void add_edge(bfsgraph* g, int x, int y){
    if( x <= g->nVertices && y <= g->nVertices){
        pbfsedgenode pxy = malloc(sizeof(bfsedgenode));
        pbfsedgenode pyx = malloc(sizeof(bfsedgenode));

        pxy->nodenum = y;
        pxy->next = g->adj_list[x];
        g->adj_list[x] = pxy;

        // Remove this if the graph is directed:
        pyx->nodenum = x;
        pyx->next = g->adj_list[y];
        g->adj_list[y] = pyx;
    }
}


/*
Algorithm for breadth first search
BFS(G,s)
1.   for each u in V
2.      color[u] = WHITE
3.      d[u] = INF
4.      p[u] = NIL
5.   d[s] = 0
6.   color[s] = GRAY
7.   Q = EMPTY
8.   ENQUEUE(Q,s)
9.   while Q != EMPTY
10.    u = DEQUEUE(Q)
11.       for each v in Adj[u]
12.        if color[v] == WHITE
13.           color[v] = GRAY
14.           d[v] = d[u]+1
15.           p[v] = u
16.              ENQUEUE(Q,v)
17.    color[u] = BLACK
18.  return
*/

// Actual breadth-first search from node s
// TODO
// You can modify this algorithm or copy it and modify the copy.
// Change the funciton to be int instead of void and return 1 if
// bicoloring is possible, and 0 if it is not possible.
// In addition to predecessor table, distance table, and color table, you need an additional color tabel
void bfsearch(bfsgraph* g, int s){
    int queue[MAXVERTS];
    int head = 0;
    int tail = 0;
    int i=0;

    for(i=1; i <= g->nVertices; i++) {
        g->pred[i] = 0;
        g->dist[i] = INF;
        g->color[i] = WHITE;
    }

    g->dist[s] = 0;
    g->color[s] = GREY;
    queue[tail] = s;
    tail++;

    while( head != tail) {
        int u = queue[head];
        head++;

        pbfsedgenode pedge = g->adj_list[u];
        while(pedge != 0){
            int v = pedge->nodenum;
            if(g->color[v] == WHITE) {
                g->color[v] = GREY;
                g->dist[v] = g->dist[u] +1;
                g->pred[v] = u;
                queue[tail] = v;
                tail++;
            }
            pedge = pedge->next;
        }

        // DEBUG INFO:
        printf("%d processed: d[%d] = %d\n",u,u,g->dist[u]);

        g->color[u] = BLACK;
    }
}

// Free allocated memory
void delete_graph(bfsgraph* g, int vertices){
    int i;
    for(i=1; i <= vertices; i++) {
        pbfsedgenode pedge = g->adj_list[i];
        pbfsedgenode pnext = 0;

        while(pedge != 0) {
            pnext = pedge->next;
            free(pedge);
            pedge = pnext;
        }
    }
}

  • 写回答

5条回答 默认 最新

  • OKX安生 2023-03-20 23:13
    关注

    该回答引用于gpt与自己的思路:

    以下是修改后的代码,功能为检测无向图是否为二部图并进行着色:

    • bfsgraph.h:
    #ifndef BFSGRAPH_H_INCLUDED
    #define BFSGRAPH_H_INCLUDED
     
    // Header file for breadth-first search
    #include <limits.h>
     
    // Maximum number of vertices
    // define according to your needs
    #define MAXVERTS 1000
     
    // Nodes are numbered as integers from 1
     
    // Colors for color table
    #define WHITE 0
    #define RED 1
    #define BLUE 2
     
    // Maximum value in the distance table
    #define INF INT_MAX
     
    // Defines edge in the adjacency list
    typedef struct bfsedgnd {
        int nodenum;
        struct bfsedgnd* next;
    } bfsedgenode, *pbfsedgenode;
     
    // Defines the graph
    typedef struct bfsg {
        pbfsedgenode adj_list[MAXVERTS+1];
        int pred[MAXVERTS+1];
        int dist[MAXVERTS+1];
        int color[MAXVERTS+1];
        int nVertices;
    } bfsgraph;
     
    // Initializes graph for breadth-first search
    void init_graph(bfsgraph* g, int vertices);
     
    // Adds new edge (x,y)
    void add_edge(bfsgraph* g, int x, int y);
     
    // Actual breadth-first search from node s, return 1 if bicoloring is possible, 0 otherwise
    int bfsearch(bfsgraph* g, int s);
     
    // Frees allocated memory
    void delete_graph(bfsgraph* g, int vertices);
     
    #endif
    
    • bfsgraph.c:
    // DSA Programming task 4.3 - Bicoloring
    // You work on this files where the TODO is
    #include <stdio.h>
    #include <stdlib.h>
    #include "bfsgraph.h"
    
    /* Algorithm for breadth first search
    BFS(G,s)
    1.   for each u in V
    2.      color[u] = WHITE
    3.      d[u] = INF
    4.      p[u] = NIL
    5.   d[s] = 0
    6.   color[s] = RED
    7.   Q = EMPTY
    8.   ENQUEUE(Q,s)
    9.   while Q != EMPTY
    10.    u = DEQUEUE(Q)
    11.       for each v in Adj[u]
    12.        if color[v] == WHITE
    13.           color[v] = opposite(color[u])
    14.           d[v] = d[u]+1
    15.           p[v] = u
    16.              ENQUEUE(Q,v)
    17.        else if color[v] == color[u]
    18.            return 0 // Not a bipartite graph
    19.    color[u] = BLACK
    20.  return 1 // Bipartite graph
    */
    
    // Actual breadth-first search from node s, return 1 if bicoloring is possible, 0 otherwise
    int bfsearch(bfsgraph* g, int s){
        int queue[MAXVERTS];
        int head = 0;
        int tail = 0;
        int i=0;
        for(i=1; i <= g->nVertices; i++) {
            g->pred[i] = 0;
            g->dist[i] = INF;
            g->color[i] = WHITE;
        }
        g->dist[s] = 0;
        g->color[s] = RED; // Start with red
        queue[tail] = s;
        tail++;
        while( head != tail) {
            int u = queue[head];
            head++;
            pbfsedgenode pedge = g->adj_list[u];
            while(pedge != 0){
                int v = pedge->nodenum;
                if(g->color[v] == WHITE) {
                    g->color[v] = g->color[u] == RED ? BLUE : RED; // Set opposite color
                    g->dist[v] = g->dist[u] +1;
                    g->pred[v] = u;
                    queue[tail] = v;
                    tail++;
                } else if (g->color[v] == g->color[u]) {
                    return 0; // Not a bipartite graph
                }
                pedge = pedge->next;
            }
            g->color[u] = BLACK;
        }
        return 1; // Bipartite graph
    }
    
    • bfsmain.c:
    // DSA Programming task 4.3 - Bicoloring
    // Do changes here once you want to try your bi coloring.
    // Base file using breadth-first search
    #include <stdio.h>
    #include "bfsgraph.h"
    int main(){
        // Define and initialize graph with 8 nodes
        // Nodes are assumed to
    
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 3月24日
  • 修改了问题 3月16日
  • 创建了问题 3月16日

悬赏问题

  • ¥15 使用MATLAB进行余弦相似度计算加速
  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
  • ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术
  • ¥15 DockerSwarm跨节点无法访问问题
  • ¥15 使用dify通过OpenAI 的API keys添加OpenAI模型时报了“Connection Error”错误