Union and Intersection of two Graphs - GeeksforGeeks

21 Apr, 2021
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 ise1 1 2e2 1 3e3 3 4e4 2 4e5 2 5e6 4 5
G1 intersection G2 ise4 2 4Explanation: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.
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.
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 graphsvoid 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 unioun 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 unioun    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 graphsvoid 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 Codeint 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;}
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)
DFS
Graph Traversals
graph-connectivity
Graph
Hash
Mathematical
Searching
Searching
Hash
Mathematical
DFS
Graph
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Topological Sorting
Detect Cycle in a Directed Graph
Bellman–Ford Algorithm | DP-23
Floyd Warshall Algorithm | DP-16
Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Internal Working of HashMap in Java
Hashing | Set 1 (Introduction)
Count pairs with given sum
Hashing | Set 3 (Open Addressing)