Check if incoming edges in a vertex of directed graph is equal to vertex itself or not
Last Updated :
09 Sep, 2021
Given a directed Graph G(V, E) with V vertices and E edges, the task is to check that for all vertices of the given graph, the incoming edges in a vertex is equal to the vertex itself or not.
Examples:
Input:
Output: Yes
Explanation:
For vertex 0 there are 0 incoming edges, for vertex 1 there is 1 incoming edge. Same for vertex 2 nd 3.
Approach: The idea is to traverse adjacency list for every vertex, and increment the count of edges of every vertex that has an incoming edge from i. Repeat the steps for every vertex and then check the in degrees for all the vertices equal to vertex value or not.
Below is the implementation of the above approach:
C++
// C++ implementation to check if the
// incoming edges in a vertex of directed
// graph is equal to the vertex itself or not
#include <bits/stdc++.h>
using namespace std;
// A utility function to
// add an edge in an
// directed graph
void add_edge(vector<int> adj[],
int x, int y)
{
adj[x].push_back(y);
}
// Function to check that given graph
// in-degree value equal to vertex value
bool Indegree(vector<int> adj[], int v)
{
// Create array indeg
// initialized to zero
int indeg[v] = { 0 };
// Traversing across all
// vertex to compute
// in degree value
for (int i = 0; i < v; i++) {
for (int j = 0; j < adj[i].size(); j++) {
indeg[adj[i][j]]++;
}
}
// check in degree value
// equal to vertex value
for (int i = 0; i < v; i++) {
if (i == indeg[i])
continue;
else
return false;
}
return true;
}
// Driver code
int main()
{
int v = 4;
// To store adjacency list of graph
vector<int> adj[v];
add_edge(adj, 0, 1);
add_edge(adj, 1, 2);
add_edge(adj, 0, 2);
add_edge(adj, 0, 3);
add_edge(adj, 1, 3);
add_edge(adj, 2, 3);
if (Indegree(adj, v))
cout << "Yes";
else
cout << "No";
}
Java
// Java implementation to check if the
// incoming edges in a vertex of directed
// graph is equal to the vertex itself or not
import java.util.*;
class GFG{
// A utility function to
// add an edge in an
// directed graph
static void add_edge(Vector<Integer> adj[],
int x, int y)
{
adj[x].add(y);
}
// Function to check that given graph
// in-degree value equal to vertex value
static boolean Indegree(Vector<Integer> adj[],
int v)
{
// Create array indeg
// initialized to zero
int indeg[] = new int[v];
// Traversing across all
// vertex to compute
// in degree value
for(int i = 0; i < v; i++)
{
for(int j = 0; j < adj[i].size(); j++)
{
indeg[adj[i].get(j)]++;
}
}
// Check in degree value
// equal to vertex value
for(int i = 0; i < v; i++)
{
if (i == indeg[i])
continue;
else
return false;
}
return true;
}
// Driver code
public static void main(String[] args)
{
int v = 4;
// To store adjacency list of graph
@SuppressWarnings("unchecked")
Vector<Integer> []adj = new Vector[v];
for(int i = 0; i < adj.length; i++)
adj[i] = new Vector<Integer>();
add_edge(adj, 0, 1);
add_edge(adj, 1, 2);
add_edge(adj, 0, 2);
add_edge(adj, 0, 3);
add_edge(adj, 1, 3);
add_edge(adj, 2, 3);
if (Indegree(adj, v))
System.out.print("Yes");
else
System.out.print("No");
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 implementation to check if the
# incoming edges in a vertex of directed
# graph is equal to the vertex itself or not
# A utility function to
# add an edge in an
# directed graph
def add_edge(adj, x, y):
adj[x] = adj[x] + [y]
# Function to check that given graph
# in-degree value equal to vertex value
def Indegree(adj, v):
# Create array indeg
# initialized to zero
indeg = [0] * v
# Traversing across all
# vertex to compute
# in degree value
for i in range(v):
for j in range(len(adj[i])):
indeg[adj[i][j]] += 1
# Check in degree value
# equal to vertex value
for i in range(v):
if(i == indeg[i]):
continue
else:
return False
return True
# Driver code
if __name__ == '__main__':
v = 4
# To store adjacency list of graph
adj = [[]] * 4
add_edge(adj, 0, 1)
add_edge(adj, 1, 2)
add_edge(adj, 0, 2)
add_edge(adj, 0, 3)
add_edge(adj, 1, 3)
add_edge(adj, 2, 3)
if(Indegree(adj, v)):
print("Yes")
else:
print("No")
# This code is contributed by Shivam Singh
C#
// C# implementation to check if the
// incoming edges in a vertex of directed
// graph is equal to the vertex itself or not
using System;
using System.Collections.Generic;
class GFG{
// A utility function to
// add an edge in an
// directed graph
static void add_edge(List<int> []adj,
int x, int y)
{
adj[x].Add(y);
}
// Function to check that given graph
// in-degree value equal to vertex value
static bool Indegree(List<int> []adj,
int v)
{
// Create array indeg
// initialized to zero
int []indeg = new int[v];
// Traversing across all
// vertex to compute
// in degree value
for(int i = 0; i < v; i++)
{
for(int j = 0; j < adj[i].Count; j++)
{
indeg[adj[i][j]]++;
}
}
// Check in degree value
// equal to vertex value
for(int i = 0; i < v; i++)
{
if (i == indeg[i])
continue;
else
return false;
}
return true;
}
// Driver code
public static void Main(String[] args)
{
int v = 4;
// To store adjacency list of graph
List<int> []adj = new List<int>[v];
for(int i = 0; i < adj.Length; i++)
adj[i] = new List<int>();
add_edge(adj, 0, 1);
add_edge(adj, 1, 2);
add_edge(adj, 0, 2);
add_edge(adj, 0, 3);
add_edge(adj, 1, 3);
add_edge(adj, 2, 3);
if (Indegree(adj, v))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by Amit Katiyar
JavaScript
<script>
// JavaScript implementation to check if the
// incoming edges in a vertex of directed
// graph is equal to the vertex itself or not
// A utility function to
// add an edge in an
// directed graph
function add_edge(adj, x, y)
{
adj[x].push(y);
}
// Function to check that given graph
// in-degree value equal to vertex value
function Indegree(adj, v)
{
// Create array indeg
// initialized to zero
var indeg = Array(v).fill(0);
// Traversing across all
// vertex to compute
// in degree value
for (var i = 0; i < v; i++) {
for (var j = 0; j < adj[i].length; j++) {
indeg[adj[i][j]]++;
}
}
// check in degree value
// equal to vertex value
for (var i = 0; i < v; i++) {
if (i == indeg[i])
continue;
else
return false;
}
return true;
}
// Driver code
var v = 4;
// To store adjacency list of graph
var adj = Array.from(Array(v), ()=> new Array());
add_edge(adj, 0, 1);
add_edge(adj, 1, 2);
add_edge(adj, 0, 2);
add_edge(adj, 0, 3);
add_edge(adj, 1, 3);
add_edge(adj, 2, 3);
if (Indegree(adj, v))
document.write( "Yes");
else
document.write( "No");
</script>
Time Complexity: O(V + E)
Auxiliary Space Complexity: O(V)
Similar Reads
How to iterate over boost graph to get incoming and outgoing edges of vertex? What is Boost Graph? The Boost Graph (BG) are multi-graph concept, it is factored into much smaller pieces. The reason for this is that any one algorithm cannot fulfill the need for all graph operations. Furthermore, there are many graph data structures that provide a very effective implementation i
9 min read
Check if vertex X lies in subgraph of vertex Y for the given Graph Given an undirected graph and two vertices X and Y, our task is to check whether the vertex X lies in the subgraph of the vertex Y. Examples: Input: X = 2, Y = 3 Output: No Explanation: Subgraph of a vertex Y = 3 is set of all the vertex which lies below Y and are reachable by Y. Here subgraph of 3
10 min read
Check if every vertex triplet in graph contains two vertices connected to third vertex Given an undirected graph with N vertices and K edges, the task is to check if for every combination of three vertices in the graph, there exists two vertices which are connected to third vertex. In other words, for every vertex triplet (a, b, c), if there exists a path between a and c, then there s
9 min read
Check if given graph is connected or not after removal of ith node sequential Given an undirected graph with N nodes, M edges given by array edges[][2] and permutation array A[] of size N which indicates the order in which each and every node of graph will be removed. The task is to find if graph is connected or not after removal of ith node sequential from given array A[]. G
15 min read
Check if a given Graph is 2-edge connected or not Given an undirected graph G, with V vertices and E edges, the task is to check whether the graph is 2-edge connected or not. A graph is said to be 2-edge connected if, on removing any edge of the graph, it still remains connected, i.e. it contains no Bridges. Examples: Input: V = 7, E = 9 Output: Ye
12 min read
Find if there is a path between two vertices in a directed graph | Set 2 Given a Directed Graph and two vertices in it, check whether there is a path from the first given vertex to second. Example: Consider the following Graph: Input : (u, v) = (1, 3) Output: Yes Explanation: There is a path from 1 to 3, 1 -> 2 -> 3 Input : (u, v) = (3, 6) Output: No Explanation: T
7 min read