You are on page 1of 27

DATA STRUCTURES

AND
ALGORITHMS
DCS/CSC -204

Lecturer: Ntaye Emmanuel

www.ubids.edu.gh.com 1
AIM
❖ Introduce students to :

❖The fundamental building blocks for efficient computational


problems solution design.
✓Data structures
✓Algorithms
❖The course will focus on the logical structures of data, its design,
implementation and analysis of the complexity of their
implementing algorithms.

www.ubids.edu.gh.com 2
Course Outline
❑Definition of Data structure
❑Types of Data Structures
❑ Implementations in C++
✓ List
✓ Stacks
✓ Queue

❑ Algorithms and Operations


❑Time Complexity Analysis with Big O

www.ubids.edu.gh.com 3
Reading Materials:
1. Jay Wengrow (2020) A Common-Sense Gide to Data Structures
and Algorithms (Second Edition). The Pragmatic Programmers,
LLC.

2. Narashimha Karumanchi (2017) Data Structures and Algorithms


Made Easy. Career Monk.

3. Mark Allen Weiss (2014) Data Structures and Algorithms Analysis


in C++; Peason.

4. Robert Sedgewick and Kevin Wayne (2011) Algorithms. Addison-


Wesley.

www.ubids.edu.gh.com 4
What is Data Structure?
❖Data Structure is a particular way arranging, storing and
organizing data in the memory of the computer so that the data
can easily be retrieved and efficiently utilized in the future when
required.

❖Data structures define the relationship between the data


elements, how they are stored in memory, and the operations that
can be performed on them. They are designed to optimize the
efficiency of common operations such as insertion, deletion,
retrieval, and traversal of data.

www.ubids.edu.gh.com 5
What is Data Structure?
❖The choice of data structure for a particular task depends on
the type and amount of data to be processed, the operations
that need to be performed on the data, and the efficiency
requirements of the program.

❖Efficient use of data structures can greatly improve the


performance of a program, making it faster and more
memory-efficient.

❖An efficient data structure also uses minimum memory


space and execution time to process the structure.

www.ubids.edu.gh.com 6
Why should we learn Data Structures?
1) Data Structures and Algorithms are two of the key aspects of
Computer Science.

2) Data Structures allow us to organize and store data, whereas


Algorithms allow us to process that data meaningfully.

3) Learning Data Structures and Algorithms will help us become


better Programmers.

4) We will be able to write code that is more effective and


reliable.

5) We will also be able to solve problems more quickly and


efficiently
www.ubids.edu.gh.com 7
Objectives of Data Structures

• Correctness: Data Structures are designed to operate correctly for


all kinds of inputs based on the domain of interest. In order words,
correctness forms the primary objective of Data Structure, which
always depends upon the problems that the Data Structure is meant
to solve.

• Efficiency: Data Structures also requires to be efficient. It should


process the data quickly without utilizing many computer
resources like memory space. In a real-time state, the efficiency of
a data structure is a key factor in determining the success and
failure of the process

www.ubids.edu.gh.com 8
Significant Features of Data Structures

• Robustness: Generally, all computer programmers aim to produce


software that yields correct output for every possible input, along
with efficient execution on all hardware platforms. This type of
robust software must manage both valid and invalid inputs.

• Adaptability: Building software applications like Web Browsers,


Word Processors, and Internet Search Engine include huge software
systems that require correct and efficient working or execution for
many years. Moreover, software evolves due to emerging
technologies or ever-changing market conditions.

www.ubids.edu.gh.com 9
Significant Features of Data Structures

• Reusability: The features like Reusability and Adaptability go


hand in hand. It is known that the programmer needs many
resources to build any software, making it a costly enterprise.
However, if the software is developed in a reusable and adaptable
way, then it can be applied in most future applications. Thus, by
executing quality data structures, it is possible to build reusable
software, which appears to be cost-effective and timesaving

www.ubids.edu.gh.com 10
Basic terminologies related to Data Structures
Data Structures are the building blocks of any software or
program. Selecting the suitable data structure for a program is an
extremely challenging task for a programmer.
✓Data: We can define data as an elementary value or a
collection of values. For example, the Employee's name and
ID are the data related to the Employee.
✓Data Items: A single unit of value is known as Data Item.

✓ Group Items: Data Items that have subordinate data items


are known as Group Items. For example, an employee's name
can have a first, middle, and last name.

www.ubids.edu.gh.com 11
Basic terminologies related to Data
Structures
✓ Field: A single elementary unit of information symbolizing the
Attribute of an Entity is known as Field.
✓ Record: A collection of different data items are known as a
Record. For example, if we talk about the employee entity, then
its name, id, address, and job title can be grouped to form the
record for the employee.
✓Elementary Items: Data Items that are unable to divide into sub-
items are known as Elementary Items. For example, the ID of an
Employee.
✓Entity and Attribute: A class of certain objects is represented by
an Entity. It consists of different Attributes. Each Attribute
symbolizes the specific property of that Entity. For example
www.ubids.edu.gh.com 12
✓File: A collection of different records of one entity type is known as a
File. For example, if there are 100 employees, there will be 25 records in
the related file containing data about each employee

www.ubids.edu.gh.com 13
TYPES OF DATA STRUCTURES
Data Structures

Primitive Non-Primitive
Structure Data Structure

Linear Non-linear
int
(Sequential) (Random)

float Static Dynamic Trees Graphs

char Arrays Stacks Lists Queues Binary Directional graphs

One-Dimensional Non-directional
duoble Siingle-linked list Binary Search Trees
Arrays graphs

Two -Dimensioanl
Pointer Double-Linked List B-trees
Arrays

Multi-dimensional
Arrays Circular-Linked List AVL tree
www.ubids.edu.gh.com 14
Primitive Data Structures
❖Primitive data structures: These are the most basic and
predefined data structures and are usually built into programming
languages.
❖Examples include:
✓Integer
✓Float
✓Character
✓Boolean
✓Double
✓Pointer
✓Void www.ubids.edu.gh.com 15
Non-Primitive data structures
❖Non-primitive data structures: These are complex data structures
that are built using primitive data types.
❖ Non-primitive data structures can be further categorized into the
following types:
✓Arrays: A collection of elements of the same data type, stored in
contiguous memory locations.
✓Linked lists: A collection of elements that are connected by links
or pointers.
✓Stacks: A collection of elements that follow the Last-In-First-Out
(LIFO) principle.

www.ubids.edu.gh.com 16
Non-Primitive data structures
✓Queues: A collection of elements that follow the First-In-
First-Out (FIFO) principle.
✓Trees: A hierarchical data structure consisting of nodes
connected by edges.
✓Graphs: A non-linear data structure consisting of nodes and
edges.

www.ubids.edu.gh.com 17
Differences between
Primitive and Non-Primitive Data Structures
Primitive Data Types Non-Primitive Data Types
1. Primitive data types are 1. Non-primitive data types are
predefined and provided by the
created by the programmer and are
programming language. also called user-defined data types.
2. They are atomic and store a
2. They are composite and store a
single value. collection of values or objects.
3.Primitive data types have a
3. Non-primitive data types can
fixed size in memory. have varying sizes depending on the
number of elements or objects they
contain.
4. They are passed by value 4. They are passed by reference,
when used as arguments in which means a reference to the
methods or functions. original object is passed rather than
the object itself.
www.ubids.edu.gh.com 18
Differences between
Primitive and Non-Primitive Data Structures
5. Primitive data types are stored 1. Non-primitive data types are
on the stack. stored on the heap.
6. They are initialized with default 6. Non-primitive data types need
values automatically (e.g., 0 for to be explicitly initialized by the
integers, false for booleans). programmer.
7. Primitive data types include 7. Non-primitive data types
integers, floating-point numbers, include arrays, strings, classes,
characters, booleans, structures, etc.
etc.
8. They have a limited set of 8. Non-primitive data types can
operations that can be performed have custom operations defined
directly on them (e.g., arithmetic by the programmer through
operations on numbers). methods or functions. 19
www.ubids.edu.gh.com
Differences between
Primitive and Non-Primitive Data Structures
9. Primitive data types are 9. Non-primitive data types may
generally faster to access and require more complex operations
manipulate. and have a higher overhead in
terms of performance.
10. They have a shorter lifespan 10. Non-primitive data types can
as they are short-lived and are have a longer lifespan as they can
destroyed as soon as they go out be dynamically allocated and
of scope. deallocated during program
execution

www.ubids.edu.gh.com 20
www.ubids.edu.gh.com 21
Linear Data Structure

❖ A Linear data structure is a type of data structure in which


data elements are arranged in a sequential order, and each
element has a unique predecessor and successor, except for the
first and last elements. Linear data structures are one-
dimensional and can be traversed sequentially from the first to
the last element. Elements are arranged in one dimension ,also
known as linear dimension.
Element 1 Element 2 Element 3 Element 4 Element 5 Element 5 ...

❖ Example: Arrays, lists, stack, queue, etc.

www.ubids.edu.gh.com 22
Linear Data Structure

Arrays Queues

Single-Linked list
Stacks www.ubids.edu.gh.com 23
Non-Linear Data Structure
❖ Non-linear data structure is a type of data structure in which
data elements are not arranged in a sequential order, and each
element may have one or more predecessors and successors.

❖ Non-linear data structures can represent complex relationships


between data elements, such as hierarchies, networks, and
graphs.

❖Elements are arranged in one-many, many-one and many-many


dimensions.

❖Example: tree, graph, table, etc.


www.ubids.edu.gh.com 24
Difference between Linear and Non-Linear
Data Structures
Linear Data Structures Non-Linear Data Structures
1. Data elements are arranged in a sequential 1. Data elements are not arranged in a
manner where each element has a unique sequential order and may have multiple
predecessor and successor. predecessors or successors.

2. Examples include arrays, linked lists, 2. Examples include trees and graphs.
stacks, and queues.
3. Linear data structures have a simple 3. Non-linear data structures have a
organizational structure. more complex organizational structure.

4. Elements can be accessed and processed 4. Elements may be accessed and


in a sequential manner. processed in various orders, depending
on the structure.
www.ubids.edu.gh.com 25
Difference between Linear and Non-Linear
Data Structures
5. Linear data structures have a 5. Non-linear data structures
fixed traversal order from the start can have multiple traversal
to the end. paths, loops, or cycles.
6. Insertion and deletion of 6. Insertion and deletion
elements are relatively operations can be more
straightforward and can be done at complex and depend on the
the beginning, end, or specific structure of the data.
positions.
7. Linear data structures are 7. Non-linear data structures
typically one-dimensional. can be multi-dimensional, with
relationships between multiple
elements.
www.ubids.edu.gh.com 26
Difference between Linear and Non-Linear
Data Structures
8. Searching for an element is usually 8. Searching for an element may
straightforward and involves traversing involve more complex algorithms,
through the structure sequentially. such as depth-first search or breadth-
first search.
9. Linear data structures have a simpler 9. Non-linear data structures often
memory allocation and deallocation require dynamic memory allocation
process. and more complex memory
management.
10. Linear data structures are suitable for 10. Non-linear data structures are
scenarios where the order and sequence of suitable for scenarios with complex
elements matter. relationships and interconnections
between elements

www.ubids.edu.gh.com 27

You might also like