Algorithms Lectures
Algorithms Lectures
Albaath university
Faculty of Mechanical and Electrical Engineering
Department of automatic control and computers
Department of Electronic and Communication
First lecture
• Introduction.
• Data structures.
• Liner data structures.
• Motivation.
• Terminologies.
• Asymptotic Notations.
• 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.
• 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.
• 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.
• 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.
• 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.
• 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.
• Stack
• This idea of the stack is probably inspired by a real-world stack of books
• 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.
• 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.
• Graph
• 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.
• 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.
• 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.