Given a directed graph, the task is to count the in and out degree of each vertex of the graph.
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:
Vertex In Out 0 1 2 1 2 1 2 2 3 3 2 2 4 2 2 5 2 2 6 2 1
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Construct a graph from given degrees of all vertices
- Check whether given degrees of vertices represent a Graph or Tree
- Number of trees whose sum of degrees of all the vertices is L
- Find K vertices in the graph which are connected to at least one of remaining vertices
- Detect cycle in the graph using degrees of nodes of graph
- Sum of degrees of all nodes of a undirected graph
- Difference Between sum of degrees of odd and even degree nodes in an Undirected Graph
- Articulation Points (or Cut Vertices) in a Graph
- Find if there is a path between two vertices in a directed graph | Set 2
- Maximum and minimum isolated vertices in a graph
- Minimum number of edges between two vertices of a Graph
- Minimum number of edges between two vertices of a graph using DFS
- Find if there is a path between two vertices in a directed graph
- Find if there is a path between two vertices in an undirected graph
- Number of Simple Graph with N Vertices and M Edges
- Find two disjoint good sets of vertices in a given 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
- Building an undirected graph and finding shortest path using Dictionaries in Python
- Finding minimum vertex cover size of a graph using binary search
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.