# Finding in and out degrees of all vertices in a graph

• Difficulty Level : Easy
• Last Updated : 07 Oct, 2021

Given a directed graph, the task is to count the in and out degree of each vertex of the graph.
Examples:

Input:

Output:
Vertex    In    Out
0         1    2
1          2    1
2          2    3
3          2    2
4          2    2
5          2    2
6          2    1

Approach: Traverse adjacency list for every vertex, if size of the adjacency list of vertex i is x then the out degree for i = x and increment the in degree of every vertex that has an incoming edge from i. Repeat the steps for every vertex and print the in and out degrees for all the vertices in the end.
Below is the implementation of the above approach:

## C#

Output:

Vertex    In    Out
0    1    2
1    2    1
2    2    3
3    2    2
4    2    2
5    2    2
6    2    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 + E).

My Personal Notes arrow_drop_up