np-hard

Recursively finding all partitions of a set of n objects into k non-empty subsets

爱⌒轻易说出口 提交于 2021-02-11 15:01:20
问题 I want to find all partitions of a n elements into k subsets, this is my algorithm based on recursive formula for finding all Stirling second numbers fun main(args: Array<String>) { val s = mutableSetOf(1, 2, 3, 4, 5) val partitions = 3 val res = mutableSetOf<MutableSet<MutableSet<Int>>>() partition(s, partitions, res) //println(res) println("Second kind stirling number ${res.size}") } fun partition(inputSet: MutableSet<Int>, numOfPartitions: Int, result: MutableSet<MutableSet<MutableSet<Int>

CodeForces 687A NP-Hard Problem

て烟熏妆下的殇ゞ 提交于 2020-03-03 21:40:34
Portal: http://codeforces.com/problemset/problem/687/A 二分图染色 好模板题 有SPJ 值得注意的是,因为C++的奇妙的运算机制 若在vector变量x.size()=0,则x.size()-1会溢出(此处坑了我3h) 这不禁让我想起了文明2里的甘地 1 #include<iostream> 2 #include<algorithm> 3 #include<set> 4 #include<cstdio> 5 #include<cstdlib> 6 #include<cmath> 7 #include<vector> 8 using namespace std; 9 #define FOR(i,j,k) for(int i=j;i<=k;i++) 10 #define FORD(i,j,k) for(int i=j;i>=k;i--) 11 #define LL long long 12 #define maxn 100010 13 #define SZ(x) int(x.size()) 14 int n,m,j,x,y; 15 bool flag=true; 16 vector<int> a[maxn]; 17 vector<int> ans[2]; 18 int col[maxn],len[maxn],vis[maxn];

Codeforces C. NP-Hard Problem 搜索

醉酒当歌 提交于 2020-02-25 00:31:46
C. NP-Hard Problem time limit per test:2 seconds memory limit per test:256 megabytes input:standard input output:standard output Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting. Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. or (or both). Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of

max-weight k-clique in a complete k-partite graph

牧云@^-^@ 提交于 2020-01-02 01:20:12
问题 My Problem Whether there's an efficient algorithm to find a max-weight (or min-weight) k -clique in a complete k -partite graph (a graph in which vertices are adjacent if and only if they belong to different partite sets according to wikipedia)? More Details about the Terms Max-weight Clique: Every edge in the graph has a weight. The weight of a clique is the sum of the weights of all edges in the clique. The goal is to find a clique with the maximum weight. Note that the size of the clique

NP-Complete VS NP-Hard

回眸只為那壹抹淺笑 提交于 2020-01-01 11:50:16
问题 I am trying to understand the difference between NP-Complete and NP-Hard. Below is my understanding An NP-Hard problem is one that is not solvable in polynomial time but can be verified in polynomial time. An NP-Complete problem is one that is in NP and is also NP-Hard. Is the above definition correct? If so, What about problems not In NP but NP-Hard. Wouldn't they be harder than NP-Complete problem, say they can only be solved and verified in exponential time? 回答1: A NP problem (not NP-Hard

Boolean formula encoding

会有一股神秘感。 提交于 2019-12-23 05:26:02
问题 i am wondering how many bits required to encode a boolean formula like @(x1,x2,x3,x4) = (x1 OR x2 OR NOT(x3) OR x4) AND ((NOT)x2 OR x3) AND (x1 OR (NOT)x4) @ is an instance of SAT. I think it is 4 bits since total number of possible combinations are 2(power4). Is that correct? Should i count OR, NOT, AND to calculate number of bits needed for encoding? I searched a lot but couldn't find anything on this. 回答1: You can always convert your expression into a logically equivalent CNF with

Numberlink/Flow Game: How to spot NP-Complete problems?

一个人想着一个人 提交于 2019-12-23 04:41:18
问题 I was trying to find a way to solve the problem in the famous game Flow. http://moh97.us/flow/ After googling I find out that this is a NP-complete problem. A good solution would make use of heuristics and cuts. How can I spot a NP-complete problem easily? Sometimes when I block, I can't see the obvious solution. When this happens with an NP-complete, it's better to recognise it quickly and move on to the next problem. 回答1: When you have an explosion of objects (say objects whose count grows

Numberlink/Flow Game: How to spot NP-Complete problems?

﹥>﹥吖頭↗ 提交于 2019-12-23 04:41:01
问题 I was trying to find a way to solve the problem in the famous game Flow. http://moh97.us/flow/ After googling I find out that this is a NP-complete problem. A good solution would make use of heuristics and cuts. How can I spot a NP-complete problem easily? Sometimes when I block, I can't see the obvious solution. When this happens with an NP-complete, it's better to recognise it quickly and move on to the next problem. 回答1: When you have an explosion of objects (say objects whose count grows

Minimum cost strongly connected digraph

我只是一个虾纸丫 提交于 2019-12-21 02:05:12
问题 I have a digraph which is strongly connected (i.e. there is a path from i to j and j to i for each pair of nodes (i, j) in the graph G). I wish to find a strongly connected graph out of this graph such that the sum of all edges is the least. To put it differently, I need to get rid of edges in such a way that after removing them, the graph will still be strongly connected and of least cost for the sum of edges. I think it's an NP hard problem. I'm looking for an optimal solution, not

P、NP、NP-Complete、NP-Hard问题

人盡茶涼 提交于 2019-12-21 00:28:36
P、 NP、NP-Complete、NP-Hard这些 概念都是用来描述一个问题的难度的。即一个问题能否在以上时间内求解,或者验证一个解是否符合一个问题。 在下面的讨论中,我们假设问题的输入规模是n,那么问题的解决时间,或者验证时间都应该是n的一个函数,记为f(n). 1、 规约(Reduction) :将一个问题,等价转换成另一个问题的子问题的方式。 2、P问题:P即Polynomial time,多项式时间。f(n)=a0+a1*n1+a2*n2+a3*n3+…. 。 意思是说给定一个问题,能在多项式时间内 找到 符合该问题的解。此时,问题的时间复杂度是O(nj)。 3、NP问题(Non-deterministic problem):即非确定性多项式时间问题。 NP 就是指能在多项式时间内 验证 一个解是否满足的一类问题。 什么是非确定性问题呢? 有些计算问题是确定性的,比如加减乘除之类,你只要按照公式推导,按部就班一步步来,就可以得到结果。但是,有些问题是无法按部就班直接地计算出。比如,找大质数的问题。有没有一个公式,你一套公式,就可以一步步推算出来,下一个质数应该是多少呢?这样的公式是没有的。再比如,大的合数分解质因数的问题,有没有一个公式,把合数代进去,就直接可以算出,它的因子各自是多少?也没有这样的公式。 这种问题的答案,是无法直接计算得到的,只能通过间接的“猜算