[Algorithms]图的搜索

Posted huisclos

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Algorithms]图的搜索相关的知识,希望对你有一定的参考价值。

未完待续


 

知道图的存储,DFS,BFS

 二分图判定

 1 //二分图判定
 2 //给定一个有n个顶点的图,给图上每个顶点染色,要使相邻顶点颜色不同,是否能用最多2种颜色染色?无重边和自环。
 3 //只用两种颜色,确定一个顶点的颜色后,和它相邻的顶点的颜色随之确定的那个。
 4 //选择任意一个顶点出发,依次确定相邻顶点的颜色,判断是否可被2种颜色染色,深度优先搜索
 5 
 6 //输入
 7 vector<int> G[MAX_V];//
 8 int V; //顶点数
 9 int color[MAX_V];//顶点i的颜色(1 or -1)
10 
11 //把顶点染成1或-1
12 bool dfs(int v,int c){
13     color[v] = c;//把顶点v染成颜色c
14     for (int i = 0;i < G[v].size();i++){
15         //如果相邻的顶点同色,则返回false
16         if(color[G[v][i]] == c)
17             return false;
18         if(color[G[v][i]]== 0 && !dfs(G[v][i],-c))
19             return false;
20     }
21     //如果所有顶点都染过色了,则返回true
22     return true;
23 }
24 void solve(){
25     for(int i = 0;i < V;i++){
26         if(color[i] == 0){
27             //如果顶点i还没被染色,则染成1
28             if(!dfs(i,1)){
29                 printf("No
");
30                 return;
31             }
32         }
33     }
34     printf("Yes
");
35 }

DFS也可求图的拓扑序。

以上是关于[Algorithms]图的搜索的主要内容,如果未能解决你的问题,请参考以下文章

《Fast Suboptimal Algorithms for the Computation of Graph Edit Distance》论文阅读

NTU课程:MAS714 Graph Algorithms

片段类在意图的 startactivity 方法中显示错误

Algorithms - Data Structure - Binary Search Tree - 数据结构之二叉搜索树

Algorithms - Data Structure - Binary Search Tree - 数据结构之二叉搜索树

如何保存底部导航片段的状态 - 具有单个导航图的 Android 导航组件