Lecture Notes for
stringlengths
1
100
Unnamed: 1
stringclasses
7 values
Unnamed: 2
float64
Unnamed: 3
float64
algorithms based upon them.
null
null
null
Often we want to talk about data structures without having to worry about all the im-
null
null
null
plementational details associated with particular programming languages, or how the data is
null
null
null
stored in computer memory. We can do this by formulating abstract mathematical models
null
null
null
of particular classes of data structures or data types which have common features. These are
null
null
null
called abstract data types, and are defined only by the operations that may be performed on
null
null
null
them. Typically, we specify how they are built out of more primitive data types (e.g., integers
null
null
null
or strings), how to extract that data from them, and some basic checks to control the flow of
null
null
null
processinginalgorithms. Theideathattheimplementationaldetailsarehiddenfromtheuser
null
null
null
and protected from outside access is known as encapsulation. We shall see many examples of
null
null
null
abstract data types throughout these notes.
null
null
null
At an even higher level of abstraction are design patterns which describe the design of
null
null
null
algorithms, rather the design of data structures. These embody and generalize important
null
null
null
design concepts that appear repeatedly in many problem contexts. They provide a general
null
null
null
structure for algorithms, leaving the details to be added as required for particular problems.
null
null
null
These can speed up the development of algorithms by providing familiar proven algorithm
null
null
null
structures that can be applied straightforwardly to new problems. We shall see a number of
null
null
null
familiar design patterns throughout these notes.
null
null
null
1.4 Textbooks and web-resources
null
null
null
To fully understand data structures and algorithms you will almost certainly need to comple-
null
null
null
ment the introductory material in these notes with textbooks or other sources of information.
null
null
null
The lectures associated with these notes are designed to help you understand them and fill in
null
null
null
some of the gaps they contain, but that is unlikely to be enough because often you will need
null
null
null
to see more than one explanation of something before it can be fully understood.
null
null
null
There is no single best textbook that will suit everyone. The subject of these notes is a
null
null
null
classical topic, so there is no need to use a textbook published recently. Books published 10
null
null
null
or 20 years ago are still good, and new good books continue to be published every year. The
null
null
null
reasonisthatthesenotescoverimportantfundamentalmaterialthatistaughtinalluniversity
null
null
null
degrees in computer science. These days there is also a lot of very useful information to be
null
null
null
found on the internet, including complete freely-downloadable books. It is a good idea to go
null
null
null
to your library and browse the shelves of books on data structures and algorithms. If you like
null
null
null
any of them, download, borrow or buy a copy for yourself, but make sure that most of the
null
null
null
7topics in the above contents list are covered. Wikipedia is generally a good source of fairly
null
null
null
reliable information on all the relevant topics, but you hopefully shouldn’t need reminding
null
null
null
that not everything you read on the internet is necessarily true. It is also worth pointing
null
null
null
out that there are often many different equally-good ways to solve the same task, different
null
null
null
equally-sensible names used for the same thing, and different equally-valid conventions used
null
null
null
by different people, so don’t expect all the sources of information you find to be an exact
null
null
null
match with each other or with what you find in these notes.
null
null
null
1.5 Overview
null
null
null
These notes will cover the principal fundamental data structures and algorithms used in
null
null
null
computerscience,andbringtogetherabroadrangeoftopicscoveredelsewhereintoacoherent
null
null
null
framework. Data structures will be formulated to represent various types of information in
null
null
null
such a way that it can be conveniently and efficiently manipulated by the algorithms we
null
null
null
develop. Throughout, the recurring practical issues of algorithm specification, verification
null
null
null
and performance analysis will be discussed.
null
null
null
We shall begin by looking at some widely used basic data structures (namely arrays,
null
null
null
linked lists, stacks and queues), and the advantages and disadvantages of the associated
null
null
null
abstract data types. Then we consider the ubiquitous problem of searching, and how that
null
null
null
leads on to the general ideas of computational efficiency and complexity. That will leave
null
null
null
us with the necessary tools to study three particularly important data structures: trees (in
null
null
null
particular, binarysearchtreesandheaptrees), hashtables, andgraphs. Weshalllearnhowto
null
null
null
develop and analyse increasingly efficient algorithms for manipulating and performing useful
null
null
null
operations on those structures, and look in detail at developing efficient processes for data
null
null
null
storing, sorting, searching and analysis. The idea is that once the basic ideas and examples
null
null
null
covered in these notes are understood, dealing with more complex problems in the future
null
null
null
should be straightforward.
null
null
null
8Chapter 2
null
null
null
Arrays, Iteration, Invariants
null
null
null
Data is ultimately stored in computers as patterns of bits, though these days most program-
null
null
null
ming languages deal with higher level objects, such as characters, integers, and floating point
null
null
null
numbers. Generally, we need to build algorithms that manipulate collections of such objects,
null
null
null
so we need procedures for storing and sequentially processing them.
null
null
null
2.1 Arrays
null
null
null
In computer science, the obvious way to store an ordered collection of items is as an array.
null
null
null
Array items are typically stored in a sequence of computer memory locations, but to discuss
null
null
null
them, we need a convenient way to write them down on paper. We can just write the items
null
null
null
in order, separated by commas and enclosed by square brackets. Thus,
null
null
null
[1,4,17,3,90,79,4,6,81]
null
null
null
is an example of an array of integers. If we call this array a, we can write it as:
null
null
null
a = [1,4,17,3,90,79,4,6,81]
null
null
null
This array a has 9 items, and hence we say that its size is 9. In everyday life, we usually start
null
null
null
counting from 1. When we work with arrays in computer science, however, we more often
null
null
null
(though not always) start from 0. Thus, for our array a, its positions are 0,1,2,...,7,8. The
null
null
null
element in the 8th position is 81, and we use the notation a[8] to denote this element. More
null
null
null
generally, for any integer i denoting a position, we write a[i] to denote the element in the ith
null
null
null
position. This position i is called an index (and the plural is indices). Then, in the above
null
null
null
example, a[0] = 1, a[1] = 4, a[2] = 17, and so on.
null
null
null
It is worth noting at this point that the symbol = is quite overloaded. In mathematics,
null
null
null
it stands for equality. In most modern programming languages, = denotes assignment, while
null
null
null
equality is expressed by ==. We will typically use = in its mathematical meaning, unless it
null
null
null
is written as part of code or pseudocode.
null
null
null
We say that the individual items a[i] in the array a are accessed using their index i, and
null
null
null
one can move sequentially through the array by incrementing or decrementing that index,
null
null
null
or jump straight to a particular item given its index value. Algorithms that process data
null
null
null
stored as arrays will typically need to visit systematically all the items in the array, and apply
null
null
null
appropriate operations on them.
null
null
null
92.2 Loops and Iteration
null
null
null
The standard approach in most programming languages for repeating a process a certain
null
null
null
numberoftimes,suchasmovingsequentiallythroughanarraytoperformthesameoperations
null
null
null
on each item, involves a loop. In pseudocode, this would typically take the general form
null
null
null
For i = 1,...,N,
null
null
null
do something
null
null
null
and in programming languages like C and Java this would be written as the for-loop
null
null
null
for( i = 0 ; i < N ; i++ ) {
null
null
null
// do something
null
null
null
}
null
null
null
in which a counter i keep tracks of doing “the something” N times. For example, we could
null
null
null
compute the sum of all 20 items in an array a using
null
null
null
for( i = 0, sum = 0 ; i < 20 ; i++ ) {
null
null
null