Algorithm
- sequence of computational steps that transform the input into the output.
- tool for solving a well specified computational problem
- describes a computational process for achieving the desired input/output relationship
Sorting Problem
- sort a sequence of numbers into non decreasing order.
- Input: A sequence of numbers <a1, a2, a3… an>
- Output: A permutation reordering <a1,, a2,,… an,>
Instance of a problem
- Consists of the input (following whatever constraints are imposed in the problem
statement) needed to compute a solution to the problem.
Correct Algorithm
- Halts at the correct output for all instances.
- Solves the given computational problem
Incorrect Algorithms
- Can be useful if we can control their error rate
Data Structures
- A way to store and organise data in order to facilitate access and modifications
NP complete
- No efficient algorithm for an NP- Complete problem has ever been found.
- Nobody has proven that an efficient algorithm for an NP- Complete problem cannot
exist.
- If an efficient algorithm exists for one of them then it exists for all of them
- Several of these are similar but not identical to problems we do know of efficient
algorithms.
- If you can show that a problem is NP- complete then you can use your time wisely to
make a good enough solution.
- An example is the “travelling salesman problem”: A delivery company has a central
depot from where trucks go out every day to deliver goods. They must come back to
the depot in the night. Company wants to select an order of delivery locations so that
the distance travelled by each truck is equal.
Parallelism
- Power density increases superlinearly with clock speed. This causes a limitation on the
maximum achievable clock speed.
- To perform more operations per second chips are designed to contain several cores.
- Multicore computers can be considered similar to several sequential computers on a
single chip – basically a parallel computer.
Algorithms as technology
- Computing time and memory are bounded resources.
- Insertion sort takes approximately c1n2 time to sort n items where c is a constant that
does not depend on n. Merge sort takes time roughly equal to c 2log2n. Insertion sort
usually has smaller time constant than merge sort. But constant factors have far less
impact than input size n.
Insertion sort time = c1n. n
Merge sort time = c2.n. lg n (where lg n is the same as log2n)
Log2n <<< n
For small input sizes insertion sort works faster but for larger input sizes merge sort
works much faster.
Chapter 2
Insertion sort
- Numbers that we wish to sort are called the keys.
- Input is conceptually a sequence but works as an array of n elements.
- In pseudocode we use whatever language is most clear and concise to specify a given
algorithm.
- Data abstraction, error handling and modularity are often ignored in pseudo code.
- Insertion sort works the way many people sort a hand of playing cards. We start with an
empty left hand and the cards face down on the table. We then remove one card at a
time from the table and insert it into the correct position in the left hand. To find the
correct position for a card, we compare it with each of the cards already in the hand,
from right to left.