ch#1
ch#1
INTRODUCTION TO DATA
STRUCTURES AND
ALGORITHMS
INTRODUCTION
A program is written in order to solve a problem.
A solution to a problem actually consists of two
things:
A way to organize the data
Sequence of steps to solve the problem
5
DATA TYPES VS DATA STRUCTURE
What is a data type?
The two things we must know about the data
types are:
It defines a certain domain of values.
It defines operations allowed on those values.
For example:
If the data is of int (integer) type, Then it takes
only integer values
Data type is one of the forms of a variable to which
the value can be assigned of a given type only. This
value can be used throughout the program.
Data structure is a collection of data of different
data types. This collection of data can be represented
using an object and can be used throughout the
6
program.
CON’T…
Based on the type of data store, there are two different
kinds of data structures.
Primitive Data Structures are basic data structures
provided by programming languages to represent single
values, such as integers, floating-point numbers,
characters, and Booleans.
Abstract Data Structures are higher-level data
structures that are built using primitive data types and
provide more complex and specialized operations.
Some common examples of abstract data structures
include arrays, linked lists, stacks, queues, trees, and
graphs.
Basically, based on organize and process a very large amount of data, data
structures are divided into two categories:
Linear data structure 7
Non-linear data structure
CON’T…
8
Linear data structures
In linear data structures, the elements are arranged in sequence one
after the other.
Since elements are arranged in particular order, they are easy to
implement.
However, when the complexity of the program increases, the linear
data structures might not be the best choice because of operational
complexities.
Popular linear data structures are:
i. Array Data Structure
In an array, elements in memory are arranged in continuous memory.
All the elements of an array are of the same type.
And, the type of elements that can be stored in the form of arrays is
determined by the programming language.
In a stack, operations can be performing only from one end (top here).
iii. Queue Data Structure
Unlike stack, the queue data structure works in the FIFO principle where first
element stored in the queue will be removed first.
It works just like a queue of people in the ticket counter where first person on the
queue will get the ticket first.
In a queue, addition and removal are performed from separate ends.
Linked list
Nonlinear data structures
Unlike linear data structures, elements in non-linear data structures are not in any sequence.
Instead they are arranged in a hierarchical manner where one element will be connected to one or more elements.
Non-linear data structures are further divided into graph and tree based data structures.
Adjacency Matrix
Adjacency List
The data items are arranged in sequential The data items are arranged in non-
order, one after the other. sequential order (hierarchical manner).
All the items are present on the single layer. The data items are present at different layers.
It can be traversed on a single run. That is, if It requires multiple runs. That is, if we start
we start from the first element, we can from the first element it might not be
traverse all the elements sequentially in a possible to traverse all the elements in a
single pass. single pass.
13
ALGORITHM
An algorithm is a well-defined computational procedure that takes some
value or a set of values as input and produces some value or a set of values as
output.
Data structures model the static part of the world. They are unchanging while
the world is changing.
Algorithms are the dynamic part of a program’s world model.
An algorithm transforms data structures from one state to
another state in two ways:
An algorithm may change the value held by a data
structure
An algorithm may change the data structure itself
The quality of a data structure is related to its ability to
successfully model the characteristics of the world.
Similarly, the quality of an algorithm is related to its ability to
successfully simulate the changes in the world.
14
Generally speaking, correct data structures lead to simple and
efficient algorithms and correct algorithms lead to accurate
and efficient data structures.
PROPERTIES OF AN ALGORITHM
Finiteness:
Definiteness:
Sequence:
Feasibility
Correctness:
Language Independence
Completeness
Effectiveness:
Efficiency:
Generality:
15
Input/Output:
GOOD ALGORITHMS?
Run in less time
Consume less memory
Internal structure
2. Theoretical Algorithm Analysis
Determining the quantity of resources required
using mathematical concept.
Analyze an algorithm according to the number
= 5n+5 = O(n)
4. int sum (int n)
{
int partial_sum = 0;
for (int i = 1; i <= n; i++)
partial_sum = partial_sum +(i * i * i);
return partial_sum;
}
Time Units to Compute
-------------------------------------------------
1 for the assignment.
1 assignment, n+1 tests, and n increments.
n loops of 4 units for an assignment, an addition, and two
multiplications.
1 for the return statement.
T (n)= 1+(1+n+1+n)+4n+1 = 6n+4 = O(n)
28
Formal Approach to Analysis
In the above examples we have seen that analyzing
Loop statements is so complex. However, it can be simplified by
using some formal approach in which case we can ignore
initializations, loop control, and updates.
For Loops: Formally
In general, a for loop translates to a summation. The index and
bounds of the summation are the same as the index and bounds of the
for loop.
29
Consecutive Statements: Formally
Add the running times of the separate blocks of your code.
30
Conditionals: Formally
If (test) s1 else s2: Compute the maximum of the running
31
Big-oh notation and others
Definition of Big O notation
In plain words, Big O notation describes the
complexity of your code using algebraic terms.
To understand what Big O notation is, we can
to make sure the ith element is the ith smallest element in the
list, this algorithm first iterates through the list with a for loop.
Then for every element it uses another for loop to find the
smallest element in the remaining part of the list.
SelectionSort(List) {
for(i from 0 to List.Length) {
SmallestElement = List[i]
for(j from i to List.Length) {
if(SmallestElement > List[j]) {
SmallestElement = List[j]
}
}
Swap(List[i], SmallestElement)
33
}
}
In this scenario, we consider the variable List as the input, thus input size n is
the number of elements inside List. Assume the if statement, and the value
assignment bounded by the if statement, takes constant time. Then we can find the
big O notation for the SelectionSort function by analyzing how many times the
statements are executed.
First the inner for loop runs the statements inside n times. And then after i is
incremented, the inner for loop runs for n-1 times… …until it runs once, then both
of the for loops reach their terminating conditions.
34
Big O, Little O, Omega & Theta
Big O: “f(n) is O(g(n))” iff for some constants c and N₀, f(N) ≤