Prerequisite: Graph and its representations
Given a adjacency matrix representation of a Graph. The task is to convert the given Adjacency Matrix to Adjacency List representation.
Examples:
Input: arr[][] = [ [0, 0, 1], [0, 0, 1], [1, 1, 0] ]
Output: The adjacency list is:
0 -> 2
1 -> 2
2 -> 0 -> 1
Input: arr[][] = [ [0, 1, 0, 0, 1], [1, 0, 1, 1, 1], [0, 1, 0, 1, 0], [0, 1, 1, 0, 1], [1, 1, 0, 1, 0] ]
Output: The adjacency list is:
0 -> 1 -> 4
1 -> 0 -> 2 -> 3 -> 4
2 -> 1 -> 3
3 -> 1 -> 2 -> 4
4 -> 0 -> 1 -> 3
Adjacency Matrix: Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let the 2D array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j.
Adjacency List: An array of lists is used. The size of the array is equal to the number of vertices. Let the array be array[]. An entry array[i] represents the list of vertices adjacent to the ith vertex.
To convert an adjacency matrix to the adjacency list. Create an array of lists and traverse the adjacency matrix. If for any cell (i, j) in the matrix “mat[i][j] != 0“, it means there is an edge from i to j, so insert j in the list at i-th position in the array of lists.
Below is the implementation of the above approach:
C++
#include<bits/stdc++.h>
using namespace std;
vector<vector< int >> convert( vector<vector< int >> a)
{
vector<vector< int >> adjList(a.size());
for ( int i = 0; i < a.size(); i++)
{
for ( int j = 0; j < a[i].size(); j++)
{
if (a[i][j] != 0)
{
adjList[i].push_back(j);
}
}
}
return adjList;
}
int main()
{
vector<vector< int >> a;
vector< int > p({0, 0, 1});
vector< int > q({0, 0, 1});
vector< int > r({1, 1, 0});
a.push_back(p);
a.push_back(q);
a.push_back(r);
vector<vector< int >> AdjList = convert(a);
cout<< "Adjacency List:" <<endl;
for ( int i = 0; i < AdjList.size(); i++)
{
cout << i;
for ( int j = 0; j < AdjList[i].size(); j++)
{
if (j == AdjList[i].size() - 1)
{
cout << " -> " << AdjList[i][j] << endl;
break ;
}
else
cout << " -> " << AdjList[i][j];
}
}
}
|
Java
import java.util.*;
public class GFG {
static ArrayList<ArrayList<Integer>> convert( int [][] a)
{
int l = a[ 0 ].length;
ArrayList<ArrayList<Integer>> adjListArray
= new ArrayList<ArrayList<Integer>>(l);
for ( int i = 0 ; i < l; i++) {
adjListArray.add( new ArrayList<Integer>());
}
int i, j;
for (i = 0 ; i < a[ 0 ].length; i++) {
for (j = 0 ; j < a.length; j++) {
if (a[i][j] != 0 ) {
adjListArray.get(i).add(j);
}
}
}
return adjListArray;
}
static void printArrayList(ArrayList<ArrayList<Integer>>
adjListArray)
{
for ( int v = 0 ; v < adjListArray.size(); v++) {
System.out.print(v);
for (Integer u : adjListArray.get(v)) {
System.out.print( " -> " + u);
}
System.out.println();
}
}
public static void main(String[] args)
{
int [][] a = { { 0 , 0 , 1 },
{ 0 , 0 , 1 },
{ 1 , 1 , 0 } };
ArrayList<ArrayList<Integer>> adjListArray = convert(a);
System.out.println( "Adjacency List: " );
printArrayList(adjListArray);
}
}
|
Python3
from collections import defaultdict
def convert(a):
adjList = defaultdict( list )
for i in range ( len (a)):
for j in range ( len (a[i])):
if a[i][j] ! = 0 :
adjList[i].append(j)
return adjList
a = [[ 0 , 0 , 1 ], [ 0 , 0 , 1 ], [ 1 , 1 , 0 ]]
AdjList = convert(a)
print ( "Adjacency List:" )
for i in AdjList:
print (i, end = "")
for j in AdjList[i]:
print ( " -> {}" . format (j), end = "")
print ()
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static List<List< int >> convert( int [,] a)
{
int l = a.GetLength(0);
List<List< int >> adjListArray = new List<List< int >>(l);
int i, j;
for (i = 0; i < l; i++)
{
adjListArray.Add( new List< int >());
}
for (i = 0; i < a.GetLength(0); i++)
{
for (j = 0; j < a.GetLength(1); j++)
{
if (a[i,j] != 0)
{
adjListArray[i].Add(j);
}
}
}
return adjListArray;
}
static void printList(List<List< int >> adjListArray)
{
for ( int v = 0; v < adjListArray.Count; v++)
{
Console.Write(v);
foreach ( int u in adjListArray[v])
{
Console.Write( " -> " + u);
}
Console.WriteLine();
}
}
public static void Main(String[] args)
{
int [,] a = { { 0, 0, 1 }, { 0, 0, 1 }, { 1, 1, 0 } };
List<List< int >> adjListArray = convert(a);
Console.WriteLine( "Adjacency List: " );
printList(adjListArray);
}
}
|
Javascript
function convert(a) {
let adjList = new Array(a.length);
for (let i = 0; i < a.length; i++) {
for (let j = 0; j < a[i].length; j++) {
if (a[i][j] != 0) {
if (!adjList[i]) {
adjList[i] = [];
}
adjList[i].push(j);
}
}
}
return adjList;
}
let a = [[0, 0, 1], [0, 0, 1], [1, 1, 0]];
let AdjList = convert(a);
console.log( "Adjacency List:" );
for (let i = 0; i < AdjList.length; i++) {
let output = i;
for (let j = 0; j < AdjList[i].length; j++) {
if (j === AdjList[i].length - 1) {
output += " -> " + AdjList[i][j];
break ;
} else {
output += " -> " + AdjList[i][j];
}
}
console.log(output);
}
|
OutputAdjacency List:
0 -> 2
1 -> 2
2 -> 0 -> 1
Time Complexity: O(N2).
Auxiliary Space: O(N2).