0% found this document useful (0 votes)
9 views

Subset of Graph

Uploaded by

sri.srikarthi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Subset of Graph

Uploaded by

sri.srikarthi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Skip to content

 Courses
 DSA
 Practice
 Tutorials
 Jobs

  


 Sign In

 DSA Course
 DSA
 Interview Problems on Graph
 Practice Graph
 MCQs on Graph
 Graph Tutorial
 Graph Representation
 Graph Properties
 Types of Graphs
 Graph Applications
 BFS on Graph
 DFS on Graph
 Graph VS Tree
 Transpose Graph
 Dijkstra's Algorithm
 Minimum Spanning Tree
 Prim’s Algorithm
 Topological Sorting
 Floyd Warshall Algorithm
 Strongly Connected Components
 Advantages & Disadvantages

 Go Premium
 Share Your Experiences
 Largest subset of Graph vertices with edges of 2 or more colors
 Number of Simple Graph with N Vertices and M Edges
 Minimum number of edges between two vertices of a graph using DFS
 Minimize cost to color all the vertices of an Undirected Graph
 Count of distinct graphs that can be formed with N vertices
 Size of the Largest Trees in a Forest formed by the given Graph
 Maximum Bitwise XOR of node values of an Acyclic Graph made up of N given vertices using
M edges
 Make a tree with n vertices , d diameter and at most vertex degree k
 Minimize cost to color all the vertices of an Undirected Graph using given operation
 Maximize number of nodes which are not part of any edge in a Graph
 How to iterate over boost graph to get incoming and outgoing edges of vertex?
 Largest subtree sum for each vertex of given N-ary Tree
 Number of spanning trees of a weighted complete Graph
 Maximum product of a pair of nodes from largest connected component in a Graph
 Find K vertices in the graph which are connected to at least one of remaining vertices
 Program to find total number of edges in a Complete Graph
 Minimum power required to destroy all vertices in a Graph
 Largest subarray sum of all connected components in undirected graph
 Print node whose each neighboring Tree has all nodes of same color
 Smallest vertex in the connected components of all the vertices in given undirect graph
 DSA to Development Course

Largest subset of Graph vertices with edges of 2 or


more colors
Last Updated : 13 Sep, 2023



Given an undirected complete graph with N nodes or vertices. Edges of the graph are colored,
find the largest subset of vertices with edges of 2 or more colors. We are given graph as
adjacency matrix C[][] where C[i][j] is color of edge from vertex i to vertex j. Since graph is
undirected, values C[i][j] of C[j][i] are same.

We define C[i][i] to be zero, although there is no such edge present. i.e. the graph does not
contain self-loops.

Examples:

Example 1:

Input : C[][]= {{0, 1, 2},

{1, 0, 3},

{2, 3, 0}}

Output : 3
Example 2:

Input : C[][]= {{0, 1, 1},

{1, 0, 3},

{1, 3, 0}}

Output : 0
Since graph is complete, each edge can be one of n*(n-1)/2 +1 different colors. These colors
are labeled from 0 to n*(n-1)/2, inclusive. But not all these n*(n-1)/2 +1 colors need to be
used. i.e., it is possible that two different edges could have the same color.

Let’s call a vertex “bad” if all its neighbors are of the same color. Obviously, we can’t have
any such bad vertex in our subset, so remove such bad vertex from the graph. This might
introduce some more bad vertices, but we can keep repeating this process until we find a
subset free of bad vertices. So, at last, we should remain through a graph which does not have
any bad vertex means every vertex of our subset has at least two different color edges with
other adjacent vertices.

Example:
Input :
let C[6][6]:
{{0, 9, 2, 4, 7, 8},
{9, 0, 9, 9, 7, 9},
{2, 9, 0, 3, 7, 6},
{4, 9, 3, 0, 7, 1},
{7, 7, 7, 7, 0, 7},
{8, 9, 6, 1, 7, 0}};
Step I: First of all, we can see that row 5(node ‘e’) contains only 7 means node ‘e’ is
connected through edges having color code 7 so it does not have more than one color edge so
we have to remove 5 from subset. Now, our graph will contain only 5 vertex and are as:
C[5][5]:
{{0, 9, 2, 4, 8},
{9, 0, 9, 9, 9},
{2, 9, 0, 3, 6},
{4, 9, 3, 0, 1},
{8, 9, 6, 1, 0}};
Step II: Further, we can see that row 2 (node ‘b’) also doesn’t contain more than 1 color edge,
so we should remove row 2 and column 2 also. Which result in our new graph as:
C[4][4]:
{{0, 2, 4, 8},
{2, 0, 3, 6},
{4, 3, 0, 1},
{8, 6, 1, 0}};
Step III: Now, we can see that each vertex has more than 1 different color edge. So, the total
number of vertices in the subset is 4.

Implementation:

// C++ program to find size of subset of graph vertex

// such that each vertex has more than 1 color edges

#include <bits/stdc++.h>

using namespace std;

// Number of vertices

const int N = 6;
// function to calculate max subset size

int subsetGraph(int C[][N])

// set for number of vertices

set<int> vertices;

for (int i = 0; i < N; ++i)

vertices.insert(i);

// loop for deletion of vertex from set

while (!vertices.empty())

// if subset has only 1 vertex return 0

if (vertices.size() == 1)

return 1;

// for each vertex iterate and keep removing

// a vertex while we find a vertex with all

// edges of same color.

bool someone_removed = false;

for (int x : vertices)

// note down different color values

// for each vertex

set<int> values;

for (int y : vertices)

if (y != x)

values.insert(C[x][y]);

// if only one color is found

// erase that vertex (bad vertex)

if (values.size() == 1)

vertices.erase(x);
someone_removed = true;

break;

// If no vertex was removed in the

// above loop.

if (!someone_removed)

break;

return (vertices.size());

// Driver program

int main()

int C[][N] = {{0, 9, 2, 4, 7, 8},

{9, 0, 9, 9, 7, 9},

{2, 9, 0, 3, 7, 6},

{4, 9, 3, 0, 7, 1},

{7, 7, 7, 7, 0, 7},

{8, 9, 6, 1, 7, 0}

};

cout << subsetGraph(C);

return 0;

Output
4

Unlock a distraction-free, high-quality learning experience with GeeksforGeeks Premium!


Get unlimited access to 35+ expert-led tech courses covering everything from
programming languages, DSA to Web Development and Data Science, all designed to
help you ace any interview.

Upgrade today and enjoy a 1-year free extension with your subscription, plus exclusive
perks like unlimited article summarization, a 100% ad-free environment, AI-powered
assistance in coding problems, and so much more. Go Premium!

Shivam Pradhan (anuj_charm)

Next Article

Number of Simple Graph with N Vertices and M Edges

Read More

Similar Reads

Pendant Vertices, Non-Pendant Vertices, Pendant Edges and Non-Pendant Edges in Graph

Pre-requisites: Handshaking theorem. Pendant Vertices Let G be a graph, A vertex v of G is called a


pendant vertex if and only if v has degree 1. In other words, pendant vertices are the vertices that
have degree 1, also called pendant vertex. Note: Degree = number of edges connected to a vertex In
the case of trees, a pendant vertex is known as a

7 min read

Connect a graph by M edges such that the graph does not contain any cycle and Bitwise AND of
connected vertices is maximum

Given an array arr[] consisting of values of N vertices of an initially unconnected Graph and an
integer M, the task is to connect some vertices of the graph with exactly M edges, forming only one
connected component, such that no cycle can be formed and Bitwise AND of the connected vertices
is maximum possible. Examples: Input: arr[] = {1, 2, 3, 4

9 min read

Color tree with minimum colors such that colors of edges incident to a vertex are different

Given a tree with N nodes. The task is to color the tree with the minimum number of colors(K) such
that the colors of the edges incident to a vertex are different. Print K in first-line and then in next line
print N - 1 space-separated integer represents the colors of the edges. Examples: Input: N = 3,
edges[][] = {{0, 1}, {1, 2}} 0 / 1 / 2 Output:

10 min read
Find K vertices in the graph which are connected to at least one of remaining vertices

Given a connected graph with N vertices. The task is to select k(k must be less than or equals to n/2,
not necessarily minimum) vertices from the graph such that all these selected vertices are connected
to at least one of the non selected vertex. In case of multiple answers print any one of them.
Examples: Input : Output : 1 Vertex 1 is connected

8 min read

Number of Simple Graph with N Vertices and M Edges

Given two integers N and M, the task is to count the number of simple undirected graphs that can be
drawn with N vertices and M edges. A simple graph is a graph that does not contain multiple edges
and self-loops. Examples: Input: N = 3, M = 1 Output: 3 The 3 graphs are {1-2, 3}, {2-3, 1}, {1-3, 2}.
Input: N = 5, M = 1 Output: 10 Approach: The N ve

6 min read

Minimum number of edges between two vertices of a graph using DFS

Given an undirected graph G(V, E) with N vertices and M edges. We need to find the minimum
number of edges between a given pair of vertices (u, v). We have already discussed this problem
using the BFS approach, here we will use the DFS approach. Examples: Input: For the following given
graph, find the minimum number of edges between vertex pair (0,

10 min read

Maximum Bitwise XOR of node values of an Acyclic Graph made up of N given vertices using M edges

Given N nodes valued by [1, N], an array arr[] consisting of N positive integers such that the ith node
( 1-based indexing ) has the value arr[i] and an integer M, the task is to find the maximum Bitwise
XOR of node values of an acyclic graph formed by M edges. Examples: Input: arr[]= {1, 2, 3, 4}, M =
2Output: 7Explanation:Acyclic graphs having M(

7 min read

Minimum number of edges to be removed from given Graph such that no path exists between given
pairs of vertices

Given an undirected graph consisting of N valued over the range [1, N] such that vertices (i, i + 1) are
connected and an array arr[] consisting of M pair of integers, the task is to find the minimum
number of edges that should be removed from the graph such that there doesn't exist any path for
every pair (u, v) in the array arr[]. Examples: Input
8 min read

Minimum number of edges between two vertices of a Graph

You are given an undirected graph G(V, E) with N vertices and M edges. We need to find the
minimum number of edges between a given pair of vertices (u, v). Examples: Input: For given graph
G. Find minimum number of edges between (1, 5). Output: 2Explanation: (1, 2) and (2, 5) are the
only edges resulting into shortest path between 1 and 5. The idea

8 min read

Ways to Remove Edges from a Complete Graph to make Odd Edges

Given a complete graph with N vertices, the task is to count the number of ways to remove edges
such that the resulting graph has odd number of edges. Examples: Input: N = 3 Output: 4 The initial
graph has 3 edges as it is a complete graph. We can remove edges (1, 2) and (1, 3) or (1, 2) and (2, 3)
or (1, 3) and (2, 3) or we do not remove any of th

4 min read

Article Tags :

 DSA
 Graph

Practice Tags :

 Graph

1265k+ interested Geeks

Data Structures and Algorithms - Self Paced [Online Course]


292k+ interested Geeks

Data Structures & Algorithms in Python - Self Paced

44k+ interested Geeks

Data Structures & Algorithms in JavaScript - Self Paced Course

Corporate & Communications Address:- A-143, 9th Floor, Sovereign Corporate Tower, Sector- 136,
Noida, Uttar Pradesh (201305) | Registered Address:- K 061, Tower K, Gulshan Vivante Apartment,
Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305

 Company
 About Us
 Legal
 Careers
 In Media
 Contact Us
 Advertise with us
 GFG Corporate Solution
 Placement Training Program

 Explore
 Job-A-Thon Hiring Challenge
 Hack-A-Thon
 GfG Weekly Contest
 Offline Classes (Delhi/NCR)
 DSA in JAVA/C++
 Master System Design
 Master CP
 GeeksforGeeks Videos
 Geeks Community

 Languages
 Python
 Java
 C++
 PHP
 GoLang
 SQL
 R Language
 Android Tutorial

 DSA
 Data Structures
 Algorithms
 DSA for Beginners
 Basic DSA Problems
 DSA Roadmap
 DSA Interview Questions
 Competitive Programming

 Data Science & ML


 Data Science With Python
 Data Science For Beginner
 Machine Learning
 ML Maths
 Data Visualisation
 Pandas
 NumPy
 NLP
 Deep Learning

 Web Technologies
 HTML
 CSS
 JavaScript
 TypeScript
 ReactJS
 NextJS
 NodeJs
 Bootstrap
 Tailwind CSS

 Python Tutorial
 Python Programming Examples
 Django Tutorial
 Python Projects
 Python Tkinter
 Web Scraping
 OpenCV Tutorial
 Python Interview Question

 Computer Science
 GATE CS Notes
 Operating Systems
 Computer Network
 Database Management System
 Software Engineering
 Digital Logic Design
 Engineering Maths

 DevOps
 Git
 AWS
 Docker
 Kubernetes
 Azure
 GCP
 DevOps Roadmap

 System Design
 High Level Design
 Low Level Design
 UML Diagrams
 Interview Guide
 Design Patterns
 OOAD
 System Design Bootcamp
 Interview Questions

 School Subjects
 Mathematics
 Physics
 Chemistry
 Biology
 Social Science
 English Grammar

 Commerce
 Accountancy
 Business Studies
 Economics
 Management
 HR Management
 Finance
 Income Tax

 Databases
 SQL
 MYSQL
 PostgreSQL
 PL/SQL
 MongoDB

 Preparation Corner
 Company-Wise Recruitment Process
 Resume Templates
 Aptitude Preparation
 Puzzles
 Company-Wise Preparation
 Companies
 Colleges

 Competitive Exams
 JEE Advanced
 UGC NET
 UPSC
 SSC CGL
 SBI PO
 SBI Clerk
 IBPS PO
 IBPS Clerk

 More Tutorials
 Software Development
 Software Testing
 Product Management
 Project Management
 Linux
 Excel
 All Cheat Sheets
 Recent Articles

 Free Online Tools


 Typing Test
 Image Editor
 Code Formatters
 Code Converters
 Currency Converter
 Random Number Generator
 Random Password Generator

 Write & Earn


 Write an Article
 Improve an Article
 Pick Topics to Write
 Share your Experiences
 Internships

 DSA/Placements
 Data Structures and Algorithms - Self Paced [Online Course]
 Data Structures & Algorithms in JavaScript - Self Paced Course
 Data Structures & Algorithms in Python - Self Paced
 C Programming Course Online - Learn C with Data Structures
 Complete Interview Preparation
 Master Competitive Programming - Complete Beginner to Advanced
 Core Computer Science Subject for Interview Preparation
 Mastering System Design: From Low-Level to High-Level Solutions
 Tech Interview 101 - From DSA to System Design for Working professional [LIVE]
 DSA to Development: A complete guide [HYBRID]
 Placement Preparation Crash Course [LIVE]

 Development/Testing
 JavaScript Full Course Online | Learn JavaScript with Certification
 React JS Course Online - React JS Certification Course
 React Native Course Online: Learn React Native Mobile App Development
 Complete Django Web Development Course - Basics to Advance
 Complete Bootstrap Course For Beginners [Online]
 Full Stack Development with React & Node JS - [LIVE]
 JAVA Backend Development - [LIVE]
 Complete Software Testing Course - Beginner to Advance - [LIVE]
 Android Mastery with Kotlin: Beginner to Advanced - [LIVE]

 Machine Learning/Data Science


 Mastering Generative AI and ChatGPT
 Data Analytics Training using Excel, SQL, Python & PowerBI - [LIVE]
 Complete Machine Learning & Data Science Program - [LIVE]
 Data Science Training Program - [LIVE]

 Programming Languages
 C Programming Course Online - Learn C with Data Structures
 C++ Programming Course Online - Complete Beginner to Advanced
 Java Programming Online Course [Complete Beginner to Advanced]
 Python Full Course Online - Complete Beginner to Advanced
 JavaScript Full Course Online | Learn JavaScript with Certification

 Clouds/Devops
 DevOps Engineering - Planning to Production
 AWS Solutions Architect Certification Live Training Program
 Salesforce Certified Administrator Online CourseSalesforce Certified Administrator Online
Course

 GATE
 GATE CS & IT Test Series - 2025
 GATE Data Science and Artificial Intelligence Test Series 2025
 GATE Computer Science & Information Technology - 2025
 GATE Data Science and Artificial Intelligence 2025

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved


We use cookies to ensure you have the best browsing experience on our website. By using our site,
you acknowledge that you have read and understood our Cookie Policy & Privacy Policy

You might also like