There can be scenario for a developer when working with data structures, to find complement of a graph.
The foll. code snippet demonstrates to find the G' for G.
You first take adjacency matrix of G as input, then find the G'
The code is:
static void Main(string[] args)
{
Console.Write("Please enter the no. of vertices in graph: ");
//Declare total no of vertices.
int totalVertices = int.Parse(Console.ReadLine());
//Declare Input Graph G
int [,] inputGraph=new int [totalVertices,totalVertices];
//Store the complement graph G'
int[,] complementGraph = new int[totalVertices, totalVertices];
Console.WriteLine("\nEnter the Adjacency Matrix for Graph (It contains only 1's and 0's) \n");
for (int row = 0; row < totalVertices; row++)
{
for (int col = 0; col < totalVertices; col++)
{
Console.Write("Enter inputGraph[" + (row + 1) + ","+(col+1)+"]: ");
inputGraph[row, col]=int.Parse(Console.ReadLine());
}
}
Console.WriteLine("\nYour input graph is:\n");
for (int row = 0; row < totalVertices; row++)
{
for (int col = 0; col < totalVertices; col++)
{
Console.Write(inputGraph[row, col]+"\t");
}
Console.WriteLine("\n");
}
//Compute Complement
for (int row = 0; row < totalVertices; row++)
{
for (int col = 0; col < totalVertices; col++)
{
if(inputGraph[row,col]==1)
{
complementGraph[row,col]=0;
}
else if (inputGraph[row, col] == 0)
{
complementGraph[row, col] = 1;
}
else
{
}
}
}
Console.WriteLine("\nComplement of graph is:\n");
for (int row = 0; row < totalVertices; row++)
{
for (int col = 0; col < totalVertices; col++)
{
Console.Write(complementGraph[row, col] + "\t");
}
Console.WriteLine("\n");
}
Console.Read();
}