Union and Intersection of two Graphs
Given two graphs G1 and G2, the task is to find the union and intersection of the two given graphs, i.e. (G1 ∪ G2) and (G1 ∩ G2).
Examples:
Input: G1 = { (“e1”, 1, 2), (“e2”, 1, 3), (“e3”, 3, 4), (“e4”, 2, 4) }, G2 = = { (“e4”, 2, 4), (“e5”, 2, 5), (“e6”, 4, 5) }
Output:
G1 union G2 is
e1 1 2
e2 1 3
e3 3 4
e4 2 4
e5 2 5
e6 4 5
G1 intersection G2 is
e4 2 4
Explanation:
Union of the graphs G1 and G2:
Intersection of the graphs G1 and G2:
Approach: Follow the steps below to solve the problem:
- Define a function, say Union(G1, G2), to find the union of the G1 and G2:
- Initialize a map, say added, that stores if an edge is already been added or not.
- Iterate over the edges of the graph G1 and push all the edges in a graph, say G, and mark all the edges visited in added.
- Now, again traverse over the edges of the graph G2 and push the edge in the G if the edge is not already been added, and then mark the edge added in the map added.
- Define a function say Intersection(G1, G2) to find the Intersection of the G1 and G2:
- Initialize a map, say added , that stores if an edge is already been added or not.
- Traverse over the edges of the graph G1 and marked all the edges visited in the map added.
- Now, again traverse over the edges of the graph G2 and push the edge in the graph G, if the edge is already been added. Then, mark the edge added in the map.
- Now, print the graphs obtained after the function call of Union(G1, G2) and Intersection(G1, G2).
Below is the implementation of the above approach:
C++14
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find union of two graphs void find_union( vector<tuple<string, int , int > > G1, vector<tuple<string, int , int > > G2) { // Stores an edge of the graph G1 map<string, pair< int , int > > added; // Stores the union graph G1 vector<tuple<string, int , int > > G; // Iterate over the edges // of the graph G1 for ( auto p : G1) { string a = get<0>(p); // Get the edges int b = get<1>(p); int c = get<2>(p); // Insert the current // edges into graph G G.push_back( make_tuple(a, b, c)); added[a] = { b, c }; } // Iterate over the edges // of the graph G1 for ( auto p : G2) { string a = get<0>(p); int b = get<1>(p); int c = get<2>(p); pair< int , int > x = { b, c }; pair< int , int > y = { c, b }; // If either edge x or // y is already added if (added[a] == x || added[a] == y) continue ; // Otherwise G.push_back(make_tuple(a, b, c)); } // Print the union cout << "G1 union G2 is\n" ; for ( auto p : G) { string a = get<0>(p); int b = get<1>(p); int c = get<2>(p); cout << a << " " << b << " " << c << endl; } } // Function to find intersection of two graphs void find_intersection( vector<tuple<string, int , int > > G1, vector<tuple<string, int , int > > G2) { // Stores an edge map<string, pair< int , int > > added; // Stores the graph of intersection vector<tuple<string, int , int > > G; // Iterate over edges of graph G1 for ( auto p : G1) { string a = get<0>(p); int b = get<1>(p); int c = get<2>(p); added[a] = { b, c }; } // Iterate over edges of graph G2 for ( auto p : G2) { string a = get<0>(p); int b = get<1>(p); int c = get<2>(p); pair< int , int > x = { b, c }; pair< int , int > y = { c, b }; // If either edge x or // y is already added if (added[a] == x || added[a] == y) G.push_back(make_tuple(a, b, c)); } // Print the graph G cout << "G1 intersection G2 is\n" ; for ( auto p : G) { string a = get<0>(p); int b = get<1>(p); int c = get<2>(p); cout << a << " " << b << " " << c << endl; } } // Driver Code int main() { vector<tuple<string, int , int > > G1 = { make_tuple( "e1" , 1, 2), make_tuple( "e2" , 1, 3), make_tuple( "e3" , 3, 4), make_tuple( "e4" , 2, 4) }; vector<tuple<string, int , int > > G2 = { make_tuple( "e4" , 2, 4), make_tuple( "e5" , 2, 5), make_tuple( "e6" , 4, 5) }; // Function call for finding the // Union of the given graph find_union(G1, G2); // Function call for finding the // Intersection of the given graph find_intersection(G1, G2); return 0; } |
Java
// Java codeto implement the approach import java.util.*; class GFG { // Function to find union of two graphs static void findUnion(List<Edge> g1, List<Edge> g2) { // Stores an edge of the graph G1 Map<String, Edge> added = new HashMap<>(); // Stores the union graph G1 List<Edge> g = new ArrayList<>(); // Iterate over the edges of the graph G1 for (Edge edge : g1) { // Insert the current edges into graph G g.add(edge); added.put(edge.label, edge); } // Iterate over the edges of the graph G2 for (Edge edge : g2) { Edge x = edge; Edge y = new Edge(edge.label, edge.v2, edge.v1); // If either edge x or y is already added if (added.containsKey(edge.label) && (added.get(edge.label).equals(x) || added.get(edge.label).equals(y))) continue ; // Otherwise g.add(edge); } // Print the union System.out.println( "G1 union G2 is" ); for (Edge edge : g) { System.out.println(edge.label + " " + edge.v1 + " " + edge.v2); } } // Function to find intersection of two graphs static void findIntersection(List<Edge> g1, List<Edge> g2) { // Stores an edge Map<String, Edge> added = new HashMap<>(); // Stores the graph of intersection List<Edge> g = new ArrayList<>(); // Iterate over edges of graph G1 for (Edge edge : g1) { added.put(edge.label, edge); } // Iterate over edges of graph G2 for (Edge edge : g2) { Edge x = edge; Edge y = new Edge(edge.label, edge.v2, edge.v1); // If either edge x or y is already added if (added.containsKey(edge.label) && (added.get(edge.label).equals(x) || added.get(edge.label).equals(y))) g.add(edge); } // Print the graph G System.out.println( "G1 intersection G2 is" ); for (Edge edge : g) { System.out.println(edge.label + " " + edge.v1 + " " + edge.v2); } } // Driver code public static void main(String[] args) { List<Edge> g1 = Arrays.asList( new Edge( "e1" , 1 , 2 ), new Edge( "e2" , 1 , 3 ), new Edge( "e3" , 3 , 4 ), new Edge( "e4" , 2 , 4 )); List<Edge> g2 = Arrays.asList( new Edge( "e4" , 2 , 4 ), new Edge( "e5" , 2 , 5 ), new Edge( "e6" , 4 , 5 )); // Function call to find the Union of the // given graph findUnion(g1, g2); // Function call for finding intersection // of the given graph findIntersection(g1, g2); } // Edge class definition static class Edge { String label; int v1, v2; // Constructor Edge(String label, int v1, int v2) { this .label = label; this .v1 = v1; this .v2 = v2; } // Implementing the equals method @Override public boolean equals(Object o) { if ( this == o) return true ; if (!(o instanceof Edge)) return false ; Edge edge = (Edge)o; return v1 == edge.v1 && v2 == edge.v2; } // Implementing the hashCode method @Override public int hashCode() { return Objects.hash(v1, v2); } } } // This code is contributed by phasing17 |
Python3
# Python3 code to implement the approach # Function to find union of two graphs def findUnion(G1, G2): # Stores an edge of the graph G1 added = {} # Stores the union graph G1 G = [] # Iterate over the edges # of the graph G1 for p in G1: a = p[ 0 ] b = p[ 1 ] c = p[ 2 ] # Insert the current # edges into graph G G.append([a, b, c]) added[a] = [b, c] # Iterate over the edges # of the graph G1 for p in G2: a = p[ 0 ] b = p[ 1 ] c = p[ 2 ] x = [b, c] y = [c, b] # If either edge x or # y is already added if added.get(a) = = x or added.get(a) = = y: continue # Otherwise G.append([a, b, c]) # Print the union print ( "G1 union G2 is" ) for p in G: a = p[ 0 ] b = p[ 1 ] c = p[ 2 ] print (a, b, c) # Function to find intersection of two graphs def findIntersection(G1, G2): # Stores an edge added = {} # Stores the graph of intersection G = [] # Iterate over edges of graph G1 for p in G1: a = p[ 0 ] b = p[ 1 ] c = p[ 2 ] added[a] = [b, c] # Iterate over edges of graph G2 for p in G2: a = p[ 0 ] b = p[ 1 ] c = p[ 2 ] x = [b, c] y = [c, b] # If either edge x or # y is already added if added.get(a) = = x or added.get(a) = = y: G.append([a, b, c]) # Print the graph G print ( "G1 intersection G2 is" ) for p in G: print ( * p) # Driver code G1 = [[ "e1" , 1 , 2 ], [ "e2" , 1 , 3 ], [ "e3" , 3 , 4 ], [ "e4" , 2 , 4 ]]; G2 = [[ "e4" , 2 , 4 ],[ "e5" , 2 , 3 ], [ "e6" , 3 , 5 ], [ "e7" , 4 , 5 ]]; findUnion(G1, G2); findIntersection(G1, G2); # This code is contributed by phasing17 |
C#
// C# code to implement the approach using System; using System.Collections.Generic; class GFG { // Function to find union of two graphs static void FindUnion(List<( string , int , int )> G1, List<( string , int , int )> G2) { // Stores an edge of the graph G1 Dictionary< string , ( int , int )> added = new Dictionary< string , ( int , int )>(); // Stores the union graph G1 List<( string , int , int )> G = new List<( string , int , int )>(); // Iterate over the edges // of the graph G1 foreach (( string a, int b, int c) in G1) { // Insert the current // edges into graph G G.Add((a, b, c)); added[a] = (b, c); } // Iterate over the edges // of the graph G1 foreach (( string a, int b, int c) in G2) { ( int x, int y) = (b, c); ( int y2, int x2) = (c, b); // If either edge x or // y is already added if (added.ContainsKey(a) && (added[a] == (x, y) || added[a] == (y2, x2))) continue ; // Otherwise G.Add((a, b, c)); } // Print the union Console.WriteLine( "G1 union G2 is" ); foreach (( string a, int b, int c) in G) { Console.WriteLine(a + " " + b + " " + c); } } // Function to find intersection of two graphs static void FindIntersection(List<( string , int , int )> G1, List<( string , int , int )> G2) { // Stores an edge Dictionary< string , ( int , int )> added = new Dictionary< string , ( int , int )>(); // Stores the graph of intersection List<( string , int , int )> G = new List<( string , int , int )>(); // Iterate over edges of graph G1 foreach (( string a, int b, int c) in G1) { added[a] = (b, c); } // Iterate over edges of graph G2 foreach (( string a, int b, int c) in G2) { ( int x, int y) = (b, c); ( int y2, int x2) = (c, b); // If either edge x or // y is already added if (added.ContainsKey(a) && (added[a] == (x, y) || added[a] == (y2, x2))) G.Add((a, b, c)); } // Print the graph G Console.WriteLine( "G1 intersection G2 is" ); foreach (( string a, int b, int c) in G) { Console.WriteLine(a + " " + b + " " + c); } } // Driver code static void Main( string [] args) { List<( string , int , int )> G1 = new List<( string , int , int )> { ( "e1" , 1, 2), ( "e2" , 1, 3), ( "e3" , 3, 4), ( "e4" , 2, 4) }; List<( string , int , int )> G2 = new List<( string , int , int )> { ( "e4" , 2, 4), ( "e5" , 2, 5), ( "e6" , 4, 5) }; // Function call for finding the // Union of the given graph FindUnion(G1, G2); // Function call for finding the // Intersection of the given graph FindIntersection(G1, G2); } } // This code is contributed by phasing17 |
Javascript
// Function to find union of two graphs function findUnion(G1, G2) { // Stores an edge of the graph G1 var added = new Map(); // Stores the union graph G1 var G = []; // Iterate over the edges // of the graph G1 for (const p of G1) { const a = p[0]; // Get the edges const b = p[1]; const c = p[2]; // Insert the current // edges into graph G G.push([a, b, c]); added.set(a, [b, c]); } // Iterate over the edges // of the graph G1 for (const p of G2) { const a = p[0]; const b = p[1]; const c = p[2]; const x = [b, c]; const y = [c, b]; // If either edge x or // y is already added if (JSON.stringify(added.get(a)) === JSON.stringify(x) || JSON.stringify(added.get(a)) === JSON.stringify(y)) continue ; // Otherwise G.push([a, b, c]); } // Print the union console.log( "G1 union G2 is" ); for (const p of G) { const a = p[0]; const b = p[1]; const c = p[2]; console.log(a + " " + b + " " + c); } } // Function to find intersection of two graphs function findIntersection(G1, G2) { // Stores an edge var added = new Map(); // Stores the graph of intersection var G = []; // Iterate over edges of graph G1 for (const p of G1) { const a = p[0]; const b = p[1]; const c = p[2]; added.set(a, [b, c]); } // Iterate over edges of graph G2 for (const p of G2) { const a = p[0]; const b = p[1]; const c = p[2]; const x = [b, c]; const y = [c, b]; // If either edge x or // y is already added if (JSON.stringify(added.get(a)) === JSON.stringify(x) || JSON.stringify(added.get(a)) === JSON.stringify(y)) G.push([a, b, c]); } // Print the graph G console.log( "G1 intersection G2 is" ); for (const p of G) { const a = p[0]; const b = p[1]; const c = p[2]; console.log(a + " " + b + " " + c); } } // Driver Code const G1 = [ [ "e1" , 1, 2], [ "e2" , 1, 3], [ "e3" , 3, 4], [ "e4" , 2, 4] ]; const G2 = [ [ "e4" , 2, 4], [ "e5" , 2, 3], [ "e6" , 3, 5], [ "e7" , 4, 5] ]; findUnion(G1, G2); findIntersection(G1, G2); |
Output:
G1 union G2 is e1 1 2 e2 1 3 e3 3 4 e4 2 4 e5 2 5 e6 4 5 G1 intersection G2 is e4 2 4
Time Complexity: O(N * log(N))
Auxiliary Space: O(N)
Contact Us