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

Data Structures and Algorithm Lecture

Uploaded by

njokojunior1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Data Structures and Algorithm Lecture

Uploaded by

njokojunior1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

HIBMAT BUEA

Course: Data Structures & Algorithms


Course Instructor: NGOLAH Kenneth
Level: HND1

CONTENT
What is Data Structure?

Needs for Data Structure

Data Structures - Algorithms Basics

Data Structures - Asymptotic Analysis

Data Structures - Greedy Algorithms

Data Structures - Divide and Conquer

Data Structures - Dynamic Programming

Data Structures and Algorithms – Arrays, LINK LIST, STACK, QUEUE

Data Structure - Expression Parsing

Data Structure and Algorithms Linear Search, binary search, Hash Table

Data Structure - Sorting Techniques

Data Structure - Graph Data Structure

Data Structure and Algorithms – Tree

Heap Data Structures

Data Structure - Recursion

What is Data Structure


Data Structure is a systematic way to organize data in order to use it efficiently.

Characteristics of a Data Structure


 Correctness − Data structure implementation should implement its interface correctly.

 Time Complexity − Running time or the execution time of operations of data structure
must be as small as possible.

 Space Complexity − Memory usage of a data structure operation should be as little as


possible.

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH [1]


Need for Data Structure
As applications are getting complex and data rich, there are three common problems
that applications face now-a-days.

 Data Search − Consider an inventory of 1 million(106) items of a store. If the application


is to search an item, it has to search an item in 1 million(106) items every time slowing
down the search. As data grows, search will become slower.

 Processor speed − Processor speed although being very high, falls limited if the data
grows to billion records.

 Multiple requests − As thousands of users can search data simultaneously on a web


server, even the fast server fails while searching the data.

To solve the above-mentioned problems, data structures come to rescue. Data can
be organized in a data structure in such a way that all items may not be required to
be searched, and the required data can be searched almost instantly.

Execution Time Cases


There are three cases which are usually used to compare various data structure's
execution time in a relative manner.

 Worst Case − This is the scenario where a particular data structure operation takes
maximum time it can take. If an operation's worst case time is ƒ(n) then this operation
will not take more than ƒ(n) time where ƒ(n) represents function of n.

 Average Case − This is the scenario depicting the average execution time of an operation
of a data structure. If an operation takes ƒ(n) time in execution, then m operations will
take mƒ(n) time.

 Best Case − This is the scenario depicting the least possible execution time of an
operation of a data structure. If an operation takes ƒ(n) time in execution, then the actual
operation may take time as the random number which would be maximum as ƒ(n).

Data Structures - Algorithms Basics


Algorithm is a step-by-step procedure, which defines a set of instructions to be
executed in a certain order to get the desired output. Algorithms are generally
created independent of underlying languages, i.e. an algorithm can be implemented
in more than one programming language.

From the data structure point of view, following are some important categories of
algorithms −

 Search − Algorithm to search an item in a data structure.

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH [2]


 Sort − Algorithm to sort items in a certain order.

 Insert − Algorithm to insert item in a data structure.

 Update − Algorithm to update an existing item in a data structure.

 Delete − Algorithm to delete an existing item from a data structure.

Characteristics of an Algorithm
Not all procedures can be called an algorithm. An algorithm should have the
following characteristics −

 Unambiguous − Algorithm should be clear and unambiguous. Each of its steps (or
phases), and their inputs/outputs should be clear and must lead to only one meaning.

 Input − An algorithm should have 0 or more well-defined inputs.

 Output − An algorithm should have 1 or more well-defined outputs, and should match the
desired output.

 Finiteness − Algorithms must terminate after a finite number of steps.

 Feasibility − Should be feasible with the available resources.

 Independent − An algorithm should have step-by-step directions, which should be


independent of any programming code.

How to Write an Algorithm?


There are no well-defined standards for writing algorithms. Rather, it is problem and
resource dependent. Algorithms are never written to support a particular
programming code.

As we know that all programming languages share basic code constructs like loops
(do, for, while), flow-control (if-else), etc. These common constructs can be used to
write an algorithm.

We write algorithms in a step-by-step manner, but it is not always the case.


Algorithm writing is a process and is executed after the problem domain is well-
defined. That is, we should know the problem domain, for which we are designing a
solution.

Algorithms can be represented in pseudo code or flow chart

Example (pseudo code)


Let's try to learn algorithm-writing by using an example.

Problem − Design an algorithm to add two numbers and display the result.
Step 1 − START
Step 2 − declare three integers a, b & c
LECTURES NOTES @ HIBMAT BUEA ©NGOLAH [3]
Step 3 − define values of a & b
Step 4 − add values of a & b
Step 5 − store output of step 4 to c
Step 6 − print c
Step 7 − STOP

Algorithms tell the programmers how to code the program. Alternatively, the
algorithm can be written as −
Step 1 − START ADD
Step 2 − get values of a & b
Step 3 − c ← a + b
Step 4 − display c
Step 5 − STOP

In design and analysis of algorithms, usually the second method is used to describe
an algorithm. It makes it easy for the analyst to analyze the algorithm ignoring all
unwanted definitions. He can observe what operations are being used and how the
process is flowing.

Writing step numbers, is optional.

We design an algorithm to get a solution of a given problem. A problem can be


solved in more than one ways.

Hence, many solution algorithms can be derived for a given problem. The next step
is to analyze those proposed solution algorithms and implement the best suitable
solution.

Example (Flow Chart)


See notes

Algorithm Analysis
Efficiency of an algorithm can be analyzed at two different stages, before
implementation and after implementation. They are the following −

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH [4]


 A Priori Analysis − This is a theoretical analysis of an algorithm. Efficiency of an
algorithm is measured by assuming that all other factors, for example, processor speed,
are constant and have no effect on the implementation.

 A Posterior Analysis − This is an empirical analysis of an algorithm. The selected


algorithm is implemented using programming language. This is then executed on target
computer machine. In this analysis, actual statistics like running time and space required,
are collected.

We shall learn about a priori algorithm analysis. Algorithm analysis deals with the
execution or running time of various operations involved. The running time of an
operation can be defined as the number of computer instructions executed per
operation.

Algorithm Complexity
Suppose X is an algorithm and n is the size of input data, the time and space used
by the algorithm X are the two main factors, which decide the efficiency of X.

 Time Factor − Time is measured by counting the number of key operations such as
comparisons in the sorting algorithm.

 Space Factor − Space is measured by counting the maximum memory space required by
the algorithm.

The complexity of an algorithm f(n) gives the running time and/or the storage space
required by the algorithm in terms of n as the size of input data.

Space Complexity
Space complexity of an algorithm represents the amount of memory space required
by the algorithm in its life cycle. The space required by an algorithm is equal to the
sum of the following two components −

 A fixed part that is a space required to store certain data and variables, that are
independent of the size of the problem. For example, simple variables and constants used,
program size, etc.

 A variable part is a space required by variables, whose size depends on the size of the
problem. For example, dynamic memory allocation, recursion stack space, etc.

Space complexity S(P) of any algorithm P is S(P) = C + SP(I), where C is the fixed
part and S(I) is the variable part of the algorithm, which depends on instance
characteristic I. Following is a simple example that tries to explain the concept −
Algorithm: SUM(A, B)
Step 1 - START
Step 2 - C ← A + B + 10
Step 3 - Stop

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH [5]


Here we have three variables A, B, and C and one constant. Hence S(P) = 1 + 3.
Now, space depends on data types of given variables and constant types and it will
be multiplied accordingly.

Time Complexity
Time complexity of an algorithm represents the amount of time required by the
algorithm to run to completion. Time requirements can be defined as a numerical
function T(n), where T(n) can be measured as the number of steps, provided each
step consumes constant time.

For example, addition of two n-bit integers takes n steps. Consequently, the total
computational time is T(n) = c ∗ n, where c is the time taken for the addition of two
bits. Here, we observe that T(n) grows linearly as the input size increases.

Data Structures - 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.

Asymptotic analysis is input bound i.e., if there's no input to the algorithm, it is


concluded to work in a constant time. Other than the "input" all other factors are
considered constant.

Asymptotic analysis refers to computing the running time of any operation in


mathematical units of computation. For example, the running time of one operation
is computed as f(n) and may be for another operation it is computed as g(n2). This
means the first operation running time will increase linearly with the increase
in n and the running time of the second operation will increase exponentially
when n increases. Similarly, the running time of both operations will be nearly the
same if n is significantly small.

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.

Asymptotic Notations
Following are the commonly used asymptotic notations to calculate the running time
complexity of an algorithm.

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH [6]


 Ο Notation

 Ω Notation

 θ Notation

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.

For example, for a function f(n)


Ο(f(n)) = { g(n) : there exists c > 0 and n0 such that f(n) ≤ c.g(n) for all n > n0. }

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.

For example, for a function f(n)


Ω(f(n)) ≥ { g(n) : there exists c > 0 and n0 such that g(n) ≤ c.f(n) for all n > n0. }

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 is represented as follows −

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH [7]


θ(f(n)) = { g(n) if and only if g(n) = Ο(f(n)) and g(n) = Ω(f(n)) for all n > n0. }

Common Asymptotic Notations


Following is a list of some common asymptotic notations −

Constant − Ο(1)

Logarithmic − Ο(log n)

Linear − Ο(n)

n log n − Ο(n log n)

Quadratic − Ο(n2)

Cubic − Ο(n3)

Polynomial − nΟ(1)

Exponential − 2Ο(n)

Data Structures - Greedy Algorithms


An algorithm is designed to achieve optimum solution for a given problem. In greedy
algorithm approach, decisions are made from the given solution domain. As being
greedy, the closest solution that seems to provide an optimum solution is chosen.

Greedy algorithms try to find a localized optimum solution, which may eventually
lead to globally optimized solutions. However, generally greedy algorithms do not
provide globally optimized solutions.

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH [8]


Counting Coins
This problem is to count to a desired value by choosing the least possible coins and
the greedy approach forces the algorithm to pick the largest possible coin. If we are
provided coins of ₹ 1, 2, 5 and 10 and we are asked to count ₹ 18 then the greedy
procedure will be −

 1 − Select one ₹ 10 coin, the remaining count is 8

 2 − Then select one ₹ 5 coin, the remaining count is 3

 3 − Then select one ₹ 2 coin, the remaining count is 1

 4 − And finally, the selection of one ₹ 1 coins solves the problem

Though, it seems to be working fine, for this count we need to pick only 4 coins. But
if we slightly change the problem then the same approach may not be able to
produce the same optimum result.

For the currency system, where we have coins of 1, 7, 10 value, counting coins for
value 18 will be absolutely optimum but for count like 15, it may use more coins
than necessary. For example, the greedy approach will use 10 + 1 + 1 + 1 + 1 + 1,
total 6 coins. Whereas the same problem could be solved by using only 3 coins (7 +
7 + 1)

Hence, we may conclude that the greedy approach picks an immediate optimized
solution and may fail where global optimization is a major concern.

Examples
Most networking algorithms use the greedy approach. Here is a list of few of them −

 Travelling Salesman Problem

 Prim's Minimal Spanning Tree Algorithm

 Kruskal's Minimal Spanning Tree Algorithm

 Dijkstra's Minimal Spanning Tree Algorithm

 Graph - Map Coloring

 Graph - Vertex Cover

 Knapsack Problem

 Job Scheduling Problem

There are lots of similar problems that uses the greedy approach to find an optimum
solution.

Data Structures - Divide and Conquer


LECTURES NOTES @ HIBMAT BUEA ©NGOLAH [9]
In divide and conquer approach, the problem in hand, is divided into smaller sub-
problems and then each problem is solved independently. When we keep on dividing
the subproblems into even smaller sub-problems, we may eventually reach a stage
where no more division is possible. Those "atomic" smallest possible sub-problem
(fractions) are solved. The solution of all sub-problems is finally merged in order to
obtain the solution of an original problem.

Broadly, we can understand divide-and-conquer approach in a three-step process.

Divide/Break
This step involves breaking the problem into smaller sub-problems. Sub-problems
should represent a part of the original problem. This step generally takes a recursive
approach to divide the problem until no sub-problem is further divisible. At this
stage, sub-problems become atomic in nature but still represent some part of the
actual problem.

Conquer/Solve
This step receives a lot of smaller sub-problems to be solved. Generally, at this level,
the problems are considered 'solved' on their own.

Merge/Combine
When the smaller sub-problems are solved, this stage recursively combines them
until they formulate a solution of the original problem. This algorithmic approach
works recursively and conquer & merge steps works so close that they appear as
one.

Examples
LECTURES NOTES @ HIBMAT BUEA ©NGOLAH
[10]
The following computer algorithms are based on divide-and-conquerprogramming
approach −

 Merge Sort

 Quick Sort

 Binary Search

 Strassen's Matrix Multiplication

 Closest pair (points)

There are various ways available to solve any computer problem, but the mentioned
are a good example of divide and conquer approach.

Data Structures - Dynamic Programming


Dynamic programming approach is similar to divide and conquer in breaking down
the problem into smaller and yet smaller possible sub-problems. But unlike, divide
and conquer, these sub-problems are not solved independently. Rather, results of
these smaller sub-problems are remembered and used for similar or overlapping
sub-problems.

Dynamic programming is used where we have problems, which can be divided into
similar sub-problems, so that their results can be re-used. Mostly, these algorithms
are used for optimization. Before solving the in-hand sub-problem, dynamic
algorithm will try to examine the results of the previously solved sub-problems. The
solutions of sub-problems are combined in order to achieve the best solution.

So we can say that −

 The problem should be able to be divided into smaller overlapping sub-problem.

 An optimum solution can be achieved by using an optimum solution of smaller sub-


problems.

 Dynamic algorithms use Memoization.

Comparison
In contrast to greedy algorithms, where local optimization is addressed, dynamic
algorithms are motivated for an overall optimization of the problem.

In contrast to divide and conquer algorithms, where solutions are combined to


achieve an overall solution, dynamic algorithms use the output of a smaller sub-
problem and then try to optimize a bigger sub-problem. Dynamic algorithms use
Memoization to remember the output of already solved sub-problems.

Example
LECTURES NOTES @ HIBMAT BUEA ©NGOLAH
[11]
The following computer problems can be solved using dynamic programming
approach −

 Fibonacci number series

 Knapsack problem

 Tower of Hanoi

 All pair shortest path by Floyd-Warshall

 Shortest path by Dijkstra

 Project scheduling

Dynamic programming can be used in both top-down and bottom-up manner. And of
course, most of the times, referring to the previous solution output is cheaper than
recomputing in terms of CPU cycles.

Data Structures & Algorithm Basic Concepts


This chapter explains the basic terms related to data structure.

Data Definition
Data Definition defines a particular data with the following characteristics.

 Atomic − Definition should define a single concept.

 Traceable − Definition should be able to be mapped to some data element.

 Accurate − Definition should be unambiguous.

 Clear and Concise − Definition should be understandable.

Data Object
Data Object represents an object having a data.

Data Type
Data type is a way to classify various types of data such as integer, string, etc. which
determines the values that can be used with the corresponding type of data, the type
of operations that can be performed on the corresponding type of data. There are
two data types −

 Built-in Data Type

 Derived Data Type

Built-in Data Type

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH


[12]
Those data types for which a language has built-in support are known as Built-in
Data types. For example, most of the languages provide the following built-in data
types.

 Integers

 Boolean (true, false)

 Floating (Decimal numbers)

 Character and Strings

Derived Data Type


Those data types which are implementation independent as they can be
implemented in one or the other way are known as derived data types. These data
types are normally built by the combination of primary or built-in data types and
associated operations on them. For example −

 List

 Array

 Stack

 Queue

Basic Operations
The data in the data structures are processed by certain operations. The particular
data structure chosen largely depends on the frequency of the operation that needs
to be performed on the data structure.

 Traversing

 Searching

 Insertion

 Deletion

 Sorting

 Merging

Data Structures and Algorithms - Arrays


Array is a container which can hold a fix number of items and these items should be
of the same type. Most of the data structures make use of arrays to implement their
algorithms. Following are the important terms to understand the concept of Array.

 Element − Each item stored in an array is called an element.

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH


[13]
 Index − Each location of an element in an array has a numerical index, which is used to
identify the element.

Data Structure and Algorithms - Linked List


A linked list is a sequence of data structures, which are connected together via links.

Linked List is a sequence of links which contains items. Each link contains a
connection to another link. Linked list is the second most-used data structure after
array. Following are the important terms to understand the concept of Linked List.

 Link − Each link of a linked list can store a data called an element.

 Next − Each link of a linked list contains a link to the next link called Next.

 LinkedList − A Linked List contains the connection link to the first link called First.

Data Structure - Doubly Linked List


Doubly Linked List is a variation of Linked list in which navigation is possible in both
ways, either forward and backward easily as compared to Single Linked List.
Following are the important terms to understand the concept of doubly linked list.

 Link − Each link of a linked list can store a data called an element.

 Next − Each link of a linked list contains a link to the next link called Next.

 Prev − Each link of a linked list contains a link to the previous link called Prev.

 LinkedList − A Linked List contains the connection link to the first link called First and to
the last link called Last.

Data Structure - Circular Linked List


Circular Linked List is a variation of Linked list in which the first element points to the
last element and the last element points to the first element. Both Singly Linked List
and Doubly Linked List can be made into a circular linked list.

Data Structure and Algorithms - Stack


A stack is an Abstract Data Type (ADT), commonly used in most programming
languages. It is named stack as it behaves like a real-world stack, for example – a
deck of cards or a pile of plates, etc.

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH


[14]
A real-world stack allows operations at one end only. For example, we can place or
remove a card or plate from the top of the stack only. Likewise, Stack ADT allows all
data operations at one end only. At any given time, we can only access the top
element of a stack.

This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the
element which is placed (inserted or added) last, is accessed first. In stack
terminology, insertion operation is called PUSH operation and removal operation is
called POP operation.

Stack Representation
The following diagram depicts a stack and its operations −

A stack can be implemented by means of Array, Structure, Pointer, and Linked List.
Stack can either be a fixed size one or it may have a sense of dynamic resizing.
Here, we are going to implement stack using arrays, which makes it a fixed size
stack implementation.

Basic Operations
Stack operations may involve initializing the stack, using it and then de-initializing it.
Apart from these basic stuffs, a stack is used for the following two primary
operations −

 push() − Pushing (storing) an element on the stack.

 pop() − Removing (accessing) an element from the stack.

When data is PUSHed onto stack.


LECTURES NOTES @ HIBMAT BUEA ©NGOLAH
[15]
To use a stack efficiently, we need to check the status of stack as well. For the same
purpose, the following functionality is added to stacks −

 peek() − get the top data element of the stack, without removing it.

 isFull() − check if stack is full.

 isEmpty() − check if stack is empty.

At all times, we maintain a pointer to the last PUSHed data on the stack. As this
pointer always represents the top of the stack, hence named top. The toppointer
provides top value of the stack without actually removing it.

Data Structure - Expression Parsing


The way to write arithmetic expression is known as a notation. An arithmetic
expression can be written in three different but equivalent notations, i.e., without
changing the essence or output of an expression. These notations are −

 Infix Notation

 Prefix (Polish) Notation

 Postfix (Reverse-Polish) Notation

These notations are named as how they use operator in expression. We shall learn
the same here in this chapter.

Infix Notation
We write expression in infix notation, e.g. a - b + c, where operators are used in-
between operands. It is easy for us humans to read, write, and speak in infix
notation but the same does not go well with computing devices. An algorithm to
process infix notation could be difficult and costly in terms of time and space
consumption.

Prefix Notation
In this notation, operator is prefixed to operands, i.e. operator is written ahead of
operands. For example, +ab. This is equivalent to its infix notation a + b. Prefix
notation is also known as Polish Notation.

Postfix Notation
This notation style is known as Reversed Polish Notation. In this notation style,
the operator is postfixed to the operands i.e., the operator is written after the
operands. For example, ab+. This is equivalent to its infix notation a + b.

The following table briefly tries to show the difference in all three notations −
LECTURES NOTES @ HIBMAT BUEA ©NGOLAH
[16]
Sr.No. Infix Notation Prefix Notation Postfix Notation

1 a+b +ab ab+

2 (a + b) ∗ c ∗+abc ab+c∗

3 a ∗ (b + c) ∗a+bc abc+∗

4 a/b+c/d +/ab/cd ab/cd/+

5 (a + b) ∗ (c + d) ∗+ab+cd ab+cd+∗

6 ((a + b) ∗ c) - d -∗+abcd ab+c∗d-

Parsing Expressions
As we have discussed, it is not a very efficient way to design an algorithm or
program to parse infix notations. Instead, these infix notations are first converted
into either postfix or prefix notations and then computed.

To parse any arithmetic expression, we need to take care of operator precedence


and associativity also.

Precedence
When an operand is in between two different operators, which operator will take the
operand first, is decided by the precedence of an operator over others. For example

As multiplication operation has precedence over addition, b * c will be evaluated


first. A table of operator precedence is provided later.

Associativity
Associativity describes the rule where operators with the same precedence appear in
an expression. For example, in expression a + b − c, both + and – have the same
precedence, then which part of the expression will be evaluated first, is determined
by associativity of those operators. Here, both + and − are left associative, so the
expression will be evaluated as (a + b) − c.

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH


[17]
Precedence and associativity determines the order of evaluation of an expression.
Following is an operator precedence and associativity table (highest to lowest) −

Sr.No. Operator Precedence Associativity

1 Exponentiation ^ Highest Right Associative

2 Multiplication ( ∗ ) & Division ( / ) Second Highest Left Associative

3 Addition ( + ) & Subtraction ( − ) Lowest Left Associative

The above table shows the default behavior of operators. At any point of time in
expression evaluation, the order can be altered by using parenthesis. For example −

In a + b*c, the expression part b*c will be evaluated first, with multiplication as
precedence over addition. We here use parenthesis for a + b to be evaluated first,
like (a + b)*c.

Data Structure and Algorithms - Queue


Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a
queue is open at both its ends. One end is always used to insert data (enqueue) and
the other is used to remove data (dequeue). Queue follows First-In-First-Out
methodology, i.e., the data item stored first will be accessed first.

A real-world example of queue can be a single-lane one-way road, where the vehicle
enters first, exits first. More real-world examples can be seen as queues at the ticket
windows and bus-stops.

Queue Representation
As we now understand that in queue, we access both ends for different reasons. The
following diagram given below tries to explain queue representation as data structure

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH


[18]
As in stacks, a queue can also be implemented using Arrays, Linked-lists, Pointers
and Structures. For the sake of simplicity, we shall implement queues using one-
dimensional array.

Basic Operations
Queue operations may involve initializing or defining the queue, utilizing it, and then
completely erasing it from the memory. Here we shall try to understand the basic
operations associated with queues −

 enqueue() − add (store) an item to the queue.

 dequeue() − remove (access) an item from the queue.

Few more functions are required to make the above-mentioned queue operation
efficient. These are −

 peek() − Gets the element at the front of the queue without removing it.

 isfull() − Checks if the queue is full.

 isempty() − Checks if the queue is empty.

In queue, we always dequeue (or access) data, pointed by front pointer and while
enqueing (or storing) data in the queue we take help of rear pointer.

Data Structure and Algorithms Linear Search


Linear search is a very simple search algorithm. In this type of search, a sequential
search is made over all items one by one. Every item is checked and if a match is
found then that particular item is returned, otherwise the search continues till the
end of the data collection.

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH


[19]
Algorithm
Linear Search ( Array A, Value x)

Step 1: Set i to 1
Step 2: if i > n then go to step 7
Step 3: if A[i] = x then go to step 6
Step 4: Set i to i + 1
Step 5: Go to Step 2
Step 6: Print Element x Found at index i and go to step 8
Step 7: Print element not found
Step 8: Exit

Data Structure and Algorithms Binary Search


Binary search is a fast search algorithm with run-time complexity of Ο(log n). This
search algorithm works on the principle of divide and conquer. For this algorithm to
work properly, the data collection should be in the sorted form.

Binary search looks for a particular item by comparing the middle most item of the
collection. If a match occurs, then the index of item is returned. If the middle item is
greater than the item, then the item is searched in the sub-array to the left of the
middle item. Otherwise, the item is searched for in the sub-array to the right of the
middle item. This process continues on the sub-array as well until the size of the
subarray reduces to zero.

How Binary Search Works?


For a binary search to work, it is mandatory for the target array to be sorted. We
shall learn the process of binary search with a pictorial example. The following is our
sorted array and let us assume that we need to search the location of value 31 using
binary search.

Data Structure and Algorithms - Hash Table


Hash Table is a data structure which stores data in an associative manner. In a hash
table, data is stored in an array format, where each data value has its own unique
index value. Access of data becomes very fast if we know the index of the desired
data.

Thus, it becomes a data structure in which insertion and search operations are very
fast irrespective of the size of the data. Hash Table uses an array as a storage
medium and uses hash technique to generate an index where an element is to be
inserted or is to be located from.

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH


[20]
Hashing
Hashing is a technique to convert a range of key values into a range of indexes of an
array. We're going to use modulo operator to get a range of key values. Consider an
example of hash table of size 20, and the following items are to be stored. Item are
in the (key,value) format.

Data Structure - Sorting Techniques


Sorting refers to arranging data in a particular format. Sorting algorithm specifies the
way to arrange data in a particular order. Most common orders are in numerical or
lexicographical order.

The importance of sorting lies in the fact that data searching can be optimized to a
very high level, if data is stored in a sorted manner. Sorting is also used to represent
data in more readable formats. Following are some of the examples of sorting in
real-life scenarios −

 Telephone Directory − The telephone directory stores the telephone numbers of people
sorted by their names, so that the names can be searched easily.

 Dictionary − The dictionary stores words in an alphabetical order so that searching of any
word becomes easy.

Data Structure - Bubble Sort Algorithm


Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based
algorithm in which each pair of adjacent elements is compared and the elements are
swapped if they are not in order. This algorithm is not suitable for large data sets as
its average and worst case complexity are of Ο(n2) where n is the number of items.

Data Structure and Algorithms Insertion Sort


This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained
which is always sorted. For example, the lower part of an array is maintained to be
sorted. An element which is to be 'insert'ed in this sorted sub-list, has to find its
LECTURES NOTES @ HIBMAT BUEA ©NGOLAH
[21]
appropriate place and then it has to be inserted there. Hence the name, insertion
sort.

The array is searched sequentially and unsorted items are moved and inserted into
the sorted sub-list (in the same array). This algorithm is not suitable for large data
sets as its average and worst case complexity are of Ο(n2), where n is the number of
items.

Data Structure and Algorithms Selection Sort


Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place
comparison-based algorithm in which the list is divided into two parts, the sorted
part at the left end and the unsorted part at the right end. Initially, the sorted part is
empty and the unsorted part is the entire list.

The smallest element is selected from the unsorted array and swapped with the
leftmost element, and that element becomes a part of the sorted array. This process
continues moving unsorted array boundary by one element to the right.

This algorithm is not suitable for large data sets as its average and worst case
complexities are of Ο(n2), where n is the number of items.

Data Structures - Merge Sort Algorithm


Merge sort is a sorting technique based on divide and conquer technique. With
worst-case time complexity being Ο(n log n), it is one of the most respected
algorithms.

Merge sort first divides the array into equal halves and then combines them in a
sorted manner.

Shell sort is a highly efficient sorting algorithm and is based on insertion sort
algorithm. This algorithm avoids large shifts as in case of insertion sort, if the smaller
value is to the far right and has to be moved to the far left.

This algorithm uses insertion sort on a widely spread elements, first to sort them and
then sorts the less widely spaced elements. This spacing is termed as interval. This
interval is calculated based on Knuth's formula as −

Knuth's Formula
h = h * 3 + 1
where −
h is interval with initial value 1

This algorithm is quite efficient for medium-sized data sets as its average and worst
case complexity are of Ο(n), where n is the number of items.

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH


[22]
Data Structure and Algorithms - Shell Sort
Shell sort is a highly efficient sorting algorithm and is based on insertion sort
algorithm. This algorithm avoids large shifts as in case of insertion sort, if the smaller
value is to the far right and has to be moved to the far left.

This algorithm uses insertion sort on a widely spread elements, first to sort them and
then sorts the less widely spaced elements. This spacing is termed as interval. This
interval is calculated based on Knuth's formula as −

Knuth's Formula
h = h * 3 + 1
where −
h is interval with initial value 1

This algorithm is quite efficient for medium-sized data sets as its average and worst-
case complexity of this algorithm depends on the gap sequence the best known is
Ο(n), where n is the number of items. And the worst case space complexity is O(n).

Data Structure and Algorithms - Quick Sort


Quick sort is a highly efficient sorting algorithm and is based on partitioning of array
of data into smaller arrays. A large array is partitioned into two arrays one of which
holds values smaller than the specified value, say pivot, based on which the partition
is made and another array holds values greater than the pivot value.

Quick sort partitions an array and then calls itself recursively twice to sort the two
resulting subarrays. This algorithm is quite efficient for large-sized data sets as its
average and worst case complexity are of Ο(n2), where n is the number of items.

Data Structure - Graph Data Structure


A graph is a pictorial representation of a set of objects where some pairs of objects
are connected by links. The interconnected objects are represented by points termed
as vertices, and the links that connect the vertices are called edges.

Formally, a graph is a pair of sets (V, E), where V is the set of vertices and Eis the
set of edges, connecting the pairs of vertices. Take a look at the following graph −

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH


[23]
In the above graph,

V = {a, b, c, d, e}

E = {ab, ac, bd, cd, de}

Graph Data Structure


Mathematical graphs can be represented in data structure. We can represent a graph
using an array of vertices and a two-dimensional array of edges. Before we proceed
further, let's familiarize ourselves with some important terms −

 Vertex − Each node of the graph is represented as a vertex. In the following example, the
labeled circle represents vertices. Thus, A to G are vertices. We can represent them using
an array as shown in the following image. Here A can be identified by index 0. B can be
identified using index 1 and so on.

 Edge − Edge represents a path between two vertices or a line between two vertices. In
the following example, the lines from A to B, B to C, and so on represents edges. We can
use a two-dimensional array to represent an array as shown in the following image. Here
AB can be represented as 1 at row 0, column 1, BC as 1 at row 1, column 2 and so on,
keeping other combinations as 0.

 Adjacency − Two node or vertices are adjacent if they are connected to each other
through an edge. In the following example, B is adjacent to A, C is adjacent to B, and so
on.

 Path − Path represents a sequence of edges between the two vertices. In the following
example, ABCD represents a path from A to D.

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH


[24]
Basic Operations
Following are basic primary operations of a Graph −

 Add Vertex − Adds a vertex to the graph.

 Add Edge − Adds an edge between the two vertices of the graph.

 Display Vertex − Displays a vertex of the graph.

Data Structure - Depth First Traversal


Depth First Search (DFS) algorithm traverses a graph in a depthward motion and
uses a stack to remember to get the next vertex to start a search, when a dead end
occurs in any iteration.

As in the example given above, DFS algorithm traverses from S to A to D to G to E to


B first, then to F and lastly to C. It employs the following rules.

 Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push it in a
stack.

 Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack. (It will pop up all
the vertices from the stack, which do not have adjacent vertices.)

 Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty.

Data Structure - Breadth First Traversal

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH


[25]
Breadth First Search (BFS) algorithm traverses a graph in a breadthward motion and
uses a queue to remember to get the next vertex to start a search, when a dead end
occurs in any iteration.

As in the example given above, BFS algorithm traverses from A to B to E to F first


then to C and G lastly to D. It employs the following rules.

 Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it in a
queue.

 Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.

 Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.

Data Structure and Algorithms - Tree


Tree represents the nodes connected by edges. We will discuss binary tree or binary
search tree specifically.

Binary Tree is a special datastructure used for data storage purposes. A binary tree
has a special condition that each node can have a maximum of two children. A binary
tree has the benefits of both an ordered array and a linked list as search is as quick
as in a sorted array and insertion or deletion operation are as fast as in linked list.

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH


[26]
Important Terms
Following are the important terms with respect to tree.

 Path − Path refers to the sequence of nodes along the edges of a tree.

 Root − The node at the top of the tree is called root. There is only one root per tree and
one path from the root node to any node.

 Parent − Any node except the root node has one edge upward to a node called parent.

 Child − The node below a given node connected by its edge downward is called its child
node.

 Leaf − The node which does not have any child node is called the leaf node.

 Subtree − Subtree represents the descendants of a node.

 Visiting − Visiting refers to checking the value of a node when control is on the node.

 Traversing − Traversing means passing through nodes in a specific order.

 Levels − Level of a node represents the generation of a node. If the root node is at level
0, then its next child node is at level 1, its grandchild is at level 2, and so on.

 keys − Key represents a value of a node based on which a search operation is to be


carried out for a node.

Binary Search Tree Representation

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH


[27]
Binary Search tree exhibits a special behavior. A node's left child must have a value
less than its parent's value and the node's right child must have a value greater than
its parent value.

Data Structure & Algorithms - Tree Traversal


Traversal is a process to visit all the nodes of a tree and may print their values too.
Because, all nodes are connected via edges (links) we always start from the root
(head) node. That is, we cannot randomly access a node in a tree. There are three
ways which we use to traverse a tree −

 In-order Traversal

 Pre-order Traversal

 Post-order Traversal

Generally, we traverse a tree to search or locate a given item or key in the tree or to
print all the values it contains.

In-order Traversal
In this traversal method, the left subtree is visited first, then the root and later the
right sub-tree. We should always remember that every node may represent a
subtree itself.

If a binary tree is traversed in-order, the output will produce sorted key values in an
ascending order.

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH


[28]
We start from A, and following in-order traversal, we move to its left subtree B. B is
also traversed in-order. The process goes on until all the nodes are visited. The
output of inorder traversal of this tree will be −

D→B→E→A→F→C→G

Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.

Pre-order Traversal
In this traversal method, the root node is visited first, then the left subtree and
finally the right subtree.

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH


[29]
We start from A, and following pre-order traversal, we first visit A itself and then
move to its left subtree B. B is also traversed pre-order. The process goes on until all
the nodes are visited. The output of pre-order traversal of this tree will be −

A→B→D→E→C→F→G

Algorithm
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.

Post-order Traversal
In this traversal method, the root node is visited last, hence the name. First we
traverse the left subtree, then the right subtree and finally the root node.

We start from A, and following Post-order traversal, we first visit the left
subtree B. B is also traversed post-order. The process goes on until all the nodes are
visited. The output of post-order traversal of this tree will be −

D→E→B→F→G→C→A

Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.

To check the C implementation of tree traversing, please click here.

Data Structure - Binary Search Tree


A Binary Search Tree (BST) is a tree in which all the nodes follow the below-
mentioned properties −
LECTURES NOTES @ HIBMAT BUEA ©NGOLAH
[30]
 The left sub-tree of a node has a key less than or equal to its parent node's key.

 The right sub-tree of a node has a key greater than to its parent node's key.

Thus, BST divides all its sub-trees into two segments; the left sub-tree and the right
sub-tree and can be defined as −
left_subtree (keys) ≤ node (key) ≤ right_subtree (keys)

Representation
BST is a collection of nodes arranged in a way where they maintain BST properties.
Each node has a key and an associated value. While searching, the desired key is
compared to the keys in BST and if found, the associated value is retrieved.

Following is a pictorial representation of BST −

We observe that the root node key (27) has all less-valued keys on the left sub-tree
and the higher valued keys on the right sub-tree.

Basic Operations
Following are the basic operations of a tree −

 Search − Searches an element in a tree.

 Insert − Inserts an element in a tree.

 Pre-order Traversal − Traverses a tree in a pre-order manner.

 In-order Traversal − Traverses a tree in an in-order manner.

 Post-order Traversal − Traverses a tree in a post-order manner.

Data Structure & Algorithms - Spanning Tree


A spanning tree is a subset of Graph G, which has all the vertices covered with
minimum possible number of edges. Hence, a spanning tree does not have cycles
and it cannot be disconnected..

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH


[31]
By this definition, we can draw a conclusion that every connected and undirected
Graph G has at least one spanning tree. A disconnected graph does not have any
spanning tree, as it cannot be spanned to all its vertices.

We found three spanning trees off one complete graph. A complete undirected graph
can have maximum nn-2 number of spanning trees, where n is the number of nodes.
In the above addressed example, n is 3, hence 33−2 = 3spanning trees are possible.

General Properties of Spanning Tree


We now understand that one graph can have more than one spanning tree. Following
are a few properties of the spanning tree connected to graph G −

 A connected graph G can have more than one spanning tree.

 All possible spanning trees of graph G, have the same number of edges and vertices.

 The spanning tree does not have any cycle (loops).

 Removing one edge from the spanning tree will make the graph disconnected, i.e. the
spanning tree is minimally connected.

 Adding one edge to the spanning tree will create a circuit or loop, i.e. the spanning tree
is maximally acyclic.

Mathematical Properties of Spanning Tree


 Spanning tree has n-1 edges, where n is the number of nodes (vertices).

 From a complete graph, by removing maximum e - n + 1 edges, we can construct a


spanning tree.

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH


[32]
 A complete graph can have maximum nn-2 number of spanning trees.

Thus, we can conclude that spanning trees are a subset of connected Graph G and
disconnected graphs do not have spanning tree.

Application of Spanning Tree


Spanning tree is basically used to find a minimum path to connect all nodes in a
graph. Common application of spanning trees are −

 Civil Network Planning

 Computer Network Routing Protocol

 Cluster Analysis

Let us understand this through a small example. Consider, city network as a huge
graph and now plans to deploy telephone lines in such a way that in minimum lines
we can connect to all city nodes. This is where the spanning tree comes into picture.

Minimum Spanning Tree (MST)


In a weighted graph, a minimum spanning tree is a spanning tree that has minimum
weight than all other spanning trees of the same graph. In real-world situations, this
weight can be measured as distance, congestion, traffic load or any arbitrary value
denoted to the edges.

Minimum Spanning-Tree Algorithm


We shall learn about two most important spanning tree algorithms here −

 Kruskal's Algorithm

 Prim's Algorithm

Both are greedy algorithms.

Heap Data Structures


Heap is a special case of balanced binary tree data structure where the root-node
key is compared with its children and arranged accordingly. If α has child
node β then −

key(α) ≥ key(β)

As the value of parent is greater than that of child, this property generates Max
Heap. Based on this criteria, a heap can be of two types −
For Input → 35 33 42 10 14 19 27 44 26 31

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH


[33]
Min-Heap − Where the value of the root node is less than or equal to either of its
children.

Max-Heap − Where the value of the root node is greater than or equal to either of
its children.

Both trees are constructed using the same input and order of arrival.

Max Heap Construction Algorithm


We shall use the same example to demonstrate how a Max Heap is created. The
procedure to create Min Heap is similar but we go for min values instead of max
values.

Data Structure - Recursion Basics


Some computer programming languages allow a module or function to call itself. This
technique is known as recursion. In recursion, a function α either calls itself directly
or calls a function β that in turn calls the original function α. The function α is called
recursive function.

Data Structure & Algorithms Fibonacci Series


LECTURES NOTES @ HIBMAT BUEA ©NGOLAH
[34]
Fibonacci series generates the subsequent number by adding two previous numbers.
Fibonacci series starts from two numbers − F0 & F1. The initial values of F0 & F1 can
be taken 0, 1 or 1, 1 respectively.

Fibonacci series satisfies the following conditions −


Fn = Fn-1 + Fn-2

Hence, a Fibonacci series can look like this −

F8 = 0 1 1 2 3 5 8 13

or, this −

F8 = 1 1 2 3 5 8 13 21

LECTURES NOTES @ HIBMAT BUEA ©NGOLAH


[35]

You might also like