Download as pdf or txt
Download as pdf or txt
You are on page 1of 51

Unit 19 :

Data Structures And Algorithms

Ms. Rangi Dahanayake


MSc.(Computer Science), BSc.(Software Engineering)
Lecturer
Faculty of Computing

Email: rangi.dahanayake@cinec.edu
Unit 19 : Data Structures and Algorithm

Unit Code T/618/7430


Unit Type Core
Unit Level 5
Credit Value 15
Methodology Combination of Lectures, Practical and Tutorials
Scheme of Evaluation Assignment
Lecturer Ms. Rangi Dahanayake
Lecturer Email rangi.dahanayake@cinec.edu
Learning Outcomes
• By the end of this unit students will be able to:

LO1 Examine abstract data types, concrete data structures and algorithms
LO2 Specify abstract data types and algorithms in a formal notation
LO3 Implement complex data structures and algorithms
LO4 Present Assess the effectiveness of data structures and algorithms
Chapter 01
Introduction
What is a Data Structure?
• Data structure is a representation of data and the operations allowed
on that data.

• A data structure is a way to store and organize data in order to


facilitate the access and modifications.

• Data Structure are the method of representing of logical relationships


between individual data elements related to the solution of a given
problem.
Basic Data Structures

Basic Data Structures

Linear Data Structures Non-Linear Data Structures

Arrays Linked Lists Stacks Queues Trees Graphs Hash Tables


Basic Data Structures
• Linear Data Structures :
- Imagine a line of people waiting for ice cream. This line represents a linear data structure.
People (data values) are arranged one after another, and how we access them depends on the specific
structure:
Basic Data Structures
Linear Data Structures
➢ Array: Fixed-size
➢ Linked-list: Variable-size
➢ Stack: Add to top and remove from top
➢ Queue: Add to back and remove from front

• Non-Linear: The data values in this structure are not arranged in order.
➢ Hash tables: Unordered lists which use a ‘hash function’ to insert and search
➢ Tree: Data is organized in branches.
➢ Graph: A more general branching structure, with less strict connection
conditions than for a tree
Selection of Data Structures
• The choice of particular data model depends on two consideration:

➢ It must be rich enough in structure to represent the relationship between data


elements.

➢ The structure should be simple enough that one can effectively process the
data when necessary
Represent Data Structure
• We can represent Data Structures in two ways.:

- Mathematical/Abstract model : This is like a blueprint or a recipe for the


data structure. It defines the essential properties and behaviors of the
structure, without worrying about the specific details of how it will be
implemented in a computer program.

- Implementation: This is the actual code that creates the data structure in a
specific programming language. It takes the abstract model and translates it
into concrete instructions for the computer to follow. The implementation
details will vary depending on the programming language, the desired
performance characteristics, and other factors.
Abstract Data Type
Imagine you have a toolbox.

The toolbox itself is the ADT: It holds different tools (data) and allows you to
perform specific actions with them (operations).
You don't need to know how the tools are built inside (implementation): You
just know what each tool does and how to use it (abstract model).
For example:
A hammer: Tool for hitting nails (data). You swing it and hit something
(operation).
A screwdriver: Tool for turning screws (data). You twist it in a specific way
(operation).
Abstract Data Type

So, in simpler terms:


- Abstract Data Types (ADTs) stores data and allow various operations on the data
to access and change it.

▪ ADTs are like toolboxes: They organize data and operations without showing how
they're built internally.
▪ Mathematical models: They describe ADTs using rules and logic, similar to a
toolbox blueprint.
▪ Operations: These are like "actions" you can perform on the data, like using the
tools in a toolbox.
▪ Abstraction: It's like focusing on what the ADT does, not how it does it. This
makes it easier to use and understand.
The Core Operations of ADTs
Every Collection ADT should provide a way to:
✓add an item
✓remove an item
ADT = Data + Operations
✓find, retrieve, or access an item

Many, many more possibilities


✓is the collection empty
✓make the collection empty
Arrays

Arrays acts to store related data under a single variable name with an
index.

It is easiest to think of an array as simply a list or ordered grouping of


variables.
Array Declaration
Syntax
type arr-name[];
OR
type[] arr-name;

Ex:
int intArray[];
or int[] intArray;
Example : Arrays

Note that array index numbers start at 0

If the array has n elements, the last array index number is n-1

n-1
Example: No of elements(n) = 5
0 1 2 3 4

age 5 6 8 7 9
Example : Arrays
You can define a variable called marks, which
represents not a single value of a marks, but an marks
entire set of grades.
0 14
Each element of the set can then be referenced 1 56
by means of a number called an index
2 44
If you need the marks of the 1st student, it is at: 3 36
marks[0] 4 89
If you need the marks of the 4th student, it is at:
5 43
marks[3]
Index Number
Assigning Values to Array Elements
marks[0] = 14 marks
marks[1] = 56
marks[2] = 44 0 14
marks[3] = 36 1 56
marks[4] = 89 2 44
marks[5] = 43 3 36
4 89
The values may come as an input from user 5 43
or from a file etc..

Index Number
Assigning Values to Array Elements
marks
You can also assign the values at the time you are
declaring the array. 0 14
int marks[6] = {14,56,44,36,89,43} 1 56
2 44
Determined the number of elements automatically 3 36
based on the number of initialization elements 4 89
int marks[] = {14,56,44,36,89,43} 5 43

Index Number
Stack
• Allows access to only one data item; the last item inserted

• If you remove this item, then you can access the next-to-last item
inserted
Stack
In a stack all insertions and deletions are made at
one end (Top). Insertions and deletions are
restricted from the Middle and at the End of a Stack
Top
Adding an item is called Push
Removing an item is called Pop

Elements are removed from a Stack in the reverse


order of that in which the elements
were inserted into the Stack
The elements are inserted and removed according
to the Last-In-First-Out (LIFO) principle.
Stack - Push

Push item 49

Before inserting Item 49 is inserted on top After inserting


Stack - Pop

Pop

Before removing Top item is removed After removing


Stack - Peek
49

Peek

Stack remains the same

Peek is used to read the value from the top of the stack without removing it. You can peek
only the Top item, all the other items are invisible to the stack user.
Activity 01
Draw the stack frame after performing the below operations to the stack given
below.

25 Top
300

500
Push item 50
Push item 500
Peek
Push item 100 Push
Pop 50
25
Pop
Pop 300
Pop
Queues
In a queue all insertions are made at the Rear end
and deletions are made at the Front end.
Insertions and deletions are restricted from the
Middle of the Queue.

Adding an item is called insert


Removing an item is called remove

The elements are inserted and removed according to


the First-In-First-Out (FIFO) principle.
Queue - Insert

5 4 3 2 1 0 5 4 3 2 1 0 5 4 3 2 1 0
15 27 39 23 15 27 39 23 45 15 27 39 23

Rear Front Rear Front Rear Front


45

New Element
(Insert)

Before inserting Item 45 is inserted to the rear After inserting


Queue - Remove

5 4 3 2 1 0 5 4 3 2 1 0 5 4 3 2 1 0
15 27 39 23 15 27 39 23 15 27 39

Rear Front Rear Front Rear Front

23

Before removing Front item is removed After removing


Queue – Peek Front

5 4 3 2 1 0 5 4 3 2 1 0
15 27 39 23 15 27 39 23
Peek → 23

Rear Front Rear Front

Queue remains the same

Peek is used to read the value from the Front of the queue without removing it.
You can peek only the Front item, all the other items are invisible to the queue
user.
Activity 02
Draw the Queue frame after performing the below operations to the queue
given below.
6 5 4 3 2 1 0
24 67 99 47 60

Rear Front

i) Insert item 33
ii) Insert item 53
iii) Peek Front
iv) remove
v) remove
vi) remove
vii) remove
viii) remove
Activity 03
Draw the Queue frame after performing the below operations to the queue
given below.
6 5 4 3 2 1 0
67 12 22 55 34 70 60

Rear Front

i) remove
ii) remove
iii) remove
iv) Insert item 88
What is Circular Queue?
The operations are performed based on FIFO (First In First Out) principle. It is
also called ‘Ring Buffer’.

In a normal Queue, we can insert elements until queue


becomes full. But once queue becomes full, we can not
insert the next element even if there is a space in front of
queue.
Explanation of Circular Queue
Activity
Draw the Queue frame after performing the below operations to the circular queue
given below.
insert(14);
insert(29);
insert(33);
insert(88);
peekFront();
remove();
remove();
insert(90);
insert(100);
peekFront();
Linked List
Linked lists are probably the second most commonly used general purpose
storage structures after arrays.

Linked List Link Link Link Link


Data Data Data Data
first Null
next next next next

In a linked list each data item is embedded in a link.


There are many similar links.
Each link object contains a reference to the next link in the list.
In a typical application there would be many more data items in a link.
Linked List
Mainly the following operations can be performed
on a linked list.

- Find
Find a link with a specified key value.
- Insert
Insert links anywhere in the list.
- Delete
Delete a link with the specified value.
Linked List
Mainly the following operations can be performed
on a linked list.

- Find
Find a link with a specified key value.
- Insert
Insert links anywhere in the list.
- Delete
Delete a link with the specified value.
Operations - Find
Start with the first item, go to the second link, then the third, until you find what you are looking for.

Current Value = Find Value X


Current = Current Value.Next

Step 1 : Ex: Find Item 35


first 120 17 35 88 NULL Found ?
No
current
Step 2 :
first 120 17 35 88 NULL Found ?
No
current
Step 3 :
first 120 17 35 88 NULL Found ?
Yes
current
Operations - Insert
Inserting an item at the beginning of the list
Before inserting first 120 17 NULL

Step 1 : create a new link 55

Step 2 : ‘next’ field of the new link points to the old first link

first 120 17 NULL

55

Step 3 : ‘first’ points to the newly created link

first 120 17 NULL


55
Operations - Insert
Inserting an item in the middle of the list

Before inserting first 120 17 63 NULL

Question 05:

What steps need to be followed if a new link is inserted after the link ‘17’ ?
Operations - Insert
Inserting an item in the middle of the list

Before inserting first 120 17 63 NULL

first 120 17 63 NULL

55
Operations - Delete
Deleting an item from the beginning of the list
First Value = FirstValue.Next

Step 1 : Save reference to first link


first 120 17 35 NULL

temp

Step 2 : Disconnect the first link by rerouting first to point to the second link

first 120 17 35 NULL

temp

Step 3 : Return the deleted link (temp)


Operations - Delete
Deleting an item from the beginning of the list

first 120 17 35 NULL

Question 06:

What steps need to be followed to delete the link ‘17’ ?


Operations - Delete
Deleting an item from the beginning of the list

Search Value = SearchValue.Next

first 120 17 35 NULL

first 120 17 35 NULL

first 120 17 35 NULL


Trees
A Tree is a collection of elements called nodes.
One of the node is distinguished as a root,
Root
along with a relation (“parenthood”) that
places a hierarchical structure on the nodes.
Introduction to Algorithms
Problem – Set of work that must be done with
human.

Algorithm and programming can solve


problems.

Programs are implementations of algorithms


using any programming language.

Algorithm is a sequence of steps to solve


problems.
Definitions

❖ ALGORITHMS
Step by step procedure which can be applied to data to achieve some goal

❖ PROGRAM
Implements an algorithm

❖ DATA STRUCTURES
The manner in which data is represented in the computer to facilitate access and
manipulate by an algorithm.
What are data structures and algorithms good for?

Real-world data storage


To keep the details of set of people

Programmers' tools
The data structures that are meant for access of the program itself
Stack, Queue…

Real-world modelling
to model real world situations
Graphs, queues…
Algorithm Analysis

A framework is needed to compare the performance and efficiency of different


algorithms and data structures

Big ‘O’ notation


It’s useful to have a shorthand way to say how efficient a computer algorithm is.
In computer science this measure is called Big O notation.

Big O notation is a way to describe how the running time of an algorithm grows as
the size of the input grows.
Thank You

You might also like