// C# program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to insert vertices to adjacency list
static void insert(List<int> []adj,
int u, int v)
{
// Insert a vertex v to vertex u
adj[u].Add(v);
return;
}
// Function to display adjacency list
static void printList(List<int> []adj,
int V)
{
for(int i = 0; i < V; i++)
{
Console.Write(i);
foreach(int j in adj[i])
Console.Write(" --> " + j);
Console.WriteLine();
}
Console.WriteLine();
}
// Function to convert adjacency
// list to adjacency matrix
static int[,] convert(List<int> []adj,
int V)
{
// Initialize a matrix
int [,]matrix = new int[V, V];
for(int i = 0; i < V; i++)
{
foreach(int j in adj[i])
matrix[i, j] = 1;
}
return matrix;
}
// Function to display adjacency matrix
static void printMatrix(int[,] adj, int V)
{
for(int i = 0; i < V; i++)
{
for(int j = 0; j < V; j++)
{
Console.Write(adj[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine();
}
// Driver code
public static void Main(String[] args)
{
int V = 5;
List<int> []adjList = new List<int>[V];
for(int i = 0; i < adjList.Length; i++)
adjList[i] = new List<int>();
// Inserting edges
insert(adjList, 0, 1);
insert(adjList, 0, 4);
insert(adjList, 1, 0);
insert(adjList, 1, 2);
insert(adjList, 1, 3);
insert(adjList, 1, 4);
insert(adjList, 2, 1);
insert(adjList, 2, 3);
insert(adjList, 3, 1);
insert(adjList, 3, 2);
insert(adjList, 3, 4);
insert(adjList, 4, 0);
insert(adjList, 4, 1);
insert(adjList, 4, 3);
// Display adjacency list
Console.Write("Adjacency List: \n");
printList(adjList, V);
// Function call which returns
// adjacency matrix after conversion
int[,] adjMatrix = convert(adjList, V);
// Display adjacency matrix
Console.Write("Adjacency Matrix: \n");
printMatrix(adjMatrix, V);
}
}
// This code is contributed by amal kumar choubey