Given a 2d matrix that represents the node connections of an adjacency matrix. Our goal is to create a graph, which is in the form of an adjacency list, using the given adjacency matrix. For example.

In above example adjacency matrix is form of directed graph. And adjacency list is indicates the constructed result.

Here given code implementation process.

``````import java.util.ArrayList;
/*
Java Program
*/
public class Graph
{
public int vertices;
public ArrayList < ArrayList < Integer >> adgeList;
public Graph(int[][] matrix)
{
this.vertices = matrix.length;
this.adgeList = new ArrayList < ArrayList < Integer >> (matrix.length);
for (int i = 0; i < this.vertices; ++i)
{
}
}
public void addEdge(int u, int v)
{
if (u < 0 || u >= this.vertices ||
v < 0 || v >= this.vertices)
{
return;
}
}
{
for (int i = 0; i < this.vertices; ++i)
{
for (int j = 0; j < this.vertices; ++j)
{
if (matrix[i][j] == 1)
{
}
}
}
}
// Display graph nodes and edges
public void printGraph()
{
for (int i = 0; i < this.vertices; ++i)
{
System.out.print(" \n [" + i + "] :");
// iterate edges of i node
for (int j = 0; j < this.adgeList.get(i).size(); j++)
{
if (j != 0)
{
System.out.print(" → ");
}
}
}
}
public static void main(String[] args)
{
int [][]matrix = {
{0,1,1,0,1},
{1,0,1,0,1},
{1,1,0,1,0},
{0,1,0,0,1},
{1,1,0,1,0}
};
Graph g = new Graph(matrix);
// Display graph element
g.printGraph();
}
}``````

#### Output

`````` Graph Adjacency List
[0] : 1 →  2 →  4
[1] : 0 →  2 →  4
[2] : 0 →  1 →  3
[3] : 1 →  4
[4] : 0 →  1 →  3``````
``````// Include header file
#include <iostream>
#include <vector>
#define N 5
using namespace std;
/*
C++ Program
*/
class Graph
{
public: int vertices;
vector < vector < int > > adgeList;
Graph(int matrix[N][N])
{

for (int i = 0; i < N; ++i)
{
}
}
{
if (u < 0 || u >= N ||
v < 0 || v >= N)
{
return;
}
}
{
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
{
if (matrix[i][j] == 1)
{
}
}
}
}
// Display graph nodes and edges
void printGraph()
{
cout << "\n Graph Adjacency List ";
for (int i = 0; i < N; ++i)
{
cout << " \n [" << i << "] :";
// iterate edges of i node
for (int j = 0; j < this->adgeList.at(i).size(); j++)
{
if (j != 0)
{
cout << " → ";
}
cout << " " << this->adgeList.at(i).at(j);
}
}
}
};
int main()
{
int matrix[N][N] = {
{
0 , 1 , 1 , 0 , 1
} , {
1 , 0 , 1 , 0 , 1
} , {
1 , 1 , 0 , 1 , 0
} , {
0 , 1 , 0 , 0 , 1
} , {
1 , 1 , 0 , 1 , 0
}
};
Graph *g = new Graph(matrix);
// Display graph element
g->printGraph();
return 0;
}``````

#### Output

`````` Graph Adjacency List
[0] : 1 →  2 →  4
[1] : 0 →  2 →  4
[2] : 0 →  1 →  3
[3] : 1 →  4
[4] : 0 →  1 →  3``````
``````// Include namespace system
using System;
using System.Collections.Generic;
/*
Csharp Program
*/
public class Graph
{
public int vertices;
public List < List < int >> adgeList;
public Graph(int[,] matrix)
{
this.vertices = matrix.GetLength(0);
this.adgeList = new List < List < int >> (matrix.Length);
for (int i = 0; i < this.vertices; ++i)
{
}
}
public void addEdge(int u, int v)
{
if (u < 0 || u >= this.vertices ||
v < 0 || v >= this.vertices)
{
return;
}
}
{
for (int i = 0; i < this.vertices; ++i)
{
for (int j = 0; j < this.vertices; ++j)
{
if (matrix[i,j] == 1)
{
}
}
}
}
// Display graph nodes and edges
public void printGraph()
{
for (int i = 0; i < this.vertices; ++i)
{
Console.Write(" \n [" + i + "] :");
// iterate edges of i node
for (int j = 0; j < this.adgeList[i].Count; j++)
{
if (j != 0)
{
Console.Write(" → ");
}
}
}
}
public static void Main(String[] args)
{
int[,] matrix = {
{
0 , 1 , 1 , 0 , 1
},
{
1 , 0 , 1 , 0 , 1
},
{
1 , 1 , 0 , 1 , 0
},
{
0 , 1 , 0 , 0 , 1
},
{
1 , 1 , 0 , 1 , 0
}
};
Graph g = new Graph(matrix);
// Display graph element
g.printGraph();
}
}``````

#### Output

`````` Graph Adjacency List
[0] : 1 →  2 →  4
[1] : 0 →  2 →  4
[2] : 0 →  1 →  3
[3] : 1 →  4
[4] : 0 →  1 →  3``````
``````package main
import "fmt"
/*
Go Program
*/
type Graph struct {
vertices int
}
func getGraph(matrix [][]int ) * Graph {
var me *Graph = &Graph {}
me.vertices = len(matrix)
for i := 0 ; i < me.vertices ; i++ {
}
return me
}
func(this Graph) addEdge(u, v int) {
if u < 0 || u >= this.vertices ||
v < 0 || v >= this.vertices {
return
}
}
for i := 0 ; i < this.vertices ; i++ {
for j := 0 ; j < this.vertices ; j++ {
if matrix[i][j] == 1 {
}
}
}
}
// Display graph nodes and edges
func(this Graph) printGraph() {
for i := 0 ; i < this.vertices ; i++ {
fmt.Print(" \n [", i, "] :")
// iterate edges of i node
for j := 0 ; j < len(this.adgeList[i]) ; j++ {
if j != 0 {
fmt.Print(" → ")
}
}
}
}
func main() {
var matrix = [][] int {
{0,1,1,0,1},
{1,0,1,0,1},
{1,1,0,1,0},
{0,1,0,0,1},
{1,1,0,1,0},
};
var g * Graph = getGraph(matrix)
// Display graph element
g.printGraph()
}``````

#### Output

`````` Graph Adjacency List
[0] : 1 →  2 →  4
[1] : 0 →  2 →  4
[2] : 0 →  1 →  3
[3] : 1 →  4
[4] : 0 →  1 →  3``````
``````<?php
/*
Php Program
*/
class Graph
{
public \$vertices;
public  function __construct(\$matrix)
{
\$this->vertices = count(\$matrix);
for (\$i = 0; \$i < \$this->vertices; ++\$i)
{
}
}
{
if (\$u < 0 || \$u >= \$this->vertices ||
\$v < 0 || \$v >= \$this->vertices)
{
return;
}
}
{
for (\$i = 0; \$i < \$this->vertices; ++\$i)
{
for (\$j = 0; \$j < \$this->vertices; ++\$j)
{
if (\$matrix[\$i][\$j] == 1)
{
}
}
}
}
// Display graph nodes and edges
public  function printGraph()
{
for (\$i = 0; \$i < \$this->vertices; ++\$i)
{
echo(" \n [".\$i."] :");
// iterate edges of i node
for (\$j = 0; \$j < count(\$this->adgeList[\$i]); \$j++)
{
if (\$j != 0)
{
echo(" → ");
}
}
}
}
}

function main()
{
\$matrix = array(
array(0, 1, 1, 0, 1),
array(1, 0, 1, 0, 1),
array(1, 1, 0, 1, 0),
array(0, 1, 0, 0, 1),
array(1, 1, 0, 1, 0)
);
\$g = new Graph(\$matrix);
// Display graph element
\$g->printGraph();
}
main();``````

#### Output

`````` Graph Adjacency List
[0] : 1 →  2 →  4
[1] : 0 →  2 →  4
[2] : 0 →  1 →  3
[3] : 1 →  4
[4] : 0 →  1 →  3``````
``````/*
Node JS Program
*/
class Graph
{
constructor(matrix)
{
this.vertices = matrix.length;
for (var i = 0; i < this.vertices; ++i)
{
}
}
{
if (u < 0 || u >= this.vertices ||
v < 0 || v >= this.vertices)
{
return;
}
}
{
for (var i = 0; i < this.vertices; ++i)
{
for (var j = 0; j < this.vertices; ++j)
{
if (matrix[i][j] == 1)
{
}
}
}
}
// Display graph nodes and edges
printGraph()
{
for (var i = 0; i < this.vertices; ++i)
{
process.stdout.write(" \n [" + i + "] :");
// iterate edges of i node
for (var j = 0; j < this.adgeList[i].length; j++)
{
if (j != 0)
{
process.stdout.write(" → ");
}
}
}
}
}

function main()
{
var matrix = [
[0, 1, 1, 0, 1],
[1, 0, 1, 0, 1],
[1, 1, 0, 1, 0],
[0, 1, 0, 0, 1],
[1, 1, 0, 1, 0]
];
var g = new Graph(matrix);
// Display graph element
g.printGraph();
}
main();``````

#### Output

`````` Graph Adjacency List
[0] : 1 →  2 →  4
[1] : 0 →  2 →  4
[2] : 0 →  1 →  3
[3] : 1 →  4
[4] : 0 →  1 →  3``````
``````#    Python 3 Program
class Graph :
def __init__(self, matrix) :
self.vertices = len(matrix)
i = 0
while (i < self.vertices) :
i += 1

if (u < 0 or u >= self.vertices or v < 0 or v >= self.vertices) :
return

i = 0
while (i < self.vertices) :
j = 0
while (j < self.vertices) :
if (matrix[i][j] == 1) :

j += 1

i += 1

#  Display graph nodes and edges
def printGraph(self) :
print("\n Graph Adjacency List ", end = "")
i = 0
while (i < self.vertices) :
print(" \n [", i ,"] : ", end = "",sep = "")
j = 0
#  iterate edges of i node
if (j != 0) :
print(" → ", end = "",sep = "")

print("", self.adgeList[i][j], end = "",sep = "")
j += 1

i += 1

def main() :
matrix = [
[0, 1, 1, 0, 1],
[1, 0, 1, 0, 1],
[1, 1, 0, 1, 0],
[0, 1, 0, 0, 1],
[1, 1, 0, 1, 0]
]
g = Graph(matrix)
#  Display graph element
g.printGraph()

if __name__ == "__main__": main()``````

#### Output

`````` Graph Adjacency List
[0] : 1 → 2 → 4
[1] : 0 → 2 → 4
[2] : 0 → 1 → 3
[3] : 1 → 4
[4] : 0 → 1 → 3``````
``````#    Ruby Program
class Graph
# Define the accessor and reader of class Graph
def initialize(matrix)
self.vertices = matrix.length
i = 0
while (i < self.vertices)
i += 1
end

end

if (u < 0 || u >= self.vertices ||
v < 0 || v >= self.vertices)
return
end

end

i = 0
while (i < self.vertices)
j = 0
while (j < self.vertices)
if (matrix[i][j] == 1)
end

j += 1
end

i += 1
end

end

#  Display graph nodes and edges
def printGraph()
i = 0
while (i < self.vertices)
print(" \n [", i ,"] :")
j = 0
#  iterate edges of i node
if (j != 0)
print(" → ")
end

j += 1
end

i += 1
end

end

end

def main()
matrix = [
[0, 1, 1, 0, 1],
[1, 0, 1, 0, 1],
[1, 1, 0, 1, 0],
[0, 1, 0, 0, 1],
[1, 1, 0, 1, 0]
]
g = Graph.new(matrix)
#  Display graph element
g.printGraph()
end

main()``````

#### Output

`````` Graph Adjacency List
[0] : 1 →  2 →  4
[1] : 0 →  2 →  4
[2] : 0 →  1 →  3
[3] : 1 →  4
[4] : 0 →  1 →  3``````
``````import scala.collection.mutable._;
/*
Scala Program
*/
class Graph(var vertices: Int,
{
def this(matrix: Array[Array[Int]])
{
this(matrix.length, new ArrayBuffer[ArrayBuffer[Int]](matrix.length))
var i: Int = 0;
while (i < this.vertices)
{
i += 1;
}

}
def addEdge(u: Int, v: Int): Unit = {
if (u < 0 || u >= this.vertices || v < 0 || v >= this.vertices)
{
return;
}
}
def makeAdjacencyList(matrix: Array[Array[Int]]): Unit = {
var i: Int = 0;
while (i < this.vertices)
{
var j: Int = 0;
while (j < this.vertices)
{
if (matrix(i)(j) == 1)
{
}
j += 1;
}
i += 1;
}
}
// Display graph nodes and edges
def printGraph(): Unit = {
var i: Int = 0;
while (i < this.vertices)
{
print(" \n [" + i + "] :");
var j: Int = 0;
// iterate edges of i node
{
if (j != 0)
{
print(" → ");
}
j += 1;
}
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var matrix: Array[Array[Int]] = Array(
Array(0, 1, 1, 0, 1),
Array(1, 0, 1, 0, 1),
Array(1, 1, 0, 1, 0),
Array(0, 1, 0, 0, 1),
Array(1, 1, 0, 1, 0)
);
var g: Graph = new Graph(matrix);
// Display graph element
g.printGraph();
}
}``````

#### Output

`````` Graph Adjacency List
[0] : 1 →  2 →  4
[1] : 0 →  2 →  4
[2] : 0 →  1 →  3
[3] : 1 →  4
[4] : 0 →  1 →  3``````
``````import Foundation;
/*
Swift 4 Program
*/
class Graph
{
var vertices: Int;
init(_ matrix: [[Int]])
{
self.vertices = matrix.count;
var i: Int = 0;
while (i < self.vertices)
{
i += 1;
}
}
func addEdge(_ u: Int, _ v: Int)
{
if (u < 0 || u >= self.vertices || v < 0 || v >= self.vertices)
{
return;
}
}
[Int]
])
{
var i: Int = 0;
while (i < self.vertices)
{
var j: Int = 0;
while (j < self.vertices)
{
if (matrix[i][j] == 1)
{
}
j += 1;
}
i += 1;
}
}
// Display graph nodes and edges
func printGraph()
{
print("\n Graph Adjacency List ", terminator: "");
var i: Int = 0;
while (i < self.vertices)
{
print(" \n [", i ,"] : ",separator :"", terminator: "");
var j: Int = 0;
// iterate edges of i node
{
if (j  != 0)
{
print(" → ",separator :"", terminator: "");
}
j += 1;
}
i += 1;
}
}
}
func main()
{
let matrix: [
[Int]
] = [
[0, 1, 1, 0, 1],
[1, 0, 1, 0, 1],
[1, 1, 0, 1, 0],
[0, 1, 0, 0, 1],
[1, 1, 0, 1, 0]
];
let g: Graph = Graph(matrix);
// Display graph element
g.printGraph();
}
main();``````

#### Output

`````` Graph Adjacency List
[0] :  1 →  2 →  4
[1] :  0 →  2 →  4
[2] :  0 →  1 →  3
[3] :  1 →  4
[4] :  0 →  1 →  3``````
``````/*
Kotlin Program
*/
class Graph
{
var vertices: Int;
var adgeList: MutableList < MutableList < Int >>  ;
constructor(matrix: Array < Array < Int >> )
{
this.vertices = matrix.count();
var i: Int = 0;
while (i < this.vertices)
{
i += 1;
}
}
fun addEdge(u: Int, v: Int): Unit
{
if (u < 0 || u >= this.vertices ||
v < 0 || v >= this.vertices)
{
return;
}
}
fun makeAdjacencyList(matrix: Array < Array < Int >> ): Unit
{
var i: Int = 0;
while (i < this.vertices)
{
var j: Int = 0;
while (j < this.vertices)
{
if (matrix[i][j] == 1)
{
}
j += 1;
}
i += 1;
}
}
// Display graph nodes and edges
fun printGraph(): Unit
{
var i: Int = 0;
while (i < this.vertices)
{
print(" \n [" + i + "] : ");
var j: Int = 0;
// iterate edges of i node
{
if (j != 0)
{
print(" → ");
}
j += 1;
}
i += 1;
}
}
}
fun main(args: Array < String > ): Unit
{
val matrix: Array < Array < Int >> = arrayOf(arrayOf(0, 1, 1, 0, 1), arrayOf(1, 0, 1, 0, 1), arrayOf(1, 1, 0, 1, 0), arrayOf(0, 1, 0, 0, 1), arrayOf(1, 1, 0, 1, 0));
val g: Graph = Graph(matrix);
// Display graph element
g.printGraph();
}``````

#### Output

`````` Graph Adjacency List
[0] : 1 → 2 → 4
[1] : 0 → 2 → 4
[2] : 0 → 1 → 3
[3] : 1 → 4
[4] : 0 → 1 → 3``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.