min cost max flow算法示例
问题描述
给定g个group,n个id,n<=g.我们将为每个group分配一个id(各个group的id不同)。但是每个group分配id需要付出不同的代价cost,需要求解最优的id分配方案,使得整体cost之和最小。
例子
例如以下4个group,三个id,value矩阵A:
value | id1 | id2 | id3 |
---|---|---|---|
H1 | 4 | 3 | 0 |
H2 | 1 | 0 | 0 |
H3 | 2 | 0 | 2 |
H4 | 3 | 1 | 0 |
id_i分配给H_j的代价\(changing cost[i, j]=\sum(A[j,:])-A[j,i]\)。
例如,如果给H1指定id1,则value=4被保留,但是需要付出changing cost为3.
我们需要为H1-H4分别指定一个id1-id3,id4(新建的id),目标是是的总体的changing cost最小。
例子中最优的分配结果是:
H1 <- id2,
H2 <- New ID,
H3 <- id3,
H4 <- id1,
对应的changing cost=8 (4 + 1 + 2 + 1)。
Min-cost Max flow算法
Use min-cost max flow here
Connect source to all ids with capacity 1, connect each id to each h with capacity 1 and cost= -a[id[i], h[j]] (as you need to find maximums actually), and then connect all hs with sink with capacity 1.
After applying min-cost max flow, you will have flow in those (i, j) where you should assign i-th id to j-th h. New ids for other hs.
因为capacity=1,算法最终结果f[i,j]只可能取值0/1。所以,如果f[i,j]=1,则id_i被分配给h_j.
Here is a possible solution of the problem with some help of [min cost max flow algorithm:
http://web.mit.edu/~ecprice/acm/acm08/MinCostMaxFlow.java https://en.wikipedia.org/wiki/Minimum-cost_flow_problem.
The basic idea is to translate consumer id, group id to vertex of graph, translate our constrains to constrains of MinCostMaxFlow problem.
As for POC, I used the source code from website (web.mit.edu), did some change and checked in the algorithm to trunk.
I added unit test RuleBasedOptimizerTest.test6() to test the 66x 4 case, which runs successfully in milliseconds.
Also, test was done on the data which caused time out before, and this time it is fast.
Steps of the algorithm:
Create the flow network:
- Introduce a source vertex, a sink vertex;
- Each consumerid is a vertex, each groupid is a vertex;
- Connect source to each consumerId, each edge has capacity 1;
- Connect each consumerId to groupId, each edge has capacity 1;
- Connect each groupId to sink, each edge has capacity 1;
- The cost of a(u, v) is from the cost table, but we need to take -1 x frequency.
Calculate max flow of the network, and get the flow matrix.
- If there is flow from cid_i to gid_k then we assign the cid_i to the gid_k;
- If there is no flow to gid_k, then we assign a new id to gid_k.
Algorithm complex
is O(min(|V|^2 * totflow, |V|^3 * totcost)), where |V|=(#groupid + #consumerId + 2).
min cost max flow算法示例的更多相关文章
- LeetCode算法题-Min Cost Climbing Stairs(Java实现)
这是悦乐书的第307次更新,第327篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第176题(顺位题号是746).在楼梯上,第i步有一些非负成本成本[i]分配(0索引). ...
- LeetCode 746. 使用最小花费爬楼梯(Min Cost Climbing Stairs) 11
746. 使用最小花费爬楼梯 746. Min Cost Climbing Stairs 题目描述 数组的每个索引做为一个阶梯,第 i 个阶梯对应着一个非负数的体力花费值 cost[i].(索引从 0 ...
- C#LeetCode刷题之#746-使用最小花费爬楼梯( Min Cost Climbing Stairs)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4016 访问. 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个 ...
- HackerRank "Training the army" - Max Flow
First problem to learn Max Flow. Ford-Fulkerson is a group of algorithms - Dinic is one of it.It is ...
- [Swift]LeetCode746. 使用最小花费爬楼梯 | Min Cost Climbing Stairs
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
- backpropagation算法示例
backpropagation算法示例 下面举个例子,假设在某个mini-batch的有样本X和标签Y,其中\(X\in R^{m\times 2}, Y\in R^{m\times 1}\),现在有 ...
- BZOJ4390: [Usaco2015 dec]Max Flow
BZOJ4390: [Usaco2015 dec]Max Flow Description Farmer John has installed a new system of N−1 pipes to ...
- Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs)
Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs) 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost ...
- 详解 Flink DataStream中min(),minBy(),max(),max()之间的区别
解释 官方文档中: The difference between min and minBy is that min returns the minimum value, whereas minBy ...
随机推荐
- [RF]怎样用Robot Framework写好Test Case?
1.介绍 这是一个关于如何用Robot Framework写好Test Case的高层次的指导准则 怎样实际的与系统进行交互不在此文档范围内 最重要的准则是使测试用例尽可能的让熟悉此领域的人觉得简单易 ...
- h5页面适配iPhone X的方法
一.原生适配iphoneX 原生适配很简单,查看机型图: 只要用 #define KIsiPhoneX ([UIScreen mainScreen].bounds.size.height>8 ...
- 解决maven update project 后项目jdk变成1.5的问题
一.问题描述 在Eclipse中新建了一个Maven工程, 然后更改JDK版本为1.7, 结果每次使用Maven > Update project的时候JDK版本都恢复成1.5. 二.原因分析 ...
- 提升HTML5的性能体验系列之一 避免切页白屏
窗体切换白屏的现实问题 HTML5的性能比原生差很多,比如切页时白屏.列表滚动不流畅.下拉刷新和上拉翻页卡顿.在低端Android手机上,很多原生App常用的功能和体验效果都很难使用HTML5技术模拟 ...
- dedecms首页搜索 添加仿百度下拉框
1:找到uploads/templets/default/head.htm 2: 找到 <input name="q" type="text" clas ...
- Select isnull(,)
类似于 select isnull(...,1) from table 这样的 isnull函数防止查询结果为空,防止接下来需要这个值时可能造成空指针异常
- 双向循环链表涉及双向指针的基本操作(C语言)
链表大概分为有无头指针,有无尾指针,是否循环,单向还是双向, 这些都很简单,前提是你要把指针和单链表理解透彻.这些都是基于单链表 的变形,要根据实际问题,选择链表的类型. 头指针的指针域储存着储存头节 ...
- 690. Employee Importance
好几种写法,这里贴几个出来 第一种:暴力解法,除去递归栈,空间复杂度O(1).时间复杂度略高 /* // Employee info class Employee { public: // It's ...
- substr()和substring()函数
区别:主要是两者的参数不同 功能:相似. substr :返回一个从指定位置开始的指定长度的子字符串 substring :返回位于 String 对象中指定位置的子字符串. 用法: stringva ...
- HMM(隐马尔可夫模型)不断学习中
HMM(隐马尔可夫模型)是用来描述隐含未知参数的统计模型,举一个经典的例子:一个东京的朋友每天根据天气{下雨,天晴}决定当天的活动{公园散步,购物,清理房间}中的一种,我每天只能在twitter上看到 ...