What Is Degree of Graph in Data Structure?

//

Scott Campbell

When working with graphs in data structures, one important concept to understand is the degree of a graph. The degree of a graph refers to the number of edges connected to each vertex or node in the graph. In simple terms, it tells us how many neighbors or connections each node has.

What is a Graph?

Before diving into the degree of a graph, let’s quickly recap what a graph is. In computer science, a graph is a non-linear data structure consisting of nodes (also called vertices) and edges that connect these nodes. Nodes represent entities or elements, and edges represent relationships between these entities.

Degree of an Undirected Graph

In an undirected graph, each edge connects two nodes without any directionality. The degree of a node in an undirected graph is simply the count of edges connected to that node. To calculate the degree, we count all the edges that are connected to the particular node.

Example:

Consider an undirected graph with four nodes labeled A, B, C, and D. The edges in this graph are AB, AC, BD, and CD. Let’s find out the degrees of each node:

  • Node A: Degree = 1 (Connected to B)
  • Node B: Degree = 2 (Connected to A and D)
  • Node C: Degree = 2 (Connected to A and D)
  • Node D: Degree = 2 (Connected to B and C)

From this example, we can see that nodes B, C, and D have a degree of 2 since they are connected to two other nodes.

Degree of a Directed Graph

In contrast to an undirected graph, a directed graph has edges with directionality. Each edge has a source node and a destination node. The degree of a node in a directed graph is further classified into two types:

1. In-Degree: The in-degree of a node is the count of incoming edges to that node.

2. Out-Degree: The out-degree of a node is the count of outgoing edges from that node.

Let’s consider a directed graph with three nodes labeled X, Y, and Z. The edges in this graph are XY, YZ, and ZX. Now, let’s find out the degrees for each type:

  • Node X: In-Degree = 1 (Inward edge from Z), Out-Degree = 1 (Outward edge to Y)
  • Node Y: In-Degree = 1 (Inward edge from X), Out-Degree = 1 (Outward edge to Z)
  • Node Z: In-Degree = 2 (Inward edges from X and Y), Out-Degree = 0 (No outward edges)

From this example, we can see that nodes X and Y have an in-degree and out-degree of 1 since they are connected with one inward and one outward edge. Node Z has an in-degree of 2 because it receives two inward edges but no outward edges.

In Summary

The degree of a graph is an essential concept when analyzing and understanding graphs in data structures. It provides valuable information about the connectivity and relationships between nodes within the graph. Whether working with undirected or directed graphs, calculating the degree allows us to gain insights into how nodes are interconnected.

Next time you encounter a graph problem or analyze data relationships using graphs, remember to consider the degree of the graph for better comprehension.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy