当前位置: 首页 > 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.lryc.cn/news/574891.html

相关文章:

  • 【世纪龙科技】新能源汽车VR虚拟体验展示馆-解锁认知新维度
  • 计算机网络 网络层:数据平面(二)
  • Excel基础:选择和移动
  • java 对接ETH(以太坊) 交易相关资料
  • 量学云讲堂2025朱永海慢牛开启第58期视频课程
  • 物流涂层科技赋能仓储:创冷科技引领高温环境下的仓储物流安全升级
  • 了解笔记本电脑制造:从品牌到代工厂的全产业链
  • GaussDB实例级自动备份策略:构建数据安全的“自动防护网”
  • 设计模式:揭秘Java原型模式——让复杂对象的创建不再复杂
  • 气象数据技术解析:格点模型与区域站观测的原理、差异及数据接口获取
  • 【笔记】Docker 配置阿里云镜像加速(公共地址即开即用,无需手动创建实例)
  • 《解码音频:从基础到未来的听觉探索》
  • window系统上labelImg的安装与使用
  • Jenkins X + AI:重塑云原生时代的持续交付范式
  • React19源码系列之 API (react)
  • Spring Boot 系统开发:打造高效、稳定、可扩展的企业级应用
  • Flutter MobX 响应式原理与实战详解
  • 将Python Tkinter程序转换为手机可运行的Web应用 - 详细教程
  • 高效学习的系统化策略
  • 初识Tomcat
  • Linux系统之Tomcat服务
  • web端rtmp推拉流测试、抽帧识别计数,一键式生成巡检报告
  • 覆盖迁移工具选型、增量同步策略与数据一致性校验
  • Serverless架构下的OSS应用:函数计算FC自动处理图片/视频转码(演示水印添加+缩略图生成流水线)
  • 多模态大模型(从0到1)
  • Netty内存池核心PoolArena源码解析
  • React 表单太卡?也许你用错了控制方式
  • 有AI后,还用学编程吗?
  • C# WinForm跨平台串口通讯实现
  • python中学物理实验模拟:摩檫力