📜  两张图的并集和相交

📅  最后修改于: 2021-04-29 17:06:33             🧑  作者: Mango

给定两个图G1G2 ,任务是找到两个给定图的并集交集,即(G1∪G2)(G1∩G2)

例子:

方法:请按照以下步骤解决问题:

  • 定义一个函数,例如Union(G1,G2) ,以找到G1G2的并集:
    • 初始化一个地图(例如, added) ,该地图存储是否已添加边。
    • 遍历图形G1的边缘,并推动图形中的所有边缘(例如G) ,并标记添加的所有边缘。
    • 现在,再次遍历图形G2的边缘,如果尚未添加边缘,则将其推入G的边缘,然后在已添加的地图中标记添加的边缘
  • 定义一个函数,例如Intersection(G1,G2)来查找G1G2的交集:
    • 初始化一个地图(例如, added) ,该地图存储是否已添加边。
    • 遍历图形G1的边缘并标记添加的地图中所有已访问的边缘
    • 现在,再次遍历图G2的边缘,并在图G中推动边缘(如果已添加边缘)。然后,标记在地图中添加的边。
  • 现在,打印在Union(G1,G2)Intersection(G1,G2)函数调用之后获得的图形。

下面是上述方法的实现:

C++14
// C++ program for the above approach
#include 
using namespace std;
  
// Function to find union of two graphs
void find_union(
    vector > G1,
    vector > G2)
{
    // Stores an edge of the graph G1
    map > added;
  
    // Stores the unioun graph G1
    vector > 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 x = { b, c };
        pair 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 graphs
void find_intersection(
    vector > G1,
    vector > G2)
{
    // Stores an edge
    map > added;
  
    // Stores the graph of intersection
    vector > 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 x = { b, c };
        pair 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 > G1
        = { make_tuple("e1", 1, 2),
            make_tuple("e2", 1, 3),
            make_tuple("e3", 3, 4),
            make_tuple("e4", 2, 4) };
  
    vector > 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

时间复杂度: O(N * log(N))
辅助空间: O(N)