0% found this document useful (0 votes)
30 views34 pages

Unit 03 - Non Linear Data Structure

Uploaded by

vivekmengwal1234
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
30 views34 pages

Unit 03 - Non Linear Data Structure

Uploaded by

vivekmengwal1234
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 34

NON LINEAR DATA STRUCTURE ..

Non-linear data structure


o A non-linear data structure is another important type in which data elements
are not arranged sequentially; mainly, data elements are arranged in random
order without forming a linear structure.
o Data elements are present at the multilevel, for example, tree.
o In trees, the data elements are arranged in the hierarchical form, whereas in
graphs, the data elements are arranged in random order, using the edges and
vertex.
o Multiple runs are required to traverse through all the elements completely.
Traversing in a single run is impossible to traverse the whole data structure.
o Each element can have multiple paths to reach another element.
o The data structure where data items are not organized sequentially is called a
non-linear data structure. In other words, data elements of the non-linear data
structure could be connected to more than one element to reflect a special
relationship among them.

TREE :DEFINITION AND CONCEPTS:

In C++, a tree is a hierarchical data structure consisting of nodes connected by edges. Each
node has a value (or data), and the edges represent the relationships between nodes. Trees
are widely used in computer science for various applications such as representing
hierarchical data, organizing data for efficient searching and sorting, and implementing
various algorithms.

What is Tree Data structure?


Trees: Unlike Arrays, Stack, Linked Lists, and queues, which are linear data structures,
trees are hierarchical.

o A tree data structure is a collection of objects or entities known as nodes linked


together to represent or simulate hierarchy.
o This data is not arranged in a sequential contiguous location like as we have
observed in an array, the homogeneous data elements are placed at the
contiguous memory location so that the retrieval of data elements is simpler.
o A tree data structure is non-linear because it does not store sequentially. It is a
hierarchical structure as elements in a Tree are arranged in multiple levels.
o The topmost node in the Tree data structure is known as a root node. Each
node contains some data, and data can be of any type. The node contains the
employee's name in the tree structure, so the data type would be a string.
o Each node contains some data and the link or reference of other nodes that can
be called children.

Terminologies used in tree:


1. Root node: The tree consists of a root node; it is the starting node of the tree from
which any tree grows. The root node is present at level 0 in any tree. Depending on the
order of the tree, it can hold that much of child nodes. For example, if in a tree the
order is 3, then it can have at most three child nodes, and minimum, it can have 0 child
nodes.
2. Child node: The child node is the node that comes after the root node, which has a
parent node and has some ancestors. It is the node present at the next level of its
parent node. For example, if any node is present at level 5, it is sure that its parent node
is present at level 4, just above its level.
3. Edge: Edges are nothing but the link between the parent node, and the children node
is called the link or edge connectivity between two nodes.
4. Siblings: The nodes having the same parent node is called siblings of each other. Same
ancestors are not siblings, and only the parent node must be the same, defined as
siblings. For example, if node A has two child nodes, B and C, B and C are called siblings.
5. Leaf node: The leaf node is the node that does not have any child nodes. It is the
termination point of any tree. It is also known as External nodes.
6. Internal nodes: Internal nodes are non-leaf nodes, having at least one child node with
child nodes.
7. Degree of a node: The degree of a node is defined as the number of children nodes.
The degree of the leaf node is always 0 because it does not have any children, and the
internal node always has atleast one degree, as it contains atleast one child node.
8. Height of the tree: The tree's height is defined as the distance of the root node to the
leaf node present at the last level is called the height of the tree. In other words, height
is the maximum level upto which the tree is extended.

TYPES OF TREE:

Binary Tree:
• A binary tree is a hierarchical data structure where each node has at most two
children, referred to as the left child and the right child.
Ternary Tree:
• A ternary tree is a tree data structure where each node has up to three
children.
• Ternary trees are less common than binary trees but can be useful in certain
applications, such as decision trees or tries where each node represents a
character and has three branches for the next character.
N-ary Tree:
• An N-ary tree is a tree data structure where each node can have a variable
number of children, not limited to two as in binary or three as in ternary trees.
• N-ary trees are versatile and can be used in various applications such as file
systems, organization hierarchies, and representing hierarchical data
structures.
• Unlike binary trees, which have a fixed number of children per node, the
number of children in an N-ary tree can vary from node to node.

REPRESENTATION OF BINARY TREE:

In C++, binary trees can be represented using various techniques. Two common
methods are:

1. Node-based Representation: In this method, each node of the binary tree is


represented using a structure or class that contains information about the
node's value and pointers to its left and right children. This is the most
intuitive representation and is often used when implementing binary tree
algorithms.
2. Array-based Representation: In this method, the binary tree is stored in an
array where each element of the array represents a node of the tree. This
representation is more memory-efficient but requires some additional
calculations to navigate the tree.
NODE BADED REPRESENTATION OF TREE:

Sure, let's create a simple node-based representation of a binary tree in C++. In this representation,
we'll define a `TreeNode` structure to represent each node of the binary tree. Each `TreeNode` will
have an integer value and pointers to its left and right children.

Here's the code:

```cpp

#include <iostream>

// TreeNode structure representing each node of the binary tree

struct TreeNode {

int val;

TreeNode* left;

TreeNode* right;

// Constructor to initialize the node with a value

TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}


};

// Function to create a sample binary tree

TreeNode* createSampleTree() {

// Creating nodes and connecting them to form a binary tree

TreeNode* root = new TreeNode(1);

root->left = new TreeNode(2);

root->right = new TreeNode(3);

root->left->left = new TreeNode(4);

root->left->right = new TreeNode(5);

return root;

// Function to traverse the binary tree in pre-order and print the values of nodes

void preOrderTraversal(TreeNode* node) {

if (node == nullptr) return; // Base case: if node is null, return

// Print the value of the current node

std::cout << node->val << " ";

// Recursively traverse the left subtree

preOrderTraversal(node->left);

// Recursively traverse the right subtree

preOrderTraversal(node->right);

int main() {

// Create a sample binary tree

TreeNode* root = createSampleTree();

// Print the values of nodes using pre-order traversal

std::cout << "Pre-order traversal of the binary tree: ";

preOrderTraversal(root);
std::cout << std::endl;

// Deallocate memory to avoid memory leaks

// In a real application, you would typically use a tree deletion function

delete root->left->left;

delete root->left->right;

delete root->left;

delete root->right;

delete root;

return 0;

```

Explanation of the code:

- We define a `TreeNode` structure to represent each node of the binary tree. Each node contains an
integer value (`val`) and pointers to its left and right children (`left` and `right`, respectively).

- We create a sample binary tree using the `createSampleTree` function, which creates nodes and
connects them to form a binary tree.

- We define a `preOrderTraversal` function to perform a pre-order traversal of the binary tree. Pre-

order traversal visits the current node before its children.

- In the `main` function, we create a sample binary tree, then traverse it using pre-order traversal and
print the values of nodes.

- Finally, we deallocate memory to avoid memory leaks.

Output of the code:

```Pre-order traversal of the binary tree: 1 2 4 5 3

```
This output indicates that the pre-order traversal of the sample binary tree visits nodes in the order:
root, left subtree, right subtree.

ARRAY BASED REPRESENTATION OF TREE:

Sure, let's create a simple array-based representation of a binary tree in C++. In this representation,
we'll store the binary tree in an array where each element of the array represents a node of the tree.

Here's the code:

```cpp

#include <iostream>

#include <vector>

// Function to perform a level-order traversal of the binary tree

void levelOrderTraversal(const std::vector<int>& tree) {

// Traverse the array and print the values of nodes

for (int i = 0; i < tree.size(); ++i) {

std::cout << tree[i] << " ";

std::cout << std::endl;

int main() {

// Define an array to store the binary tree

std::vector<int> tree = {1, 2, 3, 4, 5}; // Binary tree representation: 1

// /\

// 2 3

// /\

// 4 5

// Print the values of nodes using level-order traversal

std::cout << "Level-order traversal of the binary tree: ";

levelOrderTraversal(tree);

return 0;

```
Explanation of the code:

- We define a vector `tree` to store the binary tree. Each element of the vector represents a node of
the tree, and the position of each element in the vector determines its relationship with other nodes.

- In this example, the binary tree represented by the vector is:

```

/\

2 3

/\

4 5

```

- We define a `levelOrderTraversal` function to perform a level-order traversal of the binary tree.


Level-order traversal visits nodes level by level, from left to right.

- In the `main` function, we create the binary tree using the vector `tree`, and then traverse it using
level-order traversal and print the values of nodes.

Output of the code:

```

Level-order traversal of the binary tree: 1 2 3 4 5

```

This output indicates that the level-order traversal of the binary tree visits nodes in the order: root,
left child, right child, left child of the left child, right child of the left child, and so on.

TYPES OF BINARY TREE:

Certainly! Here are the main types of binary trees in C++ along with visual representations:

### 1. Binary Search Tree (BST):

- Definition: A binary tree where the left child of each node contains a value less than the node's
value, and the right child contains a value greater than the node's value.

- Visual Representation:

```

/\
3 8

/\/\

2 46 9

```

### 2. Balanced Binary Tree:

- Definition: A binary tree in which the height difference between the left and right subtrees of any
node is at most one.

- Visual Representation (AVL Tree):

```

/\

3 8

/\/\

2 46 9

```

### 3. Complete Binary Tree:

- Definition: A binary tree in which every level, except possibly the last, is completely filled, and all
nodes are as far left as possible.

- Visual Representation:

```

/\

2 3

/\/

4 56

```

### 4. Full Binary Tree:

- Definition: A binary tree in which every node other than the leaves has two children.

- Visual Representation:
```

/\

2 3

/\

4 5

```

### 5. Perfect Binary Tree:

- Definition: A binary tree in which all internal nodes have exactly two children, and all leaves are at
the same level.

- Visual Representation:

```

/\

2 3

/\/\

4 56 7

```

### 6. Degenerate (or Pathological) Binary Tree:

- Definition: A binary tree in which each parent node has only one associated child node.

- Visual Representation:

```

4
```

These are the main types of binary trees in C++, each with its own characteristics and use cases. The
visual representations provide a clear picture of the structure of each type of binary tree.

BINARY TREE TRAVERSAL( INORDER,POSTORDER,PREORDER)

Sure, let's discuss three common types of binary tree traversals: Inorder, Preorder, and Postorder. I'll
provide brief explanations along with visual representations for each.

### 1. Inorder Traversal:

- In an inorder traversal, the nodes of the binary tree are visited in the order: left subtree, current
node, right subtree.

- In other words, for each node, the left subtree is visited first, then the current node is visited, and
finally, the right subtree is visited.

- Inorder traversal is commonly used to retrieve the elements of a binary search tree in sorted order.

- Visual representation:

```

/\

2 3

/\

4 5

Inorder Traversal: 4, 2, 5, 1, 3

```

### 2. Preorder Traversal:

- In a preorder traversal, the nodes of the binary tree are visited in the order: current node, left
subtree, right subtree.

- In other words, for each node, the current node is visited first, then its left subtree is visited, and
finally, its right subtree is visited.
- Preorder traversal is commonly used to create a copy of the tree or to evaluate expressions in
expression trees.

- Visual representation:

```

/\

2 3

/\

4 5

Preorder Traversal: 1, 2, 4, 5, 3

```

### 3. Postorder Traversal:

- In a postorder traversal, the nodes of the binary tree are visited in the order: left subtree, right
subtree, current node.

- In other words, for each node, its left subtree is visited first, then its right subtree is visited, and
finally, the current node is visited.

- Postorder traversal is commonly used to delete nodes from a tree or to get postfix notation in
expression trees.

- Visual representation:

```

/\

2 3

/\

4 5

Postorder Traversal: 4, 5, 2, 3, 1

```
These visual representations illustrate the order in which nodes are visited during each type of
traversal. Each traversal has its own unique order, making them suitable for different applications
based on the order in which you need to process the nodes of the tree.

BINARY SEARCH TREE :

A Binary Search Tree is a data structure used in computer science for organizing
and storing data in a sorted manner. Each node in a Binary Search Tree has at
most two children, a left child and a right child, with the left child containing
values less than the parent node and the right child containing values greater than
the parent node. This hierarchical structure allows for
efficient searching, insertion, and deletion operations on the data stored in the
tree.

APPLICATION OF BST:

Applications of BST
Last Updated : 09 Mar, 2023AAAAA

AAAAAAAAAAAAAAAAAAAAA

••
Binary Search Tree (BST) is a data structure that is commonly used to
implement efficient searching, insertion, and deletion operations. The key
feature of a BST is that it is a binary tree, where each node has at most two
child nodes, and the value of each node is greater than all the values in its left
subtree and less than all the values in its right subtree. This means that the left
subtree of a node contains values that are smaller than the node’s value, and
the right subtree contains values that are larger. Due to this property, BSTs
allow for efficient searching by repeatedly dividing the search space in half,
which makes it an important data structure in computer science and many
other fields.
• The left subtree of a node contains only nodes with keys lesser than
the node’s key.
• The right subtree of a node contains only nodes with keys greater
than the node’s key.
• The left and right subtree each must also be a binary search tree.
There must be no duplicate nodes.

Binary Search Tree – Structure

GENERAL TREE:

Difference between General tree and Binary


tree
Last Updated : 19 Feb, 2020

••
BINARY TREE VS GENERAL TREE :

In C++, both general trees and binary trees are fundamental data structures used for organizing and
representing hierarchical data. However, they have different characteristics and are suitable for
different types of applications. Let's compare general trees and binary trees in C++:

### General Tree:

1. **Definition**:

- A general tree is a hierarchical data structure where each node can have zero or more children.

- Nodes in a general tree do not have a fixed maximum number of children.

2. **Implementation**:

- In C++, a general tree can be implemented using structures or classes to represent nodes, where
each node contains a value and a list (e.g., vector) of child pointers.

3. **Traversal**:

- Traversing a general tree may involve techniques like depth-first search (DFS) or breadth-first
search (BFS) to visit nodes in a specific order.

4. **Applications**:

- General trees are used to represent hierarchical data structures such as organization charts, file
systems, XML/HTML documents, and syntax trees in compilers.

### Binary Tree:

1. **Definition**: - A binary tree is a hierarchical data structure where each node has at most two
children: a left child and a right child.
- Binary trees are characterized by their recursive structure, which is conducive to various
algorithms and operations.

2. **Implementation**:

- In C++, a binary tree is often implemented using structures or classes to represent nodes, with
each node containing a value and pointers to its left and right children.

3. **Traversal**:

- Common traversal techniques for binary trees include inorder, preorder, and postorder traversals,
each visiting nodes in a specific order relative to their children.

4. **Applications**:

- Binary trees have various applications, including binary search trees (BSTs) for efficient searching,
heaps for priority queue implementations, expression trees for evaluating arithmetic expressions,
and Huffman coding for data compression.

### Comparison:

- **Structure**: General trees can have any number of children per node, while binary trees have at
most two children per node.

- **Traversal**: Traversing a general tree may require specialized algorithms, while binary trees have
well-defined traversal orders like inorder, preorder, and postorder.

- **Space Complexity**: Binary trees can have more compact representations, especially for
complete binary trees, compared to general trees, which may have irregular structures.

- **Performance**: Certain operations, such as searching, insertion, and deletion, may be more
efficient in binary trees like BSTs due to their balanced nature compared to general trees.

In summary, general trees are more flexible and can represent a wider range of hierarchical
structures, while binary trees offer specific advantages for certain applications, particularly those
requiring efficient search and manipulation operations.

CONVERSION OF BINARY TREE TO GENERAL TREE WITH CODE :

Certainly! Converting a general tree to a binary tree involves transforming a tree where nodes can
have any number of children into a binary tree where each node has at most two children. One
common approach is to represent the general tree as a binary tree by making use of the 'left-child,
right-sibling' representation.

In this representation:

- Each node in the binary tree represents a node in the original general tree.

- The left child of a node in the binary tree corresponds to its first child in the general tree.

- The right sibling of a node in the binary tree corresponds to the next sibling of the node in the
general tree.
Here's how you can implement this conversion in C++:

```cpp

#include <iostream>

#include <vector>

// TreeNode structure representing each node of the binary tree

struct TreeNode {

int val;

TreeNode* left;

TreeNode* right;

// Constructor to initialize the node with a value

TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

};

// Node structure representing each node of the general tree

struct Node {

int val;

std::vector<Node*> children;

// Constructor to initialize the node with a value

Node(int x) : val(x) {}

};

// Function to convert a general tree to a binary tree

TreeNode* convertToBinaryTree(Node* root) {

if (root == nullptr) {

return nullptr;

TreeNode* binaryRoot = new TreeNode(root->val);

if (!root->children.empty()) {
binaryRoot->left = convertToBinaryTree(root->children[0]);

TreeNode* currentBinaryNode = binaryRoot->left;

for (int i = 1; i < root->children.size(); ++i) {

currentBinaryNode->right = convertToBinaryTree(root->children[i]);

currentBinaryNode = currentBinaryNode->right;

return binaryRoot;

// Function to perform an inorder traversal of the binary tree

void inorderTraversal(TreeNode* root) {

if (root == nullptr) {

return;

inorderTraversal(root->left);

std::cout << root->val << " ";

inorderTraversal(root->right);

int main() {

// Create a sample general tree

Node* generalRoot = new Node(1);

generalRoot->children.push_back(new Node(2));

generalRoot->children.push_back(new Node(3));

generalRoot->children[0]->children.push_back(new Node(4));

generalRoot->children[0]->children.push_back(new Node(5));

generalRoot->children[1]->children.push_back(new Node(6));
// Convert the general tree to a binary tree

TreeNode* binaryRoot = convertToBinaryTree(generalRoot);

// Print the elements of the binary tree using inorder traversal

std::cout << "Inorder traversal of the binary tree: ";

inorderTraversal(binaryRoot);

std::cout << std::endl;

// Deallocate memory to avoid memory leaks

// In a real application, you would typically use a tree deletion function

// Here, we are not deallocating memory to keep the example simple

return 0;

```

Output:

```

Inorder traversal of the binary tree: 2 4 5 3 6 1

```

Explanation of the code:

- We define structures `TreeNode` and `Node` to represent nodes of the binary tree and the general
tree, respectively.

- We define a function `convertToBinaryTree` to recursively convert the general tree to a binary tree
using the 'left-child, right-sibling' representation.

- We traverse the general tree, creating the corresponding binary tree nodes and setting the left child
and right sibling pointers accordingly.

- We define an `inorderTraversal` function to perform an inorder traversal of the resulting binary tree
and print its elements.

- In the `main` function, we create a sample general tree, convert it to a binary tree, and then
traverse and print the binary tree in inorder traversal order.
APPLICATION OF TREES:

Here are some applications of trees in Data Structures and Algorithms


(DSA) in C++:
**1. File Systems**
File systems are often represented as trees. Each folder or directory is a
node in the tree, and files are the leaves. This hierarchical structure makes
it easy to navigate and organize files.
**2. XML Parsing**
Trees are used to parse and process XML documents. XML is a markup
language that is used to store and exchange data. The tree structure of
XML documents makes it easy to parse and process them.
**3. Database Indexing**
Many databases use trees to index their data. Indexes allow databases to
quickly find specific data records. B-trees and B+trees are two types of
trees that are commonly used for database indexing.
**4. Compiler Design**
The syntax of programming languages is often defined using a tree
structure called a parse tree. Parse trees are used by compilers to generate
machine code from source code.
**5. Artificial Intelligence**
Decision trees are often used in artificial intelligence to make decisions
based on a series of criteria. For example, a decision tree could be used to
decide whether or not to approve a loan application.
**6. Computer Graphics**
Trees are used in computer graphics to represent the structure of 3D
objects. For example, a tree could be used to represent the structure of a
human body or a car.
**7. Networking**
Trees are used in networking to represent the topology of networks. For
example, a tree could be used to represent the topology of a local area
network (LAN).
These are just a few of the many applications of trees in DSA and
C++. Trees are a versatile data structure that can be used to solve a wide
variety of problems.
AVL TREES, 2-3 TREES, HEIGHT BALANCE, WEIGHT BALANCE

An AVL is a self-balancing Binary Search Tree (BST) where the


difference between the heights of left and right subtrees of any node
cannot be more than one.

An AVL is a self-balancing Binary Search Tree (BST) where the


difference between the heights of left and right subtrees of any node
cannot be more than one.

KEY POINTS

• It is height balanced tree


• It is a binary search tree
• It is a binary tree in which the height difference between the left
subtree and right subtree is almost one
• Height is the maximum depth from root to leaf

Characteristics of AVL Tree:

• It follows the general properties of a Binary Search Tree.


• Each subtree of the tree is balanced, i.e., the difference between
the height of the left and right subtrees is at most 1.
• The tree balances itself when a new node is inserted. Therefore,
the insertion operation is time-consuming

2-3 TREE:

A 2-3 Tree is a tree data structure where every node with children has
either two children and one data element or three children and two data
elements. A node with 2 children is called a 2-NODE and a node with 3
children is called a 3-NODE. A 4-NODE, with three data elements, may be
temporarily created during manipulation of the tree but is never persistently
stored in the tree.

Nodes on the outside of the tree i.e. the leaves have no children and have
either one or two data elements. All its leaves must be on the same level so
that a 2-3 tree is always height balanced. A 2-3 tree is a special case of
a B-Tree of order 3. Below is an example of a 2-3 tree.

Properties of 2-3 Trees


A 2-3 tree follows the below mentioned properties.
1. Every internal node in the tree is a 2-node or a 3-node i.e it has either
one value or two values.
2. A node with one value is either a leaf node or has exactly two
children. Values in left sub tree < value in node < values in right sub
tree.
3. A node with two values is either a leaf node or has exactly 3 children. It
cannot have 2 children. Values in left sub tree < first value in node <
values in middle sub tree < second value in node < value in right
sub tree.
4. All leaf nodes are at the same level.
You can notice that the previous example given satisfies all the above
mentioned properties.It is important to note from the above properties that
all data in a 2-3 tree is stored in a sorted manner which makes search
operations fast and effective.

HEIGHT BALANCE AND WEIGHT BALANCE IN TREE:

In the context of trees, including binary trees like AVL trees, "balance" generally refers to how evenly
distributed or weighted the branches or subtrees are relative to each other. There are two common
types of balance in trees:

1. **Height Balance**:

- Height balance, also known as "height-balanced" or "height-balancing," refers to ensuring that the
heights of subtrees from any given node in the tree are roughly equal.

- In a height-balanced tree, the difference in height between the left and right subtrees of any node
is limited to some predefined threshold (often 1 or 2).

- Maintaining height balance in trees helps ensure efficient operations like searching, insertion, and
deletion, as it keeps the tree relatively shallow and prevents performance degradation to O(n) time
complexity.

- Examples of height-balanced trees include AVL trees, which are self-balancing binary search trees
where the height difference between the left and right subtrees of any node (the balance factor) is
kept within a certain range, typically -1, 0, or 1.

2. **Weight Balance**:

- Weight balance, sometimes referred to as "weight-balanced" or simply "balanced," focuses on the


distribution of nodes or elements among the branches or subtrees of the tree.

- In a weight-balanced tree, the number of nodes or elements in the left and right subtrees of any
node is approximately equal or within some predefined range.

- Weight-balanced trees aim to evenly distribute the workload among subtrees, which can help
maintain performance and prevent skewed or degenerate tree structures.
- Examples of weight-balanced trees include B-trees and red-black trees, which ensure that the
number of elements in subtrees satisfies certain criteria to keep the tree balanced.

Both height balance and weight balance are crucial concepts in designing and implementing efficient
tree-based data structures. While height balance focuses on the structural properties of the tree,
weight balance focuses on the distribution of data within the tree. These concepts often go hand in
hand, with various tree structures and algorithms designed to maintain both types of balance to
achieve optimal performance and efficiency in operations.

GRAPH IN C++;

Graph Data Structure is a collection of nodes connected by edges. It’s


used to represent relationships between different entities. Graph
algorithms are methods used to manipulate and analyze graphs, solving
various problems like finding the shortest path or detecting cycles.

What is Graph Data Structure?


Graph is a non-linear data structure consisting of vertices and edges. The vertices
are sometimes also referred to as nodes and the edges are lines or arcs that connect
any two nodes in the graph. More formally a Graph is composed of a set of
vertices( V ) and a set of edges( E ). The graph is denoted by G(V, E).
Graph data structures are a powerful tool for representing and analyzing
complex relationships between objects or entities. They are particularly
useful in fields such as social network analysis, recommendation
systems, and computer networks. In the field of sports data science,
graph data structures can be used to analyze and understand the
dynamics of team performance and player interactions on the field.

Components of a Graph:
• Vertices: Vertices are the fundamental units of the graph.
Sometimes, vertices are also known as vertex or nodes. Every
node/vertex can be labeled or unlabeled.
• Edges: Edges are drawn or used to connect two nodes of the
graph. It can be ordered pair of nodes in a directed graph. Edges
can connect any two nodes in any possible way. There are no
rules. Sometimes, edges are also known as arcs. Every edge can
be labelled/unlabelled.

A graph is an abstract data type (ADT) which consists of a set of


objects that are connected to each other via 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 E is the set of edges, connecting the pairs of
vertices. Take a look at the following graph −

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.

Operations of Graphs
The primary operations of a graph include creating a graph with
vertices and edges, and displaying the said graph. However, one
of the most common and popular operation performed using
graphs are Traversal, i.e. visiting every vertex of the graph in a
specific order.
There are two types of traversals in Graphs −

• Depth First Search Traversal


• Breadth First Search Traversal
Depth First Search Traversal

Depth First Search is a traversal algorithm that visits all the


vertices of a graph in the decreasing order of its depth. In this
algorithm, an arbitrary node is chosen as the starting point and
the graph is traversed back and forth by marking unvisited
adjacent nodes until all the vertices are marked.

The DFS traversal uses the stack data structure to keep track of
the unvisited nodes.

Click and check Depth First Search Traversal

Breadth First Search Traversal

Breadth First Search is a traversal algorithm that visits all the


vertices of a graph present at one level of the depth before
moving to the next level of depth. In this algorithm, an arbitrary
node is chosen as the starting point and the graph is traversed by
visiting the adjacent vertices on the same depth level and
marking them until there is no vertex left.

The DFS traversal uses the queue data structure to keep track of
the unvisited nodes.

Click and check Breadth First Search Traversal

Representation of Graphs
While representing graphs, we must carefully depict the elements
(vertices and edges) present in the graph and the relationship
between them. Pictorially, a graph is represented with a finite set
of nodes and connecting links between them. However, we can
also represent the graph in other most commonly used ways, like

• Adjacency Matrix
• Adjacency List
Adjacency Matrix

The Adjacency Matrix is a V x V matrix where the values are filled


with either 0 or 1. If the link exists between Vi and Vj, it is
recorded 1; otherwise, 0.

For the given graph below, let us construct an adjacency matrix −

The adjacency matrix is −

Adjacency List

The adjacency list is a list of the vertices directly connected to the


other vertices in the graph.
The adjacency list is −

Types of graph
There are two basic types of graph −

• Directed Graph
• Undirected Graph

Directed graph, as the name suggests, consists of edges that


possess a direction that goes either away from a vertex or
towards the vertex. Undirected graphs have edges that are not
directed at all.
Directed Graph

Undirected Graph

Spanning Tree
A spanning tree is a subset of an undirected graph that contains
all the vertices of the graph connected with the minimum number
of edges in the graph. Precisely, the edges of the spanning tree is
a subset of the edges in the original graph.

If all the vertices are connected in a graph, then there exists at


least one spanning tree. In a graph, there may exist more than
one spanning tree.
Properties
• A spanning tree does not have any cycle.
• Any vertex can be reached from any other vertex.
Example

In the following graph, the highlighted edges form a spanning


tree.

Minimum Spanning Tree


A Minimum Spanning Tree (MST) is a subset of edges of a
connected weighted undirected graph that connects all the
vertices together with the minimum possible total edge weight.
To derive an MST, Prim's algorithm or Kruskal's algorithm can be
used. Hence, we will discuss Prim's algorithm in this chapter.

As we have discussed, one graph may have more than one


spanning tree. If there are n number of vertices, the spanning
tree should have n − l1 number of edges. In this context, if each
edge of the graph is associated with a weight and there exists
more than one spanning tree, we need to find the minimum
spanning tree of the graph.

Moreover, if there exist any duplicate weighted edges, the graph


may have multiple minimum spanning tree.
In the above graph, we have shown a spanning tree though it's
not the minimum spanning tree. The cost of this spanning tree
is (5+7+3+3+5+8+3+4)=38.

Shortest Path
The shortest path in a graph is defined as the minimum cost
route from one vertex to another. This is most commonly seen in
weighted directed graphs but are also applicable to undirected
graphs.

A popular real−world application of finding the shortest path in a


graph is a map. Navigation is made easier and simpler with the
various shortest path algorithms where destinations are
considered vertices of the graph and routes are the edges. The
two common shortest path algorithms are −

• Dijkstra's Shortest Path Algorithm


• Bellman Ford's Shortest Path Algorithm

Example
Following are the implementations of this operation in various
programming languages −

C C++ JavaPython

#include <bits/stdc++.h>
using namespace std;
#define V 5

// Maximum number of vertices in the graph


struct graph {

// declaring graph data structure


struct vertex *point[V];
};
struct vertex {

// declaring vertices
int end;
struct vertex *next;
};
struct Edge {

// declaring edges
int end, start;
};
struct graph *create_graph (struct Edge edges[], int x){
int i;
struct graph *graph = (struct graph *) malloc (sizeof (struct graph));
for (i = 0; i < V; i++) {
graph->point[i] = NULL;
}
for (i = 0; i < x; i++) {
int start = edges[i].start;
int end = edges[i].end;
struct vertex *v = (struct vertex *) malloc (sizeof (struct vertex));
v->end = end;
v->next = graph->point[start];
graph->point[start] = v;
}
return graph;
}
int main (){
struct Edge edges[] = { {0, 1}, {0, 2}, {0, 3}, {1, 2}, {1, 4}, {2, 4}, {2, 3}, {3, 1} };
int n = sizeof (edges) / sizeof (edges[0]);
struct graph *graph = create_graph (edges, n);
int i;
cout<<"The graph created is: ";
for (i = 0; i < V; i++) {
struct vertex *ptr = graph->point[i];
while (ptr != NULL) {
cout << "(" << i << " -> " << ptr->end << ")\t";
ptr = ptr->next;
}
cout << endl;
}
return 0;
}
Output
The graph created is:
(1 -> 3) (1 -> 0)
(2 -> 1) (2 -> 0)
(3 -> 2) (3 -> 0)
(4 -> 2) (4 -> 1)

You might also like