0% found this document useful (0 votes)
2 views

Data Structures and Algorithm

The document provides an introduction to data structures and algorithms, emphasizing their importance in programming efficiency and problem-solving. It categorizes data structures into linear and non-linear types, explains various algorithms including divide and conquer, and discusses pointers and memory allocation. Additionally, it covers Python's built-in data structures like lists, dictionaries, and sets, highlighting their functionalities and use cases.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Data Structures and Algorithm

The document provides an introduction to data structures and algorithms, emphasizing their importance in programming efficiency and problem-solving. It categorizes data structures into linear and non-linear types, explains various algorithms including divide and conquer, and discusses pointers and memory allocation. Additionally, it covers Python's built-in data structures like lists, dictionaries, and sets, highlighting their functionalities and use cases.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

# Data Structures and Algorithm: An Introduction

## Objectives

1. **Basic Data Structures**: Provide knowledge of basic data structures and their
implementations.

2. **Efficiency**: Understand the importance of data structures in writing efficient


programs.

3. **Problem Solving**: Develop skills to apply appropriate data structures in


problem-solving.

## Algorithms

In computer programming, an algorithm is a set of well-defined instructions to solve


a particular problem. It takes a set of inputs and produces the desired output. For
example, an algorithm to add two numbers can be described as follows:

1. Take two number inputs.


2. Add the numbers using the + operator.
3. Display the result.

Three ways of representing algorithms

1. Narrative
2. Pseudo code
3. Flow charts

### Qualities of a Good Algorithm

A good algorithm possesses the following qualities:

1. **Precise Input and Output**: Clearly define input and output.

2. **Clarity and Unambiguity**: Each step in the algorithm should be clear and
unambiguous.

3. **Effectiveness**: The algorithm should be among the most effective ways to


solve a problem.

4. **Language Agnostic**: It shouldn't include computer code; instead, it should be


written in a way that can be used in different programming languages.
### Sample Algorithm: Determining the Largest Number

## Data Structures and Types

• Data structure is a storage that is used to store and organize data. It is a way
of arranging data on a computer so that it can be accessed and updated
efficiently.
• Depending on your requirement and project, it is important to choose the
right data structure for your project. For example, if you want to store data
sequentially in the memory, then you can go for the Array data structure.

### Types of Data Structures

Data structures can be categorized into two main types:

#### 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
Examples:
1. Array
2. Stack
3. Queue
4. Linked List

1. 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.

2. Stack Data Structure


In stack data structure, elements are stored in the LIFO principle. That is, the last
element stored in a stack will be removed first.
It works just like a pile of plates where the last plate kept on the pile will be
removed first.

3. 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.
4. Linked List Data Structure
In linked list data structure, data elements are connected through a series of nodes.
And, each node contains the data items and address to the next node.

#### Non-linear 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.

Examples:
1. Graph
2. Trees (e.g., Binary Tree, Binary Search Tree, AVL Tree, B-Tree, B+ Tree, Red-
Black Tree)

1. Graph Data Structure


In graph data structure, each node is called vertex and each vertex is connected to
other vertices through edges.
2. Trees Data Structure
Similar to a graph, a tree is also a collection of vertices and edges. However, in tree
data structure, there can only be one edge between two vertices.

Linear Data Structures Non Linear Data Structures

The data items are arranged in The data items are arranged in non-
sequential order, one after the sequential order (hierarchical
other. manner).

All the items are present on the The data items are present at
single layer. different layers.

It can be traversed on a single


It requires multiple runs. That is, if we
run. That is, if we start from the
start from the first element it might
first element, we can traverse all
not be possible to traverse all the
the elements sequentially in a
elements in a single pass.
single pass.

Different structures utilize memory in


The memory utilization is not
different efficient ways depending on
efficient.
the need.

The time complexity increase with


Time complexity remains the same.
the data size.

Example: Arrays, Stack, Queue Example: Tree, Graph, Map

Why Data Structure?


• Knowledge about data structures help you understand the working of each data
structure. And, based on that you can select the right data structures for your
project.
• This helps you write memory and time efficient code.
Divide and Conquer Algorithm
A divide and conquer algorithm is a strategy of solving a large problem by
• breaking the problem into smaller sub-problems
• solving the sub-problems, and
• combining them to get the desired output.
How Divide and Conquer Algorithms Work?
Here are the steps involved:
Divide: Divide the given problem into sub-problems using recursion.
Conquer: Solve the smaller sub-problems recursively. If the subproblem is small
enough, then solve it directly.
Combine: Combine the solutions of the sub-problems that are part of the recursive
process to solve the actual problem.
Let us understand this concept with the help of an example.
Here, we will sort an array using the divide and conquer approach

Let the given array be:

Divide the array into two halves.

Again, divide each subpart recursively into two halves until you get individual
elements.

Now, combine the individual elements in a sorted manner.


Here, conquer and combine steps go side by side.
Divide and Conquer Vs Dynamic approach
The divide and conquer approach divides a problem into smaller subproblems;
these subproblems are further solved recursively. The result of each subproblem is
not stored for future reference, whereas, in a dynamic approach, the result of each
subproblem is stored for future reference.
Use the divide and conquer approach when the same subproblem is not solved
multiple times. Use the dynamic approach when the result of a subproblem is to be
used multiple times in the future.
Let us understand this with an example. Suppose we are trying to find the Fibonacci
series. Then,

Dynamic approach:

Advantages of Divide and Conquer Algorithm


The complexity for the multiplication of two matrices using the naive method
is O(n3), whereas using the divide and conquer approach (i.e. Strassen's matrix
multiplication) is O(n2.8074). This approach also simplifies other problems, such as the
Tower of Hanoi.
This approach is suitable for multiprocessing systems.
It makes efficient use of memory caches.
Introduction to Pointers in Data Structure
• Pointers are the variables that are used to store the location of value present
in the memory. A pointer to a location stores its memory address. The
process of obtaining the value stored at a location being referenced by a
pointer is known as dereferencing.
Why do We Need Pointers in Data Structure?
Optimization of our code and improving the time complexity of one algorithm. Using
pointers helps reduce the time needed by an algorithm to copy data from one place
to another. Since it used the memory locations directly, any change made to the
value will be reflected at all the locations.
Control Program Flow: Another use of pointers is to control the program flow.
This is implemented by control tables that use these pointers.
These pointers are stored in a table to point to each subroutine’s entry point to be
executed one after the other.
These pointers reference the addresses of the various procedures. This helps while
working with a recursive procedure or traversal of algorithms where there is a need
to store the calling step’s location.
TYPES OF POINTERS
• NULL Pointer: Such type of pointer is used to indicate that this points to an
invalid object. This type of pointer is often used to represent various
conditions such as the end of a list.
• VOID Pointer: This type of pointer can be used to point to the address of
any type of variable, but the only limitation is that it cannot be dereferenced
easily.
• WILD Pointer: It is a type of pointer which doesn’t hold the address of any
variable.
• Dangling Pointer: The type of pointers that don’t refer to a valid object and
are not specifically initialized to point a particular memory. For ex: int *ptr1 =
malloc(sizeof(char))
• Function pointer: This is a type of pointer to reference an executable code.
It is mostly used in the recursive procedure that holds the address of the
code that needs to be executed later.
Memory Allocations in Data Structures
Memory allocation is the process of setting aside sections of memory in a
program to be used to store variables, and instances of structures and classes.

Dynamic memory allocation is when an executing program requests that


the operating system give it a block of main memory. The program then uses this
memory for some purpose.

Usually the purpose is to add a node to a data structure. In object-oriented


languages, dynamic memory allocation is used to get the memory for a new object.

The memory comes from above the static part of the data segment.
Programs may request memory and may also return previously dynamically
allocated memory. Memory may be returned whenever it is no longer needed.
Memory can be returned in any order without any relation to the order in which it
was allocated. The heap may develop "holes" where previously allocated memory
has been returned between blocks of memory still in use.

A new dynamic request for memory might return a range of addresses out of
one of the holes. But it might not use up all the hole, so further dynamic requests
might be satisfied out of the original hole.
Array Data Structures in Python
An array is a fundamental data structure available in most programming languages,
and it has a wide range of uses across different algorithms.

Python’s array module provides space-efficient storage of basic C-style data types
like bytes, 32-bit integers, floating point numbers, and so on.

Arrays created with the array.array class are mutable and behave similarly to lists,
except for one important difference — they are “typed arrays” constrained to a
single data type.

Because of this constraint, array.array objects with many elements are more space-
efficient than lists and tuples. The elements stored in them are tightly packed, and
this can be useful if we need to store many elements of the same type.

Also, arrays support many of the same methods as regular lists, and we might be
able to use them as a “drop-in replacement” without requiring other changes to our
application code.

• If we want to store arbitrary objects, with mixed data types, list or


a tuple object can be used.
• We can try out the array.array when we’ve numeric data and tight packing
along with performance is important.
• We can use the built-in str objects to represent textual data as Unicode
characters.
• Immutable bytes type, or bytearray can come in handy when we want to
store contiguous block of bytes.
What is a Data Structure?
• Organizing, managing and storing data is important as it enables easier
access and efficient modifications. Data Structures allows you to organize
your data in such a way that enables you to store collections of data, relate
them and perform operations on them accordingly.

Types of Data Structures in Python


Python has implicit support for Data Structures which enable you to store and
access data. These structures are called List, Dictionary, Tuple and Set.
Python allows its users to create their own Data Structures enabling them to
have full control over their functionality. The most prominent Data Structures are
Stack, Queue, Tree, Linked List and so on which are also available to you in other
programming languages. So now that you know what are the types available to you,
why don’t we move ahead to the Data Structures and implement them using
Python.
STRUCTURE OVERVIEW

Lists
• Lists are used to store data of different data types in a sequential manner.
There are addresses assigned to every element of the list, which is called as
Index.
• The index value starts from 0 and goes on until the last element called
the positive index.
• There is also negative indexing which starts from -1 enabling you to access
elements from the last to first.
Dictionary
• Dictionaries are used to store key-value pairs. To understand better, think of
a phone directory where hundreds and thousands of names and their
corresponding numbers have been added.
• Now the constant values here are Name and the Phone Numbers which are
called as the keys. And the various names and phone numbers are the values
that have been fed to the keys.
• If you access the values of the keys, you will obtain all the names and phone
numbers. So that is what a key-value pair is. And in Python, this structure is
stored using Dictionaries.
Sets
• Sets are a collection of unordered elements that are unique. Meaning that
even if the data is repeated more than one time, it would be entered into the
set only once.
• It resembles the sets that you have learnt in arithmetic. The operations also
are the same as is with the arithmetic sets.

You might also like