Given a Directed Acyclic Graph of n nodes (numbered from 1 to n) and m edges. The task is to find the number of sink nodes. A sink node is a node such that no edge emerges out of it.
Examples:
Input: n = 4, m = 2 Edges[] = {{2, 3}, {4, 3}} Output: 2 Explanation: Only node 1 and node 3 are sink nodes.
Input: n = 4, m = 2 Edges[] = {{3, 2}, {3, 4}} Output: 3
Approach
The idea is to iterate through all the edges. And for each edge, mark the source node from which the edge emerged out. Now, for each node check if it is marked or not. And count the unmarked nodes.
Algorithm:
1. Make any array A[] of size equal to the number of nodes and initialize to 1. 2. Traverse all the edges one by one, say, u -> v. (i) Mark A[u] as 1. 3. Now traverse whole array A[] and count number of unmarked nodes.
C++
// C++ program to count number if sink nodes#include<bits/stdc++.h>usingnamespacestd;// Return the number of Sink NOdes.intcountSink(intn,intm,intedgeFrom[],intedgeTo[]){// Array for marking the non-sink node.intmark[n];memset(mark,0,sizeofmark);// Marking the non-sink node.for(inti=0;i<m;i++)mark[edgeFrom[i]]=1;// Counting the sink nodes.intcount=0;for(inti=1;i<=n;i++)if(!mark[i])count++;returncount;}// Driven Programintmain(){intn=4,m=2;intedgeFrom[]={2,4};intedgeTo[]={3,3};cout<<countSink(n,m,edgeFrom,edgeTo)<<endl;return0;}
Java
// Java program to count number if sink nodesimportjava.util.*;classGFG{// Return the number of Sink NOdes.staticintcountSink(intn,intm,intedgeFrom[],intedgeTo[]){// Array for marking the non-sink node.int[]mark=newint[n+1];// Marking the non-sink node.for(inti=0;i<m;i++)mark[edgeFrom[i]]=1;// Counting the sink nodes.intcount=0;for(inti=1;i<=n;i++)if(mark[i]==0)count++;returncount;}// Driver Codepublicstaticvoidmain(String[]args){intn=4,m=2;intedgeFrom[]={2,4};intedgeTo[]={3,3};System.out.println(countSink(n,m,edgeFrom,edgeTo));}}// This code is contributed by 29AjayKumar
Python
# Python3 program to count number if sink nodes# Return the number of Sink NOdes. defcountSink(n,m,edgeFrom,edgeTo):# Array for marking the non-sink node. mark=[0]*(n+1)# Marking the non-sink node.foriinrange(m):mark[edgeFrom[i]]=1# Counting the sink nodes. count=0foriinrange(1,n+1):if(notmark[i]):count+=1returncount# Driver Codeif__name__=='__main__':n=4m=2edgeFrom=[2,4]edgeTo=[3,3]print(countSink(n,m,edgeFrom,edgeTo))# This code is contributed by PranchalK
C#
// C# program to count number if sink nodesusingSystem;classGFG{// Return the number of Sink NOdes.staticintcountSink(intn,intm,int[]edgeFrom,int[]edgeTo){// Array for marking the non-sink node.int[]mark=newint[n+1];// Marking the non-sink node.for(inti=0;i<m;i++)mark[edgeFrom[i]]=1;// Counting the sink nodes.intcount=0;for(inti=1;i<=n;i++)if(mark[i]==0)count++;returncount;}// Driver CodepublicstaticvoidMain(String[]args){intn=4,m=2;int[]edgeFrom={2,4};int[]edgeTo={3,3};Console.WriteLine(countSink(n,m,edgeFrom,edgeTo));}}// This code is contributed by PrinciRaj1992
JavaScript
<script>// Javascript program to count number if sink nodes// Return the number of Sink NOdes.functioncountSink(n,m,edgeFrom,edgeTo){// Array for marking the non-sink node.letmark=newArray(n+1);for(leti=0;i<n+1;i++){mark[i]=0;}// Marking the non-sink node.for(leti=0;i<m;i++)mark[edgeFrom[i]]=1;// Counting the sink nodes.letcount=0;for(leti=1;i<=n;i++)if(mark[i]==0)count++;returncount;}// Driver Codeletn=4,m=2;letedgeFrom=[2,4];letedgeTo=[3,3];document.write(countSink(n,m,edgeFrom,edgeTo));// This code is contributed by rag2127</script>
Output:
2
Time Complexity: O(m + n) where n is number of nodes and m is number of edges. Space complexity : O(n) because it uses an array of size n to store the non-sink nodes.