Lecture Notes for
stringlengths
1
100
Unnamed: 1
stringclasses
7 values
Unnamed: 2
float64
Unnamed: 3
float64
11 Graphs 98
null
null
null
11.1 Graph terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
null
null
null
11.2 Implementing graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
null
null
null
11.3 Relations between graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
null
null
null
11.4 Planarity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
null
null
null
11.5 Traversals – systematically visiting all vertices. . . . . . . . . . . . . . . . . . . 104
null
null
null
11.6 Shortest paths – Dijkstra’s algorithm . . . . . . . . . . . . . . . . . . . . . . . . 105
null
null
null
11.7 Shortest paths – Floyd’s algorithm . . . . . . . . . . . . . . . . . . . . . . . . . 111
null
null
null
11.8 Minimal spanning trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
null
null
null
11.9 Travelling Salesmen and Vehicle Routing . . . . . . . . . . . . . . . . . . . . . . 117
null
null
null
12 Epilogue 118
null
null
null
A Some Useful Formulae 119
null
null
null
A.1 Binomial formulae . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
null
null
null
A.2 Powers and roots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
null
null
null
A.3 Logarithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
null
null
null
A.4 Sums . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
null
null
null
A.5 Fibonacci numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
null
null
null
4Chapter 1
null
null
null
Introduction
null
null
null
These lecture notes cover the key ideas involved in designing algorithms. We shall see how
null
null
null
theydependonthedesignofsuitabledata structures,andhowsomestructuresandalgorithms
null
null
null
are more efficient than others for the same task. We will concentrate on a few basic tasks,
null
null
null
such as storing, sorting and searching data, that underlie much of computer science, but the
null
null
null
techniques discussed will be applicable much more generally.
null
null
null
We will start by studying some key data structures, such as arrays, lists, queues, stacks
null
null
null
and trees, and then move on to explore their use in a range of different searching and sorting
null
null
null
algorithms. This leads on to the consideration of approaches for more efficient storage of
null
null
null
data in hash tables. Finally, we will look at graph based representations and cover the kinds
null
null
null
of algorithms needed to work efficiently with them. Throughout, we will investigate the
null
null
null
computational efficiency of the algorithms we develop, and gain intuitions about the pros and
null
null
null
cons of the various potential approaches for each task.
null
null
null
We will not restrict ourselves to implementing the various data structures and algorithms
null
null
null
in particular computer programming languages (e.g., Java, C, OCaml), but specify them in
null
null
null
simple pseudocode that can easily be implemented in any appropriate language.
null
null
null
1.1 Algorithms as opposed to programs
null
null
null
An algorithm for a particular task can be defined as “a finite sequence of instructions, each
null
null
null
of which has a clear meaning and can be performed with a finite amount of effort in a finite
null
null
null
length of time”. As such, an algorithm must be precise enough to be understood by human
null
null
null
beings. However, in order to be executed by a computer, we will generally need a program that
null
null
null
is written in a rigorous formal language; and since computers are quite inflexible compared
null
null
null
to the human mind, programs usually need to contain more details than algorithms. Here we
null
null
null
shall ignore most of those programming details and concentrate on the design of algorithms
null
null
null
rather than programs.
null
null
null
The task of implementing the discussed algorithms as computer programs is important,
null
null
null
of course, but these notes will concentrate on the theoretical aspects and leave the practical
null
null
null
programming aspects to be studied elsewhere. Having said that, we will often find it useful
null
null
null
to write down segments of actual programs in order to clarify and test certain theoretical
null
null
null
aspectsofalgorithmsandtheirdatastructures. Itisalsoworthbearinginmindthedistinction
null
null
null
betweendifferentprogrammingparadigms: ImperativeProgramming describescomputationin
null
null
null
terms of instructions that change the program/data state, whereas Declarative Programming
null
null
null
5specifies what the program should accomplish without describing how to do it. These notes
null
null
null
will primarily be concerned with developing algorithms that map easily onto the imperative
null
null
null
programming approach.
null
null
null
Algorithms can obviously be described in plain English, and we will sometimes do that.
null
null
null
However, for computer scientists it is usually easier and clearer to use something that comes
null
null
null
somewhere in between formatted English and computer program code, but is not runnable
null
null
null
because certain details are omitted. This is called pseudocode, which comes in a variety of
null
null
null
forms. Often these notes will present segments of pseudocode that are very similar to the
null
null
null
languages we are mainly interested in, namely the overlap of C and Java, with the advantage
null
null
null
that they can easily be inserted into runnable programs.
null
null
null
1.2 Fundamental questions about algorithms
null
null
null
Given an algorithm to solve a particular problem, we are naturally led to ask:
null
null
null
1. What is it supposed to do?
null
null
null
2. Does it really do what it is supposed to do?
null
null
null
3. How efficiently does it do it?
null
null
null
The technical terms normally used for these three aspects are:
null
null
null
1. Specification.
null
null
null
2. Verification.
null
null
null
3. Performance analysis.
null
null
null
The details of these three aspects will usually be rather problem dependent.
null
null
null
The specification should formalize the crucial details of the problem that the algorithm
null
null
null
is intended to solve. Sometimes that will be based on a particular representation of the
null
null
null
associated data, and sometimes it will be presented more abstractly. Typically, it will have to
null
null
null
specify how the inputs and outputs of the algorithm are related, though there is no general
null
null
null
requirement that the specification is complete or non-ambiguous.
null
null
null
For simple problems, it is often easy to see that a particular algorithm will always work,
null
null
null
i.e. that it satisfies its specification. However, for more complicated specifications and/or
null
null
null
algorithms, the fact that an algorithm satisfies its specification may not be obvious at all.
null
null
null
In this case, we need to spend some effort verifying whether the algorithm is indeed correct.
null
null
null
In general, testing on a few particular inputs can be enough to show that the algorithm is
null
null
null
incorrect. However, since the number of different potential inputs for most algorithms is
null
null
null
infinite in theory, and huge in practice, more than just testing on particular cases is needed
null
null
null
to be sure that the algorithm satisfies its specification. We need correctness proofs. Although
null
null
null
we will discuss proofs in these notes, and useful relevant ideas like invariants, we will usually
null
null
null
only do so in a rather informal manner (though, of course, we will attempt to be rigorous).
null
null
null
The reason is that we want to concentrate on the data structures and algorithms. Formal
null
null
null
verification techniques are complex and will normally be left till after the basic ideas of these
null
null
null
notes have been studied.
null
null
null
Finally, the efficiency or performance of an algorithm relates to the resources required
null
null
null
by it, such as how quickly it will run, or how much computer memory it will use. This will
null
null
null
6usuallydependontheprobleminstancesize, thechoiceofdatarepresentation, andthedetails
null
null
null
of the algorithm. Indeed, this is what normally drives the development of new data structures
null
null
null
and algorithms. We shall study the general ideas concerning efficiency in Chapter 5, and then
null
null
null
apply them throughout the remainder of these notes.
null
null
null
1.3 Data structures, abstract data types, design patterns
null
null
null
For many problems, the ability to formulate an efficient algorithm depends on being able to
null
null
null
organize the data in an appropriate manner. The term data structure is used to denote a
null
null
null
particular way of organizing data for particular types of operation. These notes will look at
null
null
null
numerous data structures ranging from familiar arrays and lists to more complex structures
null
null
null
such as trees, heaps and graphs, and we will see how their choice affects the efficiency of the
null
null
null