[Graph Theory]
一刀不二
这个作者很懒,什么都没留下…
展开
-
网络流最大流 Edmonds-Karp算法
#include #include #include #include using namespace std;const int MAX_SIZE = 100;int capacity[MAX_SIZE][MAX_SIZE];int parent[MAX_SIZE];bool visit[MAX_SIZE];int vertex_num;int edge_原创 2013-12-17 19:29:10 · 1245 阅读 · 0 评论 -
最小生成树 -- Prim算法
#include #include #include using namespace std;#define INF INT_MAX#define SIZE 6int matrix[SIZE][SIZE]={ {INF,6,1,5,INF,INF}, {6,INF,5,INF,3,INF}, {1,5,INF,5,6,4},原创 2013-10-14 14:53:26 · 711 阅读 · 0 评论 -
POJ_1511_Invitation Cards(SPFA)
//懒得解释#include #include #include using namespace std;#define Max 1001#define initVal -1#define maxDist 101int city1[Max][Max];int city2[Max][Max];bool visit1[Max];bool visit2[Max原创 2013-10-14 14:52:21 · 670 阅读 · 0 评论 -
二分图的判定 -- 着色
哇咔咔,很有童趣。#include "stdafx.h"#include #include #include using namespace std;#define MAX_SIZE 100bool graph[MAX_SIZE][MAX_SIZE];int node_color[MAX_SIZE];int vertex;int edge;bool原创 2013-12-14 22:26:09 · 972 阅读 · 0 评论 -
昂贵的聘礼(SPFA最短路)
//懒得解释#include #include #include using namespace std;#define Max 110#define levelInit -1#define visitInit false#define peopleInit -1#define priceInit 9999999#define start 1in原创 2013-10-14 14:48:21 · 897 阅读 · 0 评论 -
POJ 1753 棋盘反转(位运算+BFS)
#include #include #include using namespace std;#define ROW 1#define COL 2#define MAX 6#define ENDSTATE ((1<<16)-1)int arr[MAX][MAX];const int dir[5][3]={ {0,0,0},{0,-1,0},{0,1,0},{0,原创 2013-09-30 03:16:52 · 987 阅读 · 0 评论 -
最大流算法 -- Dinic算法
#include #include #include using namespace std;const int INF = 1 << 30;const int MAX_SIZE = 100;int capacityGraph[MAX_SIZE][MAX_SIZE];int level[MAX_SIZE];int src, des;bool BFS(){原创 2013-12-21 22:12:54 · 1228 阅读 · 0 评论 -
POJ_2112_Optimal Milking( 二分查找 + Floyd + 最大流EK )
#include #include #include #include using namespace std;const int INF = 40000;const int MAX_SIZE = 300;int graphCapacity[MAX_SIZE][MAX_SIZE];int minDisGraph[MAX_SIZE][MAX_SIZE];bool原创 2013-12-20 03:49:27 · 1238 阅读 · 0 评论 -
POJ_1698_Alice's Chance
#include #include #include #include using namespace std;const int MAX_SIZE = 400;int capacity[MAX_SIZE][MAX_SIZE];int parent[MAX_SIZE];bool visit[MAX_SIZE];bool Edmonds_Karp( int s原创 2013-12-18 22:27:08 · 803 阅读 · 0 评论 -
POJ 1273 Drainage Ditches 网络流最大流
#include #include #include #include using namespace std;const int MAX_SIZE = 210;int capacity[MAX_SIZE][MAX_SIZE];int parent[MAX_SIZE];bool visit[MAX_SIZE];int vertex_num;int edge_原创 2013-12-17 21:10:40 · 864 阅读 · 0 评论 -
Ford Fulkerson 算法
#include #include #include using namespace std;#define MAX_V 100#define INF 1 << 30struct Edge{ int to; int cap; int rec; Edge( int to, int cap, int rec ){ this->to原创 2014-10-28 15:56:42 · 889 阅读 · 0 评论 -
欧拉回路算法
#include #include #include #include using namespace std;#define MAPSIZE 100#define STARTNODE 1bool g_bVisit[MAPSIZE];int g_nGraph[MAPSIZE][MAPSIZE];int g_nVNum;int g_nENum;int原创 2013-10-24 23:25:30 · 885 阅读 · 0 评论 -
强连通分量 -- Kosaraju算法
#include #include using namespace std;const int MAX_SIZE = 100;int G[MAX_SIZE][MAX_SIZE];int GT[MAX_SIZE][MAX_SIZE];bool visit[MAX_SIZE];int parent[MAX_SIZE];vectorvec;int V = 10;原创 2013-12-01 15:40:02 · 991 阅读 · 0 评论 -
图力导引布局算法
图布局力导引算法的研究与实现:http://www.docin.com/p-834916360.html(从 Eades 弹力模型,到 FR 算法结合模拟退火,能量模型的 KK 算法都有阐述);Efficient and High Quality Forced-Directed Graph Drawing:http://yifanhu.net/PUB/graph_draw_small.pd原创 2015-06-11 20:06:53 · 7159 阅读 · 0 评论 -
POJ 1149 PIGS ( 网络流 )
主要是建图有些小烦,若几个节点流量的来源或者去向完全相同,且流量为 INF,将它们合并成一个节点。若从两点间有且仅有一条容量为 INF 的边,将两点合并成一个节点。#include #include #include using namespace std;const int INF = 0x7fffffff;const int MAXN = 110;int capac原创 2015-07-02 01:08:20 · 1065 阅读 · 0 评论 -
最大导出匹配数
若 G= ( X, Y ) 是连通二分图,则 G 的导出匹配数为 iμ( G ) = Max{ | S | | S ⊆ X 且对于任意 T ⊆ S 有 Neighbor_In_G( S ) ≠ Neighbor_In_G( T ) }设具备性质 P 的集合 T,对于任意 T ⊆ S 有 Neighbor_In_G( S ) ≠ Neighbor_In_G( T ) 。设 k =原创 2014-12-20 12:46:29 · 1029 阅读 · 0 评论 -
POJ 2449 Remmarguts' Date ( Dijkstra + A* 求解第K短路 )
#include #include #include #include using namespace std;#define E 100005#define V 1005#define INF 1 << 30int heads[V], r_heads[V];int dists[V];bool visits[V];int nEdgeNum, nNodeNum, nEdg原创 2014-10-20 23:31:54 · 1351 阅读 · 0 评论 -
POJ 3625 Building Road(Prim)
#include #include #include using namespace std;#define MAXV 1005#define INF 1e12const int start = 1;bool visit[MAXV] = { false };double dist[MAXV];int preNode[MAXV];int vertex_size;int road原创 2014-01-21 23:02:57 · 955 阅读 · 0 评论 -
图的一因子
图特定理:对于任意点集S ∈V( G ),满足o( G - S ) ≤ | S |,G则存在一因子。( o代表分量中点数为奇数的分量个数 )证明:必要性:很显然,若是G存在一因子,G - S中的奇分量的个数必然要小于S的个数,不然G - S中的某个奇分量中的点得不到匹配。充分性:反证,假设G满足任意S,o( G - S ) ≤ | S |,但是G不存在一因子。但原创 2014-05-06 18:31:17 · 2411 阅读 · 2 评论 -
Edmonds 开花算法
Edmonds 开花算法input:图G,匹配M,未饱和点uidea: 查找从 u 开始的 M-交错路径,对每个顶点记录父亲节点。发现花朵,则收缩。维护 S 和 T,S 表示沿着已经饱和的边抵达的顶点构成的集合,收缩过程中的新顶点也属于 S,T表示当前图中沿着未饱和的边抵达的顶点构成的集合 ,一旦遇到另一个未饱和的顶点,则得到增广路。init原创 2014-12-21 00:23:39 · 2956 阅读 · 0 评论 -
链式前向星储存
#include #include using namespace std;#define E 10000#define V 100struct Edge{ int to_node; int edge_val; int next_edge; Edge(){} Edge( int to, int val, int next ){原创 2014-10-20 10:48:11 · 806 阅读 · 0 评论 -
POJ 1416 Shredding Company
#include #include #include using namespace std;int nLenEnd;int best;int maxVal;char szArr[7];bool rejected;bool error;int path[7];int tempVal[7];int cut;void DFS(int head原创 2013-10-08 22:05:36 · 560 阅读 · 0 评论 -
压入与重标记算法(预流推进算法)
最大流算法之压入与重标记算法(预流推进算法)今天是平安夜,明天是圣诞节,明天也是我的生日,到目前都是直接贴代码,懒动手,今天好好写一篇博客。 — —Pandora_Scheme1.算法发现者: Goldberg && Tarjan( 87 ); 不了解原创 2013-12-24 18:07:00 · 5152 阅读 · 3 评论 -
Havel--Hakimi定理判断可图化 python
list1 = [ 4, 7, 7, 3, 3, 3, 2, 1 ]list2 = [ 5, 4, 3, 3, 2, 2, 2, 1, 1, 1 ]def havel_hakimi_algo( degree_list ): degree_list.sort( reverse = True ) print degree_list for degr原创 2014-05-02 23:55:30 · 2268 阅读 · 0 评论 -
POJ 2186 Popular Cows -- tarjan 缩点
链接:POJ 2186 Popular Cows题意:每一头牛都希望在牛群里面备受瞩目,在一个牛群中有N头牛(1输入:第一行两个整数 N 和 M第2 到 M + 1 行,两个分开的数 A,B,意味着 A认为 B 更受欢迎。输出:被所有牛认为受欢迎的牛的个数比如输入:3 31 22 1原创 2014-06-06 16:51:16 · 950 阅读 · 0 评论 -
DAG图的拓扑排序 python
在DAG中DFS中顶点的出栈顺序即逆拓扑序。def topological_sort( graph ): is_visit = dict( ( node, False ) for node in graph ) li = [] def dfs( graph, start_node ): for end_node in原创 2014-05-21 15:30:20 · 5584 阅读 · 0 评论 -
矩阵树定理速证
凯莱公式:spanning_trees_num( G ) = spanning_trees_num( G - e ) + spanning_trees_num( G · e )矩阵树定理:G 对应的拉普拉斯矩阵(度矩阵 - 邻接矩阵)L( G ) 删除任意一行一列得到的行列式的值det( L*( G ) )即生成树的个数,即spanning_trees_num( G原创 2014-06-22 17:04:19 · 2714 阅读 · 0 评论 -
bellman_ford算法 python实现
def bellman_ford( graph, source ): distance = {} parent = {} for node in graph: distance[node] = float( 'Inf' ) parent[node] = None distance[source] =原创 2014-04-01 19:48:13 · 4129 阅读 · 3 评论 -
有向图的强连通算法 -- tarjan算法
在有向图 G 中,若两个顶点相互可达,则称两个顶点强连通(strongly connected)。如果有向图G的每两个顶点都强连通,称G是一个强连通图。非强连通图有向图的极大强连通子图,称为强连通分量(strongly connected components)。比如下面第一幅图( a, b, e ), ( d, c, h ), ( f, g ) 分别为三个 SCC。原创 2014-06-05 18:51:10 · 2110 阅读 · 0 评论 -
蚁群算法求解旅行商问题 python
这组数据,刚开始只运行到2879m,于450m差的比较远,后来发现是路径距离算错了,再后来变成了1789m,发现是初始化点没选择,改了下,最后运行结果是440m。python有点慢,需要运行一段时间。import randomimport copyimport timeimport sys( ALPHA, BETA, RHO, Q ) = ( 1.0, 2.0, 0.原创 2014-04-12 15:41:01 · 10425 阅读 · 5 评论 -
二分图最大匹配 -- 匈牙利算法
Algorithm.( Augmenting Path Algorithm )Input: An X-Y bigraph G, a matching M in G, and the set U of M-unsaturated vertices in X. Idea: Explore M-alternating paths form原创 2014-07-09 23:51:03 · 1506 阅读 · 0 评论 -
状态压缩动态规划 -- 旅行商问题
旅行商问题:N个点(N并且路径上边的权值和最小(或者最大),或者求一条具有这样性质的回路。状态压缩:将二进制表示十进制数N的点集,比如:10 = 00001010 代表第1和3个点已经路过18 = 00010010 代表第1和4个点已经路过一个整数就是一个点集,dp_arr[i][j]代表经过点集i中,当前终点为j,且路径最短的值,若该状态不存在就是原创 2014-06-17 03:47:56 · 2343 阅读 · 0 评论 -
Python 图论工具
networkx:一个用Python语言开发的图论与复杂网络建模工具,内置了常用的图与复杂网络分析算法,可以方便的进行复杂网络数据分析、仿真建模等工作。依赖工具:numpy pyparsing datautil matplotlib networkx 采用随机图做个实验:from random im原创 2014-06-17 20:20:47 · 6836 阅读 · 0 评论 -
传递闭包
传递闭包:import numpyA0 = numpy.array( [ [False, True, False, False], [False, False, False, True], [False, False, False, False], [True, False, True,原创 2014-06-17 18:48:43 · 2094 阅读 · 0 评论 -
diameter - degree problem
现在要构建一个网络模型,网络中的每个节点最多和 d 个节点相连接,且信息的传播从任意一个节点到另外任意一个节点的“最短路径”(路径按照单位路径算)都不能超过 k,问网络中最多安排多少个节点。这是《图论导引》里面看到的 diameter - degree 问题。转化为图模型就是,一个无向图 G 中,节点最大度为 d,直径为 k,问 G 中的 n 上界。书上要证明的是:n ≤原创 2014-06-14 01:56:44 · 1497 阅读 · 0 评论 -
超级立方体小记
在多指令流多数据流MIMD里面有用到基于超立方体互联的网络,用《图论导引》里面简单的描述,就是处理器能通信,当且仅当他们的邻接(k元祖代表了处理器的地址)一个 k 维立方体(或者超立方体Qk)是一种简单图,每个顶点{0,1}标记的k元祖来表示。相邻的顶点之间的 k 元祖只有一个位置上数字不同,Qk 的生成立方体 Qj 和 Qj 本身同构。这是Q3的表示:原创 2014-06-15 02:14:43 · 5283 阅读 · 0 评论 -
完全图中的圈数
今天问了 J,Z,D完全图中圈数的问题。如下是K5:其中有很多C4,比如:也有很多C5,比如:先问里面有多少环,C3 + C4 + C5的个数?Kn里面有多少环数?===========================================喝杯咖啡===========================原创 2014-06-10 21:41:12 · 3470 阅读 · 0 评论 -
简单图模板 Graph
仿写 networkx 的功能# -*- coding: cp936 -*-''' 简单图 Graph:要求: 关于节点: 功能1.add_node: 通过 add_node 一次性加一个节点 字符串,数字,任何可以被哈希的 python 对象都可以当做节点原创 2014-06-30 16:50:28 · 2760 阅读 · 0 评论