Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
79 views10 pages

Introduction To Data Structures and Algorithms

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

Introduction to Data Structures and Algorithms

Data Structure is a way of collecting and organising data in such a way that we can
perform operations on these data in an effective way. Data Structures is about rendering
data elements in terms of some relationship, for better organization and storage. For
example, we have some data which has, player's name "Virat" and age 26. Here "Virat"
is of String data type and 26 is of integer data type.

We can organize this data as a record like Player record, which will have both player's
name and age in it. Now we can collect and store player's records in a file or database as
a data structure. For example: "Dhoni" 30, "Gambhir" 31, "Sehwag" 33

If you are aware of Object Oriented programming concepts, then a class also does the
same thing, it collects different type of data under one single entity. The only difference
being, data structures provides for techniques to access and manipulate data efficiently.

In simple language, Data Structures are structures programmed to store ordered data,
so that various operations can be performed on it easily. It represents the knowledge of
data to be organized in memory. It should be designed and implemented in such a way
that it reduces the complexity and increases the efficiency.

Basic types of Data Structures

As we have discussed above, anything that can store data can be called as a data
structure, hence Integer, Float, Boolean, Char etc, all are data structures. They are
known as Primitive Data Structures.

Then we also have some complex Data Structures, which are used to store large and
connected data. Some example of Abstract Data Structure are:

Linked List

 Tree
 Graph
 Stack, Queue etc.
All these data structures allow us to perform different operations on data. We select
these data structures based on which type of operation is required. We will look into
these data structures in more details in our later lessons.

The data structures can also be classified on the basis of the following characteristics:

Characteristic Description

Linear In Linear data structures, the data items are arranged in a linear sequence.
Example: Array

Non-Linear In Non-Linear data structures,the data items are not in sequence.


Example: Tree, Graph

Homogeneous In homogeneous data structures,all the elements are of same type.


Example: Array
Non- In Non-Homogeneous data structure, the elements may or may not be of
Homogeneous the same type. Example: Structures

Static Static data structures are those whose sizes and structures associated
memory locations are fixed, at compile time. Example: Array

Dynamic Dynamic structures are those which expands or shrinks depending upon
the program need and its execution. Also, their associated memory
locations changes. Example: Linked List created using pointers

What is an Algorithm ?

An algorithm is a finite set of instructions or logic, written in order, to accomplish a


certain predefined task. Algorithm is not the complete code or program, it is just the
core logic(solution) of a problem, which can be expressed either as an informal high
level description as pseudocode or using a flowchart.

Every Algorithm must satisfy the following properties:

1. Input- There should be 0 or more inputs supplied externally to the algorithm.

2. Output- There should be atleast 1 output obtained.

3. Definiteness- Every step of the algorithm should be clear and well defined.

4. Finiteness- The algorithm should have finite number of steps.

5. Correctness- Every step of the algorithm must generate a correct output.

An algorithm is said to be efficient and fast, if it takes less time to execute and consumes
less memory space. The performance of an algorithm is measured on the basis of
following properties:

1. Time Complexity

2. Space Complexity
Space Complexity
It’s the amount of memory space required by the algorithm, during the course of its
execution. Space complexity must be taken seriously for multi-user systems and in
situations where limited memory is available.
An algorithm generally requires space for following components :

 Instruction Space: It’s the space required to store the executable version of the
program. This space is fixed, but varies depending upon the number of lines of
code in the program.
 Data Space: It’s the space required to store all the constants and
variables(including temporary variables) value.
 Environment Space: It’s the space required to store the environment information
needed to resume the suspended function.

Time Complexity
Time Complexity is a way to represent the amount of time required by the program to
run till its completion. It's generally a good practice to try to keep the time required
minimum, so that our algorithm completes it's execution in the minimum time possible.
We will study about Time Complexity in details in later sections.

What is Stack Data Structure?

Stack is an abstract data type with a bounded (predefined) capacity. It is a simple data
structure that allows adding and removing elements in a particular order. Every time
an element is added, it goes on the top of the stack and the only element that can be
removed is the element that is at the top of the stack, just like a pile of objects.
Basic features of Stack

1. Stack is an ordered list of similar data type.


2. Stack is a LIFO(Last in First out) structure or we can say FILO(First in Last out).
3. push() function is used to insert new elements into the Stack and pop() function
is used to remove an element from the stack. Both insertion and removal are
allowed at only one end of Stack called Top.
4. Stack is said to be in Overflow state when it is completely full and is said to be
in Underflow state if it is completely empty.

Applications of Stack
The simplest application of a stack is to reverse a word. You push a given word to stack
- letter by letter - and then pop letters from the stack.
There are other uses also like:

1. Parsing
2. Expression Conversion(Infix to Postfix, Postfix to Prefix etc)
Implementation of Stack Data Structure
Stack can be easily implemented using an Array or a Linked List. Arrays are quick, but
are limited in size and Linked List requires overhead to allocate, link, unlink, and
deallocate, but is not limited in size. Here we will implement Stack using array.

Algorithm for PUSH operation

1. Check if the stack is full or not.


2. If the stack is full, then print error of overflow and exit the program.
3. If the stack is not full, then increment the top and add the element.

Algorithm for POP operation

1. Check if the stack is empty or not.


2. If the stack is empty, then print error of underflow and exit the program.
3. If the stack is not empty, then print the element at the top and decrement the top.
Position of Top Status of Stack

-1 Stack is Empty

0 Only one element in Stack

N-1 Stack is Full

N Overflow state of Stack

Analysis of Stack Operations

Below mentioned are the time complexities for various operations that can be
performed on the Stack data structure.

 Push Operation : O(1)


 Pop Operation : O(1)
 Top Operation : O(1)
 Search Operation : O(n)

The time complexities for push() and pop() functions are O(1) because we always have
to insert or remove the data from the top of the stack, which is a one step process.
What is a Queue Data Structure?
Queue is also an abstract data type or a linear data structure, just like stack data
structure, in which the first element is inserted from one end called the REAR(also
called tail), and the removal of existing element takes place from the other end called
as FRONT(also called head).
This makes queue as FIFO(First in First Out) data structure, which means that element
inserted first will be removed first.
Which is exactly how queue system works in real world. If you go to a ticket counter to
buy movie tickets, and are first in the queue, then you will be the first one to get the
tickets. Right? Same is the case with Queue data structure. Data inserted first, will leave
the queue first.
The process to add an element into queue is called Enqueue and the process of removal
of an element from queue is called Dequeue.

Basic features of Queue

1. Like stack, queue is also an ordered list of elements of similar data types.
2. Queue is a FIFO( First in First Out ) structure.
3. Once a new element is inserted into the Queue, all the elements inserted before
the new element in the queue must be removed, to remove the new element.
4. peek( ) function is oftenly used to return the value of first element without
dequeuing it.
Applications of Queue
Queue, as the name suggests is used whenever we need to manage any group of objects
in an order in which the first one coming in, also gets out first while the others wait for
their turn, like in the following scenarios:

1. Serving requests on a single shared resource, like a printer, CPU task scheduling
etc.
2. In real life scenario, Call Center phone systems uses Queues to hold people
calling them in an order, until a service representative is free.
3. Handling of interrupts in real-time systems. The interrupts are handled in the
same order as they arrive i.e First come first served.

Implementation of Queue Data Structure


Queue can be implemented using an Array, Stack or Linked List. The easiest way of
implementing a queue is by using an Array.
Initially the head(FRONT) and the tail(REAR) of the queue points at the first index of
the array (starting the index of array from 0). As we add elements to the queue,
the tail keeps on moving ahead, always pointing to the position where the next element
will be inserted, while the head remains at the first index.
When we remove an element from Queue, we can follow two possible approaches
(mentioned [A] and [B] in above diagram). In [A] approach, we remove the element
at head position, and then one by one shift all the other elements in forward position.
In approach [B] we remove the element from head position and then move head to the
next position.
In approach [A] there is an overhead of shifting the elements one position
forward every time we remove the first element.
In approach [B] there is no such overhead, but whenever we move head one position
ahead, after removal of first element, the size on Queue is reduced by one space each
time.

Algorithm for ENQUEUE operation

1. Check if the queue is full or not.


2. If the queue is full, then print overflow error and exit the program.
3. If the queue is not full, then increment the tail and add the element.

Algorithm for DEQUEUE operation

1. Check if the queue is empty or not.


2. If the queue is empty, then print underflow error and exit the program.
3. If the queue is not empty, then print the element at the head and increment the
head.

Complexity Analysis of Queue Operations

Just like Stack, in case of a Queue too, we know exactly, on which position new element
will be added and from where an element will be removed, hence both these operations
requires a single step.

 Enqueue: O(1)
 Dequeue: O(1)
 Size: O(1)

You might also like