Indegree of a Graph
Last Updated :
11 Oct, 2023
Indegree of a vertex is defined as the number of incoming edges incident on a vertex in a directed graph.
Significance Of Indegree:
- Indegree of nodes in a tree is equal to 1 in most of the cases if it becomes more than one then the data structure changes to graph.
- If the Indegree of a node is equal to zero, then the node/vertex does not have any parent vertex and it is either the root of the graph or an isolated vertex.
How to calculate Indegree of a node?
Example of directed graph
Consider the above directed graph for calculation of indegree of a node or vertex.
For calculating the indegree, we calculate the number of arrows pointing towards the node. For e.g. for vertex V4 there are two arrows pointing toward the node with edges as e3 and e4. therefore Indegree(V4) =2
Similarly,
- Indegree(V5) =1 as there is only one arrow with edge e5.
- Indegree(V1) =1 as there is only one arrow with edge e6.
- Indegree(V2) =2 as there are two arrows with edges e1 and e7.
- Indegree(V3) =1 as there is only one arrow with edge e2.
This is how we calculate the indegree of a node in a directed graph.
Finding Indegre of Directed Graph using its adjacency list:
Approach:
- Make a array of size N name as indgree where indgree[i] denotes the no. of node that are pointing towards the ith Node
- For ith Node there would be list which will get by adjaceny.get(i). For this list element do indgree[element]++ for all elements
Below is implementation of Above:
C++
#include <iostream>
#include <vector>
using namespace std;
void findInDegree( const vector<vector< int >>& adjList, int n)
{
vector< int > inDegree(n, 0);
for ( const vector< int >& list : adjList)
{
for ( int element : list)
{
inDegree[element]++;
}
}
for ( int k = 0; k < n; k++)
{
cout << "Vertex " << k << " has in-degree: " << inDegree[k] << endl;
}
}
int main()
{
vector<vector< int >> adjacency = {
{3, 4},
{3, 2},
{},
{2, 4},
{2, 3},
{1, 4, 6},
{5}
};
int n = adjacency.size();
findInDegree(adjacency, n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static void findInDegree(List<List<Integer> > adjList,
int n)
{
int indgree[] = new int [n];
for (List<Integer> list : adjList) {
for ( int element : list)
indgree[element]++;
}
for ( int k = 0 ; k < n; k++) {
System.out.println( "Vertex " + k
+ " has indgree"
+ "\t" + indgree[k]);
}
}
public static void main(String args[])
{
List<List<Integer> > adjacency = new ArrayList<>();
List<Integer> tmp
= new ArrayList<Integer>(Arrays.asList( 3 , 4 ));
adjacency.add(tmp);
tmp = new ArrayList<Integer>(Arrays.asList( 3 , 2 ));
adjacency.add(tmp);
tmp = new ArrayList<Integer>(Arrays.asList());
adjacency.add(tmp);
tmp = new ArrayList<Integer>(Arrays.asList( 2 , 4 ));
adjacency.add(tmp);
tmp = new ArrayList<Integer>(Arrays.asList( 2 , 3 ));
adjacency.add(tmp);
tmp = new ArrayList<Integer>(
Arrays.asList( 1 , 4 , 6 ));
adjacency.add(tmp);
tmp = new ArrayList<Integer>(Arrays.asList( 5 ));
adjacency.add(tmp);
int n = adjacency.size();
findInDegree(adjacency, n);
}
}
|
Python3
def find_in_degree(adj_list, n):
in_degree = [ 0 ] * n
for adj_vertices in adj_list:
for vertex in adj_vertices:
in_degree[vertex] + = 1
for k in range (n):
print (f "Vertex {k} has in-degree: {in_degree[k]}" )
if __name__ = = "__main__" :
adjacency = [
[ 3 , 4 ],
[ 3 , 2 ],
[],
[ 2 , 4 ],
[ 2 , 3 ],
[ 1 , 4 , 6 ],
[ 5 ]
]
n = len (adjacency)
find_in_degree(adjacency, n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void findindgree(List<List< int >> adjList, int n)
{
int [] indgree = new int [n];
foreach (List< int > list in adjList)
{
foreach ( int element in list)
{
indgree[element]++;
}
}
for ( int k = 0; k < n; k++)
{
Console.WriteLine( "Vertex " + k + " has indgree" + "\t" + indgree[k]);
}
}
public static void Main( string [] args)
{
List<List< int >> adjacency = new List<List< int >>();
List< int > tmp = new List< int >( new int [] { 3, 4 });
adjacency.Add(tmp);
tmp = new List< int >( new int [] { 3, 2 });
adjacency.Add(tmp);
tmp = new List< int >();
adjacency.Add(tmp);
tmp = new List< int >( new int [] { 2, 4 });
adjacency.Add(tmp);
tmp = new List< int >( new int [] { 2, 3 });
adjacency.Add(tmp);
tmp = new List< int >( new int [] { 1, 4, 6 });
adjacency.Add(tmp);
tmp = new List< int >( new int [] { 5 });
adjacency.Add(tmp);
int n = adjacency.Count;
findindgree(adjacency, n);
}
}
|
Javascript
function findInDegree(adjList, n) {
let inDegree = Array(n).fill(0);
for (let list of adjList) {
for (let element of list) {
inDegree[element]++;
}
}
for (let k = 0; k < n; k++) {
console.log(`Vertex ${k} has in -degree: ${inDegree[k]}`);
}
}
function main() {
let adjacency = [
[3, 4],
[3, 2],
[],
[2, 4],
[2, 3],
[1, 4, 6],
[5]
];
let n = adjacency.length;
findInDegree(adjacency, n);
}
main();
|
Output
Vertex 0 has indgree 0
Vertex 1 has indgree 1
Vertex 2 has indgree 3
Vertex 3 has indgree 3
Vertex 4 has indgree 3
Vertex 5 has indgree 1
Vertex 6 has indgree 1
Time Complexity: O(V + E) where V and E are the numbers of vertices and edges in the graph respectively.
Auxiliary Space: O(V) As additionally we only use indegree array to store output
Share your thoughts in the comments
Please Login to comment...