Data structures and
algorithms
BCT2202 (4CU’s)
By Mrs. Barbara A. Mwijukye
What are Algorithms About?
• Solving problems in everyday life
• Travel from Bolzano to Munich; Cook Spaghetti, eggs; Register for a
Bachelor thesis at FUB
• For all these problems, there are
• Instructions, Recipes, Procedures,
• Which describe a complex operation in terms of
• elementary operations (“beat well …”); control structures and conditions
(“… until fluffy”)
Algorithms
• Problems involving numbers, strings, mathematical objects:
• For two numbers, determine their sum, product, …
• For two numbers, find their greatest common divisor
• For a sequence of strings, find an alphabetically sorted permutation of the
sequence
• For two arithmetic expressions, find out if they are equivalent
• On a map, find for a given house the closest bus stop
• We call instructions, recipes, for such problems algorithms
Overall Picture
• Using a computer to help solve problems.
• Precisely specify the problem
• Designing programs
• Architecture
• Algorithm
• Writing programs
• Verifying (testing) programs
Overall Picture
• Data Structure and Algorithm Design Goals
• Correctness
• Efficiency
• Implementation Goals
• Robustness
• Reusability
• Adaptability
This course is not about:
• Programming languages
• Computer architecture
• Software architecture
• SW design and implementation principles
• We will only touch upon the theory of complexity and
computability.
Data Structures and Algorithms
• Data structure
• Organization of data to solve the problem at hand
• Algorithm
• Outline, the essence of a computational procedure, step-by-step
instructions
• Program
• Implementation of an algorithm in some programming language
Data Structure Example Applications
• How does Google quickly find web pages that contain a search term?
• What’s the fastest way to broadcast a message to a network of
computers?
• How can a subsequence of DNA be quickly found within the genome?
• How does your operating system track which memory (disk or RAM)
is free?
What is a Data Structure Anyway?
• It’s an agreement about:
• How to store a collection of objects in memory,
• What operations we can perform on that data,
• The algorithms for those operations, and
• How time and space efficient those algorithms are.
• Ex. vector in C++:
• Stores objects sequentially in memory; Can access, change, insert or delete
objects; Algorithms for insert & delete will shift items as needed
Algorithmic problem
• Data structuring is the best way of organizing the data, associated
with that problem.
• An algorithmic problem is essentially, that you have a certain
specifications of an input and specify what the output should be
like.
Specification of
Specification
? output as a
of input
function of input
Algorithmic problem
• There is an infinite number of possible input instances satisfying
the specification.
• For example:
• A sorted, non-decreasing sequence of natural numbers, on nonzero, finite length:
• 1, 20, 908, 909, 100000, 1000000000
• 3
• Above are the two examples of input, which meets the
specification and I have not given any output specification.
What is an instance?
• A sorted, non-decreasing sequence of natural numbers
of non-zero, finite length forms an instance.
• Those two examples are the instances of the input.
• You can have any possible number of instances that may
take sequence of sorted, non-decreasing numbers as
input.
Algorithmic Solution
• An algorithm is essentially, describing the actions that one should
take on the input instance to get the specified output.
• Also there can be infinitely many input instances and algorithms
for solving certain problem. (Each one of you could do it in a
different way).
Input Instance Output related
adhering to Algorithm to the input
Specification as required
Algorithmic Solution
• Algorithm describes actions on the input instance
• There may be many correct algorithms for the same
algorithmic problem.
Good algorithm
• There are so many different algorithms for solving a certain problem.
• What is a good algorithm?
• Good algorithm is an efficient algorithm.
• What is efficient?
• Efficient is something, which has small running time and takes less memory
(space used).
• These will be the two measures of efficiency we will be working with.
• There could also be other measures of efficiency.
Efficiency
• We will be spending more time on analyzing;
• The running time of an algorithm
• The space used.
• We will be interested in the efficiency of algorithms, as a function
of input size.
What is a good algorithm?
• Efficient:
• Running time
• Space used
• Efficiency as a function of input size:
• The number of bits in the input number
• Number of data elements (numbers, points)
• How should we measure the running time of an algorithm?
Measuring the running time
• Experimental study:
• Write a program that implements the algorithm
• Run the program with data sets of varying size and
composition
• Use a method like the system current time millis () to get an
accurate measure of the actual running time
Measuring the running time
• Limitations of experimental studies
• It necessary to implement and test the algorithm in order to determine
its running time.
• Experiments can be done only a limited set of inputs and may not be
indicative of the running time on other inputs not included in the
experiment.
• In order to compare two algorithms, the same hardware and software
environments should be used.
Beyond Experimental Studies
• We will develop a general methodology for analyzing the running time of algorithms.
• We are going to do it as follows:
• First we are going to develop a high level description of an algorithm instead of
testing one of its implementations.
• A methodology that would help us to take into account of all possible input
instances.
• Also it will allow us to evaluate the efficiency of the algorithm in a way that it is
independent of the hardware and software environment (platform) we are
using.
Definition
• An algorithm is a sequence of unambiguous instructions of
solving a problem, i.e., for obtaining a required output for any
legitimate input
in a finite amount of time.
• This presumes a mechanism to execute the algorithm
• Properties of algorithms:
• Correctness; Termination; (Non-)Determinism; Run Time
How to Develop an Algorithm
• Precisely define the problem.
• Precisely specify the input and output and consider all cases.
• Come up with a simple plan to solve the problem at hand.
• The plan is independent of a (programming) language
• The precise problem specification influences the plan.
• Turn the plan into an implementation
• The problem representation (data structure) influences the implementation
Pre-conditions, Post-conditions
• Specify pre-conditions and post-conditions of algorithms:
• Pre-condition: what does the algorithm get as input?
• Post-condition: what does the algorithm produce as output?
how does this relate to the input?
• Make sure you have considered the special cases:
• Empty set, number 0, pointer nil, …
Any questions..??