给定一个有V个顶点和E 个边的有向图 G(V, E) ,任务是检查给定图的所有顶点,顶点中的传入边是否等于顶点本身。
例子:
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.
方法:思想是遍历每个顶点的邻接表,并增加每个顶点的边数,该顶点的边数来自 i。对每个顶点重复这些步骤,然后检查所有顶点是否等于顶点值的度数。
下面是上述方法的实现:
C++
// C++ implementation to check if the
// incoming edges in a vertex of directed
// graph is equal to the vertex itself or not
#include
using namespace std;
// A utility function to
// add an edge in an
// directed graph
void add_edge(vector 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 adj[], int v)
{
// Create array indeg
// intialized 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 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 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 adj[],
int v)
{
// Create array indeg
// intialized 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 []adj = new Vector[v];
for(int i = 0; i < adj.length; i++)
adj[i] = new Vector();
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
# intialized 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 []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 []adj,
int v)
{
// Create array indeg
// intialized 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 []adj = new List[v];
for(int i = 0; i < adj.Length; i++)
adj[i] = new List();
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
输出:
Yes
时间复杂度: O(V + E)
辅助空间复杂度: O(V)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live