Unit-3
Linked Lists
Need of Dynamic Data Structures
Data structures can be of two types, static and dynamic data structures. Static data
structures are the ones in which the size of the structure is predefined and fixed. On
the other hand, dynamic data structures are the ones whose size and data both can be
modified at the runtime. Sometimes, we cannot predict the size of the data structure
beforehand, therefore in such situations, we use dynamic data structures since it is
flexible to increase or decrease the size. Some of the examples of dynamic data
structures are Linked List, Stack, Vector, etc.
What is Dynamic Data Structure?
The dynamic data structures are the ones that do not have a fixed size. Therefore,
these structures have the flexibility to increase or decrease their size during runtime.
These data structures grow or shrink in size as per the demand comes. It gives the
power to the programmer to control exactly how much memory should be utilized
while storing the data by occupying only that amount of space as much as there is
the data to be stored. These data structures can allocate or deallocate the unused
memory as per the need. However, since the allocation of memory is not fixed,
therefore, there is a possibility of overflow if the data structure exceeds the
maximum memory limit or underflow if the structure becomes empty.
Use of Dynamic Data Structure
The main use of dynamic data structures is that they easily facilitate the change in
size without affecting the other operations that are performed on the data structures
during runtime. However, the dynamic data structures are also widely used in
competitive programming where the constraints on the memory limit are high and
we cannot exceed the memory limit specified in the constraints. Given a high
memory constraint, we cannot allocate a static data structure of such a large size
therefore, dynamic data structures are useful in such cases.
Dynamic Data Structures VS Static Data Structures
Static data structure Dynamic data structure
1. A static data structure has a fixed and
1. Dynamic data structures can store any
predefined size because of which it can
amount of data in the memory since it does
store only a particular amount of data in
not have a fixed size.
the memory.
2. The size of the structure cannot grow 2. It can easily grow or shrink in size as
or shrink. per the need.
3. A dynamic data structure can modify
3. A static data structure can only modify
both the data items as well as the size of
the data in the memory.
the structure during runtime.
4. They are not very efficient as 4. The size of the dynamic data structures
compared to dynamic data structures as is randomly updated during runtime which
they have a fixed size. is why it is considered more efficient.
5. Static memory allocation can only be 5. Dynamic memory allocation can be
done on the stack. done on both stack and heap.
6. The dynamic data structure is
6. They are not as flexible as the
more flexible than the static data structures
dynamic data structures because of their
because of their easily grow able
fixed size.
and shrinkable size.
7. An example of a static data structure is 7. An example of a dynamic data structure
an array. can be a linked list.
Example for Dynamic Data Structure
Some major examples of the dynamic data structures include -
1. Vector
2. Linked List
3. Stack
4. Queue
5. Tree
Advantages and Disadvantages of Dynamic Data Structures
It makes efficient use of space in the memory.
It only uses that amount of space that is needed at any time for storage.
The unused space of the memory can be returned to the system for other
purposes.
However, the disadvantages of the dynamic data structures are as follows:
It is difficult to program the dynamic data structures as the system needs to
keep track of the size and data item's locations at all times.
Since the memory allocation is dynamic, there is a possibility of overflow and
underflow in case the structure exceeds its maximum limit or if the structure
becomes empty respectively.
It is slow for implementing searches such as accessing an element in a linked
list takes a linear time.
Single Link List and Its Dynamic Implementation
o Linked List can be defined as collection of objects called nodes that are
randomly stored in the memory.
o A node contains two fields i.e. data stored at that particular address and the
pointer which contains the address of the next node in the memory.
o The last node of the list contains pointer to the null.
Uses of Linked List
o The list is not required to be contiguously present in the memory. The node
can reside anywhere in the memory and linked together to make a list. This
achieves optimized utilization of space.
o List size is limited to the memory size and doesn't need to be declared in
advance.
o Empty node cannot be present in the linked list.
o We can store values of primitive types or objects in the singly linked list.
Singly linked list or One way chain
Singly linked list can be defined as the collection of ordered set of elements. The
number of elements may vary according to need of the program. A node in the singly
linked list consist of two parts: data part and link part. Data part of the node stores
actual information that is to be represented by the node while the link part of the
node stores the address of its immediate successor.
One way chain or singly linked list can be traversed only in one direction. In other
words, we can say that each node contains only next pointer, therefore we can not
traverse the list in the reverse direction.
Consider an example where the marks obtained by the student in three subjects are
stored in a linked list as shown in the figure.
In the above figure, the arrow represents the links. The data part of every node
contains the marks obtained by the student in the different subject. The last node in
the list is identified by the null pointer which is present in the address part of the last
node. We can have as many elements we require, in the data part of the list.
Traversing, Insertion,
You can insert a node at three different positions, they are:
At the beginning
At the end
At a specific position after a node
What Are the Benefits of a Singly Linked List?
You can perform operations like insertion and deletion with ease
It is a dynamic data structure, i.e., it does not have a fixed size
It doesn’t require the movement of nodes for insertion and deletion
It doesn’t need elements to be stored in consecutive memory spaces
It does not waste space as it uses space according to the requirement
What Are the Limitations of a Singly Linked List?
It requires more storage space because it also stores the next pointer with data
If you have to reach any node, then you have to go through every node before it
You can not traverse it from anywhere but the head node
It requires a different amount of time to access any elements
Sorting is complex in this linked list
Doubly Link List,
Doubly Linked List in C++ is very similar to a linked list, except that
each node also contains a pointer to the node previous to the current one.
This means that in a doubly linked list in C++ we can travel not only in
the forward direction, but also in the backward direction, which was not
possible with a palin linked list in C++.
A doubly linked list is a type of linked list in which each node consists
of 3 components:
• *prev - address of the previous node
• data - data item
• *next - address of next node
Representation of Doubly Linked List
Let's see how we can represent a doubly linked list on an algorithm/code.
Suppose we have a doubly linked list:
Newly created doubly linked list
Here, the single node is represented as
struct node {
int data;
struct node *next;
struct node *prev;
}
Doubly Linked List Complexity
Doubly Linked List Complexity Time Complexity Space Complexity
Insertion Operation O(1) or O(n) O(1)
Deletion Operation O(1) O(1)
1. Complexity of Insertion Operation
The insertion operations that do not require traversal have the time complexity
of O(1) .
And, insertion that requires traversal has time complexity of O(n) .
The space complexity is O(1) .
2. Complexity of Deletion Operation
All deletion operations run with time complexity of O(1) .
And, the space complexity is O(1) .
Doubly Linked List Applications
1. Redo and undo functionality in software.
2. Forward and backward navigation in browsers.
3. For navigation systems where forward and backward navigation is required.
Singly Linked List Vs Doubly Linked List
Singly Linked List Doubly Linked List
Each node consists of a data value and a Each node consists of a data value, a pointer to the next node,
pointer to the next node. and a pointer to the previous node.
Traversal can occur in one way only (forward
Traversal can occur in both ways.
direction).
It requires less space. It requires more space because of an extra pointer.
It has multiple usages. It can be implemented on the stack, hea
It can be implemented on the stack.
and binary tree.
Circular Link Lists
A Circular linked list is a linear data structure that is similar to a singly
linked list with only one change that its last node instead of containing a
NULL value contains the address of the head node or first node. Every
node of the circular linked list consists of data value and the pointer to the
next node and as it is circular, any node can be considered as the head
node.
There are basically two types of circular linked list:
1. Circular Singly Linked List
Here, the address of the last node consists of the address of the first node.
Circular Linked List Representation
2. Circular Doubly Linked List
Here, in addition to the last node storing the address of the first node, the first
node will also store the address of the last node.
Circular Doubly Linked List Representation
Note: We will be using the singly circular linked list to represent the working of
circular linked list.
Representation of Circular Linked List
Let's see how we can represent a circular linked list on an algorithm/code.
Suppose we have a linked list:
Initial circular linked list
Here, the single node is represented as
struct Node {
int data;
struct Node * next;
};
Insertion on a Circular Linked List
We can insert elements at 3 different positions of a circular linked list:
1. Insertion at the beginning
2. Insertion in-between nodes
3. Insertion at the end
Suppose we have a circular linked list with elements 1, 2, and 3.
Initial circular linked list
Let's add a node with value 6 at different positions of the circular linked list we
made above. The first step is to create a new node.
allocate memory for newNode
assign the data to newNode
New node
1. Insertion at the Beginning
store the address of the current first node in the newNode (i.e. pointing
the newNode to the current first node)
point the last node to newNode (i.e making newNode as head)
Insert at the beginning
2. Insertion in between two nodes
Let's insert newNode after the first node.
travel to the node given (let this node be p )
point the next of newNode to the node next to p
store the address of newNode at next of p
Insertion at a node
3. Insertion at the end
store the address of the head node to next of newNode (making newNode the
last node)
point the current last node to newNode
make newNode as the last node
Insert at the end
Deletion on a Circular Linked List
Suppose we have a double-linked list with elements 1, 2, and 3.
Initial circular linked list
1. If the node to be deleted is the only node
free the memory occupied by the node
store NULL in last
2. If last node is to be deleted
find the node before the last node (let it be temp )
store the address of the node next to the last node in temp
free the memory of last
make temp as the last node
Delete the last node
3. If any other nodes are to be deleted
travel to the node to be deleted (here we are deleting node 2)
let the node before node 2 be temp
store the address of the node next to 2 in temp
free the memory of 2
1. Complexity of Insertion Operation
The insertion operations that do not require traversal have the time complexity
of O(1) .
And, an insertion that requires traversal has a time complexity of O(n) .
The space complexity is O(1) .
2. Complexity of Deletion Operation
All deletion operations run with a time complexity of O(1) .
And, the space complexity is O(1) .
Why Circular Linked List?
1. The NULL assignment is not required because a node always points to
another node.
2. The starting point can be set to any node.
3. Traversal from the first node to the last node is quick.
Circular Linked List Applications
It is used in multiplayer games to give a chance to each player to play the
game.
Multiple running applications can be placed in a circular linked list on an
operating system. The os keeps on iterating over these applications.
Benefits of implementing a stack using a singly linked list include:
Dynamic memory allocation: The size of the stack can be increased or
decreased dynamically by adding or removing nodes from the linked list, without
the need to allocate a fixed amount of memory for the stack upfront.
Efficient memory usage: Since nodes in a singly linked list only have a next
pointer and not a prev pointer, they use less memory than nodes in a doubly
linked list.
Easy implementation: Implementing a stack using a singly linked list is
straightforward and can be done using just a few lines of code.
Versatile: Singly linked lists can be used to implement other data structures such
as queues, linked lists, and trees.
In summary, implementing a stack using a singly linked list is a simple and
efficient way to create a dynamic stack data structure in Python.
Real time examples of stack:
Stacks are used in various real-world scenarios where a last-in, first-out (LIFO)
data structure is required. Here are some examples of real-time applications of
stacks:
Function call stack: When a function is called in a program, the return address
and all the function parameters are pushed onto the function call stack. The stack
allows the function to execute and return to the caller function in the reverse order
in which they were called.
Undo/Redo operations: In many applications, such as text editors, image
editors, or web browsers, the undo and redo functionalities are implemented
using a stack. Every time an action is performed, it is pushed onto the stack.
When the user wants to undo the last action, the top element of the stack is
popped and the action is reversed.
Browser history: Web browsers use stacks to keep track of the pages visited by
the user. Every time a new page is visited, its URL is pushed onto the stack.
When the user clicks the “Back” button, the last visited URL is popped from the
stack and the user is directed to the previous page.
Expression evaluation: Stacks are used in compilers and interpreters to
evaluate expressions. When an expression is parsed, it is converted into postfix
notation and pushed onto a stack. The postfix expression is then evaluated using
the stack.
Call stack in recursion: When a recursive function is called, its call is pushed
onto the stack. The function executes and calls itself, and each subsequent call
is pushed onto the stack. When the recursion ends, the stack is popped, and the
program returns to the previous function call.
In summary, stacks are widely used in many applications where LIFO
functionality is required, such as function calls, undo/redo operations, browser
history, expression evaluation, and recursive function calls.
Unit-4
Trees: Definition and Basic Terminology,
A tree is a nonlinear hierarchical data structure that consists of nodes connected by
edges.
Why Tree Data Structure?
Other data structures such as arrays, linked list, stack, and queue are linear data
structures that store data sequentially. In order to perform any operation in a linear
data structure, the time complexity increases with the increase in the data size. But,
it is not acceptable in today's computational world. Different tree data structures
allow quicker and easier access to the data as it is a non-linear data structure.
Tree Node
A node is a structure that contains a key or value and pointers in its child node in the
tree data structure.
struct node
int data;
struct node *leftchild;
struct node *rightchild;
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
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.
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.
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
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.
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:
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.
Tree Traversal
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
Step 2- Visit root node
Step 3- Recursively traverse right subtree
Tree Applications
Binary Search Trees(BSTs) are used to quickly check whether an element is
present in a set or not.
Heap is a kind of tree that is used for heap sort.
A modified version of a tree called Tries is used in modern routers to store
routing information.
Most popular databases use B-Trees and T-Trees, which are variants of the
tree structure we learned above to store their data
Compilers use a syntax tree to validate the syntax of every program you write.
Binary Tree,
A binary tree is a tree data structure in which each parent node can have at
most two children. Each node of a binary tree consists of three items:
data item
address of left child
address of right child
Binary Tree
Types of Binary Tree
1. Full Binary Tree
A full Binary tree is a special type of binary tree in which every parent
node/internal node has either two or no children.
Full Binary Tree
External and Internal Nodes,
I ROOT (root is also an INTERNAL NODE, unless it is leaf)
/ \
I I INTERNAL NODES
/ /\
o o o EXTERNAL NODES (or leaves)
An external node is one without child branches, while an internal node has at least one child
branch. The size of a binary tree refers to the number of nodes it has. The distance from a
node B to the root node is the level of B. The root is at level 0, the root's children are at level
1, and so on.
In the figure above, 3 and 6 are internal nodes, while 10, 14 and 13 are external nodes.
Applications of Binary Tree:
In compilers, Expression Trees are used which is an application of binary
trees.
Huffman coding trees are used in data compression algorithms.
Priority Queue is another application of binary tree that is used for searching
maximum or minimum in O(1) time complexity.
Represent hierarchical data.
Used in editing software like Microsoft Excel and spreadsheets.
Useful for indexing segmented at the database is useful in storing cache in
the system,
Syntax trees are used for most famous compilers for programming like GCC,
and AOCL to perform arithmetic operations.
For implementing priority queues.
Used to find elements in less time (binary search tree)
Used to enable fast memory allocation in computers.
Used to perform encoding and decoding operations.
Binary trees can be used to organize and retrieve information from large
datasets, such as in inverted index and k-d trees.
Binary trees can be used to represent the decision-making process of
computer-controlled characters in games, such as in decision trees.
Binary trees can be used to implement searching algorithms, such as in
binary search trees which can be used to quickly find an element in a sorted
list.
Binary trees can be used to implement sorting algorithms, such as in heap
sort which uses a binary heap to sort elements efficiently.
Binary Tree Traversals: Pre-Order, In-Order and Post-Order
Traversals.
You can traverse binary trees in three ways:
Preorder
When you traverse a tree in a specific order, i.e., root then left subtree and then the
right subtree.
Inorder
You traverse a tree from the left subtree, then root, and then to the right subtree.
Postorder
You traverse in order from the left subtree, then the right subtree, to the root.
Representation of Infix, Post-Fix and Prefix Expressions using
Trees.
Expression Tree is used to represent expressions. Let us look at
some examples of prefix, infix and postfix expressions from
expression tree for 3 of the expresssions:
a*b+c
a+b*c+d
a+b-c*d+e*f
Expression Tree for a*b+c
Expressions from Expression Tree
Infix expression a*b+c
Prefix expression +*abc
Postfix expression ab*c+
Infix, Prefix and Postfix Expressions
from Expression Tree for a+b*c+d
Expression Tree for a + b * c + d can be represent ed as:
Expression Tree for a + b *
c+d
Expressions for the binary expression tree above can be written as
Infix expression a+b*c+d
Prefix expression *+ab+cd
Postfix expression ab+cd+*
Infix, Prefix and Postfix Expressions
from Expression Tree for a+b-c*d+e*f
Expression Tree for a + b - c * d + e * f can be represented as:
Expression Tree for
a+b-c*d+e*f
Expressions for the binary expression tree above can be written as
Infix expression a + b - c *d + e * f
Prefix expression *+a-bc+d*ef
Postfix expression abc-+def*+*
Graphs: Basic Terminology,
Graphs in data structures are non-linear data structures made up of a finite number of
nodes or vertices and the edges that connect them. Graphs in data structures are used
to address real-world problems in which it represents the problem area as a network like
telephone networks, circuit networks, and social networks. For example, it can
represent a single user as nodes or vertices in a telephone network, while the link
between them via telephone represents edges.
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
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.
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.
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.
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.
After you learn about the many types of graphs in graphs in data structures, you will
move on to graph terminologies.
Terminologies of Graphs in Data Structures
Following are the basic terminologies of graphs in data structures:
An edge is one of the two primary units used to form graphs. Each edge has two ends, which
are vertices to which it is attached.
If two vertices are endpoints of the same edge, they are adjacent.
A vertex's outgoing edges are directed edges that point to the origin.
A vertex's incoming edges are directed edges that point to the vertex's destination.
The total number of edges occurring to a vertex in a graph is its degree.
The out-degree of a vertex in a directed graph is the total number of outgoing edges,
whereas the in-degree is the total number of incoming edges.
A vertex with an in-degree of zero is referred to as a source vertex, while one with an out-
degree of zero is known as sink vertex.
An isolated vertex is a zero-degree vertex that is not an edge's endpoint.
A path is a set of alternating vertices and edges, with each vertex connected by an edge.
The path that starts and finishes at the same vertex is known as a cycle.
A path with unique vertices is called a simple path.
For each pair of vertices x, y, a graph is strongly connected if it contains a directed path from
x to y and a directed path from y to x.
A directed graph is weakly connected if all of its directed edges are replaced with undirected
edges, resulting in a connected graph. A weakly linked graph's vertices have at least one
out-degree or in-degree.
A tree is a connected forest. The primary form of the tree is called a rooted tree, which is a
free tree.
A spanning subgraph that is also a tree is known as a spanning tree.
A connected component is the unconnected graph's most connected subgraph.
A bridge, which is an edge of removal, would sever the graph.
Forest is a graph without a cycle.
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
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
You will now see which all operations are conducted in graphs data structure after
understanding the representation of graphs in the data structure.