C++ Edmonds-Karp与Ford-Fulkerson算法求解最大流问题

C/C++ 专栏收录该内容
¥99.90 ¥299.90
170篇文章

C++ Edmonds-Karp与Ford-Fulkerson算法求解最大流问题

最大流问题是图论中的经典问题,旨在寻找从源节点到汇节点的最大流量。Edmonds-Karp和Ford-Fulkerson算法都是用于求解最大流问题的经典算法。本文将会介绍这两种算法的实现,并附上完整的C++代码。

Edmonds-Karp算法

该算法的核心在于使用广度优先搜索(BFS)来查找增广路径。增广路径指的是从源节点到汇节点的一条路径,其中每条边的残量大于0且最小,称为瓶颈容量。对于找到的增广路径,可以通过增加瓶颈容量来增加总的流量。

具体实现过程如下:

  1. 首先给每个边赋予一个初始的残量值,初始化为图的源节点到其它节点的流量上限;

  2. 在每次查找增广路径时,使用BFS来搜索一条到汇节点的路径,并计算出路径上残量最小的边的残量值;

  3. 在找到增广路径之后,更新所有路径上的边的残量值,并累加最大流量;

  4. 重复执行步骤2和3,直到无法找到增广路径。

以下是该算法的C++实现代码:

const int MAX = 100;
int n;
int capacity[MAX][MAX], flow[MAX][MAX];
int parent[MAX];

int bfs(int s, int t) {
    memset(parent, -1, sizeof(parent));
    parent[s] = -2;
    queue<pair<int,
Edmonds-Karp算法和Ford-Fulkerson算法求解最大流问题 最大流问题是在一个网络流图中寻找从源节点到汇节点的最大流量的路径。Edmonds-Karp算法和Ford-Fulkerson算法是两种常用的解决最大流问题的算法。本文将详细介绍这两种算法的原理,并给出相应的C++源代码实现。通过上述代码,我们可以使用C++实现Edmonds-Karp算法和Ford-Fulkerson算法来求解最大流问题。上述代码实现了Ford-Fulkerson算法的具体过程。上述代码实现了Edmonds-Karp算法的具体过程。数组表示流图的邻接矩阵,数组表示流图的邻接矩阵, 阅读详情

相关推荐

PDF批量加水印 与 去除水印实践

我们可以给PDF加图片水印或文字水印,要去除图片水印,一般只需要删除最后一个图片对象即可。要去除文字水印,需要保证主体内容和文字水印在/Contents中位于不同的对象内,这样我们只需要删除文字水印对应的对象即可删除水印。而对于主体内容和文字水印已经混合在一个对象时,本文的提供的方法则无能为力,需要进一步深入分析PDF细节。

小小明-代码实体的专栏 2774

工程伦理ppt

工程伦理学 第一章 工程与伦理 1.1 如何理解工程 一、技术与工程的区别 二、技术与工程的联系 三、工程的定义 五、工程具有不确定性和探索性 六、理解工程活动的7个维度 1.2 如何理解伦理 一、道德与伦理 二、不同的伦理立场 三、伦理困境与伦理选择 1.3 工程实践中的伦理问题 一、工程活动中的行动者网络(具有动态性和网络性) 二、主要的工程伦理问题 三、工程伦理问题的特点 1.4 如何处理工程实践中的伦理问题 一、工程实践中伦理问题的辨识 二、处理工程伦理问题的基本原则 三、应对工程伦理问题的基本思路

weixin_51656605的博客 7万+

从OSGB到3DTiles:倾斜摄影数据高效转换实战指南

本文提供了将倾斜摄影OSGB数据高效转换为Web友好的3DTiles格式的完整实战指南。详细介绍了使用开源工具进行转换的步骤,包括环境搭建、核心命令、参数调优及在Cesium中加载与问题排查,旨在帮助GIS开发者和三维应用工程师打通数据到Web展示的最后一公里,实现大规模三维模型的流畅网络发布。

u0v1w2x3的博客 578

K-Means聚类算法 — 算法原理、质心计算、距离度量、聚类效果评价及优缺点

分类知识   分类是根据样本某些属性或某类特征(可以融合多类特征),把样本类型归为已确定的某一类别中。机器学习中常见的分类算法有:SVM(支持向量机)、KNN(最邻近法)、Decision Tree(决策树分类法)、Naive Bayes(朴素贝叶斯分类)、Neural Networks(神经网络法)等。   但当对海量数据进行分类时,为了降低数据满足分类算法要求所需要的预处理代价,往往需要选...

zhf的博客 1万+

西门子AI面试问答(STAR法则回答实例)

通过建立良好的沟通渠道、倡导透明和积极的沟通,并建立信任和共享目标的文化,我成功地改善了团队合作的氛围和效果。对于一些存在分歧和冲突的情况,我采用了积极的解决问题的态度,促进双方的沟通和理解,寻找共同的利益点和解决方案。我的成果得到了评委和观众的认可,并为我的学术发展和进一步的科学研究奠定了基础。首先,我主动与团队成员进行一对一的沟通,倾听他们的观点和意见,了解他们在合作中遇到的问题和需求。我还参加了学校组织的科学俱乐部和研究小组,与其他热衷科学的同学一起进行讨论和实验,加深我的理解和实践技能。

kashine的博客 7263

STM32F030F4P6实战:LL库+DMA驱动WS2812全流程(附避坑指南)

本文详细介绍了如何使用STM32F030F4P6的LL库和DMA驱动WS2812 LED灯带的全流程,包括硬件选型、CubeMX配置、LL库与DMA关键实现、数据格式转换及调试技巧。特别提供了避坑指南,帮助开发者避免常见错误,提升开发效率。

weixin_29194693的博客 493

第33次CCF-CSP认证考试 第一题 词频统计 满分题解

本人小白一枚,抱着学习的态度想到网上找一些csp的参考答案,但由于实力有限,大佬的代码大都看不太懂,于是萌生了想用一些简单,基础的语句完成csp前两题的想法并公开,以便与像我这样实力不强的同学交流学习,如有更好的建议,还请斧正,感谢!

qq_75242106的博客 1875

[QMT和ptrade量化交易小白入门]-125、菜场大妈加上四大现金行业指数风控,最大回撤从22%减少了一半

群里面有人建议,在菜场大妈策略的基础上,加入申万的行业指数(银行,钢铁,有色,煤炭)测算其宽度,当这四大行业其中有一个高就会清仓(用来控制回撤)。这背后的逻辑是,在存量资金博弈的市场环境中,资金总量相对固定,当银行、钢铁、有色、煤炭等大市值周期行业因基本面改善(如涨价、政策催化)或避险需求而走强时,会吸引大量资金涌入,导致其他板块(尤其是小市值股票)面临流动性虹吸。经过回测,对比上一个版本发现确实是可以降低一半的回撤,最大回撤从22.3%降到了11.4%,而且有很多时间是空仓等待。

木头大左的博客 243

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: C语言实现归并排序算法
下一篇: 使用 Boost.GIL 调整图像大小的示例
普通网友
博客等级 码龄3年 312粉丝 505原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

techDM

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址