社区
数据结构与算法
帖子详情
有关二部图的算法
lemoner2003
2008-11-23 01:53:09
写出采用深度优先搜索图的思想判定任意给定一个无向连通图是否二部图的算法。
...全文
261
3
打赏
收藏
有关二部图的算法
写出采用深度优先搜索图的思想判定任意给定一个无向连通图是否二部图的算法。
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
3 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
A2d3sk3r
2008-11-24
打赏
举报
回复
其实就是黑白染色的问题
任意选定一个节点,染黑,与之相连的节点染白,
用深搜或者广搜都行,如果碰到一个顶点已经被染色且与将该染的色相反,则说明不是二部图。
否则继续,直到深搜完毕(如果广搜,则是广搜的队列为空)
如果图不联通,则继续选择为遍历的节点继续如上遍历。
roadblossom
2008-11-24
打赏
举报
回复
帮你up一下
tailzhou
2008-11-23
打赏
举报
回复
使用一个辅助数组f[n];
f[v]==0表示节点v尚未遍历;
f[v]==1表示节点v属于集合A;
f[v]==-1表示节点v属于集合B;
1)初始化f[n]={0};
2)从图中任取一尚未遍历过的节点v[i],即f[i]==0;
置f[i]=1,从v[i]开始遍历,;
遍历的过程中,对与"当前节点v[j] "有边相连的所有的"节点v[j'] "进行判断;
a) 若f[j']==0,则f[j']=-f[j]
b) 若f[j']==-f[j],则不做任何操作;
c) 若f[j']==f[j],则图不是二分图,完毕;
直到当前的连通分量遍历完毕;
3) 若图还有尚未遍历的连通分量,继续2)直到所有节点都遍历完毕;
《数据结构
算法
实现及解析(高一凡)源代码
《数据结构
算法
实现及解析(高一凡)源代码
Introduction to Algorithms, 3rd edtion
中文名:
算法
导论 原名: Introduction to Algorithms 作者: Thomas H.Cormen, 达特茅斯学院计算机科学系副教授 Charles E.Leiserson, 麻省理工学院计算机科学与电气工程系教授 Ronald L.Rivest, 麻省理工学院计算机科学系Andrew与Erna Viterbi具名教授 Clifford Stein, 哥伦比亚大学工业工程与运筹学副教授 资源格式: PDF(完整书签目录) 出版社: The MIT Press ISBN 978-0-262-03384-8 (hardcover : alk. paper)—ISBN 978-0-262-53305-8 (pbk. : alk. paper) 发行时间: 2009年09月30日 地区: 美国 语言: 英文 1 The Role of Algorithms in Computing 5 1.1 Algorithms 5 1.2 Algorithms as a technology 11 2 Getting Started 16 2.1 Insertion sort 16 2.2 Analyzing algorithms 23 2.3 Designing algorithms 29 3 Growth of Functions 43 3.1 Asymptotic notation 43 3.2 Standard notations and common functions 53 4 Divide-and-Conquer 65 4.1 The maximum-subarray problem 68 4.2 Strassen's algorithm for matrix multiplication 75 4.3 The substitution method for solving recurrences 83 4.4 The recursion-tree method for solving recurrences 88 4.5 The master method for solving recurrences 93 4.6 Proof of the master theorem 97 5 Probabilistic Analysis and Randomized Algorithms 114 5.1 The hiring problem 114 5.2 Indicator random variables 118 5.3 Randomized algorithms 122 5.4 Probabilistic analysis and further uses of indicator random variables 130 II Sorting and Order Statistics Introduction 147 6 Heapsort 151 6.1 Heaps 151 6.2 Maintaining the heap property 154 6.3 Building a heap 156 6.4 The heapsort algorithm 159 6.5 Priority queues 162 7 Quicksort 170 7.1 Description of quicksort 170 7.2 Performance of quicksort 174 7.3 A randomized version of quicksort 179 7.4 Analysis of quicksort 180 8 Sorting in Linear Time 191 8.1 Lower bounds for sorting 191 8.2 Counting sort 194 8.3 Radix sort 197 8.4 Bucket sort 200 9 Medians and Order Statistics 213 9.1 Minimum and maximum 214 9.2 Selection in expected linear time 215 9.3 Selection in worst-case linear time 220 III Data Structures Introduction 229 10 Elementary Data Structures 232 10.1 Stacks and queues 232 10.2 Linked lists 236 10.3 Implementing pointers and objects 241 10.4 Representing rooted trees 246 11 Hash Tables 253 11.1 Direct-address tables 254 11.2 Hash tables 256 11.3 Hash functions 262 11.4 Open addressing 269 11.5 Perfect hashing 277 12 Binary Search Trees 286 12.1 What is a binary search tree? 286 12.2 Querying a binary search tree 289 12.3 Insertion and deletion 294 12.4 Randomly built binary search trees 299 13 Red-Black Trees 308 13.1 Properties of red-black trees 308 13.2 Rotations 312 13.3 Insertion 315 13.4 Deletion 323 14 Augmenting Data Structures 339 14.1 Dynamic order statistics 339 14.2 How to augment a data structure 345 14.3 Interval trees 348 IV Advanced Design and Analysis Techniques Introduction 357 15 Dynamic Programming 359 15.1 Rod cutting 360 15.2 Matrix-chain multiplication 370 15.3 Elements of dynamic programming 378 15.4 Longest common subsequence 390 15.5 Optimal binary search trees 397 16 Greedy Algorithms 414 16.1 An activity-selection problem 415 16.2 Elements of the greedy strategy 423 16.3 Huffman codes 428 16.4 Matroids and greedy methods 437 16.5 A task-scheduling problem as a matroid 443 17 Amortized Analysis 451 17.1 Aggregate analysis 452 17.2 The accounting method 456 17.3 The potential method 459 17.4 Dynamic tables 463 V Advanced Data Structures Introduction 481 18 B-Trees 484 18.1 Definition of B-trees 488 18.2 Basic operations on B-trees 491 18.3 Deleting a key from a B-tree 499 19 Fibonacci Heaps 505 19.1 Structure of Fibonacci heaps 507 19.2 Mergeable-heap operations 510 19.3 Decreasing a key and deleting a node 518 19.4 Bounding the maximum degree 523 20 van Emde Boas Trees 531 20.1 Preliminary approaches 532 20.2 A recursive structure 536 20.3 The van Emde Boas tree 545 21 Data Structures for Disjoint Sets 561 21.1 Disjoint-set operations 561 21.2 Linked-list representation of disjoint sets 564 21.3 Disjoint-set forests 568 21.4 Analysis of union by rank with path compression 573 VI Graph Algorithms Introduction 587 22 Elementary Graph Algorithms 589 22.1 Representations of graphs 589 22.2 Breadth-first search 594 22.3 Depth-first search 603 22.4 Topological sort 612 22.5 Strongly connected components 615 23 Minimum Spanning Trees 624 23.1 Growing a minimum spanning tree 625 23.2 The algorithms of Kruskal and Prim 631 24 Single-Source Shortest Paths 643 24.1 The Bellman-Ford algorithm 651 24.2 Single-source shortest paths in directed acyclic graphs 655 24.3 Dijkstra's algorithm 658 24.4 Difference constraints and shortest paths 664 24.5 Proofs of shortest-paths properties 671 25 All-Pairs Shortest Paths 684 25.1 Shortest paths and matrix multiplication 686 25.2 The Floyd-Warshall algorithm 693 25.3 Johnson's algorithm for sparse graphs 700 26 Maximum Flow 708 26.1 Flow networks 709 26.2 The Ford-Fulkerson method 714 26.3 Maximum bipartite matching 732 26.4 Push-relabel algorithms 736 26.5 The relabel-to-front algorithm 748 VII Selected Topics Introduction 769 27 Multithreaded Algorithms Sample Chapter - Download PDF (317 KB) 772 27.1 The basics of dynamic multithreading 774 27.2 Multithreaded matrix multiplication 792 27.3 Multithreaded merge sort 797 28 Matrix Operations 813 28.1 Solving systems of linear equations 813 28.2 Inverting matrices 827 28.3 Symmetric positive-definite matrices and least-squares approximation 832 29 Linear Programming 843 29.1 Standard and slack forms 850 29.2 Formulating problems as linear programs 859 29.3 The simplex algorithm 864 29.4 Duality 879 29.5 The initial basic feasible solution 886 30 Polynomials and the FFT 898 30.1 Representing polynomials 900 30.2 The DFT and FFT 906 30.3 Efficient FFT implementations 915 31 Number-Theoretic Algorithms 926 31.1 Elementary number-theoretic notions 927 31.2 Greatest common divisor 933 31.3 Modular arithmetic 939 31.4 Solving modular linear equations 946 31.5 The Chinese remainder theorem 950 31.6 Powers of an element 954 31.7 The RSA public-key cryptosystem 958 31.8 Primality testing 965 31.9 Integer factorization 975 32 String Matching 985 32.1 The naive string-matching algorithm 988 32.2 The Rabin-Karp algorithm 990 32.3 String matching with finite automata 995 32.4 The Knuth-Morris-Pratt algorithm 1002 33 Computational Geometry 1014 33.1 Line-segment properties 1015 33.2 Determining whether any pair of segments intersects 1021 33.3 Finding the convex hull 1029 33.4 Finding the closest pair of points 1039 34 NP-Completeness 1048 34.1 Polynomial time 1053 34.2 Polynomial-time verification 1061 34.3 NP-completeness and reducibility 1067 34.4 NP-completeness proofs 1078 34.5 NP-complete problems 1086 35 Approximation Algorithms 1106 35.1 The vertex-cover problem 1108 35.2 The traveling-salesman problem 1111 35.3 The set-covering problem 1117 35.4 Randomization and linear programming 1123 35.5 The subset-sum problem 1128 VIII Appendix: Mathematical Background Introduction 1143 A Summations 1145 A.1 Summation formulas and properties 1145 A.2 Bounding summations 1149 B Sets, Etc. 1158 B.1 Sets 1158 B.2 Relations 1163 B.3 Functions 1166 B.4 Graphs 1168 B.5 Trees 1173 C Counting and Probability 1183 C.1 Counting 1183 C.2 Probability 1189 C.3 Discrete random variables 1196 C.4 The geometric and binomial distributions 1201 C.5 The tails of the binomial distribution 1208 D Matrices 1217 D.1 Matrices and matrix operations 1217 D.2 Basic matrix properties 122
二部图
判定
算法
之染色法
二部图
判定
算法
——染色法染色法初始化条件步骤复杂度 染色法 对任意图G,利用红、蓝两种颜色(或其它任意两种不同颜色)对其顶点进行染色来判断图G是否为
二部图
。 初始化条件 顶点集合V = V(G),已染色顶点集合C = 空集,已扫描顶点集合S = 空集。 步骤 从V\C(未染色顶点集合)中任取一个未染色的顶点,染成红色,并入已染色顶点集合C中; 判断:已扫描顶点集合S==顶点集合V?是,则
算法
结束...
数据结构与
算法
分析:你真的理解网络流
算法
吗——
二部图
匹配(代码详解+万字长文)
匹配问题有多种形式。我们考虑如下这种情况,五个求职者会面试五个职位。每个求职者都已经列出了满足能力要求的职位。现在的任务就是将尽可能多地分配工作给求职者,但是每份工作只能提供给一位求职者。我们会非常惊讶地发现可以使用Ford-Fulkerson
算法
来解
二部图
匹配问题。在计算机科学中,这种方法叫做“问题归约”。我们将
二部图
匹配问题归约为在流网络上的最大流问题,同时会介绍(a)如何将
二部图
匹配问题的输入映射成最大流问题的输入,以及(b)怎么将最大流问题的输出映射成
二部图
匹配问题的输出。如果要能够高效地归约,那么
使用matlab对
二部图
进行匹配(匈牙利
算法
)
使用matlab应用匈牙利
算法
对
二部图
进行匹配。
算法
的代码主体来自知乎用户洪九(李戈)的专栏文章策略
算法
工程师之路-图优化
算法
(一)(二分图&最小费用最大流) 具体
算法
思想文章里和其它地方都有,不在这里赘述。主要通过例子来说明
算法
具体运行过程中所作的操作对匈牙利
算法
进行说明。 首先对于如下图的一个
二部图
左侧有6个点,右侧有7个点,点之间的边都画出来了。 第一步:对x1x_1x1进行匹配,找到它的第一个可匹配项y1y_1y1,发现此时y1y_1y1还未被匹配,因此将x1x_1x1和y1y
数据结构与算法
33,027
社区成员
35,335
社区内容
发帖
与我相关
我的任务
数据结构与算法
数据结构与算法相关内容讨论专区
复制链接
扫一扫
分享
社区描述
数据结构与算法相关内容讨论专区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章