4,670 questions
-5
votes
1
answer
86
views
Asymptotic time complexity where a variable has a known upper bound?
Say I have an algorithm that runs in O(N*M + N + M). Say that although M can vary, it can only be between 1-20. For asymptotic complexity is M treated as a constant? Because you can't really take the ...
2
votes
3
answers
119
views
Big-O notation if n is a constant [duplicate]
Let's say I have this pseudocode:
int n = 100
for (int i = 1; i<=n; i++)
System.out.println("Hello!");
for (int j = 1; j<=n; j++)
System.out.println("World!!!");
end
...
4
votes
2
answers
280
views
What is the time complexity of the following algorithm:
int k = 0;
for (int a = n; a >= 1; a /= 2)
for (int b = a; b >= 1; b--)
k++;
cout << k;
I would say that the answer is O(n), but the book where I have found the exercise says that ...
1
vote
2
answers
213
views
Big O for possibly non terminating algorithm
Suppose we want to generate a 5-digit unique student ID
i.e. there are N = 100,000 possible values (from 00000 to 99999).
What is the big O of the algorithm below?
- while True:
- Generate a random ...
0
votes
1
answer
105
views
Asymptotic analysis: Time cost of allocating space for a set of elements
What is the time cost (asymptotically) of the following method?:
void fillList(int i)
{
list = new int[i];
}
I have been told that it takes O(1) time because no initialization is done and the ...
-1
votes
1
answer
42
views
Does the value of n matter when comparing asymptotic complexities?
Is the complexity defined by the big-O notation e.g., n! is always more complex than n^3, regardless of the value of n?
What if I know n = 3? Would I be able to say that in this case O(n!) is less ...
-2
votes
1
answer
146
views
How to determine the complexity of a Turing machine from its state table?
I have an algorithm that is implemented by the following state table for a Turing machine:
𝑞1
𝑞2
𝑞3
0
0R 𝑞1
1L 𝑞3
0L 𝑞3
1
1R 𝑞1
0L 𝑞2
1L 𝑞3
_
_L 𝑞2
1S 𝑞0
_3 𝑞0
I want to determine the ...
1
vote
1
answer
90
views
Can't tell the difference between two python n-queens solutions
Reading up on backtracking led me to a page on geeksforgeeks.org about solutions to the n-queens problem. The first solution is introduced as the "naive approach" that generates all possible ...
4
votes
1
answer
155
views
Algorithm to find the number of specific continuous substrings
I am trying to solve the following algorithmic problem:
Every day, Bob goes to work and does one of 26 possible tasks. The tasks are coded with the letters of the English alphabet from a to z. ...
0
votes
0
answers
46
views
Feasibility of using product DFA to show two sets are disjoint
I have two finite sets of strings S_1, S_2 that are described by DFAs (e.g. these sets are regular languages, but I want to emphasize the fact that they are finite of known maximum length).
I need to ...
2
votes
0
answers
115
views
Finding the minimum cost at each point in time from a set of weighted intervals
Let’s say we have a list of intervals, each of which has a cost associated with it. Assume that there is always at least one interval active.
The solution would be a timeline of non overlapping ...
1
vote
1
answer
128
views
Isn't every algorithm technically ω(0)?
Isn't every function is ω(0)?
From my understanding, ω (little omega) notation is used as a strict lower bound for any function. Since no function can run in less than 0 "time" then ω(0) ...
0
votes
0
answers
73
views
How can I reduce the complexity of my validations in python
I am trying to lower the complexity of my code and I use Scrutinizer to measure those values. In my code, most of the complexity is due to validations, so how can I improve them?
class gestionRegister:...
-1
votes
1
answer
158
views
Smooth GPS tracking with google maps
I am working on a vehicle tracking application, and im trying to get the moving animation of the car smooth, with no "jumps" and a continous movement of the map with the current location.
...
0
votes
2
answers
78
views
How to write a unit test to determine if an operation takes about linear time?
I am writing unit tests in JUnit 5 for a Java codebase, though I'm happy to take examples in any language.
I have the ability to count the exact number of times certain operations occur. For the sake ...
-4
votes
1
answer
232
views
Time Complexity of Backtracking solution - Leetcode 473. Matchsticks to Square [duplicate]
The following is a Leetcode problem: 473. Matchsticks to Square (https://leetcode.com/problems/matchsticks-to-square/description/)
Problem Statement
You have an array, matchsticks, of size n, with ...
1
vote
2
answers
257
views
Can we get a better complexity than O(n) for cumulative sum while using multithreading?
I'm new to multithreading algorithms and I'm trying do recode cumulative sum function to get a better complexity than O(n).
Have you any hint? Or we can't get better than O(n)?
I tried to use divide ...
-1
votes
1
answer
97
views
Complexity of this function
I would like to know how to properly determinate the complexity of this function :
let rec fact n =
match n with
| 0 -> 1
| n -> n * fact (n - 1)
let biggest_factorial_below n =
let i = ...
2
votes
2
answers
89
views
Want to know time complexity for rec Function
I tried to solve leetcode problem Select Cells in Grid With Maximum Score.
You are given a 2D matrix grid consisting of positive integers.
You have to select one or more cells from the matrix such ...
1
vote
1
answer
63
views
Determine the asymptotic complexity of a function
This is a homework problem I have. I could not find whether it was against the rules or not to post such a question. I cannot figure this one out. I would appreciate help in understanding why the ...
-4
votes
1
answer
107
views
Why is my time computation not the same evolution curve as the complexity? [closed]
Lets say I have a certain number of schools and a certain number of students.
For each student i want to compute a new grade from their old grade using a random coefficient.
I use as much arrays as I ...
-2
votes
1
answer
149
views
Time Complexities n(log(n)) and log(n^n)
I was studying Time Complexities in my DSA lecture when this doubt popped in my mind. So firstly, is O(nlog(n)) = O(log(n^n))?
If yes, what type of Time Complexity does the O(log(n^n)) belong to? Is ...
0
votes
0
answers
60
views
How to optimize the function below so that it might run under 1 second
Below is a code that makes a network where each node of the network is a Stuart-Landau oscillator. The equations of evolution are coupled and also depend on it's neighbors. The code runs 10^6 ...
1
vote
1
answer
66
views
Spanning disjoint trees in directed bipartite graphs
Assume we have a directed bipartite graph G with two partitions, A and B. All the edges are assumed to start from A and end in B . Assume that every vertex has at least one adjacent edge. I want to ...
1
vote
2
answers
126
views
calculating Time complexity of a function
Need help on how to calculate this time complexity
int func(int n) {
int a = 0;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= n - i; ++j) {
for (int k = 0; k &...
4
votes
1
answer
303
views
Radix sort slower than expected compared to standard sort
I've implemented two versions of radix sort (the version that allows sorting integers whose values go up to n² where n is the size of the list to sort) in Python for benchmarking against the standard ...
-1
votes
1
answer
160
views
is it true that the complexity of the function is O((log(log(n))^2)?
int func2(int arr[], int n) {
int i = 0, j, counter = 0;
MergeSort(arr, n / 6); // 1
while (i < n / 6) // 2
{
for (j = 6; j < n / 6; j *= j) // 3
counter++;
...
1
vote
2
answers
87
views
Is a loop i<n has the same complexity as i<100?
I am trying to understnad if the following codes have the same complexity O(n)?:
for(int i=0;i<n;i++)
for(int i=0;i<100000;i++)
i would be happy to hear an explanation too, thank you !
I think ...
0
votes
1
answer
84
views
How to figure out time complexity of complex programs
For example:
#include <iostream>
using namespace std;
int main() {
int n, s = 0, i, j, k;
cin >> n;
for (i = 1; i <= n * n; i++) {
for (j = 1; j <= i / 2; j++) ...
0
votes
0
answers
95
views
Is there any difference between the terms asymptotic analysis and complexity analysis?
I've read some articles about algorithms analysis and sometimes they seem to use two terms interchangeably and sometimes there seems to be a subtle difference. So I'd like to know about it explicitly, ...
0
votes
1
answer
64
views
Complexity of the linear search of the several elements in the list
I'm doing a comparison between brute force search and my custom search approach.
A problem arose in estimating the complexity of a brute-force search. As part of the search, I need to check whether ...
1
vote
1
answer
85
views
Number of routes of Königsberg's Bridges problem in Christopher Moore's "The Nature of Computation" [closed]
This is supposed to be a very simple question.
In the Prologue of Christopher Moore's book "The Nature of Computation" (2011), page 3, the number of possible routes of the Königsberg problem ...
1
vote
0
answers
65
views
What is the time complexity of a function f(n) that is O(g(n)) where "part" of g(n) is o(h(n))?
This is in the context of graph algorithms, so I'll refer to V as the set of vertices in the graph and E as the set of edges in the graph (i.e., the input is a graph G = (V, E)). Let's say a graph ...
0
votes
0
answers
64
views
Constructing an algorithm to find the smallest log(log(n)) elements from an array of length n
I am a math student and I have been asked to help grade answer sheets for an Algorithms course as one of my professors is unwell. While I am quite familiar with combinatorial algorithms, I am not well ...
0
votes
1
answer
319
views
How to choose base case for substitution method for solving recurrences?
I tried to find out how to solve recurrences using substitution method, but I don't understand: how is the base case chosen when the base case is not given explicitly?
I have two problems:
T(n) = 2T(...
0
votes
3
answers
201
views
What is the exact time complexity of this algorithm?
I attempted to solve the problem at this HackerRank link (https://www.hackerrank.com/challenges/diagonal-difference/problem?isFullScreen=true) Given a square matrix, calculate the absolute difference ...
1
vote
0
answers
255
views
When is a Range Query Sparse Table useful compared to a Range Query Segment Tree?
Let's look at the complexities of the <preprocess, query, update an element> operations. Let's denote n the size of the initial array and q the number of queries.
Sparse table <O(nlogn) , O(...
0
votes
2
answers
165
views
Find some better algorithm for finding the smallest difference between the elements of an array
Input: Array of integers A[0..n – 1]
Output: Smallest difference between two elements of a dmin array
dmin = ∞
for i = 0 to n − 1 do
for j = 0 to n − 1 do
if i != j &&|A[i] − A[j]| &...
0
votes
1
answer
61
views
Given partially sorted array of type x<y => first apperance of x comes before first of y, sort in average O(n)
i was given an assignment to find an algorithm that takes an array A such that for every x<y we have the first appearance of x coming before the first appearance of y
and sorts it in average time ...
0
votes
1
answer
97
views
How to find big o of dependent loops and recursive functions?
I want to be able to measure any code snippet time complexity. Is there a general rule or step by step approach to measure any big o (besides dominant term, removing constants and factors)? What math ...
-1
votes
1
answer
38
views
How to determine the time complexity of a recursive function that has a loop enclosed in it?
I am trying to solve LeetCode problem 143. Reorder List:
You are given the head of a singly linked-list. The list can be represented as:
L0 → L1 → … → Ln − 1 → Ln
Reorder the list to be on the ...
1
vote
1
answer
59
views
Does the square root of an input lie in the middle of that input?
I started reading the Competitive Programmers Handbook, and on page 20 the author writes about varying algorithmic complexity classes. When covering √𝑛, he writes:
O(√𝑛) A square root algorithm is ...
1
vote
1
answer
35
views
Hash Table creation runtime complexity
An insert into a hash table has a worst case complexity of O(n).
But when creating a new hash table and inserting n elements, as far as I understand this would result in n insertions into a growing ...
0
votes
0
answers
34
views
Optimization - Algorithm for finding load set combination that returns the maximum Von Mises stress
I recently stumbled upon an optimization problem while working on structural analysis. While I'm confident in my MATLAB skills as an aerospace engineer, I must admit my theoretical knowledge in ...
0
votes
0
answers
58
views
Time complexity of a divide and conquer algorithm that searches for a value in an n x m table, rows and columns are sorted
I am confused about the time complexity of this algorithm because I am thinking it could be something like T(n) = 3T(n/2) ... but since the array has a size of nxm instead of nxn and I am using ...
4
votes
1
answer
132
views
Big O notation of string permutation in Python
def permutation(str): #str = string input
if len(str) == 0:
return [""]
result = []
for i, char in enumerate(str):
for p in permutation(str[:i] + str[i + 1 :]...
1
vote
1
answer
77
views
finding the time complexity of the program
How do we find the time complexity of below program? For the below program should the time complexity be O(N) or O(N*M) or O(N*M*M)?
Take-1: O(N)
to scan N elements in the input array
Take-2: O(N*M)
...
-4
votes
1
answer
289
views
how do i find the time complexity (big O) of this triple loop?
for (int i = 0; i < n^2; i++) {
for (int j = 1; j < i; j = 2j) {
for (int k = 0; k < j; k++) {
System.out.println("x");
}
}
}
My thoughts are that the outer loop ...
0
votes
2
answers
121
views
determine the big o running time of the method
Static void doIt (int n ) {
int i; // 1 operations
int j; ← (2 x n) // 1 operations
while loop (j > 0) { // n operations
I; ← n // (n+1) operations
...
0
votes
1
answer
75
views
How to implement the Sosic and Gu linear algorithm for the n-queens problem
I'm tryng to implement Sosic and Gu algorithm for the n-queens problem-which presents a phase of initialization called initial_search().
The algorithm starts by assigning queens to random positions on ...