当前位置: 首页 > news >正文

数据结构篇-二分图

  • 定义
  • 示例
  • 应用

定义

  • 一个图是二分图;
  • 一个图具有二着色性;
  • 一个图不包含任何奇数长度的环;

实现

/*** Program 18.6 Two-colorability* ---------------------------------------------------------------------------------------------------------------------* This DFS function assigns the values 0 or 1 to the vertex-indexed array `G->color` and* indicates in the return value whether or not it was able to do the assignment such that,* for each graph edge `v-w`, `G->color[v]` and `G->color[w]` are different.* ---------------------------------------------------------------------------------------------------------------------* Two-colorability, bipartiteness, odd cycle* - Is there a way to assign one of two colors to each vertex of a graph such that*   no edge connects two vertices of the same color?* - Is a given graph bipartite (see Section 17.1)?* - Does a given graph have a cycle of odd length?*/
int dfsRcolor(Graph G, int v, int c) {int t;G->color[v] = 1-c;for (t = 0; t < G->V; t++) {if (G->adj[v][t] != 0) {if (G->color[t] == -1) {//tree link: t是v的孩子节点if (!dfsRcolor(G, t, 1-c)) {return 0;}}else if (G->color[t] == c) {//parent link: t是v的父节点}else if (G->color[t] != c) {//back edge: t是v的祖先,且t不是v的父节点return 0;}}}return 1;
}int GRAPHtwocolor(Graph G) {int v;G->color = malloc(G->V * sizeof(int));for (v = 0; v < G->V; v++) {G->color[v] = -1;}for (v = 0; v < G->V; v++) {if (G->color[v] == -1) {if (!dfsRcolor(G, v, 0)) {return 0;}}}return 1;
}

示例

示例1 判定下图是否为二分图?请画出对应的DFS递归树增强版。
Fig1805
TestTwoColorabilityForAdjacenyMatrix

示例2 判定下图是否为二分图?请画出对应的DFS递归树增强版。
Fig17.5
TestTwoColorabilityForAdjacenyMatrix

http://www.lqws.cn/news/518437.html

相关文章:

  • Class00.2线性代数
  • 【评估指标】IoU 交并比
  • Day.42
  • 高等数学》(同济大学·第7版)第七章 微分方程 第五节可降阶的高阶微分方程
  • 【网站内容安全检测】之1:获取网站所有链接sitemap数据
  • Web3D技术协议的AI革命:生成式模型如何改写交互标准?
  • 操作系统之内存管理(王道)
  • LeeCode349. 两个数的交集
  • 基于大模型的甲状腺结节预测及综合诊疗技术方案大纲
  • 防火墙快速管理软件,66K超小巧
  • Java 日志框架选型:SLF4J + Logback vs. Log4j2 的深度解析
  • iClone 中创建的面部动画导入 Daz 3D
  • Spring AOP 中有多个切面时执行顺序是怎样的?
  • Android14音频子系统-Audio HAL分析
  • 南北差异之——跨端理解能力
  • sql格式化自动识别SQL语法结构
  • gsql: command not found
  • OpenLayers 上传Shapefile文件
  • 基于 Python 的批量文件重命名软件设计与实现
  • 智哪儿专访 | Matter中国提速:开放标准如何破局智能家居“生态孤岛”?
  • 舵机在智能家居里的应用
  • 第k个数字
  • 归并排序算法
  • 企业内部安全组网技术解析:安全通道选型、零信任架构与数据合规加密防护
  • 计算机网络-----详解HTTP协议
  • 基于springboot+vue的智慧农业专家远程指导系统
  • 苹果签名应用掉签频繁原因排查,以及如何避免
  • Mysql使用窗口函数查询
  • 左神算法之有序二维矩阵中的目标值查找
  • vscode管理go多个版本