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

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:

`// Java program to find the in and out degrees ` `// of the vertices of the given graph ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to print the in and out degrees ` ` ` `// of all the vertices of the given graph ` ` ` `static` `void` `findInOutDegree(List<List<Integer> > adjList, ` `int` `n) ` ` ` `{ ` ` ` `int` `in[] = ` `new` `int` `[n]; ` ` ` `int` `out[] = ` `new` `int` `[n]; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < adjList.size(); i++) { ` ` ` ` ` `List<Integer> list = adjList.get(i); ` ` ` ` ` `// Out degree for ith vertex will be the count ` ` ` `// of direct paths from i to other vertices ` ` ` `out[i] = list.size(); ` ` ` `for` `(` `int` `j = ` `0` `; j < list.size(); j++) ` ` ` ` ` `// Every vertex that has an incoming ` ` ` `// edge from i ` ` ` `in[list.get(j)]++; ` ` ` `} ` ` ` ` ` `System.out.println(` `"Vertex\tIn\tOut"` `); ` ` ` `for` `(` `int` `k = ` `0` `; k < n; k++) { ` ` ` `System.out.println(k + ` `"\t"` `+ in[k] + ` `"\t"` `+ out[k]); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `// Adjacency list representation of the graph ` ` ` `List<List<Integer> > adjList = ` `new` `ArrayList<>(); ` ` ` ` ` `// Vertices 1 and 2 have an incoming edge ` ` ` `// from vertex 0 ` ` ` `List<Integer> tmp = ` ` ` `new` `ArrayList<Integer>(Arrays.asList(` `1` `, ` `2` `)); ` ` ` `adjList.add(tmp); ` ` ` ` ` `// Vertex 3 has an incoming edge from vertex 1 ` ` ` `tmp = ` `new` `ArrayList<Integer>(Arrays.asList(` `3` `)); ` ` ` `adjList.add(tmp); ` ` ` ` ` `// Vertices 0, 5 and 6 have an incoming ` ` ` `// edge from vertex 2 ` ` ` `tmp = ` ` ` `new` `ArrayList<Integer>(Arrays.asList(` `0` `, ` `5` `, ` `6` `)); ` ` ` `adjList.add(tmp); ` ` ` ` ` `// Vertices 1 and 4 have an incoming edge ` ` ` `// from vertex 3 ` ` ` `tmp = ` `new` `ArrayList<Integer>(Arrays.asList(` `1` `, ` `4` `)); ` ` ` `adjList.add(tmp); ` ` ` ` ` `// Vertices 2 and 3 have an incoming edge ` ` ` `// from vertex 4 ` ` ` `tmp = ` `new` `ArrayList<Integer>(Arrays.asList(` `2` `, ` `3` `)); ` ` ` `adjList.add(tmp); ` ` ` ` ` `// Vertices 4 and 6 have an incoming edge ` ` ` `// from vertex 5 ` ` ` `tmp = ` `new` `ArrayList<Integer>(Arrays.asList(` `4` `, ` `6` `)); ` ` ` `adjList.add(tmp); ` ` ` ` ` `// Vertex 5 has an incoming edge from vertex 6 ` ` ` `tmp = ` `new` `ArrayList<Integer>(Arrays.asList(` `5` `)); ` ` ` `adjList.add(tmp); ` ` ` ` ` `int` `n = adjList.size(); ` ` ` `findInOutDegree(adjList, n); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**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

## Recommended Posts:

- Construct a graph from given degrees of all vertices
- Check whether given degrees of vertices represent a Graph or Tree
- Sum of degrees of all nodes of a undirected graph
- Articulation Points (or Cut Vertices) in a Graph
- Minimum number of edges between two vertices of a graph using DFS
- Maximum and minimum isolated vertices in a graph
- Number of Simple Graph with N Vertices and M Edges
- Minimum number of edges between two vertices of a Graph
- Find if there is a path between two vertices in a directed graph
- Largest subset of Graph vertices with edges of 2 or more colors
- Calculate number of nodes between two vertices in an acyclic Graph by Disjoint Union method
- Finding minimum vertex cover size of a graph using binary search
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
- Count all possible paths between two vertices
- Longest path between any pair of vertices

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.