Data Structures and Algorithms - Chapter 1 - LMS2020
Data Structures and Algorithms - Chapter 1 - LMS2020
Data Structures and Algorithms - Chapter 1 - LMS2020
AND ALGORITHMS
MODULE 1 : ADTS AND RECURSION
LEARNING OUTCOMES
• Representation – This design aims to show relationships among the components of the data storage. The result
is called a data structure.
• This is a design on how to store instances of the ADT.
• Implementation – This is a procedural description of how each operation is called out using the data structure.
• Each procedural description is an algorithm. Algorithms can be written using pseudocode or a specific programming
language.
EXAMPLE #1:
Fig. 1: Five nodes connected sequentially to show ordering (student’s name and
average grade)
A node is a container of data. It represents a group of data and separates data from other
data.
are often drawn in diagrams as closed figures such as circles and rectangles.
these nodes are then connected by lines to indicate some kind of relationship among
the nodes.
ADT can also be represented by a data structure that uses two
parallel arrays indexed from 1 to 5.
Index 1 2 3 4 5
name Ana Belen Kulas Maria Pedro
Index 1 2 3 4 5
Avg grade 83 92 86 94 75
The following algorithm can be used to implement the operation to
determine the topnotcher:
1. Initialize the ixMax variable with 1, to index the first student and grade.
2. For each of the other values in the array of grades.
3. if it is higher than the grade indexed of this higher grade
4. Return the name of the student indexed by ixMax from the array of
name as the topnotcher.
DATA STRUCTURE
Data structures show relationships among the data items using primitive
structures or other data structures available in a programming environment to
facilitate operations of the ADTs.
Data structures are programmed using features of programming languages. Simple
data structures are programmed using primitive data types while complex
data structures use composite or user-defined data types.
The programming language may also provide composite data types such as
the following:
Arrays – These allow data of the same type to be organized in cells. These cells are indexed from 1 to a given
size, or from 0 to a given size minus 1. Each array is stored in a contiguous part of memory allowing direct
access to each cell.
Strings – These are sequence of characters. Each string has a length. Some operations can be implemented by
the programming language such as getting substrings, converting to upper case, etc.
Structs – These allow data to be grouped as a single data type. Instances of structs can be created. Data within
the struct can be individually referenced.
Classes – These allow data and operations to be defined as a single data type. Instances of classes are called
objects. Classes are available in OOP languages. The fields of a class represent the data structure while the
methods of a class implement the algorithms.
ALGORITHMS
A computational problem is solved by performing a finite set of instructions called an algorithm.
Algorithm is a well-defined procedure that processes input values to generate desired output values to solve
a problem.
The following are some properties of a good algorithm:
Has input - has an instance of the problem to be solved.
Has output – produces results to solve the given instance of the problem.
Definite – Each step is unambiguous. These is only one way to interpret each step that is precisely specified.
Effective – Each step executes as expected in an finite amount of time.
Finite – terminates after producing the output in a finite number of steps.
Correct – produces the desired output corresponding to given input.
General – correctly solves the problem for all valid problem instances.
Efficient – solves the problem with less resources such as time and space.
An algorithm is often written in pseudocode before it is implemented in a specific programming
language.
Pseudocodes combine English with basic programming language constructs.
Data structures, together with algorithms, form programs.
Algorithms are implemented as methods or procedures in a programming language.
Some algorithms are functions. They perform computations on their inputs and return values.
Integer sum(integer x, integer y) {
1 integer z = x + y
2 return z
}
It could be called as part of a statement like:
Print(“The sum of 24 and 37 is”, sum(24,37))
Some algorithms are subroutines that produce results as side effects;
Example: Algorithm “sum” below is a subroutine.
1 𝑖𝑓 𝑛 = 0
n!=
𝑛𝑥 𝑛−1 ! 𝑖𝑓 𝑛 > 0
To compute fo n!, the value of (n – 1)! Must be computed first. (n – 1) is a simpler or smaller part of
the factorial of n. Thus for 4!, the computation proceeds as follows:
RECURSION
4! = 4 x 3!
= 4 x (3 x 2!)
= 4 x (3 x (2 x 1!))
= 4 x (3 x (2 x (1 x 0!)))
= 4 x (3 x (2 x (1 x 1)))
= 4 x (3 x (2 x 1))
= 4 x (3 x 2)
=4x6
= 24
RECURSIVE ALGORITHMS
The Recursive algorithm can be translated directly from the recursive definitions.
Ex. The following pseudocode computes the factorial of n using the recursive definition in
example 7:
Integer factorialR(n: a non-negative integer) {
1 if n = 0 then return 1 // basis
2 Else return n x factorialR(n – 1) // recursive step
}
RECURSIVE ALGORITHMS
𝑛 𝑖𝑓 𝑛=0 𝑜𝑟 1
𝐹𝑖𝑏 𝑛 = 𝐹𝑖𝑏 𝑛 − 1 + 𝐹𝑖𝑏 𝑛 − 2 𝑖𝑓 𝑛 > 1
The Fibonacci sequence is 0, 1,1,2,3,5,8,13,21,… The value of Fib(4) is 3. The Fibonacci
sequence requires two basis values since the recursive step refers back to the previous two
terms. Repeated application of this step ends with the two lowest n’s that can be reached:
namely 0 and 1.
First, the terms are numbered from 0 onwards like this:
n= 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...
The reference to fibR(n-1) and fibR(n-2) result in two recursive calls with parameters (n – 1) and (n – 2) respectively.
Suppose fibR is called for the first time by the statement:
In the process of solving the problem for a large input, is the problem also being solved for a
subset of the input or for
a smaller input?
How can the problem be broken down into one or more smaller versions of the problem?
How can the solution for the problem on a subset of the input be used to solve the problem
on the entire input?
THE TOWER OF HANOI PROBLEM
There are three posts: the left, the middle, and the right. n ≥ 0 disks of increasing radii are resting on the left
post. The other two posts are empty. The objective is to move the n disks to the right post. Only one disk
may be moved at a time from one post to another, and no larger disk can be placed on top of a smaller disk.
How should the disk be moved to accomplish this task
The mission is to move all the disks to some another tower without
violating the sequence of arrangement. A few rules to be followed for Tower
of Hanoi are −
Only one disk can be moved among the towers at any given time.
Only the "top" disk can be removed.
No large disk can sit over a small disk.
PROS AND CONS OF RECURSION
Recursion provides an alternative way to solve a problem. It is useful when the problem
to be solved involves a countable number of items (eg. A number n, n characters, a set
with n elements, an array with length n, etc.
The biggest obstacle in writing recursive algorithms is coming up with a recurrence
relation for a solution to the problem. It often requires inventiveness to identify a
subproblem whose solution can be manipulated to solve the original problem.
SUGGESTED READINGS:
READ THE POSTED PDF FILES