Count the size of $|{(b_1, dots, b_n): (-1)^{b_1} a_1 + cdots + (-1)^{b_n} a_n =0, b_i in {0,1} }|$ in $O(n)$...
$begingroup$
What is the size of $|{(b_1, dots, b_n): (-1)^{b_1} a_1 + cdots + (-1)^{b_n} a_n =0, b_i in {0,1} }|$ when all $a_i in mathbb{Z}^+$ are given, fixed positive integers ? How can you count this size in a $O(n)$ way (especially by computer algorithm)? What is a main trick to count this in a fast way? Is there any textbook or topics that deals with such things?
number-theory algorithms
$endgroup$
add a comment |
$begingroup$
What is the size of $|{(b_1, dots, b_n): (-1)^{b_1} a_1 + cdots + (-1)^{b_n} a_n =0, b_i in {0,1} }|$ when all $a_i in mathbb{Z}^+$ are given, fixed positive integers ? How can you count this size in a $O(n)$ way (especially by computer algorithm)? What is a main trick to count this in a fast way? Is there any textbook or topics that deals with such things?
number-theory algorithms
$endgroup$
$begingroup$
Have you tried anything?
$endgroup$
– Morgan Rodgers
Jan 16 at 6:06
$begingroup$
@MorganRodgers, yes very trivial way, which takes $2^n/4$ steps to count the size
$endgroup$
– julypraise
Jan 16 at 6:13
add a comment |
$begingroup$
What is the size of $|{(b_1, dots, b_n): (-1)^{b_1} a_1 + cdots + (-1)^{b_n} a_n =0, b_i in {0,1} }|$ when all $a_i in mathbb{Z}^+$ are given, fixed positive integers ? How can you count this size in a $O(n)$ way (especially by computer algorithm)? What is a main trick to count this in a fast way? Is there any textbook or topics that deals with such things?
number-theory algorithms
$endgroup$
What is the size of $|{(b_1, dots, b_n): (-1)^{b_1} a_1 + cdots + (-1)^{b_n} a_n =0, b_i in {0,1} }|$ when all $a_i in mathbb{Z}^+$ are given, fixed positive integers ? How can you count this size in a $O(n)$ way (especially by computer algorithm)? What is a main trick to count this in a fast way? Is there any textbook or topics that deals with such things?
number-theory algorithms
number-theory algorithms
edited Jan 16 at 6:45
julypraise
asked Jan 16 at 6:02
julypraisejulypraise
1,29621023
1,29621023
$begingroup$
Have you tried anything?
$endgroup$
– Morgan Rodgers
Jan 16 at 6:06
$begingroup$
@MorganRodgers, yes very trivial way, which takes $2^n/4$ steps to count the size
$endgroup$
– julypraise
Jan 16 at 6:13
add a comment |
$begingroup$
Have you tried anything?
$endgroup$
– Morgan Rodgers
Jan 16 at 6:06
$begingroup$
@MorganRodgers, yes very trivial way, which takes $2^n/4$ steps to count the size
$endgroup$
– julypraise
Jan 16 at 6:13
$begingroup$
Have you tried anything?
$endgroup$
– Morgan Rodgers
Jan 16 at 6:06
$begingroup$
Have you tried anything?
$endgroup$
– Morgan Rodgers
Jan 16 at 6:06
$begingroup$
@MorganRodgers, yes very trivial way, which takes $2^n/4$ steps to count the size
$endgroup$
– julypraise
Jan 16 at 6:13
$begingroup$
@MorganRodgers, yes very trivial way, which takes $2^n/4$ steps to count the size
$endgroup$
– julypraise
Jan 16 at 6:13
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
Let us rewrite the main condition as
$$
tag{1} (-1)^{b_1}a_1 +...+(-1)^{b_n} a_n = sumlimits_{i: b_i = 1} a_i - sumlimits_{i: b_i = -1} a_i = 0,
$$
and hence the equivalent formulation of your question is to find the number of subsets of ${1,...,n}$ which partition ${a_1,...,a_n}$ into $2$ sets with equal sums. In this you have the partition problem which is NP-complete. In fact, the partition problem asks for existence of a single partition satisfying $(1)$, while you want to compute all possible ones, so no polynomial algorithm here, let alone linear $O(n)$ as you ask.
There is, however, an approach via dynamic programming, which is easy to program and might suffice for many practical scenarios. I'm sketching the idea below, a python program (this is closest to pseudo-code and requires less explanation of the syntax), which, given a list of integers $a$, decides if there's a partition with equal sums.
def partition(a):
# a is the array in question
# we decide whether there is a partition of a into equal sum subsets
num = sum(a)
if num%2 != 0:
return False
num = num//2
n = len(a)
memo = (num+1)*[None] # will be used for memoization, see below
for i in range(num + 1):
memo[i] = (n+1)*[False]
# memo[i][j] shows if the integer i can be represented by elements of a up to and including j
for i in range(n+1):
memo[0][i] = True # 0 can be represented by not choosing anything, the empty set
for i in range(1, num + 1):
for j in range(1, n + 1):
memo[i][j] = memo[i][j-1] or ( i >= a[j-1] and memo[i - a[j-1]][j-1] )
# either integer i can be represented by elements a_0, ..., a_{j-1}
# or i is represented by a_j and i - a_j by elements a_0,...,a_{j-1}
return memo[-1][-1] # the last element of the last row
The result of the function partition will give you whether such equal sum partition exists. Then, you can use backtracking algorithms and the values of the matrix memo to construct the actual partitions.
The above is a well-known topic and you will find further details and plenty of resources on the internet.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3075367%2fcount-the-size-of-b-1-dots-b-n-1b-1-a-1-cdots-1b-n-a%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Let us rewrite the main condition as
$$
tag{1} (-1)^{b_1}a_1 +...+(-1)^{b_n} a_n = sumlimits_{i: b_i = 1} a_i - sumlimits_{i: b_i = -1} a_i = 0,
$$
and hence the equivalent formulation of your question is to find the number of subsets of ${1,...,n}$ which partition ${a_1,...,a_n}$ into $2$ sets with equal sums. In this you have the partition problem which is NP-complete. In fact, the partition problem asks for existence of a single partition satisfying $(1)$, while you want to compute all possible ones, so no polynomial algorithm here, let alone linear $O(n)$ as you ask.
There is, however, an approach via dynamic programming, which is easy to program and might suffice for many practical scenarios. I'm sketching the idea below, a python program (this is closest to pseudo-code and requires less explanation of the syntax), which, given a list of integers $a$, decides if there's a partition with equal sums.
def partition(a):
# a is the array in question
# we decide whether there is a partition of a into equal sum subsets
num = sum(a)
if num%2 != 0:
return False
num = num//2
n = len(a)
memo = (num+1)*[None] # will be used for memoization, see below
for i in range(num + 1):
memo[i] = (n+1)*[False]
# memo[i][j] shows if the integer i can be represented by elements of a up to and including j
for i in range(n+1):
memo[0][i] = True # 0 can be represented by not choosing anything, the empty set
for i in range(1, num + 1):
for j in range(1, n + 1):
memo[i][j] = memo[i][j-1] or ( i >= a[j-1] and memo[i - a[j-1]][j-1] )
# either integer i can be represented by elements a_0, ..., a_{j-1}
# or i is represented by a_j and i - a_j by elements a_0,...,a_{j-1}
return memo[-1][-1] # the last element of the last row
The result of the function partition will give you whether such equal sum partition exists. Then, you can use backtracking algorithms and the values of the matrix memo to construct the actual partitions.
The above is a well-known topic and you will find further details and plenty of resources on the internet.
$endgroup$
add a comment |
$begingroup$
Let us rewrite the main condition as
$$
tag{1} (-1)^{b_1}a_1 +...+(-1)^{b_n} a_n = sumlimits_{i: b_i = 1} a_i - sumlimits_{i: b_i = -1} a_i = 0,
$$
and hence the equivalent formulation of your question is to find the number of subsets of ${1,...,n}$ which partition ${a_1,...,a_n}$ into $2$ sets with equal sums. In this you have the partition problem which is NP-complete. In fact, the partition problem asks for existence of a single partition satisfying $(1)$, while you want to compute all possible ones, so no polynomial algorithm here, let alone linear $O(n)$ as you ask.
There is, however, an approach via dynamic programming, which is easy to program and might suffice for many practical scenarios. I'm sketching the idea below, a python program (this is closest to pseudo-code and requires less explanation of the syntax), which, given a list of integers $a$, decides if there's a partition with equal sums.
def partition(a):
# a is the array in question
# we decide whether there is a partition of a into equal sum subsets
num = sum(a)
if num%2 != 0:
return False
num = num//2
n = len(a)
memo = (num+1)*[None] # will be used for memoization, see below
for i in range(num + 1):
memo[i] = (n+1)*[False]
# memo[i][j] shows if the integer i can be represented by elements of a up to and including j
for i in range(n+1):
memo[0][i] = True # 0 can be represented by not choosing anything, the empty set
for i in range(1, num + 1):
for j in range(1, n + 1):
memo[i][j] = memo[i][j-1] or ( i >= a[j-1] and memo[i - a[j-1]][j-1] )
# either integer i can be represented by elements a_0, ..., a_{j-1}
# or i is represented by a_j and i - a_j by elements a_0,...,a_{j-1}
return memo[-1][-1] # the last element of the last row
The result of the function partition will give you whether such equal sum partition exists. Then, you can use backtracking algorithms and the values of the matrix memo to construct the actual partitions.
The above is a well-known topic and you will find further details and plenty of resources on the internet.
$endgroup$
add a comment |
$begingroup$
Let us rewrite the main condition as
$$
tag{1} (-1)^{b_1}a_1 +...+(-1)^{b_n} a_n = sumlimits_{i: b_i = 1} a_i - sumlimits_{i: b_i = -1} a_i = 0,
$$
and hence the equivalent formulation of your question is to find the number of subsets of ${1,...,n}$ which partition ${a_1,...,a_n}$ into $2$ sets with equal sums. In this you have the partition problem which is NP-complete. In fact, the partition problem asks for existence of a single partition satisfying $(1)$, while you want to compute all possible ones, so no polynomial algorithm here, let alone linear $O(n)$ as you ask.
There is, however, an approach via dynamic programming, which is easy to program and might suffice for many practical scenarios. I'm sketching the idea below, a python program (this is closest to pseudo-code and requires less explanation of the syntax), which, given a list of integers $a$, decides if there's a partition with equal sums.
def partition(a):
# a is the array in question
# we decide whether there is a partition of a into equal sum subsets
num = sum(a)
if num%2 != 0:
return False
num = num//2
n = len(a)
memo = (num+1)*[None] # will be used for memoization, see below
for i in range(num + 1):
memo[i] = (n+1)*[False]
# memo[i][j] shows if the integer i can be represented by elements of a up to and including j
for i in range(n+1):
memo[0][i] = True # 0 can be represented by not choosing anything, the empty set
for i in range(1, num + 1):
for j in range(1, n + 1):
memo[i][j] = memo[i][j-1] or ( i >= a[j-1] and memo[i - a[j-1]][j-1] )
# either integer i can be represented by elements a_0, ..., a_{j-1}
# or i is represented by a_j and i - a_j by elements a_0,...,a_{j-1}
return memo[-1][-1] # the last element of the last row
The result of the function partition will give you whether such equal sum partition exists. Then, you can use backtracking algorithms and the values of the matrix memo to construct the actual partitions.
The above is a well-known topic and you will find further details and plenty of resources on the internet.
$endgroup$
Let us rewrite the main condition as
$$
tag{1} (-1)^{b_1}a_1 +...+(-1)^{b_n} a_n = sumlimits_{i: b_i = 1} a_i - sumlimits_{i: b_i = -1} a_i = 0,
$$
and hence the equivalent formulation of your question is to find the number of subsets of ${1,...,n}$ which partition ${a_1,...,a_n}$ into $2$ sets with equal sums. In this you have the partition problem which is NP-complete. In fact, the partition problem asks for existence of a single partition satisfying $(1)$, while you want to compute all possible ones, so no polynomial algorithm here, let alone linear $O(n)$ as you ask.
There is, however, an approach via dynamic programming, which is easy to program and might suffice for many practical scenarios. I'm sketching the idea below, a python program (this is closest to pseudo-code and requires less explanation of the syntax), which, given a list of integers $a$, decides if there's a partition with equal sums.
def partition(a):
# a is the array in question
# we decide whether there is a partition of a into equal sum subsets
num = sum(a)
if num%2 != 0:
return False
num = num//2
n = len(a)
memo = (num+1)*[None] # will be used for memoization, see below
for i in range(num + 1):
memo[i] = (n+1)*[False]
# memo[i][j] shows if the integer i can be represented by elements of a up to and including j
for i in range(n+1):
memo[0][i] = True # 0 can be represented by not choosing anything, the empty set
for i in range(1, num + 1):
for j in range(1, n + 1):
memo[i][j] = memo[i][j-1] or ( i >= a[j-1] and memo[i - a[j-1]][j-1] )
# either integer i can be represented by elements a_0, ..., a_{j-1}
# or i is represented by a_j and i - a_j by elements a_0,...,a_{j-1}
return memo[-1][-1] # the last element of the last row
The result of the function partition will give you whether such equal sum partition exists. Then, you can use backtracking algorithms and the values of the matrix memo to construct the actual partitions.
The above is a well-known topic and you will find further details and plenty of resources on the internet.
answered Jan 16 at 18:53
HaykHayk
2,6271214
2,6271214
add a comment |
add a comment |
Thanks for contributing an answer to Mathematics Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3075367%2fcount-the-size-of-b-1-dots-b-n-1b-1-a-1-cdots-1b-n-a%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Have you tried anything?
$endgroup$
– Morgan Rodgers
Jan 16 at 6:06
$begingroup$
@MorganRodgers, yes very trivial way, which takes $2^n/4$ steps to count the size
$endgroup$
– julypraise
Jan 16 at 6:13