- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ program to count maximum possible division can be made in a graph with given condition
Suppose we have an adjacency matrix of a graph G. We have to check whether we can divide the vertices into non-empty sets V1, ... Vk, such that: every edge connects two vertices belonging to two adjacent sets. If the answer is yes, we have to find the maximum possible value of sets k, in such division.
So, if the input is like
0 | 1 | 0 | 1 | 1 | 0 |
1 | 0 | 1 | 0 | 0 | 1 |
0 | 1 | 0 | 1 | 0 | 0 |
1 | 0 | 1 | 0 | 0 | 0 |
1 | 0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 0 | 0 | 0 |
then the output will be 4
Steps
To solve this, we will follow these steps −
Define an array dp of size: 210. n := size of matrix fl := 1 ans := 0 for initialize i := 0, when i < n and fl is non-zero, update (increase i by 1), do: fill dp with -1 dp[i] := 0 Define one queue q insert i into q while (q is not empty), do: x := first element of q delete element from q for initialize j := 0, when j < n, update (increase j by 1), do: if matrix[x, j] is same as 1, then: if dp[j] is same as -1, then: dp[j] := dp[x] + 1 insert j into q otherwise when |dp[j] - dp[x]| is not equal to 1, then: fl := 0 for initialize j := 0, when j < n, update (increase j by 1), do: ans := maximum of ans and dp[j] if fl is same as 0, then: return -1 Otherwise return ans + 1
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(vector<vector<int>> matrix){ int dp[210]; int n = matrix.size(); int fl = 1; int ans = 0; for (int i = 0; i < n && fl; i++){ memset(dp, -1, sizeof(dp)); dp[i] = 0; queue<int> q; q.push(i); while (!q.empty()){ int x = q.front(); q.pop(); for (int j = 0; j < n; j++){ if (matrix[x][j] == 1){ if (dp[j] == -1){ dp[j] = dp[x] + 1; q.push(j); } else if (abs(dp[j] - dp[x]) != 1) fl = 0; } } } for (int j = 0; j < n; j++) ans = max(ans, dp[j]); } if (fl == 0){ return -1; }else{ return ans + 1; } } int main(){ vector<vector<int>> matrix = { { 0, 1, 0, 1, 1, 0 }, { 1, 0, 1, 0, 0, 1 }, { 0, 1, 0, 1, 0, 0 }, { 1, 0, 1, 0, 0, 0 }, { 1, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 0 } }; cout << solve(matrix) << endl; }
Input
{ { 0, 1, 0, 1, 1, 0 }, { 1, 0, 1, 0, 0, 1 }, { 0, 1, 0, 1, 0, 0 }, { 1, 0, 1, 0, 0, 0 }, { 1, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 0 } }
Output
4
- Related Articles
- C++ code to count maximum groups can be made
- C++ Program to Check if any Graph is Possible to be Constructed for a Given Degree Sequence
- Count all possible N digit numbers that satisfy the given condition in C++
- Maximum elements that can be made equal with k updates in C++
- Count of numbers which can be made power of 2 by given operation in C++
- C++ Program to find out if a palindromic matrix can be made from a given matrix
- C++ Program to find out the maximum amount of score that can be decreased from a graph
- Maximum area of rectangle possible with given perimeter in C++
- Maximum number of parallelograms that can be made using the given length of line segments in C++
- Count number of right triangles possible with a given perimeter in C++
- C++ program to find out the maximum possible tally from given integers
- Print all possible strings that can be made by placing spaces in C++
- C++ Program to find out the maximum amount of money that can be made from selling cars
- Maximum size of sub-array that satisfies the given condition in C++ program
- C++ Program to Check Whether Topological Sorting can be Performed in a Graph

Advertisements