0% found this document useful (0 votes)
10 views11 pages

Chapter 1 - Data Struct N Algo

This document provides an introduction to data structures. It defines key terms like data, data objects, data structures, algorithms, and different types of data structures. It discusses primitive and non-primitive, linear and non-linear, static and dynamic, and persistent and ephemeral data structures. It also defines algorithms and describes how to write algorithms, including the necessary properties of an algorithm. Various data structure representations and an example algorithm to find the greatest common divisor are provided.

Uploaded by

ameeramzar85
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
10 views11 pages

Chapter 1 - Data Struct N Algo

This document provides an introduction to data structures. It defines key terms like data, data objects, data structures, algorithms, and different types of data structures. It discusses primitive and non-primitive, linear and non-linear, static and dynamic, and persistent and ephemeral data structures. It also defines algorithms and describes how to write algorithms, including the necessary properties of an algorithm. Various data structure representations and an example algorithm to find the greatest common divisor are provided.

Uploaded by

ameeramzar85
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 11

Data & File Structures (GTU) 1-1 Introduction to Data Structures

1
Introduction to Data Structures

Syllabus :
Data Management concepts, Data types-primitive and nonprimitive, Performance
Analysis and Measurement (Time and space analysis of algorithms- Average, best
and worst case analysis), Types of Data structures – Linear and Non Linear Data
Structures.

1.1 Definition :

1.1.1 Data :

Data is collection of numbers, alphabets and symbols combined to represent


information. A Computer takes raw data as input and after processing of data it produces
refined data as output. We might say that computer science is the study of data.
Atomic data are non-decomposible entity. For example, an integer value 523 or a
character value ‘A’ cannot be further divided. If we further divide the value 523 in three digits
‘5’, ‘2’ and ‘3’ then the meaning may be lost.
Composite data is a composition of several atomic data and hence it can be further
divided into atomic data.
Example :

Fig. 1.1 shows example of atomic and composite data. In this example date of birth (say
20/08/2006) can be separated into three atomic values. First one gives the day of the month,
second one gives the month and the last one is the year.
Data & File Structures (GTU) 1-2 Introduction to Data Structures

Fig. 1.1 : Example of Atomic and composite data

1.1.2 Data Object :

Data object is a set of elements such that the set may be finite or infinite.
For example : consider a set of employees working in a bank. It is a finite set, whereas set of
natural numbers is an infinite set.

1.1.3 Data Structure :

• A Data Structure is merely an instance of an Abstract Data Type (ADT).


• The way in which the data is organized.
• Data Structure is formally defined to be triplet (D,F,A) where “D” stands for a set of
domains, “F” denotes the set of operations and “A” represents the axioms defining the
functions in “F”.
• Arrays and Strings are data structures available in Java.
• The study of data structures is nothing but how the basic data structures are used to build
new data structures (linked list, stacks, queues, and binary trees) and what are the
various operations that are applied to these data structures. These new data structures are
created with the help of built-in data structures. We will study these data structures in
next few chapters.
Types of Data Structure :

1. Primitive and Non-Primitive Data Structure :

Primitive :

The integers, reals, logical data, character data, pointers and reference are primitive data
structures. These data types are available in most programming languages as built in type. Data
objects of primitive data types can be operated upon by machine level instructions.
Data & File Structures (GTU) 1-3 Introduction to Data Structures

Non-Primitive :
These data structure are derived from primitive data structures. A set of homogeneous
and heterogeneous data elements are stored together. Examples of non-primitives data
structures: Array, structure, union, linked-list, stack, queue, tree, graph.
2. Linear and Non-Linear Data Structure :

Linear :
Elements are arranged in a linear fashion (one dimension). All one-one relation can be
handled through linear data structures. Lists, stacks and queues are examples of linear data
structure. Fig. 1.2 shows the representation of linear data structure.

(a) Representation of Linear data structures in an Array

(b) Representation of Linear data structures through Linked structure


Fig. 1.2 : Representation of Linear data structures

Non-Linear :
All one-many, many-many or many-many relations are handled through non-linear data
structures. Every data element can have a number of predecessors as well as successors. Tree
graphs and table are examples of non-linear data structures (Refer Fig. 1.3). Representation of
binary tree in linked and array structure is given in Fig. 1.4.
Data & File Structures (GTU) 1-4 Introduction to Data Structures

(a) (b) (c)


Fig. 1.3 : Non-Linear data structure

(a) Representation of the binary tree through (b) Representation of binary tree through an
linked structure array
Fig. 1.4 : Representation of tree of Fig. 1.3(a)

3. Static and Dynamic Data Structure :

Static :

In case of static data structure, memory for objects is allocated at the time of loading of
the program. Amount of memory required is determined by the compiler during compilation.
Example : int A[50]; // C++ Statement
Remember following points :
• Static data structure causes under utilization of memory.
• Static data structure may cause overflow.
• No re-usability of allocated memory.
• Difficult to guess the exact size of data at the time of writing of program.
Dynamic :
In case of dynamic data structure, the memory space required by variables is calculated
and allocated during execution.
Data & File Structures (GTU) 1-5 Introduction to Data Structures

Remember following points :


• Linear data structure can be implemented either through static or dynamic data
structures. Static data structured is preferred.
• All linked structures are preferable implemented through dynamic data structure.
• Dynamic data structures provides flexibility in adding, deleting or rearranging data
objects at run time.
• Additional space can be allocated at run time.
• Unwanted space can be released at run time.
• It gives re-usability of memory space.
4. Persistent and Ephemeral Data Structure :

Persistent :

A data structure is said to be persistent if it can be accessed but can not be modified.
Any operation on such data structure creates two versions of data structures. Previous version
is saved and all changes are made in the new version. Functional data structures are persistent.
Ephemeral :

If we are able to create data cells and modify their contents, we can create ephemeral
data structures. These are data structure that changed over time.

1.2 Algorithm :

1.2.1 Definition :

The word Algorithm comes from the name of a Persian author Abu Jafar Mohammad
ibn Musba al Khowarizmi (c. 825 A.D.), who wrote the textbook on mathematics (see
Fig. 1.5). This word has taken special significance in computer science, where Algorithm has
come to refer to a method that can be used by a computer for the solution of a problem. This is
what makes algorithm different from words such as process, technique or method.

Fig. 1.5 : Abu Jafar Mohammad ibn Musba al Khowarizmi (825 AD)
Data & File Structures (GTU) 1-6 Introduction to Data Structures

An Algorithm is a finite set of


instructions that if followed,
accomplishes a particular task in a finite
amount of time. It is a good software
engineering practice to design algorithms
before we write a program. In addition, all
algorithms must satisfy the following
criteria (Refer Fig. 1.6).

Fig. 1.6

• Input : All the algorithms should have some input. The logic of the algorithm should
work on this input to give the desired result.
• Output : At least one output should be produced from the algorithm based on the input
given.
Example :

If we write a program to check the given number to be prime or not, we should get an
output as ‘number is prime’ or ‘number is not prime’.
• Definiteness : Every step of algorithm should be clear and not have any ambiguity.
• Finiteness : Every algorithm should have a proper end. The algorithm can’t lead to an
infinite condition.
Example :

If we write an algorithm to find the number of primes between 10 and 100, the output
should give the number of primes and execution should end. The computation should not get
into an infinite loop.
• Effectiveness : Every step in the algorithm should be easy to understand and can be
implemented using any programming language.

1.2.2 How to Write an Algorithm :

The algorithm is described as a series of steps of basic operations. These steps must be
performed in a sequence. Each step of the algorithm is labelled.

Algorithm is a backbone for any implementation by some desired language. One can
implement the algorithm very effectively if some method is followed in writing an algorithm.
For having the systematic approach into it we have to use some algorithmic notations. Let us
see one example of algorithm. Refer Fig. 1.7.
Data & File Structures (GTU) 1-7 Introduction to Data Structures

Fig. 1.7

Step 1 : Start.
Step 2 : Read two positive integers and store them in X and Y.
Step 3 : Divide X and Y. Let the remainder be R and the quotient be Q.
Step 4 : If R is zero then go to step 8.
Step 5 : Assign Y to X.
Step 6 : Assign R to Y.
Step 7 : Go to Step 3.
Step 8 : Print Y ( the required GCD).
Step 9 : Stop.
The steps mentioned in the above algorithm are simple and unambiguous. Anybody,
carrying out these will clearly know what to do in each step. Hence, the above algorithm
satisfies the definiteness property of an algorithm.

1.2.3 Algorithmic Strategies :

Algorithmic strategies is a general approach by which many problems can be solved


algorithmically.
Various Algorithmic strategies are :
1. Divide and conquer : In this strategy the problem is divided into smaller subproblems
and these subproblems are solved to obtain the solution to main problem.
2. Dynamic programming : The problem is divided into smaller instances and results of
smaller reoccurring instances are obtained to solve the problem.
3. Greedy technique : From a set of obtained solutions the best possible solution is chosen
each time, to obtain the final solution.
4. Back tacking : In this method, in order to obtain the solution trial and error method is
followed.
Using any of these suitable methods the problem can be solved.
Data & File Structures (GTU) 1-8 Introduction to Data Structures

1.2.4 Purpose of Analysis of Algorithm :

When a programmer builds an algorithm during design phase of software development


life cycle, he/ she might not be able to implement it immediately. This is because programming
comes in later part of the software development life cycle. There is a need to analyze the
algorithm at that stage. This will help in forecasting time of execution and amount of primary
memory algorithm might occupy when it is implemented.

1.2.5 What is Analysis of Algorithm ?

Analysis of algorithm means developing a formula or prediction of how fast the


algorithm works based on the problem size.

The problem size could be :The number of inputs/outputs in an algorithm.


Example :

For sorting algorithm, the number of inputs is the total number of elements to be
arranged in a specific order. The number of outputs is the total number of sorted elements.
The number of operations involved in the algorithm.

Example :

For a searching algorithm, the number of operations is equal to the total number of
comparisons made with the search element.
• If we are searching an element in an array having ‘n’ elements, the problem size is same
as the number of elements in the array to be searched. Here the problem size and the
input size are the same and is equal to ‘n’.
• If we sorting elements in an array, there might be some copy operations (swaps)
involved. The problem size could be the number of elements in the array or the number
of copies performed during sorting.
• If two arrays of size n and m are merged , the problem size is the sum of two array
sizes(= n+m).
• If nth factorial is being computed, the problem size is n.

1.2.5.1 Time Complexity :

The Time Complexity of an algorithm is the amount of computer time it needs to


execute the program and get the intended result.
Data & File Structures (GTU) 1-9 Introduction to Data Structures

1.2.5.2 Space Complexity :

Analysis of algorithms is also defined as the process of determining a formula for


prediction of the memory requirements (primary memory) based on the problem size. This is
called Space Complexity of the algorithm. That is, Space Complexity is defined as the amount
of memory required for running an algorithm.
In summary, analysis of algorithm is aimed at determination of Time complexity and
Space complexity of the algorithm.

1.2.6 Order of Magnitude of an Algorithm :

The Order of Magnitude of an algorithm is the sum of number of occurrences of


statements contained in it.
Consider the following code snippet to understand the order of magnitude of algorithm:
Example 1 :

for (i=0; i<n; i++)


{
/* assume there are ‘c’ statements inside the loop*/
…….
…….
}
In the algorithm containing above piece of code it is assumed that
There are ‘c’ statements inside the for loop.
Each of the ‘c’ statements take one unit of time.
Note : The assumptions are made because information on the target machine is not
known when the algorithm is built. So it is not possible to say how much time each
statement in the algorithm takes to execute. The exact time depends on the
machine on which the algorithm is run.
Based on the above assumptions,
Total time of execution for 1 loop= c*1=c
Since the loop is executed ‘n’ times
Total time of execution = n*c
Thus ‘n*c’ is defined as the Order of Magnitude of Algorithm. Since ‘c’ is a
constant (This is because while analyzing the algorithm, the exact number of
steps is not known before programming. It is assumed that the number of steps is
constant for each algorithm and only the number of iterations change.) , Order of
Magnitude is approximated to be equal to ‘n’. This approximation is because for
Data & File Structures (GTU) 1-10 Introduction to Data Structures

higher values of ‘n’ , the effect of ‘c’(constant) is not significant. Thus, constant
can be ignored.
Example 2 :
for (i=0; i<n; i++)
{
for(j=0; j<m; j++)
{
/*assume we have ‘c’ number of statements inside the loop*/
……..
……..
}
}
In the above example, based on earlier assumptions,
The total time for one execution of the for inner loop= c*1 = c
Since the inner loop is executed ‘m’ times,
The total time of execution in the inner loop = m*c
As per the nested loop concept, for every iteration of the outer loop, the inner loop
is executed ‘m’ times.
Since the outer loop is executed ‘n’ times, we have
The total time of execution of the algorithm = n*(m*c)
Since ‘c’ is constant, the order of magnitude of the above algorithm is
approximately equal to ‘n*m’.
The calculation of order of magnitude in the examples discussed above is the priori
analysis of the algorithm.

1.2.7 Worst Case, Average Case and Best Case Running Time of an
Algorithm :

While analyzing the algorithm based on Priori Analysis principles, three different cases
are identified. It is important to note that this classification is purely based on the nature of the
problem for which we are developing the algorithm.

Worst case :

Refers to maximum number of instructions / operations that could be executed by the


algorithm in order to give the desired output.
Data & File Structures (GTU) 1-11 Introduction to Data Structures

Average case :

Refers to number of instructions / operations that could be executed by the algorithm on


an average in order to give the desired output.
Best case :

Refers to minimum number of instructions/operations that could be executed by the


algorithm in order to give the desired output.
Example : Assume there are 100 students in a class.
• The worst case of the problem is none of the students clearing the exams.
• The best of the problem is all100 students clearing the exams.
• The average case of the problem is around 50 students clearing the exams.

Note

You might also like