AppuntiFacili
Torna Indietro Segnala errore

Space and Time Computational Cost

✍️ Dennis Turco 🏷️ Informatica 📘 Algoritmi e strutture dati
Ultima modifica:
#programmazione #costo computazionale #costo spaziale #costo temporale #algoritmi #strutture dati

1. Space Computational Cost

1.1 Simple operations

To analyze the space computational cost of this code, consider the following:

for i = i : 3
    for j = j : 3
        1 + 1
        2 + 2
        % etc...
        26 + 26;
        27 + 27;
     end
end
  1. Space used by variables:

    • The variable ii and the variable jj are both integer variables used for loops. Their space is negligible compared to the rest of the operations.
  2. Space used by operations inside the loop:

    • The operations 1+1,2+2,...,26+26,27+271+1, 2+2, ..., 26+26, 27+27 are simple arithmetic operations that do not consume significant memory beyond what is required to execute the operation and store the temporary result.

      1+1
      2+2
      26+26
      27+27
    • There are no accumulation variables or data structures that grow with the number of iterations.

  3. Number of iterations:

    • The outer loop (on i) iterates from 1 to 3, so it has 3 iterations.
    • The inner loop (on j) iterates from 1 to 3, so it has 3 iterations for each iteration of the outer loop.
  • Therefore, in total there are 3×3=93 \times 3 = 9 iterations However, since each arithmetic operation is executed and the result is not stored in a persistent variable, the total space used is constant.
  • The space computational cost of the code is O(1)O(1). Even though the number of iterations is 9, the memory usage does not grow with the number of iterations, since no data structures or additional variables are allocated that depend on the number of iterations.

1.2 Complex operations

To evaluate the impact of more complex operations on space cost, we can consider the following scenarios:

  1. Simple arithmetic operations:

    • If the operations are still simple and the result is not stored in persistent variables, the space cost remains O(1)O(1). Even if the operations took more time, they would not significantly affect the memory used.
  2. Operations that create new objects:

    • If the operations inside the loop generate new objects or data that are stored in a data structure (such as an array or a list), then the space cost depends on the amount of memory needed to store these objects.
    • For example, if each iteration creates and stores a new object, the total memory used would increase with the number of iterations.
  • Suppose we have more complex operations like creating and storing strings or arrays:

    for i = 1 : 3
        for j = 1 : 3
            A = create_large_object();
        end
    end

    In this case, if create_large_object() creates and stores a significant-sized object in a data structure, then the space cost becomes proportional to the number of objects created.

  • If each create_large_object() call creates and stores an object requiring O(k)O(k) space, where kk is the size of the object, and there are 3×3=93 \times 3 = 9 iterations in total, then the total memory used would be 9×O(k)9 \times O(k).

  • In this case, the total space cost would be O(nk)O(n \cdot k), where nn is the number of iterations (9 in this example) and kk is the space needed for each object.

2. Time Computational Cost

To evaluate the time computational cost of the given code, we need to consider both the number of iterations and the complexity of the operations inside the loops. Here’s the step-by-step analysis:

for i = 1 : 3
    for j = 1 : 3
        % Multiple operations
    end
end
  1. Number of iterations

    • The outer loop for i = 1 : 3 has 3 iterations.
    • The inner loop for j = 1 : 3 has 3 iterations for each iteration of the outer loop.

    In total, the number of iterations is 3×3=93 \times 3 = 9.

  2. Operations inside the loops

    Suppose each inner loop contains kk operations. The time complexity of the operations inside the loop will depend on:

    • Type of operations: If the operations are simple (e.g., additions, subtractions), each has a complexity of O(1)O(1).
    • Number of operations: If there are kk operations in each iteration of the inner loop, the time complexity for each inner loop iteration will be O(k)O(k).
  3. Total complexity

    The total time complexity of the code is given by the sum of the complexities of all iterations. Since we have 9 iterations in total, and each iteration executes kk operations of complexity O(1)O(1), the total time complexity will be:

    O(number of iterations×complexity per iteration)=O(9×k)=O(k)O(\text{number of iterations} \times \text{complexity per iteration}) = O(9 \times k) = O(k)

  4. Conclusion

    The time computational cost of the code, considering that the operations inside the loop are simple arithmetic operations, is O(k)O(k), where kk is the number of operations executed in each inner loop iteration.

    If kk is a constant number, we can further simplify the complexity to O(1)O(1). However, if kk grows with the number of iterations or depends on other variables, the time computational cost will be O(n×k)O(n \times k), where nn is the total number of iterations (in this case, 9).

Prenota una lezione