Find Indegree and Outdegree for all vertices (original) (raw)
Last Updated : 28 Oct, 2025
Given a **directed graph represented by its adjacency list **adj[][], determine the in-degree (number of incoming edges) and out-degree (number of outgoing edges) for every vertex in the graph.
**Examples:
**Input: adj[][]= [[1], [], [1, 6], [2], [3, 2], [4, 6], []]
**Output: [[0, 1], [2, 0],[2, 2], [1, 1], [1, 2], [0, 2], [2, 0]]
**Explanation:
Vertex 0 has 0 incoming and 1 outgoing edge.
Vertex 1 has 2 incoming and 0 outgoing edge.
Vertex 2 has 2 incoming and 2 outgoing edge.
Vertex 3 has 1 incoming and 1 outgoing edge.
Vertex 4 has 1 incoming and 2 outgoing edges.
Vertex 5 has 0 incoming and 2 outgoing edge.
Vertex 6 has 2 incoming and 0 outgoing edge.
**Approach:
The main idea is to calculate the in-degree and out-degree of each vertex by traversing the adjacency list. The out-degree of a vertex i is equal to the number of vertices present in its adjacency list adj[i], and for the in-degree, we increment the count for each vertex j that has an incoming edge from the current vertex i by one.
C++ `
//Driver Code Starts #include #include using namespace std;
//Driver Code Ends
vector<vector> findInOutDegree(vector<vector>& adj) { int n = adj.size(); vector inDegree(n, 0), outDegree(n, 0);
// Traverse adjacency list
for (int i = 0; i < n; i++) {
// Out-degree = number of vertices this vertex points to
outDegree[i] = adj[i].size();
// For each connected vertex, increment its in-degree
for (int v : adj[i])
inDegree[v]++;
}
// Store in-degree and out-degree together for each vertex
vector<vector<int>> result(n, vector<int>(2));
for (int i = 0; i < n; i++)
result[i] = {inDegree[i], outDegree[i]};
return result;}
//Driver Code Starts
int main() {
vector<vector<int>> adj = {{1}, {}, {1, 6}, {2}, {3, 2}, {4, 6}, {}};
vector<vector<int>> degrees = findInOutDegree(adj);
cout << "Vertex In Out"; for (int i = 0; i < adj.size(); i++) cout << i << " " << degrees[i][0] << " " << degrees[i][1] << " ";
return 0;}
//Driver Code Ends
Java
//Driver Code Starts import java.util.ArrayList;
public class GFG {
//Driver Code Ends
public static ArrayList<ArrayList<Integer>> findInOutDegree(ArrayList<ArrayList<Integer>> adj) {
int V = adj.size();
int[] inDegree = new int[V];
int[] outDegree = new int[V];
// Traverse adjacency list
for (int i = 0; i < V; i++) {
// Out-degree = number of vertices this vertex points to
outDegree[i] = adj.get(i).size();
// For each connected vertex, increment its in-degree
for (int v : adj.get(i))
inDegree[v]++;
}
// Store in-degree and out-degree together for each vertex
ArrayList<ArrayList<Integer>> result = new ArrayList<>();
for (int i = 0; i < V; i++) {
ArrayList<Integer> temp = new ArrayList<>();
temp.add(inDegree[i]);
temp.add(outDegree[i]);
result.add(temp);
}
return result;
}//Driver Code Starts
// Function to add a directed edge from u to v
public static void addEdge(ArrayList<ArrayList<Integer>> adj, int u, int v) {
adj.get(u).add(v);
}
public static void main(String[] args) {
int V=7;
ArrayList<ArrayList<Integer>> adj = new ArrayList<>();
for (int i = 0; i < V; i++)
adj.add(new ArrayList<>());
addEdge(adj, 0, 1);
addEdge(adj, 2, 1);
addEdge(adj, 2, 6);
addEdge(adj, 3, 2);
addEdge(adj, 4, 2);
addEdge(adj, 4, 3);
addEdge(adj, 5, 4);
addEdge(adj, 5, 6);
ArrayList<ArrayList<Integer>> degrees = findInOutDegree(adj);
System.out.println("Vertex In Out");
for (int i = 0; i < V; i++)
System.out.println(i + " " + degrees.get(i).get(0) + " " + degrees.get(i).get(1));
}}
//Driver Code Ends
Python
def findInOutDegree(adj): n = len(adj) inDegree = [0] * n outDegree = [0] * n
# Traverse adjacency list
for i in range(n):
# Out-degree = number of vertices this vertex points to
outDegree[i] = len(adj[i])
# For each connected vertex, increment its in-degree
for v in adj[i]:
inDegree[v] += 1
# Store in-degree and out-degree together for each vertex
result = [[inDegree[i], outDegree[i]] for i in range(n)]
return result#Driver Code Starts
if name == "main": adj = [[1], [], [1, 6], [2], [3, 2], [4, 6], []]
degrees = findInOutDegree(adj)
print("Vertex In Out")
for i in range(len(adj)):
print(f"{i} {degrees[i][0]} {degrees[i][1]}")#Driver Code Ends
C#
//Driver Code Starts using System; using System.Collections.Generic;
class GFG {
//Driver Code Ends
static List<List<int>> findInOutDegree(List<List<int>> adj)
{
int n = adj.Count;
int[] inDegree = new int[n];
int[] outDegree = new int[n];
// Traverse adjacency list
for (int i = 0; i < n; i++)
{
// Out-degree = number of vertices this vertex points to
outDegree[i] = adj[i].Count;
// For each connected vertex, increment its in-degree
foreach (int v in adj[i])
inDegree[v]++;
}
// Store in-degree and out-degree together for each vertex
List<List<int>> result = new List<List<int>>();
for (int i = 0; i < n; i++)
{
result.Add(new List<int> { inDegree[i], outDegree[i] });
}
return result;
}//Driver Code Starts
// Function to add a directed edge from u to v
static void addEdge(List<List<int>> adj, int u, int v)
{
adj[u].Add(v);
}
static void Main()
{
int V = 7;
List<List<int>> adj = new List<List<int>>();
for (int i = 0; i < V; i++)
adj.Add(new List<int>());
addEdge(adj, 0, 1);
addEdge(adj, 2, 1);
addEdge(adj, 2, 6);
addEdge(adj, 3, 2);
addEdge(adj, 4, 2);
addEdge(adj, 4, 3);
addEdge(adj, 5, 4);
addEdge(adj, 5, 6);
List<List<int>> degrees = findInOutDegree(adj);
Console.WriteLine("Vertex In Out");
for (int i = 0; i < V; i++)
Console.WriteLine($"{i} {degrees[i][0]} {degrees[i][1]}");
}}
//Driver Code Ends
` JavaScript ``
function findInOutDegree(adj) { const n = adj.length; const inDegree = new Array(n).fill(0); const outDegree = new Array(n).fill(0);
// Traverse adjacency list
for (let i = 0; i < n; i++) {
// Out-degree = number of vertices this vertex points to
outDegree[i] = adj[i].length;
// For each connected vertex, increment its in-degree
for (let v of adj[i])
inDegree[v]++;
}
// Store in-degree and out-degree together for each vertex
const result = [];
for (let i = 0; i < n; i++)
result.push([inDegree[i], outDegree[i]]);
return result;}
//Driver Code //Driver Code Starts const adj = [[1], [], [1, 6], [2], [3, 2], [4, 6], []];
const degrees = findInOutDegree(adj);
console.log("Vertex In Out");
for (let i = 0; i < adj.length; i++)
console.log(${i} <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi><mi>e</mi><mi>g</mi><mi>r</mi><mi>e</mi><mi>e</mi><mi>s</mi><mo stretchy="false">[</mo><mi>i</mi><mo stretchy="false">]</mo><mo stretchy="false">[</mo><mn>0</mn><mo stretchy="false">]</mo></mrow><annotation encoding="application/x-tex">{degrees[i][0]} </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord"><span class="mord mathnormal">d</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal">rees</span><span class="mopen">[</span><span class="mord mathnormal">i</span><span class="mclose">]</span><span class="mopen">[</span><span class="mord">0</span><span class="mclose">]</span></span></span></span></span>{degrees[i][1]});
//Driver Code Ends
``
Output
Vertex In Out 0 0 1 1 2 0 2 2 2 3 1 1 4 1 2 5 0 2 6 2 0
**Time Complexity: O(V + E) where V and E are the numbers of vertices and edges in the graph respectively.
**Auxiliary Space: O(V + E).
