What is the complexity of this function with nested loops? - time-complexity

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?

You are correct, a tight upper asymptotic bound for both the first and second nested loop blocks—say T_A(n) and T_B(n), respectively—is O(n^2), and hence the function as a whole runs as O(n^2), asymptotically.
You can analyze this in detail using Sigma notation to count the number of basic operations in the inner loop blocks for each of the nested loop blocks T_A(n) and T_B(n):
Where we've treated the System.out.print ("*"); operation as basic operation.

Related

time complexity of the following three loop dependent condition

Can anyone find me the big O notation of the following code?
sum=0;
for(i=0;i<n;i++)
{
for(int j=0;j<i*i;j++)
{
for(int k=0;k<j;k++)
sum++;
}
}

complexity of calling a function with a for loop

Im quite new to computational complexity, but i know that a nested for loop will give O(n^2). in my case i have a for loop that calls a function which has a for loop within it. will the complexity be O(n) or worse?
public static void main(String[] args) {
for(int i = 0; i < 10; i++){
if(i != 0){
System.out.println();
printt(i);
}
}
}
public static void printt(int i){
for(int j = 0; j <= 10; j++ ){
if(j !=0 ){
System.out.print(j*i+" ");
}
}
}
}
Think of the number of print statements(the second one itc) that are executed if you run this snippet of code.
The easiest way to reason about this is to go ahead and run the program, and you will notice that you have 81 values being printed out, which tells you that you have 9 calls to the nested function for each run of the outer loop (9 time again). So it ends up being O(n^2).

Big O runtime - indexOf LinkedList/ ArrayList

I have a question considering Big O runtime and the indexOf method within LinkedList, and ArrayList. How can I come up with a Big O runtime assumption and how would it be different in a Linked List as opposed to an Array List?
LinkedList indexOf()
public int indexOf(Object value)
{
int results = -1;
boolean done = false;
Node<E> ref = head.next;
for(int i = 0; i < size && !done; i++)
{
if(value.equals(ref.value))
{
results = i;
done = true;
}
ref = ref.next;
}
return results;
}
ArrayList indexOf()
if (o == null) {
for (int i = 0; i < size; i++)
if (Values[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(Values[i]))
return i;
}
return -1;
I apologize if this is a trivial question to some but I am going to need to understand how to come up with a Big O runtime of a method.
In both these implementations you have nothing better to do than go over the list one element at a time and compare it to the value you're looking for. At the worst case, you'd be going through the entire list, giving a complexity of O(n).

ArrayList Method Returns a null ArrayList, main program cannot access

I'm trying to create a basic function that calls on a method that creates the 2D ArrayList that will be used further in the main program to do things like calculate the row and column sums as well as print out the triangle.
However, after it runs the ArrayList returns null. What's going on?
Thanks,
public class Trib
{
private ArrayList<ArrayList<Integer>> triangle;
private int Asize;
public Trib (int size)
{
// convert the argument to type 'int' to be used in the program
Asize = size;
// create an ArrayList of ArrayLists, it will have 'size' number ArrayLists contained within
ArrayList<ArrayList<Integer>> triangle = new ArrayList<ArrayList<Integer>>(size);
// create the inner ArrayLists
for (int i = 0; i < size; i++)
{
// add to index 'i' of our ArrayList a new ArrayList of size (i+1)
triangle.add(new ArrayList<Integer>(i+1));
for (int j = 0; j <= i; j++)
{
if (j==0 || j == i)
{
triangle.get(i).add(1);
}
else
triangle.get(i).add(triangle.get(i-1).get(j-1)+triangle.get(i-1).get(j));
System.out.print(triangle.get(i).get(j) + " ");
}
System.out.println();
}
triangle.clone();
}
public void printTriangle()
{
System.out.print(triangle.get(1).get(1));
/*for (int i = 0; i < Asize; i++)
{
for (int j = 0; j <= i; j++)
{
System.out.print(triangle.get(1).get(1) + " ");
}
System.out.println();
}*/
}
/*public Trib()
{
this(5);
}*/
/*public int Psize()
{
return triangle.size();
}
public ArrayList<Integer> sumRows()
{
ArrayList<Integer> row_sum = new ArrayList<Integer>(Asize);
for (int i = 0; i < Asize; i++)
{
for (int j = 0; j < i; j++)
{
row_sum.add(triangle.get(i).get(j));
}
}
return row_sum;
}
public ArrayList<Integer> sumCols()
{
ArrayList<Integer> col_sum = new ArrayList<Integer>(Asize);
for (int i = 0; i < Asize; i++)
{
for (int j = 0; j < i; j++)
{
col_sum.add(triangle.get(i).get(i));
}
}
return col_sum;
}*/
public static void main(String[] args)
{
if(args.length < 1)
{
System.err.println("Sorry, this program needs an integer argument.");
System.exit(1);
}
Trib pt = new Trib(Integer.parseInt(args[0]));
pt.printTriangle();
//ArrayList<Object> sum_rows = new ArrayList<Object>(pt.Psize());
// sum_rows.add;
System.out.println("\nHere are the sum of rows:");
//for (int i = 0; i < pt.Psize(); i++)
//System.out.println(sum_rows.get(i));
//ArrayList<Integer> sum_cols = new ArrayList<Integer>(pt.Psize());
System.out.println("\nHere are the sum of columns:");
//for (int i = 0; i < pt.Psize(); i++)
//System.out.printf("%-5d", sum_cols.get(i));
}
}
Watch out what's what you are doing: Notice that you have TWO variables named "triangle": The first one is an instance variable and the second is a local variable, which is the only one you have initialized.
My suggestion to avoid this common mistake is to pre-pend "this." to any use of what you intend must be an instance variable. And, if in doubt, if you use a development environment as Eclipse, press CTRL and click on your variable to navigate to the point where it is declared.

CUDA optimization question

Here's a simple program:
void multiply(const int* v_in, const int* w_in, int n_v, int n_w, int* w_out)
{
for(int i=0; i<n_w; i++)
{
int sum=0;
for(int j=0; j<n_v; j++)
sum += (w_in[i]*v_in[j])>>1;
w_out[i]=sum;
}
}
Presume n_v, n_w ~10^6. Clearly, there's at least a dozen equivalent ways to do this in CUDA, with different ways to subdivide (n_v*n_w) operations into threads, with and without shared memory... Which way should, theoretically speaking, be the fastest?
simplest:
void multiply(const int* v_in, const int* w_in, int n_v, int n_w, int* w_out)
{
int *v = shared; // dynamic
for(int i = block.rank; i < n_w; i += block.size)
{
int w = w_in[i]; // coalesced
int sum=0;
for(int j=0; j<n_v; j += block.size) { // assumption
v[block.rank] = v_in[j+block.rank];
__synch();
for(int k = 0; k < block.size; ++k)
sum += (w*v[k])>>1; //
__synch(); // ouch
}
w_out[i] = sum; // ditto
}
}