# Number of sink nodes in a graph

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 Only node 1 and node 3 are sink nodes. Input : n = 4, m = 2 Edges[] = {{3, 2}, {3, 4}} Output : 3

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.

Below is implementation of this approach:

## C++

`// C++ program to count number if sink nodes ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Return the number of Sink NOdes. ` `int` `countSink(` `int` `n, ` `int` `m, ` `int` `edgeFrom[], ` ` ` `int` `edgeTo[]) ` `{ ` ` ` `// Array for marking the non-sink node. ` ` ` `int` `mark[n]; ` ` ` `memset` `(mark, 0, ` `sizeof` `mark); ` ` ` ` ` `// Marking the non-sink node. ` ` ` `for` `(` `int` `i = 0; i < m; i++) ` ` ` `mark[edgeFrom[i]] = 1; ` ` ` ` ` `// Counting the sink nodes. ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = 1; i <= n ; i++) ` ` ` `if` `(!mark[i]) ` ` ` `count++; ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driven Program ` `int` `main() ` `{ ` ` ` `int` `n = 4, m = 2; ` ` ` `int` `edgeFrom[] = { 2, 4 }; ` ` ` `int` `edgeTo[] = { 3, 3 }; ` ` ` ` ` `cout << countSink(n, m, edgeFrom, edgeTo) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count number if sink nodes ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Return the number of Sink NOdes. ` `static` `int` `countSink(` `int` `n, ` `int` `m, ` ` ` `int` `edgeFrom[], ` `int` `edgeTo[]) ` `{ ` ` ` `// Array for marking the non-sink node. ` ` ` `int` `[]mark = ` `new` `int` `[n + ` `1` `]; ` ` ` ` ` `// Marking the non-sink node. ` ` ` `for` `(` `int` `i = ` `0` `; i < m; i++) ` ` ` `mark[edgeFrom[i]] = ` `1` `; ` ` ` ` ` `// Counting the sink nodes. ` ` ` `int` `count = ` `0` `; ` ` ` `for` `(` `int` `i = ` `1` `; i <= n ; i++) ` ` ` `if` `(mark[i] != ` `0` `) ` ` ` `count++; ` ` ` ` ` `return` `count; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `4` `, m = ` `2` `; ` ` ` `int` `edgeFrom[] = { ` `2` `, ` `4` `}; ` ` ` `int` `edgeTo[] = { ` `3` `, ` `3` `}; ` ` ` ` ` `System.out.println(countSink(n, m, ` ` ` `edgeFrom, edgeTo)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count number if sink nodes ` ` ` `# Return the number of Sink NOdes. ` `def` `countSink(n, m, edgeFrom, edgeTo): ` ` ` ` ` `# Array for marking the non-sink node. ` ` ` `mark ` `=` `[` `0` `] ` `*` `(n ` `+` `1` `) ` ` ` ` ` `# Marking the non-sink node. ` ` ` `for` `i ` `in` `range` `(m): ` ` ` `mark[edgeFrom[i]] ` `=` `1` ` ` ` ` `# Counting the sink nodes. ` ` ` `count ` `=` `0` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `): ` ` ` `if` `(` `not` `mark[i]): ` ` ` `count ` `+` `=` `1` ` ` ` ` `return` `count ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `n ` `=` `4` ` ` `m ` `=` `2` ` ` `edgeFrom ` `=` `[` `2` `, ` `4` `] ` ` ` `edgeTo ` `=` `[` `3` `, ` `3` `] ` ` ` ` ` `print` `(countSink(n, m, edgeFrom, edgeTo)) ` ` ` `# This code is contributed by PranchalK ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** O(m + n) where n is number of nodes and m is number of edges.

**Related Article:**

The Celebrity Problem

This article is contributed by **Anuj Chauhan**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Breadth First Search or BFS for a Graph
- Find if there is a path between two vertices in a directed graph
- Depth First Search or DFS for a Graph
- Detect Cycle in a Directed Graph
- Find the number of islands | Set 1 (Using DFS)
- Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph)
- Graph and its representations
- Transitive closure of a graph
- Check whether a given graph is Bipartite or not
- Shortest Path in Directed Acyclic Graph
- Bridges in a graph
- Articulation Points (or Cut Vertices) in a Graph
- Biconnected graph
- Check if a graph is strongly connected | Set 1 (Kosaraju using DFS)
- Eulerian path and circuit for undirected graph