Algorithms Flashcards

1
Q

What is a program, and how does it differ from an algorithm?

A

An algorithm is an idea/solution/plan of how to perform a particular task. It is an unambigous step by step procedure for carrying out a task, which always terminates after a finite number of steps. it can be written in plain English.

While an algorithm is the idea behind a program, a program is the expression or implementation of that Idea in a programming language.

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

How are the contracts of routines expressed?

A

Through pre- and post conditions

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

Precondition

A

A precondition of a subroutine is something that must be true when the subroutine is called,
if the subroutine is to work correctly. For example, for the built-in function Math.sqrt(x), a
precondition is that the parameter, x, is greater than or equal to zero, since it is not possible
to take the square root of a negative number.

Preconditions most often give restrictions on the acceptable values of parameters, as in the
example of Math.sqrt(x). However, they can also refer to global variables that are used in the
subroutine. Or, if it only makes sense to call the subroutine at certain times, the precondition
might refer to the state that the program must be in when the subroutine is called.

It is the obligation of the user to ensure the preconditions are met.

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

Postcondition

A

It is something that will be true after the subroutine has run
(assuming that its preconditions were met—and that there are no bugs in the subroutine).
The postcondition of the function Math.sqrt() is that the square of the value that is returned
by this function is equal to the parameter that is provided when the subroutine is called.

It is the obligation of the routine to ensure that post conditions, such as return values, are correct.

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

What are the 3 criteria by which algorithms are measured?

A

Correctness, Robustness and Efficiency

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

Correctness

A

A program is correct if it accomplishes the task it was designed to perform

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

Robustness

A

A program is robust if it can handle illegal inputs and other unexpected situations in a reasonable way.

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

State

A

A state consists of all the information

relevant to the execution of a program at a given moment during its execution.

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

Process

A

A process is the sequence of states that the computer goes through as it executes the program.

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

Invariants

A

A loop invariant is, roughly, a statement that remains true as the loop is executed. More
precisely, we can show that a statement is an invariant for a loop if the following holds: As
long as the statement is true before the code inside the loop is executed, then it will also
be true after the code inside the loop has been executed. That is, a loop invariant is both a
precondition and a postcondition of the body of the loop.

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

Assertions

A

A statement that is either true or false (Boolean)

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

Exceptions

A

The word “exception” is meant to be more general than
“error.” It includes any circumstance that arises as the program is executed which is meant to
be treated as an exception to the normal flow of control of the program. An exception might
be an error, or it might just be a special case that you would rather not have clutter up your
elegant algorithm.

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

Efficiency

A

The term “efficiency” can refer to efficient use of almost any resource, including time,
computer memory, disk space, or network bandwidth.

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

What is the main technique used for the analysis of algorithms?

A

asymptotic analysis.

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

What does the word asymptotic mean?

A

“the tendency in the long run, as the size of the input is

increased.”

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

what does asymptotic analysis measure?

A

The analysis is asymptotic because it only
considers what happens to the run time as the size of the problem increases without limit; it
is not concerned with what happens for problems of small size or, in fact, for problems of any
fixed finite size. Only what happens in the long run, as the problem size increases without limit

17
Q

Difference between an algorithm and a heuristic

A

An “algorithm” is any set of rules for doing something. … A “heuristic” is an algorithm that does not guarantee a correct solution. A “good” heuristic is one that will get you either the correct, or a good enough solution most of the time

18
Q

Time efficiency

A

How fast an algorithim runs

19
Q

Space efficiency

A

How much memory an algorithm uses