// 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