2. Analysis of Algorithms Flashcards

1
Q

The very same approach that scientists use to understand the natural world is effective for studying the running time of programs:

A

!Observe some feature of the natural world, generally with precise measurements.

!Hypothesize a model that is consistent with the observations.

!Predict events using the hypothesis.

!Verify the predictions by making further observations.

!Validate by repeating until the hypothesis and observations agree.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The experiments we design must be ___ and the hypotheses that we formulate must be _____.

A

The experiments we design must be REPRODUCIBLE and the hypotheses that we formulate must be FALSIFIABLE.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Mathematical models.

A

The total running time of a program is determined by two primary factors: the cost of executing each statement and the frequency of execution of each statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Tilde approximations

A

We use tilde approximations, where we throw away low-order terms that complicate formulas. We write ~ f(N) to represent any function that when divided by f(N) approaches 1 as N grows. We write g(N) ~ f(N) to indicate that g(N) / f(N) approaches 1 as N grows.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

function:
(n^3/6)-(n^2/2)+(n/3)
lgN+1
3
tilde approximation:

A

tilde approximation:
n^3/6
lgN
3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Order-of-growth classifications.

A

very often the order of growth of the cost is one of just a few functions of the problem size N.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

function:
(n^3/6)-(n^2/2)+(n/3)
lgN+1
3
order of growth:

A

order of growth:
n^3
lgN
1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Cost model

A

We focus attention on properties of algorithms by articulating(формулируя) a cost model that defines the basic operations. For example, an appropriate cost model for the 3-sum problem is the number of times we access an array entry, for read or write.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

The order of growth of the running time of:

public class ThreeSum {
private ThreeSum() { }

public static void printAll(int[] a) {
    int n = a.length;
    for (int i = 0; i < n; i++) {
        for (int j = i+1; j < n; j++) {
            for (int k = j+1; k < n; k++) {
                if (a[i] + a[j] + a[k] == 0) {
                    StdOut.println(a[i] + " " + a[j] + " " + a[k]);
                }
            }
        }
    }
}

public static int count(int[] a) {
    int n = a.length;
    int count = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i+1; j < n; j++) {
            for (int k = j+1; k < n; k++) {
                if (a[i] + a[j] + a[k] == 0) {
                    count++;
                }
            }
        }
    }
    return count;
}

public static void main(String[] args)  {
    In in = new In(args[0]);
    int[] a = in.readAllInts();

    Stopwatch timer = new Stopwatch();
    int count = count(a);
    StdOut.println("elapsed time = " + timer.elapsedTime());
    StdOut.println(count);
}
A

is N^3.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

The brute-force 3-sum algorithm uses ~ ____ array accesses to compute the number of triples that sum to 0 among N numbers.

A

N^3 / 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Input models.

A

We can carefully model the kind of input to be processed. This approach is challenging because the model may be unrealistic

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Worst-case performance guarantees.

A

Running time of a program is less than a certain bound (as a function of the input size), no matter what the input. Such a conservative approach might be appropriate for the software that runs a nuclear reactor or a pacemaker or the brakes in your car.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Randomized algorithms.

A

One way to provide a performance guarantee is to introduce randomness, e.g., quicksort and hashing. Every time you run the algorithm, it will take a different amount of time. These guarantees are not absolute, but the chance that they are invalid is less than the chance your computer will be struck by lightning. Thus, such guarantees are as useful in practice as worst-case guarantees.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Amortized analysis.

A

For many applications, the algorithm input might be not just data, but the sequence of operations performed by the client. Amortized analysis provides a worst-case performance guarantee on a sequence of operations.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

In the linked-list implementation of Bag, Stack, and Queue, all operations take

A

constant time in the worst case.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

In the resizing-array implementation of Bag, Stack, and Queue, starting from an empty data structure, any sequence of N operations takes

A

time proportional to N in the worst case (amortized constant time per operation).

17
Q

Memory usage.

A

To estimate how much memory our program uses, we can count up the number of variables and weight them by the number of bytes according to their type.

18
Q

Objects Memory usage

A

To determine the memory usage of an object, we add the amount of memory used by each instance variable to the overhead associated with each object, typically 16 bytes. Moreover, the memory usage is typically padded to be a multiple of 8 bytes (on a 64-bit machine).

19
Q

A reference to an object typically is a

A

memory address and thus uses 8 bytes of memory (on a 64-bit machine).

20
Q

A nested non-static (inner) class such as our Node class requires an extra

A

8 bytes of overhead (for a reference to the enclosing instance).

21
Q

Arrays in Java are implemented as objects, typically with extra

A

overhead for the length. An array of primitive-type values typically requires 24 bytes of header information (16 bytes of object overhead, 4 bytes for the length, and 4 bytes of padding) plus the memory needed to store the values.

22
Q

A Java 7 string of length N typically uses

A

32 bytes (for the String object) plus 24 + 2N bytes (for the array that contains the characters) for a total of 56 + 2N bytes.

23
Q

Depending on context, we may or may not count the memory references by an

A

object (recursively). For example, we count the memory for the char[] array in the memory for a String object because this memory is allocated when the string is created. But, we would not ordinarily count the memory for the String objects in a StackOfStrings object because the String objects are created by the client.

24
Q

How do I increase the amount of memory and stack space that Java allocates?

A

You can increase the amount of memory allotted to Java by executing with java -Xmx200m Hello where 200m means 200 megabytes. The default setting is typically 64MB.
You can increase the amount of stack space allotted to Java by executing with java -Xss200k Hello where 200k means 200 kilobytes. The default setting is typically 128KB.
It’s possible to increase both the amount of memory and stack space by executing with java -Xmx200m -Xss200k Hello.

25
Q

What is the purpose of padding?

A

Padding makes all objects take space that is a mulitple of 8 bytes. This can waste some memory but it speeds up memory access and garbage collection.

26
Q

I get inconsistent timing information in my computational experiments. Any advice?

A

Be sure that you computation is consuming enough CPU cycles so that you can measure it accurately. Generally, 1 second to 1 minute is reasonable. If you are using huge amounts of memory, that could be the bottleneck. Consider turning off the HotSpot compiler, using java -Xint, to ensure a more uniform testing environment. The downside is that you are no long measuring exactly what you want to measure, i.e., actual running time.

27
Q

Does the linked-list implementation of a stack or queue really guarantee constant time per operation if we take into account garbage collection and other runtime processes?

A

Our analysis does not account for many system effects (such as caching, garbage collection, and just-in-time compilation)—in practice, such effects are important. In particular, the default Java garbage collector achieves only a constant amortized time per operation guarantee. However, there are real-time garbage collectors that guarantee constant time per operation in the worst case. Real time Java provides extensions to Java that provide worst-case performance guarantees for various runtime processes (such as garbage collection, class loading, Just-in-time compilation, and thread scheduling).