I have a quite simple problem, however I am a bit unsure of what the actual runtime (e.g. Big-O) is of this.

The program looks like this.

```
n <- user input
for i=1 to n
foo(i)
foo a:
for i=1 to a
foo2()
```

foo2 does an near constant amount of work which is not going to be significant.

What is the Big-O of this?

For each integer 0 <= i <= n, there's another loop which goes 0 <= j <= i.

So, the i-th integer requires i calls to `foo2()`

.

Over n integers, this adds up to an average of (n / 2) extra calls to `foo2()`

per integer -

`n + (n - 1) + ... + 1 + 0`

is the same as

`n + (n - 1 + 1) + ... + (n - n/2 + n/2)`

, or

`n * (n / 2)`

.

Since the upper bound on complexity is `n^2 / 2,`

its growth will happen as quickly as `n^2`

- so the complexity is `O(n^2)`

.

(Assuming the inner function `foo2()`

is Θ(1))

It's Θ(n^2), because the outer loop is executed once for all `i=1...n`

with the inner loop being iterated `i`

times per outer loop iteration, that makes in total `sum(i) from i=1 to n`

executions of `foo2()`

, which is equal to `(n+1)*n/2 = 1/2*n^2+1/2*n`

times.

Θ(n^2) implies O(n^2).

Similar Questions

Does anyone know good tutorials to change PBEWithMD5AndDES encryption algorithm to AES for a Java application? Specially , I want to know what precautions I should take while changing this algorithm t

A, B, C,…. Z, AA, AB, ….AZ, BA,BB,…. , ZZ,AAA, …., write a function that takes a integer n and returns the string presentation. Can somebody tell me the algorithm to find the nth value in the series?

I need to put together a Recommender Algorithm for a website. I have come up with a very simple method of achieving this but was wondering if anyone could point me towards any literature or such that

I'm having a difficult time understanding how the inflate algorithm works even after reading the RFC and reviewing c and javascript implementations. I compressed a file with the text TestingTesting

What good algorithm can I use to outline an image? currently I go pixel by pixel, if its transparent and there is a pixel around it that is not transparent, it is set to black. This works well for ali

I am having issues fully understanding how to prove some of the following statements. For instance I have a statement: n^2logn = O(n^2). Correct me if I am wrong, but this states that n^2 is bigO of n

There are quite a few algorithm lectures on the web, but what I'm looking for is a video tutorial where I can watch somebody coding on screen, going from a blank document to an algorithm that solves a

i have a question about Porter Stemmer Algorithm, I researched on the internet, but i couldn't find what the difference between understemming and overstemming. and is the Porter Algorithm understemm

I've made some researches but I didn't find a good article. I am adding multiple Vectors to one Vector, after that I am printing it: Iterator it =vector.iterator(); while(it.hasNext()){ System.out.pri

What's a good algorithm for suggesting things that someone might like based on their previous choices? (e.g. as popularised by Amazon to suggest books, and used in services like iRate Radio or YAPE wh

I'm trying to find an efficient, numerically stable algorithm to calculate a rolling variance (for instance, a variance over a 20-period rolling window). I'm aware of the Welford algorithm that effici

This question already has an answer here: Best Fit Scheduling Algorithm 4 answers I have the following problem to solve, perhaps you could give me some ideas regarding the problem - solving app

Is there any description of a diff/patch format visualization algorithm? (or maybe it is too trivial to be described)

I want to know whether any alternatives to Earliest Deadline First (EDF) scheduling algorithm are available. If yes, please provide the reference links. Thanks.

I have an algorithm that works with extremely large numbers around the order of 2 raised to the power 4,500,000. I use the BigInteger class in .NET 4 to handle these numbers. The algorithm is very sim

Given is: An array of length N. The array contains integers. The integers are not necessarily sorted. Find an algorithm that: Returns (a close approximation of)the K-th smallest array element. Has

Do you know a good encryption algorithm that isn't too difficult to implement in c++ (but neither too easy)?

I'm implementing the NSGA II genetic algorithm to develop a set of timetables for my college. I am having problems with variation of solutions. My algorithm works fine as in initialization, mutation a

I've implemented file compression using huffman's algorithm, but the problem I have is that to enable decompression of the compressed file, the coding tree used, or the codes itself should be written

Is there any papers that describe the algorithm of the FindCornerSubPix function in openCV? I cannot find any documentation which describes it.

I want to publish server-messages on Twitter, for our clients. Unfortunately, Twitter only allows posting 140 Chars or less. This is a shame. Now, I have to write an algorithm that concatenates the di

Ok, this may seem trivial to some, but I'm stuck. Here's the algorithm I'm supposed to use: Here’s a recursive algorithm. Suppose we have n integers in a non-increasing sequence, of which the first i

anyone knows how to write a function in matlab to segment the cells and compute the average cell area using the watershed algorithm? any help would be much appreciated. Thank you! Here is an image of

Originally this post requested an inverse sheep-and-goats operation, but I realized that it was more than I really needed, so I edited the title, because I only need an expand-right algorithm, which i

I'm trying to turn Apriori algorithm to Eclat algorithm. My Apriori algorithm executes transactions in vertical items in horizontal format and returns n-th frequent itemset. Eclat algorithm i require

I do understand that a complete algorithm is one where if there is a solution, the algorithm is able to find it and that optimal algorithm is one where it manages to find a least cost solution. But i

How should I test my algorithm in terms of speed? The enhanced algorithm I made and the original algorithm search the same depth and they both give the same move, they only differ in terms of speed. D

I want to implement a maze solving algorithm in Prolog. Therefore i searched for some maze solving algorithms and found the following: http://www.cs.bu.edu/teaching/alg/maze/ FIND-PATH(x, y): if (x,y

I have to build a code for algorithm. What I have to achieve with my algorithm is the following: I have three numbers, 4,6,8. I have to generate sums with two of those numbers and then all sums with t

This algorithm looks through a string and tries to find another string. The logic is simple, I guess. Though, I need help finding it's complexity. int find(string mString, string lookUp) { int i, z, j

I am currenlty looking for a simple and lightweight algorithm to compare two simple strings. For example, if we take those two strings : The quick brown fox jumps over the lazy dog The plick brown

I am trying to understand the closest pair algorithm. I understand about dividing the set in half. But I am having trouble understanding how to recursively compute the closest pair. I understand recur

I hear the term algorithm use often and have been confused by the context I am seeing it in on this site sometimes so I thought I would try and clear up my understanding. To me an algorithm is some f

Can anyone suggest an algorithm for image comparison?

Is there is a way to reduce the length of a string using String encoding algorithm. Unfortunately Huffman coding is not a solution for my case. I am searching for a coding algorithm which takes a s

I have to make some statistics for my application, so I need an algorithm with a performance as best as possible. I have some several question. I have a data structure like this in the mysql database:

Hi i am trying to calculate the complexity of the following algorithm private static List<int> GetIndexes(string strippedText, string searchText) { List<int> count = new List<int>();

I was reading about dijkstra algorithm and A* star algorithm. I know that the difference is the heuristic used. But what is a heuristic and how this influence the algorithms? Heuristic is just an way

I've notised that Stack Overflow only presents me with a captcha occasionally. Does anyone know if it has a good algorithm for sensing if I might be a robot or, instead if it's just random. Reducing

I am trying to connect to remote sftp server over ssh with JSch (0.1.44-1) but during session.connect(); I am getting this exception: com.jcraft.jsch.JSchException: Algorithm negotiation fail at co

I read this article Retiring a Great Interview Problem, the author came up with a work break problem and gave three solutions. The efficient one uses memoization algorithm and the author said its wors

I was wondering what the complexity of a graph search algorithm would be for determining a checkmate in chess in Big O notation.

I am having some trouble translating a pseudocode for Horner's algorithm into a proper code in MatLab. I think my confusion stems from the fact that the code assumes that the first vector entry can be

I'm working on an application that needs an algorithm for data synchronization to be implemented. We'd be having a main server , and multiple subordinate devices , which would need to be synced togeth

Our teacher asked us a question which goes like this. Given a list of n distinct integers and a sequence of n boxes with preset inequality signs inserted between them, design an algorithm that places

I am having some problems understanding the DPLL algorithm and I was wondering if anyone could explain it to me because I think my understanding is incorrect. The way I understand it is, I take some s

I'm making a JSON parser and I am looking for an algorithm that can find all of the matching brackets ([]) and braces ({}) and put them into a table with the positions of the pair. Examples of returne

I'm working on coding the Pohlig-Hellman Algorithm but I am having problem understand the steps in the algorithm based on the definition of the algorithm. Going by the Wiki of the algorithm: http://en

I wanted to implement the game Pacman. For the AI, I was thinking of using the A* algorithm, having seen it on numerous forums. However, I implemented the Breadth First Search for some simple pathfind

I am working on Data mining project and I've chosen Apriori algorithm for Association rules task. Briefly saying I am not satisfied with execution time the way I've implemented it. I will describe jus