You are on page 1of 40

Data Structures & Algorithms

Eight Week Industrial Training (MCA-211) Report on Data Structures &


Algorithms
(August 2022- September 2022)

Submitted by

Ajay Kumar, MCA-II Year (III Sem)


221029020011(21MC23)
Department of Computer Science and Engineering
Mahatma Jyotiba Phule Rohilkhand University
Bareilly A State University, Government of Uttar Pradesh, India

September 2022

Page. 1
CERTIFICATE OF TRAINING

Page. 2
ACKNOWLEGDEMENT

We extend our Thanks of Internshala Trainings who has given me opportunity.

Today I am feeling a great sense of Excitement on my way to successfully complete my training


on “ Data Structure and Algorithms” under the guidance of “Mr Anand Mahajan”.

I sincerely thank him for responding great confidence and faith in my work and being with me
to encourage and guide me to successful training completion.

I should also like to thank Mr. Sarvesh Agarwal, Founder & CEO, Inernshala for their support
who have created an atmosphere to encourage me from time to time making our work easy.

Thank You…..

Student Name: Ajay kumar

Roll No: 221029020011(21MC23)

Signature:

Page. 3
TABLE OF CONTENT

➢ Introduction to Data Structures

➢ Introduction to Algorithms

➢ Single & Double Dimensional Arrays

➢ Searching & Sorting

➢ Stacks & Queues

➢ Revision of relevant topics in C

➢ Implementation programs of Stacks & Queues

➢ Linear linked list

➢ Circular linked list

➢ Doubly linked list

➢ Trees

➢ Graphs

➢ References…

Page. 4
Introduction to Data Structure

Data Structure is a way of collecting and organising data in such a way that we can perform
operations on these data in an effective way. Data Structures is about rendering data elements
in terms of some relationship, for better organization and storage.

Data structures are an essential concept in computer science and programming. Here are
some reasons why they are important:
1. Efficient data processing: Data structures provide a way to organize and store data
in a way that allows for efficient retrieval, manipulation, and storage of data. For
example, using a hash table to store data can provide constant-time access to data.
2. Memory management: Proper use of data structures can help to reduce memory
usage and optimize the use of resources. For example, using dynamic arrays can
allow for more efficient use of memory than using static arrays.
3. Code reusability: Data structures can be used as building blocks in various
algorithms and programs, making it easier to reuse code.
4. Abstraction: Data structures provide a level of abstraction that allows programmers
to focus on the logical structure of the data and the operations that can be
performed on it, rather than on the details of how the data is stored and
manipulated.
5. Algorithm design: Many algorithms rely on specific data structures to operate
efficiently. Understanding data structures is crucial for designing and implementing
efficient algorithms.
Overall, data structures are essential for managing and manipulating data in an efficient and
effective way. They are a fundamental concept in computer science and are used extensively
in programming and software development.
The structure of the data and the synthesis of the algorithm are relative to each other. 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.
Here is a list of the needs for data.
• Efficient data access and manipulation: Data structures enable quick access and
manipulation of data. For example, an array allows constant-time access to
elements using their index, while a hash table allows fast access to elements based
on their key. Without data structures, programs would have to search through data
sequentially, leading to slow performance.

Page. 5
• Memory management: Data structures allow efficient use of memory by allocating
and deallocating memory dynamically. For example, a linked list can dynamically
allocate memory for each element as needed, rather than allocating a fixed amount
of memory upfront. This helps avoid memory wastage and enables efficient
memory management.
• Code reusability: Data structures can be reused across different programs and
projects. For example, a generic stack data structure can be used in multiple
programs that require LIFO (Last-In-First-Out) functionality, without having to
rewrite the same code each time.
• Optimization of algorithms: Data structures help optimize algorithms by enabling
efficient data access and manipulation. For example, a binary search tree allows fast
searching and insertion of elements, making it ideal for implementing searching and
sorting algorithms.
• Scalability: Data structures enable programs to handle large amounts of data
effectively. For example, a hash table can store large amounts of data while
providing fast access to elements based on their key.
Classification/Types of Data Structures:
Data structures can be classified into two main types: primitive data structures and non-
primitive data structures.
Primitive data structures: These are the most basic data structures and are usually built into
programming languages. Examples include:
Integer
Float
Character
Boolean
Double
Void
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.
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.

1. Linear Data Structure


Page. 6
2. Non-Linear Data Structure.
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.
• Example: lists, stack, queue, etc.
Non-Linear Data Structure:
• A 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.

Introduction to Algorithms

An algorithm is a finite set of instructions or logic, written in order, to accomplish a certain


predefined task. Algorithm is not the complete code or program, it is just the core
logic(solution) of a problem, which can be expressed either as an informal high level
description as pseudocode or using a flowchart.
Every Algorithm must satisfy the following properties:
Input- There should be 0 or more inputs supplied externally to the algorithm.
Output- There should be atleast 1 output obtained.
Definiteness- Every step of the algorithm should be clear and well defined.
Finiteness- The algorithm should have finite number of steps.
Correctness- Every step of the algorithm must generate a correct output.
Page. 7
An algorithm is said to be efficient and fast, if it takes less time to execute and consumes less
memory space. The performance of an algorithm is measured on the basis of following
properties :
Time Complexity
Space Complexity

Space Complexity:
Its the amount of memory space required by the algorithm, during the course of its execution.
Space complexity must be taken seriously for multi-user systems and in situations where
limited memory is available.
An algorithm generally requires space for following components :
Instruction Space: Its the space required to store the executable version of the program. This
space is fixed, but varies depending upon the number of lines of code in the program.
Data Space: Its the space required to store all the constants and variables(including temporary
variables) value.
Environment Space: Its the space required to store the environment information needed to
resume the suspended function.
To learn about Space Complexity in detail, jump to the Space Complexity tutorial.

Time Complexity:
Time Complexity is a way to represent the amount of time required by the program to run till
its completion. It's generally a good practice to try to keep the time required minimum, so that
our algorithm completes it's execution in the minimum time possible. We will study
about Time Complexity in details in later sections.

Single & Double Dimensional Arrays

An array is a collection of similar data types (like int, float, or char), which takes memory in a
contiguous fashion in Primary memory locations.
We can declare an array in C as
int arr[50];
Where arr is an array with can hold 50 elements of integer types.
Two Type of array:
1). Single/One Dimensional array
2). Double-Dimensional Array

One Dimensional Array


Declaration of one dimensional array:
An array can be declared with the bracket punctuators [ ], as shown in the below syntax:

Page. 8
Data_Type Array_Name[Number_Of_Elements]

The below example shows a declaration of an array having the capacity to hold 50 elements of
type integers and the name of that array is arr :
int arr[50];
Initializing the array:
We can initialize an array in C by assigning value to each index one by one or by using a single
statement as follows −
Example 1 :
Assign one value each time to the array
int arr[50];
arr[0]=12;
arr[1]= 43;
arr[2] = 14;
.
.
arr[49] = 54;
Example 2:
Using a single statement
int arr[5] = {10,32,11,45,38};

Program in C to implement One dimensional array:


#include<stdio.h>
int main () {
int n[50]; /* n is an array of 50 integers */
int i,j;
/* initialize elements of array n to 0 */
for ( i = 0; i <= 50; i++ ) {
n[ i ] = i + 50; /* set element at location i to i + 50 */
}
/* output each array element's value */
for (j = 0; j<= 50; j++ ) {
printf("Element[%d] = %d\n", j, n[j] );
}
return 0;
}
Two Dimensional Array in C
The two-dimensional array can be defined as an array of arrays. The 2D array is organized as
matrices which can be represented as the collection of rows and columns. However, 2D arrays
are created to implement a relational database lookalike data structure. It provides ease of

Page. 9
holding the bulk of data at once which can be passed to any number of functions wherever
required.

Declaration of two dimensional Array in C


The syntax to declare the 2D array is given below.
data_type array_name[rows][columns];
Consider the following example.
int twodimen[4][3];
Here, 4 is the number of rows, and 3 is the number of columns.

Initialization of 2D Array in C
In the 1D array, we don't need to specify the size of the array if the declaration and
initialization are being done simultaneously. However, this will not work with 2D arrays. We will
have to define at least the second dimension of the array. The two-dimensional array can be
declared and defined in the following way.
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};

Two-dimensional array example in C


#include<stdio.h>
int main(){
int i=0,j=0;
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
//traversing 2D array
for(i=0;i<4;i++){
for(j=0;j<3;j++){
printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
}//end of j
}//end of i
return 0;
}

Searching & Sorting


Searching Algorithms are designed to check for an element or retrieve an element from any data
structure where it is stored.

Based on the type of search operation, these algorithms are generally classified into two
categories:
Sequential Search: In this, the list or array is traversed sequentially and every element is
checked. For example: Linear Search.
Linear Search to find the element “20” in a given list of numbers

Page. 10
Interval Search: These algorithms are specifically designed for searching in sorted data-
structures. These type of searching algorithms are much more efficient than Linear Search as
they repeatedly target the center of the search structure and divide the search space in half.
For Example: Binary Search.
Binary Search to find the element “23” in a given list of numbers

A Sorting Algorithm is used to rearrange a given array or list of elements according to a


comparison operator on the elements. The comparison operator is used to decide the new
order of elements in the respective data structure.
For Example: The below list of characters is sorted in increasing order of their ASCII values. That
is, the character with a lesser ASCII value will be placed first than the character with a higher
ASCII value.

Page. 11
Some of the most common sorting algorithms are:
Selection sort.
Bubble sort.
Insertion sort.
Merge sort.
Quick sort.
Heap sort.
Counting sort.
Radix sort.

Stacks & Queues


Stack:
Stack is a linear data structure that follows a particular order in which the operations are
performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that
the element that is inserted last, comes out first and FILO implies that the element that is
inserted first, comes out last.

Queue :
A Queue is defined as a linear data structure that is open at both ends and the operations are
performed in First In First Out (FIFO) order.
We define a queue to be a list in which all additions to the list are made at one end, and all
deletions from the list are made at the other end. The element which is first pushed into the
order, the operation is first performed on that.

Page. 12
FIFO Principle of Queue:
A Queue is like a line waiting to purchase tickets, where the first person in line is the first
person served. (i.e. First come first serve).
Position of the entry in a queue ready to be served, that is, the first entry that will be removed
from the queue, is called the front of the queue(sometimes, head of the queue), similarly, the
position of the last entry in the queue, that is, the one most recently added, is called
the rear (or the tail) of the queue. See the below figure.

Revision of relevant topics in C


C pointer :
Pointers in C are used to store the address of variables or a memory location. This variable can be of
any data type i.e, int, char, function, array, or any other pointer. The pointer of type void is
called Void pointer or Generic pointer. Void pointer can hold address of any type of variable. The
size of the pointer depends on the computer architecture like 16-bit, 32-bit, and 64-bit.

Syntax:
datatype *var_name;
Let pointer “ptr” holds the address of an integer variable or holds the address of memory
whose value(s) can be accessed as integer values through “ptr”. So to define “ptr” we can do
it in four ways, which are as following:
int *ptr;
int* ptr;
int * ptr;
int*ptr;

Page. 13
Void Pointers
A Void Pointer in C can be defined as an address of any variable. It has no standard data type.
A void pointer is created by using the keyword void.

NULL Pointers
Null pointers can be created by assigning a zero value during pointer declaration. This
method is useful when no address is assigned to the pointer

Wild Pointers
Wild Pointers are pointers that have not been initialized with something yet. These types of
C-pointers can cause problems in our programs and can eventually cause them to crash.
While working with Wild Pointers Programmer must be very careful .

C – Functions :
Functions are sets of statements that take inputs, perform some operations, and produce
results. The operation of a function occurs only when it is called. Rather than writing the
same code for different inputs repeatedly, we can call the function instead of writing the
same code over and over again. Functions accept parameters, which are data. A function
performs a certain action, and it is important for reusing code. Within a function, there are a
number of programming statements enclosed by {}.
Example:
int sum(int a, int b);

return_type name_of_the_function (parameters);

Page. 14
Function declaration for the sum of two numbers is shown below
int sum(int var1, int var2);
The parameter name is not mandatory while declaring functions. We can also declare the
above function without using the name of the data variables.
int sum(int, int);

Function Definition
A function definition consists function header and a function body.
return_type function_name (parameters)
{
//body of the function
}
• Return_type: The function always starts with a return type of the function. But if
there is no return value then the void keyword is used as the return type of the
function.
• Function_Name: Name of the function which should be unique.
• Parameters: Values that are passed during the function call.
Function Call
To Call a function parameters are passed along the function name. In the below example, the
first sum function is called and 10,30 are passed to the sum function. After the function call
sum of a and b is returned and control is also returned back to the main function of the
program.

Page. 15
Structures in C :
A structure is a keyword that creates user-defined data types in C/C++. A structure creates a
data type that can be used to group items of possibly different types into a single type.

How to create a structure?


‘struct’ keyword is used to create a structure. Following is an example.
struct address
{
char name[50];
char street[100];
char city[50];
char state[20];
int pin;

Page. 16
};

Implementation programs of Stacks & Queues

C program to implement stacks using arrays


#include<stdio.h>
int stack[10],choice,n,top,x,i; // Declaration of variables

void push();
void pop();
void display();

int main()
{
top = -1; // Initially there is no element in stack
printf("\n Enter the size of STACK : ");
scanf("%d",&n);
printf("\nSTACK IMPLEMENTATION USING ARRAYS\n");
do
{
printf("\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n");
printf("\nEnter the choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
Page. 17
case 4:
{
break;
}
default:
{
printf ("\nInvalid Choice\n");
}}}
while(choice!=4);
return 0;
}

void push()
{
if(top >= n - 1)
{
printf("\nSTACK OVERFLOW\n");

}
else
{
printf("Enter a value to be pushed : ");
scanf("%d",&x);
top++; // TOP is incremented after an element is pushed
stack[top] = x; // The pushed element is made as TOP
}}

void pop()
{
if(top <= -1)
{
printf("\nSTACK UNDERFLOW\n");
}
else
{
printf("\nThe popped element is %d",stack[top]);
top--; // Decrement TOP after a pop
}}

void display()
{
Page. 18
if(top >= 0)
{
// Print the stack
printf("\nELEMENTS IN THE STACK\n\n");
for(i = top ; i >= 0 ; i--)
printf("%d\t",stack[i]);
}
else
{
printf("\nEMPTY STACK\n");
}}
OUTPUT
Stack underflow when we try to pop an element from an empty stack

C Program to Implement Queue using Array


// C program for array implementation of queue
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>

// A structure to represent a queue


struct Queue {
int front, rear, size;
unsigned capacity;
int* array;
};

// function to create a queue


// of given capacity.
// It initializes size of queue as 0
struct Queue* createQueue(unsigned capacity)
{
struct Queue* queue = (struct Queue*)malloc(
sizeof(struct Queue));
Page. 19
queue->capacity = capacity;
queue->front = queue->size = 0;

// This is important, see the enqueue


queue->rear = capacity - 1;
queue->array = (int*)malloc(
queue->capacity * sizeof(int));
return queue;
}

// Queue is full when size becomes


// equal to the capacity
int isFull(struct Queue* queue)
{
return (queue->size == queue->capacity);
}

// Queue is empty when size is 0


int isEmpty(struct Queue* queue)
{
return (queue->size == 0);
}

// Function to add an item to the queue.


// It changes rear and size
void enqueue(struct Queue* queue, int item)
{
if (isFull(queue))
return;
queue->rear = (queue->rear + 1)
% queue->capacity;
queue->array[queue->rear] = item;
queue->size = queue->size + 1;
printf("%d enqueued to queue\n", item);
}

// Function to remove an item from queue.


// It changes front and size
int dequeue(struct Queue* queue)
{
if (isEmpty(queue))
Page. 20
return INT_MIN;
int item = queue->array[queue->front];
queue->front = (queue->front + 1)
% queue->capacity;
queue->size = queue->size - 1;
return item;
}

// Function to get front of queue


int front(struct Queue* queue)
{
if (isEmpty(queue))
return INT_MIN;
return queue->array[queue->front];
}

// Function to get rear of queue


int rear(struct Queue* queue)
{
if (isEmpty(queue))
return INT_MIN;
return queue->array[queue->rear];
}

// Driver program to test above functions./


int main()
{
struct Queue* queue = createQueue(1000);

enqueue(queue, 10);
enqueue(queue, 20);
enqueue(queue, 30);
enqueue(queue, 40);

printf("%d dequeued from queue\n\n",


dequeue(queue));

printf("Front item is %d\n", front(queue));


printf("Rear item is %d\n", rear(queue));

return 0;
Page. 21
}

Output
10 enqueued to queue
20 enqueued to queue
30 enqueued to queue
40 enqueued to queue
10 dequeued from queue
Front item is 20
Rear item is 40

linked list
A linked list is a linear data structure that stores a collection of data elements dynamically.
Nodes represent those data elements, and links or pointers connect each node.
Each node consists of two fields, the information stored in a linked list and a pointer that stores
the address of its next node.
The last node contains null in its second field because it will point to no node.
A linked list can grow and shrink its size, as per the requirement.
It does not waste memory space.
Representation of a Linked List
This representation of a linked list depicts that each node consists of two fields. The first field
consists of data, and the second field consists of pointers that point to another node.

Here, the start pointer stores the address of the first node, and at the end, there is a null
pointer that states the end of the Linked List.
Creation of Node and Declaration of Linked Lists

struct node
{
int data;
struct node * next;
};

Page. 22
struct node * n;
n=(struct node*)malloc(sizeof(struct node*));

The linked list mainly has three types, they are:


Singly Linked List
Doubly Linked List
Circular Linked List

Singly Linked List


A singly linked list is the most common type of linked list. Each node has data and an address
field that contains a reference to the next node.

Doubly Linked List


There are two pointer storage blocks in the doubly linked list. The first pointer block in each
node stores the address of the previous node. Hence, in the doubly linked inventory, there are
three fields that are the previous pointers, that contain a reference to the previous node. Then
there is the data, and last you have the next pointer, which points to the next node. Thus, you
can go in both directions (backward and forward).

Circular Linked List

Page. 23
The circular linked list is extremely similar to the singly linked list. The only difference is that
the last node is connected with the first node, forming a circular loop in the circular linked list.

Circular link lists can either be singly or doubly-linked lists.


The next node's next pointer will point to the first node to form a singly linked list.
The previous pointer of the first node keeps the address of the last node to form a doubly-
linked list.
Trees

The tree is a nonlinear hierarchical data structure and comprises a collection of entities known
as nodes. It connects each node in the tree data structure using "edges”, both directed and
undirected.
The image below represents the tree data structure. The blue-colored circles depict the nodes
of the tree and the black lines connecting each node with another are called edges.
You will understand the parts of trees better, in the terminologies section.

After learning the introduction to a tree in data structures, you will see why you need a tree in
data structures.
The Necessity for a Tree in Data Structures
Other data structures like arrays, linked-list, stacks, and queues are linear data structures, and
all these data structures store data in sequential order. Time complexity increases with
increasing data size to perform operations like insertion and deletion on these linear data
structures. But it is not acceptable for today's world of computation.

Page. 24
The non-linear structure of trees enhances the data storing, data accessing, and manipulation
processes by employing advanced control methods traversal through it. You will learn about
tree traversal in the upcoming section.
But before that, understand the tree terminologies.
Tree Node
A node is a structure that contains a key or value and pointers in its child node in the tree data
structure.
In the tree data structure, you can define the tree node as follows.

struct node
{
int data;
struct node *leftchild;
struct node *rightchild;
}

Continuing with this tutorial, you will see some key terminologies of the tree in data structures.
Tree Terminologies
Root
In a tree data structure, the root is the first node of the tree. The root node is the initial node
of the tree in data structures.
In the tree data structure, there must be only one root node.

Edge

Page. 25
In a tree in data structures, the connecting link of any two nodes is called the edge of the tree
data structure.
In the tree data structure, N number of nodes connecting with N -1 number of edges.

Parent
In the tree in data structures, the node that is the predecessor of any node is known as a
parent node, or a node with a branch from itself to any other successive node is called the
parent node.

Child
The node, a descendant of any node, is known as child nodes in data structures.
In a tree, any number of parent nodes can have any number of child nodes.
In a tree, every node except the root node is a child node.

Siblings
In trees in the data structure, nodes that belong to the same parent are called siblings.

Page. 26
Leaf
Trees in the data structure, the node with no child, is known as a leaf node.
In trees, leaf nodes are also called external nodes or terminal nodes.

Internal nodes
Trees in the data structure have at least one child node known as internal nodes.
In trees, nodes other than leaf nodes are internal nodes.
Sometimes root nodes are also called internal nodes if the tree has more than one node.

Degree
In the tree data structure, the total number of children of a node is called the degree of the
node.
The highest degree of the node among all the nodes in a tree is called the Degree of Tree.
Level
In tree data structures, the root node is said to be at level 0, and the root node's children are at
level 1, and the children of that node at level 1 will be level 2, and so on.

Page. 27
Height
In a tree data structure, the number of edges from the leaf node to the particular node in the
longest path is known as the height of that node.
In the tree, the height of the root node is called "Height of Tree".
The tree height of all leaf nodes is 0.

Depth
In a tree, many edges from the root node to the particular node are called the depth of the
tree.
In the tree, the total number of edges from the root node to the leaf node in the longest path
is known as "Depth of Tree".
In the tree data structures, the depth of the root node is 0.

Path
In the tree in data structures, the sequence of nodes and edges from one node to another
node is called the path between those two nodes.
The length of a path is the total number of nodes in a path.zx

Page. 28
Subtree
In the tree in data structures, each child from a node shapes a sub-tree recursively and every
child in the tree will form a sub-tree on its parent node.

Now you will look into the types of trees in data structures.

Types of Tree in Data Structures


Here are the different kinds of tree in data structures:
General Tree:
The general tree is the type of tree where there are no constraints on the hierarchical
structure.

Properties
The general tree follows all properties of the tree data structure.
A node can have any number of nodes.

Binary Tree:
A binary tree has the following properties:
Page. 29
Properties
Follows all properties of the tree data structure.
Binary trees can have at most two child nodes.
These two children are called the left child and the right child.

Binary Search Tree


A binary search tree is a type of tree that is a more constricted extension of a binary tree data
structure.
Properties
Follows all properties of the tree data structure.
The binary search tree has a unique property known as the binary search property. This states
that the value of a left child node of the tree should be less than or equal to the parent node
value of the tree. And the value of the right child node should be greater than or equal to the
parent value.

AVL Tree
An AVL tree is a type of tree that is a self-balancing binary search tree.
Properties
Follows all properties of the tree data structure.
Self-balancing.
Each node stores a value called a balanced factor, which is the difference in the height of the
left sub-tree and right sub-tree.
All the nodes in the AVL tree must have a balance factor of -1, 0, and 1.

Tree Traversal-

Page. 30
Traversal of the tree in data structures is a process of visiting each node and prints their value.
There are three ways to traverse tree data structure.
Pre-order Traversal
In-Order Traversal
Post-order Traversal

In-Order Traversal
In the in-order traversal, the left subtree is visited first, then the root, and later the right
subtree.
Algorithm:
Step 1- Recursively traverse the left subtree
Step 2- Visit root node
Step 3- Recursively traverse right subtree

Pre-Order Traversal
In pre-order traversal, it visits the root node first, then the left subtree, and lastly right subtree.
Algorithm:
Step 1- Visit root node
Step 2- Recursively traverse the left subtree
Step 3- Recursively traverse right subtree

Post-Order Traversal
It visits the left subtree first in post-order traversal, then the right subtree, and finally the root
node.
Algorithm:
Step 1- Recursively traverse the left subtree
Page. 31
Step 2- Visit root node
Step 3- Recursively traverse right subtree

Graphs

A graph is a non-linear kind of data structure made up of nodes or vertices and edges. The
edges connect any two nodes in the graph, and the nodes are also known as vertices.

This graph has a set of vertices V= { 1,2,3,4,5} and a set of edges E= {


(1,2),(1,3),(2,3),(2,4),(2,5),(3,5),(4,50 }.
Now that you’ve learned about the definition of graphs in data structures, you will learn about
their various types.
Types of Graphs in Data Structures
There are different types of graphs in data structures, each of which is detailed below.
1. Finite Graph
The graph G=(V, E) is called a finite graph if the number of vertices and edges in the graph is
limited in number

2. Infinite Graph
Page. 32
The graph G=(V, E) is called a finite graph if the number of vertices and edges in the graph is
interminable.

3. Trivial Graph
A graph G= (V, E) is trivial if it contains only a single vertex and no edges.

4. Simple Graph
If each pair of nodes or vertices in a graph G=(V, E) has only one edge, it is a simple graph. As a
result, there is just one edge linking two vertices, depicting one-to-one interactions between
two elements.

5. Multi Graph
If there are numerous edges between a pair of vertices in a graph G= (V, E), the graph is
referred to as a multigraph. There are no self-loops in a Multigraph.

6. Null Graph
It's a reworked version of a trivial graph. If several vertices but no edges connect them, a graph
G= (V, E) is a null graph.

Page. 33
7. Complete Graph
If a graph G= (V, E) is also a simple graph, it is complete. Using the edges, with n number of
vertices must be connected. It's also known as a full graph because each vertex's degree must
be n-1.

8. Pseudo Graph
If a graph G= (V, E) contains a self-loop besides other edges, it is a pseudograph.

9. Regular Graph
If a graph G= (V, E) is a simple graph with the same degree at each vertex, it is a regular graph.
As a result, every whole graph is a regular graph.

10. Weighted Graph


A graph G= (V, E) is called a labeled or weighted graph because each edge has a value or weight
representing the cost of traversing that edge.
Page. 34
11. Directed Graph
A directed graph also referred to as a digraph, is a set of nodes connected by edges, each with
a direction.

12. Undirected Graph


An undirected graph comprises a set of nodes and links connecting them. The order of the two
connected vertices is irrelevant and has no direction. You can form an undirected graph with a
finite number of vertices and edges.

13. Connected Graph


If there is a path between one vertex of a graph data structure and any other vertex, the graph
is connected.

14. Disconnected Graph


When there is no edge linking the vertices, you refer to the null graph as a disconnected graph.

Page. 35
15. Cyclic Graph
If a graph contains at least one graph cycle, it is considered to be cyclic.

16. Acyclic Graph


When there are no cycles in a graph, it is called an acyclic graph.

17. Directed Acyclic Graph


It's also known as a directed acyclic graph (DAG), and it's a graph with directed edges but no
cycle. It represents the edges using an ordered pair of vertices since it directs the vertices and
stores some data.

18. Subgraph
The vertices and edges of a graph that are subsets of another graph are known as a subgraph.

Page. 36
Representation of Graphs in Data Structures
Graphs in data structures are used to represent the relationships between objects. Every graph
consists of a set of points known as vertices or nodes connected by lines known as edges. The
vertices in a network represent entities.
The most frequent graph representations are the two that follow:
Adjacency matrix
Adjacency list
You’ll look at these two representations of graphs in data structures in more detail:

Adjacency Matrix
A sequential representation is an adjacency matrix.
It's used to show which nodes are next to one another. I.e., is there any connection between
nodes in a graph?
You create an MXM matrix G for this representation. If an edge exists between vertex a and
vertex b, the corresponding element of G, gi,j = 1, otherwise gi,j = 0.
If there is a weighted graph, you can record the edge's weight instead of 1s and 0s.
Undirected Graph Representation

Directed Graph Representation

Weighted Undirected Graph Representation


Page. 37
Weight or cost is indicated at the graph's edge, a weighted graph representing these values in
the matrix.

Adjacency List
A linked representation is an adjacency list.
You keep a list of neighbors for each vertex in the graph in this representation. It means that
each vertex in the graph has a list of its neighboring vertices.
You have an arra of vertices indexed by the vertex number, and the
corresponding array member for each vertex x points to a singly linked list of x's neighbors.
Weighted Undirected Graph Representation Using Linked-List

Weighted Undirected Graph Representation Using an Array

Page. 38
Graph Traversal Algorithm -
Breadth-First Search or BFS
BFS is a search technique for finding a node in a graph data structure that meets a set of
criteria.
It begins at the root of the graph and investigates all nodes at the current depth level before
moving on to nodes at the next depth level.
To maintain track of the child nodes that have been encountered but not yet inspected, more
memory, generally you require a queue.
Algorithm of breadth-first search
Step 1: Consider the graph you want to navigate.
Step 2: Select any vertex in your graph, say v1, from which you want to traverse the graph.
Step 3: Examine any two data structures for traversing the graph.
Visited array (size of the graph)
Queue data structure
Step 4: Starting from the vertex, you will add to the visited array, and afterward, you will v1's
adjacent vertices to the queue data structure.
Step 5: Now, using the FIFO concept, you must remove the element from the queue, put it into
the visited array, and then return to the queue to add the adjacent vertices of the removed
element.
Step 6: Repeat step 5 until the queue is not empty and no vertex is left to be visited.

Depth-First Search or DFS

Page. 39
DFS is a search technique for finding a node in a graph data structure that meets a set of
criteria.
The depth-first search (DFS) algorithm traverses or explores data structures such as trees and
graphs. The DFS algorithm begins at the root node and examines each branch as far as feasible
before backtracking.
To maintain track of the child nodes that have been encountered but not yet inspected, more
memory, generally a stack, is required.
Algorithm of depth-first search
Step 1: Consider the graph you want to navigate.
Step 2: Select any vertex in our graph, say v1, from which you want to begin traversing the
graph.
Step 3: Examine any two data structures for traversing the graph.
Visited array (size of the graph)
Stack data structure
Step 4: Insert v1 into the array's first block and push all the adjacent nodes or vertices of vertex
v1 into the stack.
Step 5: Now, using the FIFO principle, pop the topmost element and put it into the visited
array, pushing all of the popped element's nearby nodes into it.
Step 6: If the topmost element of the stack is already present in the array, discard it instead of
inserting it into the visited array.
Step 7: Repeat step 6 until the stack data structure isn't empty.

References

https://trainings.internshala.com/
https://www.simplilearn.com/
https://www.javatpoint.com/
https://www.geeksforgeeks.org

Page. 40

You might also like