- 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
Find the maximum value permutation of a graph in C++
In this problem, we are given a graph of N nodes. Our task is to Find the maximum possible value of the minimum value of the modified array.
For the graph we have a permutation of nodes which is the number of induces with minimum 1 node on the left of it sharing a common edge.
Let’s take an example to understand the problem,
Input : N = 4, edge = {{1, 2}, {2, 3}, {3, 4}, {4, 1}} Output : 3
Solution Approach
A simple solution to the problem is by traversing the tree from one node visiting all its adjacent nodes. We will find the permutation of nodes using the formula of the number of nodes connected to it.
The formula is,
Size of component - 1.
Example
Program to illustrate the working of our solution
#include <bits/stdc++.h> using namespace std; int dfs(int x, vector<int> adjMat[], int visited[]){ int sz = 1; visited[x] = 1; for (auto ch : adjMat[x]) if (!visited[ch]) sz += dfs(ch, adjMat, visited); return sz; } int maxValPermutationGraph(int n, vector<int> adjMat[]){ int val = 0; int visited[n + 1] = { 0 }; for (int i = 1; i <= n; i++) if (!visited[i]) val += dfs(i, adjMat, visited) - 1; return val; } int main(){ int n = 4; vector<int> adjMat[n + 1] = {{1, 2}, {2, 3}, {3, 4}, {4, 1}}; cout<<"The maximum value permutation of a graph is "<<maxValPermutationGraph(n, adjMat); return 0; }
Output
The maximum value permutation of a graph is 3
- Related Articles
- C++ Program to Find the Maximum Cut in a Graph
- Maximum sum of absolute difference of any permutation in C++
- C++ program to find out the maximum sum of a minimally connected graph
- Find Permutation in C++
- Program to find maximum sum obtained of any permutation in Python
- Maximum even length sub-string that is permutation of a palindrome in C++
- Find the maximum possible value of the minimum value of modified array in C++
- Find n-th lexicographically permutation of a strings in C++
- Find smallest permutation of given number in C++
- Maximum number of edges in Bipartite graph in C++
- C++ program to find out the maximum value of i
- Find Maximum XOR value of a sub-array of size k in C++
- Draw a graph of the function p=4a, from the graph, find the value of p, when a=6.
- C++ Program to find out the maximum amount of score that can be decreased from a graph
- Maximum and minimum isolated vertices in a graph in C++

Advertisements