问题描述

给定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:

  1. Introduce a source vertex, a sink vertex;
  2. Each consumerid is a vertex, each groupid is a vertex;
  3. Connect source to each consumerId, each edge has capacity 1;
  4. Connect each consumerId to groupId, each edge has capacity 1;
  5. Connect each groupId to sink, each edge has capacity 1;
  6. 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算法示例的更多相关文章

  1. LeetCode算法题-Min Cost Climbing Stairs(Java实现)

    这是悦乐书的第307次更新,第327篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第176题(顺位题号是746).在楼梯上,第i步有一些非负成本成本[i]分配(0索引). ...

  2. LeetCode 746. 使用最小花费爬楼梯(Min Cost Climbing Stairs) 11

    746. 使用最小花费爬楼梯 746. Min Cost Climbing Stairs 题目描述 数组的每个索引做为一个阶梯,第 i 个阶梯对应着一个非负数的体力花费值 cost[i].(索引从 0 ...

  3. C#LeetCode刷题之#746-使用最小花费爬楼梯( Min Cost Climbing Stairs)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4016 访问. 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个 ...

  4. 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 ...

  5. [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 ...

  6. backpropagation算法示例

    backpropagation算法示例 下面举个例子,假设在某个mini-batch的有样本X和标签Y,其中\(X\in R^{m\times 2}, Y\in R^{m\times 1}\),现在有 ...

  7. BZOJ4390: [Usaco2015 dec]Max Flow

    BZOJ4390: [Usaco2015 dec]Max Flow Description Farmer John has installed a new system of N−1 pipes to ...

  8. Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs)

    Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs) 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost ...

  9. 详解 Flink DataStream中min(),minBy(),max(),max()之间的区别

    解释 官方文档中: The difference between min and minBy is that min returns the minimum value, whereas minBy ...

随机推荐

  1. Netty Reator(三)Reactor 模型

    Netty Reator(三)Reactor 模型 Netty 系列目录 (https://www.cnblogs.com/binarylei/p/10117436.html) 本文介绍 DC Sch ...

  2. 转录组表达量计RPKM、FPKM、TPM说明

    在转录组测序(RNA-Seq)中,基因的表达量是我们关注的重点.基因表达量的衡量指标有:RPKM.FPKM.TPM. RPKM:Reads Per Kilobase Million:说实话,这个英文说 ...

  3. 玩具谜题(NOIP2016)

    题目链接:玩具谜题 提高组日常水题. 直接模拟,有需要注意的点会在代码后讲解: #include<bits/stdc++.h> using namespace std; int main( ...

  4. mysql 执行计划分析三看, explain,profiling,optimizer_trace

    http://blog.csdn.net/xj626852095/article/details/52767963 step 1 使用explain 查看执行计划, 5.6后可以加参数 explain ...

  5. unity延时函数

    新建一个工具类 public class DelayToInvoke : MonoBehaviour{ public static IEnumerator DelayToInvokeDo(Action ...

  6. 异常处理(异常解析器) 和 对于Properties类型的属性的配置

    在程序运行中,有可能因为用户的不当操作,发生异常.. 在springmvc中可以根据不同的异常配置不同的处理方式 1.例如出现 这个类型异常 org.springframework.web.multi ...

  7. mysql之练习题4

    准备表: create table class(cid int primary key auto_increment, caption ) not null unique); INSERT into ...

  8. Bootstrap Table 超多列 使用滚动条

    overflow-x: scroll;横向滑动详细讲解 able显示滚动条,要先把table放到一个div中,控制div 属性overflow值为scroll <div style=" ...

  9. Mybatis-Plus 实战完整学习笔记(四)------全局参数配置

    一.全局配置设置 (1)全局配置Id自动生成 <!--定义mybatisplus全局配置--> <bean id="globalConfig" class=&qu ...

  10. php 制作二维码 phpqrcode.php

    phpqrcode.php 下载地址:https://sourceforge.net/projects/phpqrcode/ //测试可行 utf-8格式 <?php header(" ...