Java data structures - Understanding Complexity

Posted by biznickman on Sun, 16 Jan 2022 13:54:41 +0100

catalogue

I Algorithm efficiency

II Time complexity

1. Concept of time complexity

2. Progressive representation of large O

3. Case analysis and calculation

III Spatial complexity

1. Concept of spatial complexity

2. Case analysis and calculation

IV Write at the end 

I Algorithm efficiency

There are two kinds of algorithm efficiency analysis: time efficiency and space efficiency. Among them, time efficiency is called time complexity, and space efficiency is called space complexity.

Time complexity mainly measures the running speed of an algorithm, while space complexity mainly measures the amount required by an algorithm
Outer space

Because early computers had little storage capacity, they usually wasted time in exchange for space. With the development of high-speed computers, it is usually a waste of time

II Time complexity

1. Concept of time complexity

In computer science, the time complexity of an algorithm is a function, which quantitatively describes the running time of the algorithm. But in theory, the time spent in the execution of an algorithm cannot be calculated. You can only know when you put your program on the machine and run it. The computer test of all algorithms is obviously unrealistic. Therefore, there is an analysis method of time complexity: the execution times of basic operations in the algorithm is the time complexity of the algorithm.

Some algorithms have the worst, best and average time complexity. Generally speaking, in practice, the time complexity we seek refers to the worst case

Worst case: maximum number of runs of any input scale (upper bound)
Average case: expected number of runs of any input scale
Best case: minimum number of runs of any input scale (lower bound)

2. Progressive representation of large O

In fact, when we calculate the time complexity, we do not have to calculate the exact execution times, but only the approximate execution times. Here, we use the asymptotic representation of big O

Next, let's look at the basic principles for deriving large O-order

1. Replace all addition constants in the run time with constant 1.
2. In the modified run times function, only the highest order term is retained.
3. If the highest order term exists and is not 1, the constant multiplied by this item is removed. The result is large O-order

3. Case analysis and calculation

//Calculating the time complexity of bubbleSort
void bubbleSort(int[] array) {
        for (int end = array.length; end > 0; end--) {
            boolean sorted = true;
            for (int i = 1; i < end; i++) {
                if (array[i - 1] > array[i]) {
                    Swap(array, i - 1, i);
                    sorted = false;
                }
            }
            if (sorted == true) {
                break;
            }
        }
    }

The algorithm executes (N*(N-1))/2 times in the worst case. By deriving the large O-order method, it can be concluded that its time complexity is O(N)

// Calculate the time complexity of binarySearch
    int binarySearch(int[] array, int value) {
        int begin = 0;
        int end = array.length - 1;
        while (begin <= end) {
            int mid = begin + ((end-begin) / 2);
            if (array[mid] < value)
                begin = mid + 1;
            else if (array[mid] > value)
                end = mid - 1;
            else
                return mid;
        }
        return -1;
    }

The algorithm executes O(logN) times in the worst case, and the time complexity is O(logN). ps: in algorithm analysis, the base number is 2 and the logarithm is N. Some places will be written as lgN. The algorithm is binary search, which can be understood by drawing. The expression is: N/2^X=1, and X is the number of operations performed

// The time complexity of computing factorial recursive factorial?
    long factorial(int N) {
        return N < 2 ? N : factorial(N-1) * N;
    }

The basic operation recurses N times, and the time complexity is O(N)

// Computing the time complexity of fibonacci recursive fibonacci
    int fibonacci(int N) {
        return N < 2 ? N : fibonacci(N-1)+fibonacci(N-2);
    }

The basic operation recurses 2^N times, and the time complexity is O(2^N)

III Spatial complexity

1. Concept of spatial complexity

Space complexity is a measure of the amount of storage space temporarily occupied by an algorithm during operation. Space complexity is not how many bytes the program occupies, but the number of variables. The calculation rules of spatial complexity are basically similar to that of time complexity, and the large O asymptotic representation is also used

2. Case analysis and calculation

// Calculating the spatial complexity of bubbleSort
    void bubbleSort(int[] array) {
        for (int end = array.length; end > 0; end--) {
            boolean sorted = true;
            for (int i = 1; i < end; i++) {
                if (array[i - 1] > array[i]) {
                    Swap(array, i - 1, i);
                    sorted = false;
                }
            }
            if (sorted == true) {
                break;
            }
        }
    }

The algorithm uses two additional spaces, so the space complexity is O(1)

// Calculating the spatial complexity of fibonacci
    int[] fibonacci(int n) {
        long[] fibArray = new long[n + 1];
        fibArray[0] = 0;
        fibArray[1] = 1;
        for (int i = 2; i <= n ; i++) {
            fibArray[i] = fibArray[i - 1] + fibArray [i - 2];
        }
        return fibArray;
    }

The algorithm dynamically opens up N spaces, and the space complexity is O(N)

// Time complexity of computing Factorial recursive Factorial
    long factorial(int N) {
        return N < 2 ? N : factorial(N-1)*N;
    }

The algorithm recursively calls N times and opens up N stack frames. Each stack frame uses a constant space. Space complexity is O(N)

IV Write at the end

Due to the large number of professional courses at the end of the term and the lack of self-control, it took some time to break. The winter vacation decided to reform and make up all the broken blog posts. Bloggers may make some mistakes in the process of blogging. Please correct them and communicate with bloggers in the comment area or private letter for any questions. I hope you can gain something in winter vacation and roll it up together!

Topics: Java data structure