complexity-theory

Algorithm complexity: if/else under for loop

不问归期 提交于 2021-02-07 08:00:27
问题 I am wondering if in a situation like the following (an if/else statement under a for loop) the complexity would be O(n) or O(n^2): for character in string: if character==something: do something else: do something else. Thank you! 回答1: It will be O(n) if 'do something' and 'do something else' are O(1) O(n^2) if 'do something' and 'do something else' are O(n) Basically the complexity of the for loop will depend on the complexity of it components and the no. of loops. 回答2: It depends what you

Why is the complexity of BFS O(V+E) instead of O(V*E)?

半城伤御伤魂 提交于 2021-02-07 04:57:16
问题 Some pseudocode here (disregard my style) Starting from v1(enqueued): function BFS(queue Q) v2 = dequeue Q enqueue all unvisited connected nodes of v2 into Q BFS(Q) end // maybe minor problems here Since there are V vertices in the graph, and these V vertices are connected to E edges, and visiting getting connected nodes (equivalent to visiting connected edges) is in the inner loop (the outer loop is the recursion itself), it seems to me that the complexity should be O(V*E) rather than O(V+E)

MPI communication complexity

霸气de小男生 提交于 2021-02-07 03:59:37
问题 I'm studying the communication complexity of a parallel implementation of Quicksort in MPI and I've found something like this in a book: "A single process gathers p regular samples from each of the other p-1 processes. Since relatively few values are being passed, message latency is likely to be the dominant term of this step. Hence the communication complexity of the gather is O(log p)" (O is actually a theta and p is the number of processors). The same affirmation is made for the broadcast

MPI communication complexity

会有一股神秘感。 提交于 2021-02-07 03:57:14
问题 I'm studying the communication complexity of a parallel implementation of Quicksort in MPI and I've found something like this in a book: "A single process gathers p regular samples from each of the other p-1 processes. Since relatively few values are being passed, message latency is likely to be the dominant term of this step. Hence the communication complexity of the gather is O(log p)" (O is actually a theta and p is the number of processors). The same affirmation is made for the broadcast

What is the complexity of this function with nested loops?

别说谁变了你拦得住时间么 提交于 2021-02-05 09:36:25
问题 What is the complexity of this code? public class test5{ public static void main(String[] args) { int n = Integer.parseInt(args[0]); for (int i = 1; i<=n; i++) { for (int j = 1; j<=i; j++) { System.out.print ("*"); } System.out.println(); } for (int i = n; i>=1; i--) { for (int j = 1; j<=i; j++) { System.out.print ("*"); } System.out.println(); } } } My assumption is that it will take O(n^2) operations because n*(n/2) + n*(n/2). Am I right? 回答1: You are correct, a tight upper asymptotic bound

What is the complexity of this function with nested loops?

时光总嘲笑我的痴心妄想 提交于 2021-02-05 09:34:49
问题 What is the complexity of this code? public class test5{ public static void main(String[] args) { int n = Integer.parseInt(args[0]); for (int i = 1; i<=n; i++) { for (int j = 1; j<=i; j++) { System.out.print ("*"); } System.out.println(); } for (int i = n; i>=1; i--) { for (int j = 1; j<=i; j++) { System.out.print ("*"); } System.out.println(); } } } My assumption is that it will take O(n^2) operations because n*(n/2) + n*(n/2). Am I right? 回答1: You are correct, a tight upper asymptotic bound

Is this n-body problem O(n^2) or O(n log n)?

旧巷老猫 提交于 2021-02-04 21:33:20
问题 I'm writing an article on the n-body problem, and I'd like to be technically accurate. The code is here. And here are the comments and loops: /** * Given N bodies with mass, in a 3d space, calculate the forces of gravity to be applied to each body. * * This function is exported to JavaScript, so only takes/returns numbers and arrays. * For N bodies, pass and array of 4N values (x,y,z,mass) and expect a 3N array of forces (x,y,z) * Those forcess can be applied to the bodies mass to update the

Is this n-body problem O(n^2) or O(n log n)?

三世轮回 提交于 2021-02-04 21:33:18
问题 I'm writing an article on the n-body problem, and I'd like to be technically accurate. The code is here. And here are the comments and loops: /** * Given N bodies with mass, in a 3d space, calculate the forces of gravity to be applied to each body. * * This function is exported to JavaScript, so only takes/returns numbers and arrays. * For N bodies, pass and array of 4N values (x,y,z,mass) and expect a 3N array of forces (x,y,z) * Those forcess can be applied to the bodies mass to update the

Complexity Analysis: how to identify the “basic operation”?

风流意气都作罢 提交于 2021-01-28 05:25:44
问题 I am taking a class on complexity analysis and we try to determin basic operations of algorithms. We defined it as the following: A basic operation is one that best characterises the efficiency of the particular algorithm of interest For time analysis it is the operation that we expect to have the most influence on the algorithm’s total running time: - Key comparisons in a searching algorithm - Numeric multiplications in a matrix multiplication algorithm - Visits to nodes (or arcs) in a graph

Leibniz determinant formula complexity

时光总嘲笑我的痴心妄想 提交于 2021-01-28 03:03:11
问题 I wrote some code that calculates the determinant of a given nxn matrix using Leibniz formula for determinants. I am trying to figure out it's complexity in O-notation. I think it should be something like: O(n!) * O(n^2) + O(n) = O(n!*n^2) or O((n+2)!) Reasoning: I think O(n!) is the complexity of permutations. and O(n) the complexity of perm_parity, and O(n^2) is the multiplication of n items each iteration. this is my code: def determinant_leibnitz(self): assert self.dim()[0] == self.dim()