Algorithms Lectures

You might also like

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

Syrian Arab Republic

Albaath university
Faculty of Mechanical and Electrical Engineering
Department of automatic control and computers
Department of Electronic and Communication

Algorithms and data structures

First lecture

By: Dr. Hayyan Hasan

Algorithms and data structures - First semester 2023-2024 1


Course outlines

• Introduction.

• Data structures.
• Liner data structures.

• Non liner data structures.

Algorithms and data structures - First semester 2023-2024 2


Introduction

Algorithms and data structures - First semester 2023-2024 3


Outline

• Motivation.

• Data structures definition.

• The needs for data structures.

• Abstract Data Types (ADTs).

• Types of data structures.


• Linear data structures.
• Non linear data structures.

• The advantages of a data structure.

• Terminologies.

• Asymptotic Notations.

Algorithms and data structures - First semester 2023-2024 4


Motivation

• If you go out shopping at a grocery store,


• you would expect related items to be in the same section, such as the
fruit and vegetable section, the meat section, etc.
• These items can be further organized according to their prices and so
on.
• Such meaningful structuring of items helps the business operate
efficiently.
• Because you (or anyone) would never visit (or re-visit) a messy store
where it takes forever to find each of your required items.
• The data in the memory must be sorted in some efficient ways.

Algorithms and data structures - First semester 2023-2024 5


Data structures definition

• Data structures are an integral part of computers used for the


arrangement of data in memory.
• They are essential and responsible for organizing, processing,
accessing, and storing data efficiently.
• Data presentation must be easy to understand so the developer, as well
as the user, can make an efficient implementation of the operation.
• Data structures provide an easy way of organizing, retrieving,
managing, and storing data.

Algorithms and data structures - First semester 2023-2024 6


The needs for data structures

• Data structure modification is easy.


• It requires less time.
• Save storage memory space.
• Data representation is easy.
• Easy access to the large database.

Algorithms and data structures - First semester 2023-2024 7


Abstract Data Types (ADTs)

• Before defining abstract data types, let us consider the different view of system-defined data
types. We all know that, by default, all primitive data types (int, float, etc.) support basic
operations such as addition and subtraction. The system provides the implementations for
the primitive data types.
• For user-defined data types we also need to define operations. The implementation for these
operations can be done when we want to actually use them. That means, in general, user
defined data types are defined along with their operations.
• To simplify the process of solving problems, we combine the data structures with their
operations and we call this Abstract Data Types (ADTs). An ADT consists of two parts:
• Declaration of data
• Declaration of operations
• For example, stack uses a LIFO (Last-In-First-Out) mechanism while storing the data in
data structures. The last element inserted into the stack is the first element that gets deleted.
Common operations are: creating the stack, push an element onto the stack, pop an element
from the stack, finding the current top of the stack, finding the number of elements in the
stack, etc.

Algorithms and data structures - First semester 2023-2024 8


Types of data structures

Algorithms and data structures - First semester 2023-2024 9


Types of data structures Cont.

• Linear data structure: Data structure in which data elements are


arranged sequentially or linearly, where each element is attached to its
previous and next adjacent elements, is called a linear data structure.
• Examples of linear data structures are array, stack, queue, linked list, etc.
• Two types
• Static data structure
• Static data structure has a fixed memory size.
• Here the size of the data structure is allocated in the memory during the compile-time
thereby rendering the allocated size fixed.
• This results in the maximum size of such data structures needing to be known in advance, as
memory cannot be reallocated at a later point. That’s why they are called static.
• An example of this data structure is an array.

Algorithms and data structures - First semester 2023-2024 10


Types of data structures Cont.

• Dynamic data structure


• In dynamic data structure, the size is not fixed. It can be randomly
updated during the runtime which may be considered efficient
concerning the memory (space) complexity of the code.
• As the memory can always be reallocated depending on the
requirements, these data structures are called dynamic.
• Examples of this data structure are queue, stack, etc.

Algorithms and data structures - First semester 2023-2024 11


Types of data structures Cont.

• Non-linear data structure


• Data structures where data elements are not placed sequentially or
linearly are called non-linear data structures.
• In a non-linear data structure, we can’t traverse all the elements in a
single run only.
• Examples of non-linear data structures are trees and graphs.

Algorithms and data structures - First semester 2023-2024 12


Linear data structures

• Array
• An array is a linear data structure and it is a collection of items stored
at contiguous memory locations.
• The idea is to store multiple items of the same type together in one
place.
• It allows the processing of a large amount of data in a relatively short
period.
• The first element of the array is indexed by a subscript of 0.
• There are different operations possible in an array, like Searching,
Sorting, Inserting, Traversing, Reversing, and Deleting.

Algorithms and data structures - First semester 2023-2024 13


Linear data structures Cont.

• Linked List
• Instead of allocating the contiguous memory blocks in advance,
how about we wrap our data in a node that spits out a reference
that we can use to point to the next node.
• A linked list is a linear data structure in which elements are not
stored at contiguous memory locations. The elements in a
linked list are linked using pointers that point to the next
elements.
• Types of linked list
• Singly-linked list.
• Doubly linked list.
• Circular linked list.
• Doubly circular linked list.

Algorithms and data structures - First semester 2023-2024 14


Linear data structures Cont.

• Queue
• Queue is a linear data structure that follows a particular order in which the
operations are performed.
• The order is First In First Out (FIFO) i.e., the data item stored first will be
accessed first.
• An example of a queue is any queue of consumers for a resource where
the consumer that came first is served first.
• The entering and retrieving of data is also called enqueue and dequeue
operation in a stack.
• Types of Queue
• Simple Queue.
• Circular Queue.
• Priority Queue.
• Double Ended Queue.

Algorithms and data structures - First semester 2023-2024 15


Linear data structures Cont.

• Queue
• This idea of a queue might have been inspired by a real-world standing queue
in front of a grocery store counter or similar scenarios.

Algorithms and data structures - First semester 2023-2024 16


Linear data structures Cont.

• Stack
• Stack is a linear data structure that follows a particular order in which
the operations are performed.
• The order is Last In First Out (LIFO) Entering and retrieving data is
possible from only one end.
• The entering and retrieving of data is also called push and pop
operation in a stack.

Algorithms and data structures - First semester 2023-2024 17


Linear data structures Cont.

• Stack
• This idea of the stack is probably inspired by a real-world stack of books

Algorithms and data structures - First semester 2023-2024 18


Non-linear data structures

• Non-linear data structures are not arranged sequentially in that each


element of such data structures can have multiple paths to connect to
other elements or form a hierarchy.
• Tree
• A tree is a non-linear and hierarchal data structure where the elements are arranged in a
tree-like structure.
• In a tree, the topmost node is called the root node. Each node contains some data, and
data can be of any type.
• It consists of a many nodes which are connected via edges.

Algorithms and data structures - First semester 2023-2024 19


Non-linear data structures Cont.

• Tree
• Different tree data structures allow quicker and easier
access to the data as it is a non-linear data structure.
• A tree has various terminologies like Node, Root, Edge,
Height of a tree, Degree of a tree, etc.
• Types of tree
• Binary tree.
• AVL tree.
• Red-Black Tree.
• Etc.

Algorithms and data structures - First semester 2023-2024 20


Non-linear data structures Cont.

• Graph
• A graph is a non-linear data structure that consists of vertices (or nodes) and
edges. It consists of a finite set of vertices and set of edges that connect a pair
of nodes.
• A graph can have cycles, disjoints, and all kinds of flexibility on top of a tree.
Hence, it's important to remember that a tree falls under a special category of
graphs.
• It can be so powerful that it has a dedicated branch named 'Graph Theory’ in
mathematics.
• Graph is used to solve the most challenging and complex programming
problems. It has different terminologies which are Path, Degree, Adjacent
vertices, Connected components, etc.

Algorithms and data structures - First semester 2023-2024 21


Non-linear data structures Cont.

• Graph

Algorithms and data structures - First semester 2023-2024 22


The advantages of a data structure

• Efficiency: If the choice of a data structure for implementing a


particular DT is proper, it makes the program very efficient in terms
of time and space.
• Reusability: The data structure provides reusability means that
multiple client programs can use the data structure.
• Abstraction: The data structure also provides the level of abstraction.
The client cannot see the internal working of the data structure, so it
does not have to worry about the implementation part. The client can
only see the interface.

Algorithms and data structures - First semester 2023-2024 23


Terminologies

• Time Complexity
• Time taken by the algorithm to solve the problem. It is measured by
calculating the iteration of loops, number of comparisons etc.
• Space Complexity
• Space taken by the algorithm to solve the problem.
• It includes space used by necessary input variables and any extra
space that is used by the algorithm.

Algorithms and data structures - First semester 2023-2024 24


Asymptotic Analysis

• Asymptotic analysis of an algorithm refers to defining the


mathematical boundation /framing of its run-time performance.
• Using asymptotic analysis, we can very well conclude the best case,
average case, and worst-case scenario of an algorithm.
• Usually, the time required by an algorithm falls under three types:
• Best Case: Minimum time required for program execution.
• Average Case: Average time required for program execution.
• Worst Case: Maximum time required for program execution.

Algorithms and data structures - First semester 2023-2024 25


Asymptotic Notations

• The commonly used asymptotic notations to calculate the


running time complexity of an algorithm.
• Big Oh Notation: Ο Notation.
• Omega Notation: Ω Notation.
• Theta Notation: θ Notation.

Algorithms and data structures - First semester 2023-2024 26


Asymptotic Notations Cont.

• Big Oh Notation, Ο
• The notation Ο(n) is the formal way to express the upper bound of an
algorithm's running time.
• It measures the worst case time complexity or the longest amount of time an
algorithm can possibly take to complete.
• Omega Notation, Ω
• The notation Ω(n) is the formal way to express the lower bound of an
algorithm's running time.
• It measures the best case time complexity or the best amount of time an
algorithm can possibly take to complete.

Algorithms and data structures - First semester 2023-2024 27


Asymptotic Notations Cont.

• Theta Notation, θ
• The notation θ(n) is the formal way to express both the lower bound and the
upper bound of an algorithm's running time.
• It measures the average case time complexity or the average amount of time
an algorithm can possibly take to complete.

Algorithms and data structures - First semester 2023-2024 28

You might also like