Number of Provinces
Medium Accuracy: 51.13% Submissions: 2236 Points: 4

Given a graph with V vertices. Find the number of Provinces.
Note:
A province is a group of directly or indirectly connected cities and no other cities outside of the group.

 

Example 1:

Input:

Output:
2
Explanation:
The graph clearly has 2 Provinces. 
Example 2:
Input:

Output :
1


Your Task:  
You don't need to read input or print anything. Your task is to complete the function numProvinces() which takes an integer V and an adjacency matrix adj as input and returns the number of provinces. adj[i][j] = 1, if nodes i and j are connected and adj[i][j] = 0, if not connected.


Expected Time Complexity: O(V2)
Expected Auxiliary Space: O(V)


Constraints:
1 ≤ V ≤ 500

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.


to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Number of Provinces

Output Window