DSAL-210 LECT 1: INTRODUCTION
TO DATA STRUCTURES AND
ALGORITHMS
By
W.G. Gondwe
Lecturer, Computer Networks
CS&IT Department
Malawi University of Science & Technology
Overview
• Defining Algorithms
• Defining Data Structures
• Understanding Algorithm Complexity
• Rationale for Studying DSAL
• What to Expect
• Important Things to Note
• Practice Example
DSAL-210 Lecture 1 - Introduction 2
Defining an Algorithm
• Generally, problem solving in life involves application of a given (pre-
defined) method
• This ‘method’ is usually in a series of one or more steps
• Example: Shutting down a desktop computer -> 1. Click on the start button.
2. Select power options 3. Click on shutdown 4. Switch off power
-> In this case, the input is an active computer and output is a computer in the off state
• A more mathematical example: “Converting a decimal number into a
binary number” (Recal the methods taught in COMP-111)
• In computer science, this predefined, reproducible and proven series of
steps is called an Algorithm
DSAL-210 Lecture 1 - Introduction 3
Defining an Algorithm
• Formally:
“An algorithm is a finite, deterministic and effective series of
unambiguous steps that solve a given computational problem”
•Finite – A countable number of steps (it must stop at some point)
•Deterministic – Given the same input, it must produce the same
output everytime
•Effective – It must produce some result/effect
•Unambiguous – The steps must not be clear and not have the
potential for mis-interpretation
• Here, the term computational problem refers to any problem that can be
solved by a computer program (computational problems will be further
discussed through a class forum)
DSAL-210 Lecture 1 - Introduction 4
Defining an Algorithm cont...
• A good example of an algorithm is given below (Cooking nsima)
• In order to produce nsima, one must follow the steps below:
1. Prepare pot, six 500mlscoops of flour and 1ltr of
water
2. Boil water to 90 degrees celcius
3. Add two 500ml scoops of flour
4. Stir continuously for 10 minutes
5. Simmer for 30 minutes
6. Add 4 scoops of floor until paste is solid
7. Simmer for 10 minutes
8. Serve while hot
DSAL-210 Lecture 1 - Introduction 5
Defining an Algorithm cont...
• The above example lists 8 (finite) and umabiguous steps that will
guarantee the output (nsima), given the inputs (flour, pot and water)
• The steps are specific, unabiguous and reproducible i.e. they can be
repeated and produce the same output (nsima) in every instance
• Also notice that algorith consumes some resources - most notably
energy (cooking/heat) and time
• It must be understood that the steps are also deterministic i.e. the
output is predictable
• This implies that a series of steps that produces different results at
different times given the same input is not a algorithm
DSAL-210 Lecture 1 - Introduction 6
Defining Data Structures
• Now that we understand algorithms, let us now consider data structures
• In simple terms, a data structure is a “particular way of organizing data
in a computer system so that it can be used efficiently”
• Data structres and algorithms are two sides of the same coin
• Data structures hold the data that an algorithm works on
• Generally speaking, the better the data structure, the better the
algorithm
• Using our nsima example, you cook faster if you keep your flour, pot
and water in a rack close to you, arranged in a specific manner
DSAL-210 Lecture 1 - Introduction 7
Defining Data Structures
• Further, specific data structures lend themselves to specific
algorithms i.e. choice of data structure affects choice of
algorithm (or vice versa)
• In this course, we will look at various data structures and how
we can use them and their associated algorithms to solve
problems
• Examples of computational data structures include stacks,
queues, trees, graphs, arrays and linked lists
DSAL-210 Lecture 1 - Introduction 8
Understanding Algorithm Complexity
• In our algorithm example, we pointed out that an algorithm consumes
resources
• You may have also noted that a given (computational) problem can have
multiple algorithms e.g. nsima can be cooked in different ways
• Each of these algorithms will consume its own set of resources e,g. one
might be faster than the other
• The more the resources consumed by an algorithm, the more complex it is
• Hence, algorithm complexity is measured in terms of the resources
consumed
• In computer science, we are concerned with two major resources: Time
(CPU cycles) and Memory (RAM)
DSAL-210 Lecture 1 - Introduction 9
Understanding Algorithm Complexity
• In other words, the best algorithm is one that produces the
desired output using minimal running time and memory space
i.e. minimum time and space complexities
• Complexity theory is the study of the resources consumed by an
algorithm and how they change with increasing input
• This theory allows us to analyze and predict the complexities of
algorithms
• With such analysis, we can design the 'best' algorithms for a
given problem and produce efficient software programs
DSAL-210 Lecture 1 - Introduction 10
Rationale for Studying DSAL
• With the discussion so far, it is important to fomalize why we
study data structures and algorithms
• The following are the reasons why we must study and
understand these topics:
1. To understand the existing recipes for solving different classes of
problems (to avoid re-inventing the wheel)
2. To be able to identify the ‘best’ way to solve a particular problem
based on some criteria (complexity theory)
3. To understand problems that can be solved by a computer system
(and those that cannot i.e. computational theory)
DSAL-210 Lecture 1 - Introduction 11
What to Expect
• In order to achieve this rationale, we will cover the following
major topics:
1. Analysis of algorithms (asymptotic notation)
2. Data structures and abstract data types
3. Algorithm design approaches/paradigms including: brute force, divide
and conquer, greedy algorithm, recursion, genetic algorithms
4. Common computational problems and associated algorithms e.g.
searching, sorting, graph traversal
(Refer to the module outline for a full description of the indicative content)
DSAL-210 Lecture 1 - Introduction 12
Important Things to Note
• An algorithm can be writen in any form, as long as it adheres to the definition and properties
• An algorithm is not a computer program
• An algorithm can be implemented as a computer program using a given programming language
• To emphasize the separation between program and algorithm, we will use pseudo-code to write
our algorithms
• Pseudo-code is defined as “An informal, high-level notation (similar to a programming language)
used to design and analyze algorithms”
• It can be any (sensible) informal semblance of a programming language i.e. no standard pseudo-
code
• Example:
for each element in word
insert(stack, element)
end for each
for i := 0 to size(stack)
pop(stack)
endfor
DSAL-210 Lecture 1 - Introduction 13
More Algorithm Examples:
• We will finish this lecture with a more computer-related problem
and algorithm (forget nsima)
• Problem: Search a list for an elemen
• Input: a list of N integers and an element to search for (x)
• Output: Yes if element is found and No if not found in the list
3 6 8 4 100 5 10
Specify an appropriate algorithm (Hint: start from one end and check each cell)
DSAL-210 Lecture 1 - Introduction 14
Next: Introduction To Complexity Analysis
DSAL-210 Lecture 1 - Introduction 15