Big O confusion about upper bound of an algorithm
$begingroup$
I dont know why I am getting confused about this as this is relatively simple but I am going to ask anyway...
Consider the following algorithm: Given a value k, compute ak, and binary search for ak in an array of size N. Consider the fact that multiplication takes O(log2(a)k) time.
What would the total runtime for this algorithm be? Here are my thoughts:
In the worst case, binary search will make log2(N) comparisons, and for each comparison, it takes O(log2(a)k) to calculate ak. So the total would be O(log2(a)k(log2(N)))? Am I going about this the correct way?
asymptotics
$endgroup$
add a comment |
$begingroup$
I dont know why I am getting confused about this as this is relatively simple but I am going to ask anyway...
Consider the following algorithm: Given a value k, compute ak, and binary search for ak in an array of size N. Consider the fact that multiplication takes O(log2(a)k) time.
What would the total runtime for this algorithm be? Here are my thoughts:
In the worst case, binary search will make log2(N) comparisons, and for each comparison, it takes O(log2(a)k) to calculate ak. So the total would be O(log2(a)k(log2(N)))? Am I going about this the correct way?
asymptotics
$endgroup$
$begingroup$
How is $N$ related to $a,k$? The time of multiplication should be related to the size of the numbers being multiplied, not to the size of the array. If $N$ is given, I can choose $a,k$ to make finding $a^k$ take longer than any constant based on $N$
$endgroup$
– Ross Millikan
Dec 16 '18 at 3:35
$begingroup$
sorry my mistake i fixed it
$endgroup$
– Matt Sigfried
Dec 16 '18 at 3:36
$begingroup$
That would be fine for multiplying $a cdot a$, but multiplying $a^{10} cdot a^{10}$ takes longer as there are more bits to multiply. What you should say is that multiplication of $a^k cdot a^k$ takes $log_2(a^k)^2$ time
$endgroup$
– Ross Millikan
Dec 16 '18 at 3:37
$begingroup$
is my bound correct now?
$endgroup$
– Matt Sigfried
Dec 16 '18 at 3:38
add a comment |
$begingroup$
I dont know why I am getting confused about this as this is relatively simple but I am going to ask anyway...
Consider the following algorithm: Given a value k, compute ak, and binary search for ak in an array of size N. Consider the fact that multiplication takes O(log2(a)k) time.
What would the total runtime for this algorithm be? Here are my thoughts:
In the worst case, binary search will make log2(N) comparisons, and for each comparison, it takes O(log2(a)k) to calculate ak. So the total would be O(log2(a)k(log2(N)))? Am I going about this the correct way?
asymptotics
$endgroup$
I dont know why I am getting confused about this as this is relatively simple but I am going to ask anyway...
Consider the following algorithm: Given a value k, compute ak, and binary search for ak in an array of size N. Consider the fact that multiplication takes O(log2(a)k) time.
What would the total runtime for this algorithm be? Here are my thoughts:
In the worst case, binary search will make log2(N) comparisons, and for each comparison, it takes O(log2(a)k) to calculate ak. So the total would be O(log2(a)k(log2(N)))? Am I going about this the correct way?
asymptotics
asymptotics
edited Dec 16 '18 at 3:36
Matt Sigfried
asked Dec 16 '18 at 3:18
Matt SigfriedMatt Sigfried
11
11
$begingroup$
How is $N$ related to $a,k$? The time of multiplication should be related to the size of the numbers being multiplied, not to the size of the array. If $N$ is given, I can choose $a,k$ to make finding $a^k$ take longer than any constant based on $N$
$endgroup$
– Ross Millikan
Dec 16 '18 at 3:35
$begingroup$
sorry my mistake i fixed it
$endgroup$
– Matt Sigfried
Dec 16 '18 at 3:36
$begingroup$
That would be fine for multiplying $a cdot a$, but multiplying $a^{10} cdot a^{10}$ takes longer as there are more bits to multiply. What you should say is that multiplication of $a^k cdot a^k$ takes $log_2(a^k)^2$ time
$endgroup$
– Ross Millikan
Dec 16 '18 at 3:37
$begingroup$
is my bound correct now?
$endgroup$
– Matt Sigfried
Dec 16 '18 at 3:38
add a comment |
$begingroup$
How is $N$ related to $a,k$? The time of multiplication should be related to the size of the numbers being multiplied, not to the size of the array. If $N$ is given, I can choose $a,k$ to make finding $a^k$ take longer than any constant based on $N$
$endgroup$
– Ross Millikan
Dec 16 '18 at 3:35
$begingroup$
sorry my mistake i fixed it
$endgroup$
– Matt Sigfried
Dec 16 '18 at 3:36
$begingroup$
That would be fine for multiplying $a cdot a$, but multiplying $a^{10} cdot a^{10}$ takes longer as there are more bits to multiply. What you should say is that multiplication of $a^k cdot a^k$ takes $log_2(a^k)^2$ time
$endgroup$
– Ross Millikan
Dec 16 '18 at 3:37
$begingroup$
is my bound correct now?
$endgroup$
– Matt Sigfried
Dec 16 '18 at 3:38
$begingroup$
How is $N$ related to $a,k$? The time of multiplication should be related to the size of the numbers being multiplied, not to the size of the array. If $N$ is given, I can choose $a,k$ to make finding $a^k$ take longer than any constant based on $N$
$endgroup$
– Ross Millikan
Dec 16 '18 at 3:35
$begingroup$
How is $N$ related to $a,k$? The time of multiplication should be related to the size of the numbers being multiplied, not to the size of the array. If $N$ is given, I can choose $a,k$ to make finding $a^k$ take longer than any constant based on $N$
$endgroup$
– Ross Millikan
Dec 16 '18 at 3:35
$begingroup$
sorry my mistake i fixed it
$endgroup$
– Matt Sigfried
Dec 16 '18 at 3:36
$begingroup$
sorry my mistake i fixed it
$endgroup$
– Matt Sigfried
Dec 16 '18 at 3:36
$begingroup$
That would be fine for multiplying $a cdot a$, but multiplying $a^{10} cdot a^{10}$ takes longer as there are more bits to multiply. What you should say is that multiplication of $a^k cdot a^k$ takes $log_2(a^k)^2$ time
$endgroup$
– Ross Millikan
Dec 16 '18 at 3:37
$begingroup$
That would be fine for multiplying $a cdot a$, but multiplying $a^{10} cdot a^{10}$ takes longer as there are more bits to multiply. What you should say is that multiplication of $a^k cdot a^k$ takes $log_2(a^k)^2$ time
$endgroup$
– Ross Millikan
Dec 16 '18 at 3:37
$begingroup$
is my bound correct now?
$endgroup$
– Matt Sigfried
Dec 16 '18 at 3:38
$begingroup$
is my bound correct now?
$endgroup$
– Matt Sigfried
Dec 16 '18 at 3:38
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
Presumably you compute $a^k$ once, then search for it in the array. You should not multiply the number of operations for each part, you should add them. Using binary search assumes that the array is sorted, but we will accept that. You are right that you will do $log_2 N$ comparisons. To compute $a^k$ you can express $k$ in binary, square $a$ enough times, then multiply the values that correspond to the one bits in $k$. You should be able to express this in terms of the number of bits in $a$, which is $log_2 a$ and a function of $k$.
$endgroup$
$begingroup$
what if you are repeating this process fo multiple values of a, then do you multiply?
$endgroup$
– Matt Sigfried
Dec 16 '18 at 3:47
$begingroup$
Yes, but you have to find the amount of computation for one value of $a$ first, then multiply by the number of values of $a$. The question does not talk of multiple values of $a$
$endgroup$
– Ross Millikan
Dec 16 '18 at 4:09
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%2f3042201%2fbig-o-confusion-about-upper-bound-of-an-algorithm%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$
Presumably you compute $a^k$ once, then search for it in the array. You should not multiply the number of operations for each part, you should add them. Using binary search assumes that the array is sorted, but we will accept that. You are right that you will do $log_2 N$ comparisons. To compute $a^k$ you can express $k$ in binary, square $a$ enough times, then multiply the values that correspond to the one bits in $k$. You should be able to express this in terms of the number of bits in $a$, which is $log_2 a$ and a function of $k$.
$endgroup$
$begingroup$
what if you are repeating this process fo multiple values of a, then do you multiply?
$endgroup$
– Matt Sigfried
Dec 16 '18 at 3:47
$begingroup$
Yes, but you have to find the amount of computation for one value of $a$ first, then multiply by the number of values of $a$. The question does not talk of multiple values of $a$
$endgroup$
– Ross Millikan
Dec 16 '18 at 4:09
add a comment |
$begingroup$
Presumably you compute $a^k$ once, then search for it in the array. You should not multiply the number of operations for each part, you should add them. Using binary search assumes that the array is sorted, but we will accept that. You are right that you will do $log_2 N$ comparisons. To compute $a^k$ you can express $k$ in binary, square $a$ enough times, then multiply the values that correspond to the one bits in $k$. You should be able to express this in terms of the number of bits in $a$, which is $log_2 a$ and a function of $k$.
$endgroup$
$begingroup$
what if you are repeating this process fo multiple values of a, then do you multiply?
$endgroup$
– Matt Sigfried
Dec 16 '18 at 3:47
$begingroup$
Yes, but you have to find the amount of computation for one value of $a$ first, then multiply by the number of values of $a$. The question does not talk of multiple values of $a$
$endgroup$
– Ross Millikan
Dec 16 '18 at 4:09
add a comment |
$begingroup$
Presumably you compute $a^k$ once, then search for it in the array. You should not multiply the number of operations for each part, you should add them. Using binary search assumes that the array is sorted, but we will accept that. You are right that you will do $log_2 N$ comparisons. To compute $a^k$ you can express $k$ in binary, square $a$ enough times, then multiply the values that correspond to the one bits in $k$. You should be able to express this in terms of the number of bits in $a$, which is $log_2 a$ and a function of $k$.
$endgroup$
Presumably you compute $a^k$ once, then search for it in the array. You should not multiply the number of operations for each part, you should add them. Using binary search assumes that the array is sorted, but we will accept that. You are right that you will do $log_2 N$ comparisons. To compute $a^k$ you can express $k$ in binary, square $a$ enough times, then multiply the values that correspond to the one bits in $k$. You should be able to express this in terms of the number of bits in $a$, which is $log_2 a$ and a function of $k$.
edited Dec 16 '18 at 4:09
answered Dec 16 '18 at 3:44
Ross MillikanRoss Millikan
297k23198371
297k23198371
$begingroup$
what if you are repeating this process fo multiple values of a, then do you multiply?
$endgroup$
– Matt Sigfried
Dec 16 '18 at 3:47
$begingroup$
Yes, but you have to find the amount of computation for one value of $a$ first, then multiply by the number of values of $a$. The question does not talk of multiple values of $a$
$endgroup$
– Ross Millikan
Dec 16 '18 at 4:09
add a comment |
$begingroup$
what if you are repeating this process fo multiple values of a, then do you multiply?
$endgroup$
– Matt Sigfried
Dec 16 '18 at 3:47
$begingroup$
Yes, but you have to find the amount of computation for one value of $a$ first, then multiply by the number of values of $a$. The question does not talk of multiple values of $a$
$endgroup$
– Ross Millikan
Dec 16 '18 at 4:09
$begingroup$
what if you are repeating this process fo multiple values of a, then do you multiply?
$endgroup$
– Matt Sigfried
Dec 16 '18 at 3:47
$begingroup$
what if you are repeating this process fo multiple values of a, then do you multiply?
$endgroup$
– Matt Sigfried
Dec 16 '18 at 3:47
$begingroup$
Yes, but you have to find the amount of computation for one value of $a$ first, then multiply by the number of values of $a$. The question does not talk of multiple values of $a$
$endgroup$
– Ross Millikan
Dec 16 '18 at 4:09
$begingroup$
Yes, but you have to find the amount of computation for one value of $a$ first, then multiply by the number of values of $a$. The question does not talk of multiple values of $a$
$endgroup$
– Ross Millikan
Dec 16 '18 at 4:09
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%2f3042201%2fbig-o-confusion-about-upper-bound-of-an-algorithm%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$
How is $N$ related to $a,k$? The time of multiplication should be related to the size of the numbers being multiplied, not to the size of the array. If $N$ is given, I can choose $a,k$ to make finding $a^k$ take longer than any constant based on $N$
$endgroup$
– Ross Millikan
Dec 16 '18 at 3:35
$begingroup$
sorry my mistake i fixed it
$endgroup$
– Matt Sigfried
Dec 16 '18 at 3:36
$begingroup$
That would be fine for multiplying $a cdot a$, but multiplying $a^{10} cdot a^{10}$ takes longer as there are more bits to multiply. What you should say is that multiplication of $a^k cdot a^k$ takes $log_2(a^k)^2$ time
$endgroup$
– Ross Millikan
Dec 16 '18 at 3:37
$begingroup$
is my bound correct now?
$endgroup$
– Matt Sigfried
Dec 16 '18 at 3:38