0% found this document useful (0 votes)
11 views7 pages

Lec3 Algorithm Design24-25 Course2 Stage 2

The document discusses how to determine time complexities of code, explaining different types of statements such as sequences, if-then-else, for loops, and nested loops, along with their respective complexities. It also compares the efficiency of for loops versus recursion, stating that for loops are generally faster due to fewer function calls. Additionally, the document covers space complexity, memory usage in Java, and provides examples of memory calculations for different data types and objects.

Uploaded by

ixtx1674
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

Lec3 Algorithm Design24-25 Course2 Stage 2

The document discusses how to determine time complexities of code, explaining different types of statements such as sequences, if-then-else, for loops, and nested loops, along with their respective complexities. It also compares the efficiency of for loops versus recursion, stating that for loops are generally faster due to fewer function calls. Additionally, the document covers space complexity, memory usage in Java, and provides examples of memory calculations for different data types and objects.

Uploaded by

ixtx1674
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lecture 3 Algorithm Design and Analysis 2024-2025 Ass.Prof. Dr.

Nadia Fadhil

3.2 How to Determine time Complexities


Question: how can you determine the running time of a piece of code?
The answer is that it depends on what kinds of statements are used.
The following are four types of statements and their time complexities:
1. Sequence of statements
Statement 1;
Statement 2;
...
Statement k;
The total time is found by adding the times for all statements:
Total time = time (statement 1) + time (statement 2) + ... + time (statement k).
If each statement is "simple" (basic operations) then the time for each statement is constant
and the total time is also constant: O(1).

2- if-then-else statements
If (condition)
{Sequence of statements}
Else
{Sequence of statements}
Here, either sequence 1 will execute, or sequence 2 will execute. Therefore, the worst-case
time is the slowest of the two possibilities:
max (time (sequence 1), time (sequence 2)).
For example, if sequence 1 is O(N) and sequence 2 is O(1) the worst-case time for the whole
if-then-else statement would be O(N).

3- For loops
For (i = 0; i < N; i++)
{Sequence of statements (take O(1)) }
The loop executes N times, so the sequence of statements also executes N times.
11
Lecture 3 Algorithm Design and Analysis 2024-2025 Ass.Prof. Dr. Nadia Fadhil

We assume the statements are O(1), the total time for the for loop is N * O(1), which is O(N)
overall.

4- Nested loops
Loops where the number of iterations of the inner loop is independent of the value of the
outer loop's index.
For example:
For (i = 0; i < N; i++) {
For (j = 0; j < M; j++)
{Sequence of statements}}
The outer loop executes N times. Every time the outer loop executes, the inner loop executes
M times. As a result, the statements in the inner loop execute a total of N * M times.Thus,
the complexity is O(N * M).
In a special case where the stopping condition of the inner loop is j < N instead of j < M (i.e.,
the inner loop also executes N times).
For example, let's consider nested loops where the number of iterations of the inner loop
depends on the value of the outer loop's index.
For (i = 0; i < N; i++)
{
For (j = i+1; j < N; j++)
{Sequence of statements}
}
So the total complexity for the two loops is O(N2).

12
Lecture 3 Algorithm Design and Analysis 2024-2025 Ass.Prof. Dr. Nadia Fadhil

Question in time complexity, which is fastest the time required using recursion or time
required to execute for loop?
The answer:
The time required to execute a for loop is faster than the time required using recursion.
Recursion involves the function calling itself repeatedly until it reaches a base case, which can
result in a large number of function calls and stack frames. This can lead to a higher time
complexity, as well as potential issues with stack overflow and memory usage.
On the other hand, a for loop is a simple control structure that iterates over a range of values
and performs a set of operations. It typically involves fewer function calls and stack frames,
which can result in a lower time complexity and more efficient use of resources.

So the actual time complexity of a program depends on many factors, such as:
1. The specific algorithm.
2. The size of the input data.
3. Hardware and software environment in which the program is executed.
3.3 Common order-of-growth classifications

13
Lecture 3 Algorithm Design and Analysis 2024-2025 Ass.Prof. Dr. Nadia Fadhil

3.4 Space Complexity


The (space) complexity of a program (for a given input) is the number of elementary objects
that this program needs to store during its execution. This number is computed with respect to
the size n of the input data Space.
Complexity is measured by using polynomial amounts of memory, with an infinite amount of
time. The difference between space complexity and time complexity is that space can be reused.

3.4.1 Essentials of Data Space Measures


Bit: 0 or 1.
Byte: 8 bits.
Kilobyte (KB): 1000=103 and in binary =210 bytes.
Megabyte (MB): 1 million =106 and in binary= 220 bytes.
Gigabyte (GB): 1 billion=109 and in binary= 230 bytes.

Typical memory usage for primitive types and arrays in Java

14
Lecture 3 Algorithm Design and Analysis 2024-2025 Ass.Prof. Dr. Nadia Fadhil

Why char [ ] in java takes 2N+24 BYTE?

In Java, the size of an array is determined by the number of elements in the array and the size
of each element. For a char array, each element is a 2-byte Unicode character. Therefore, the
size of a char array in Java can be calculated using the following formula:

size of char array = 2 * N + 24 bytes where N is the number of elements in the array.

The extra 24 bytes are for the array header, which includes information about the array such as
its length, starting memory address and type.

The header is necessary to allow Java to dynamically allocate and deallocate arrays.

For example, if we have a char array with 10 elements, the size of the array in bytes would be:

size of char array = 2 * 10 + 24 bytes = 20 + 24 bytes = 44 bytes

Typical memory usage for objects in Java


Let's say we have an object with fields of various types. You need to sum up the memory usage
for each field along with the overhead. Here's a basic formula:
Memory Usage=Object Overhead+Memory Usage of Fields+Padding
Assuming the JVM is using 32-bit references, the memory usage of an instance of this object
would be approximately:
Object overhead: which is the memory required to manage that object. It include the Header
information like the object's class pointer.The overhead takes 16 bytes.
Reference: in Java, object references are essentially pointers that hold the memory address of
another object in the heap. This address allows Java to access the object or array indirectly. It
takes 8 bytes.

15
Lecture 3 Algorithm Design and Analysis 2024-2025 Ass.Prof. Dr. Nadia Fadhil

Padding (uses a multiple of 4 bytes): need to make sure that the total memory usage is aligned
to a multiple of 4 bytes.
For example, if the sum of the overhead and field sizes is 28 bytes, the padding needed would
be 0 bytes because 28 is already a multiple of 4. If it were 29 bytes, then the padding would be
3 bytes to make it 32 bytes (a multiple of 4).
Each object.
So the total memory usage for a data type value:
�Primitive type: 4 bytes for int, 8 bytes for double, …
�Object reference: 8 bytes.
�Array: 24 bytes + memory for each array entry.
�Object: 16 bytes + memory for each instance variable.
�Padding: round up to multiple of 4 bytes.

Example 1. How much a Date object uses memory usage?

So the memory usage for Date object =32 bytes of memory.

16
Lecture 3 Algorithm Design and Analysis 2024-2025 Ass.Prof. Dr. Nadia Fadhil

Example2: How much memory does WeightedQuickUnionUF use as a function of N ?

Example3: of the typical memory usage for an object with three instance variables:
public class Person {
private String name;
private int age;
private boolean isStudent;
// constructor and methods omitted
}
Assuming the JVM is using 32-bit references, the memory usage of an instance of this object
would be approximately:

 12 bytes of object header overhead (assuming a 32-bit JVM)


 4 bytes for the name reference
 4 bytes for the age integer
 1 byte for the isStudent boolean (rounded up to 4 bytes to align with the JVM's memory
allocation)
 4 bytes of padding to align the object size on an 8-byte boundary.
So the total memory usage for an instance of this object would be approximately 24 bytes.

17

You might also like