# Adjacency matrix representation of graph

Here given code implementation process.

``````//C Graph represented by Adjacency List
//Undirected graph
#include<stdio.h>
int size=5;
//Set initial values of vertex
void setData(int node[size][size]){
if(size>0){
int row=0,col=0;
for(row;row<size;row++){
for(col=0;col<size;col++){
node[row][col]=0;
}
}
}
}
//Add Edge of two nodes in graph

if(size>start && size>end){
node[start][end]=1;
node[end][start]=1;
}else{
printf("Invalid nodes location\n");
}
}

void printGraph(int node[size][size]){
if(size>0){
int row=0,col=0;
printf(" Graph Nodes");
for(row;row<size;row++){
printf("\n vertex %d: ",row );
for(col=0;col<size;col++){
printf("  %d",node[row][col]);
}
}
}
}
//printing all Adjacency Matrix of vertex by node
if(size>0){
int row=0,col=0;

for(row;row<size;row++){
printf("\n Adjacency Matrix of vertex %d: ",row );
for(col=0;col<size;col++){
if(node[row][col]==1){
printf("  %d",col);
}

}
}
}
}
int main(){
int node[size][size];
//First set node keys
setData(node);
//Connected two node with Edges

printGraph(node);

return 0;
}``````

#### Output

`````` Graph Nodes
vertex 0:   0  1  1  1  0
vertex 1:   1  0  0  0  1
vertex 2:   1  0  0  1  0
vertex 3:   1  0  1  0  1
vertex 4:   0  1  0  1  0
Adjacency Matrix of vertex 0:   1  2  3
Adjacency Matrix of vertex 1:   0  4
Adjacency Matrix of vertex 2:   0  3
Adjacency Matrix of vertex 3:   0  2  4
Adjacency Matrix of vertex 4:   1  3``````
``````//C++ Graph represented by Adjacency Matrix
//Undirected graph
#include<iostream>
using namespace std;

class Graph{
int **node;
int size;//number of nodes
public:
Graph(int);
void printGraph();
};
Graph::Graph(int size){
this->size=size;
//take bool data when use true false
//set number of nodes
node=new int*[size];//
for(int i=0;i<size;i++){
//create inner array
node[i]=new int [size];
//set default value
for(int j=0;j<size;j++){
node[i][j]=0;
}
}
}

//Add Edge from Two given Nodes
void Graph ::addEdge(int start ,int end){
//add edge form start to end
if(start<size && end <size){
node[start][end]=1;
node[end][start]=1;
}else{
//not valid Vertices
cout<<"Invalid Node Vertices "<< start<<" "<<end;
}
}

//printing all Adjacency Matrix of vertex by node
if(size>0){
int row=0,col=0;

for(row;row<size;row++){
cout<<"\nAdjacency Matrix of vertex "<<row <<" :";
for(col=0;col<size;col++){
if(node[row][col]==1){
cout<<"  "<<col;
}

}
}
}
}
int main(){
//Create Object
Graph g(5);

//Connected two node with Edges
return 0;
}``````

#### Output

``````Adjacency Matrix of vertex 0 :  1  2  3
Adjacency Matrix of vertex 1 :  0  4
Adjacency Matrix of vertex 2 :  0  3
Adjacency Matrix of vertex 3 :  0  2  4
Adjacency Matrix of vertex 4 :  1  3``````
``````//Java Graph implemented by Adjacency Matrix
//Undirected graph

public class Graph{
private int [][]node;
private int size;
Graph(int size){
node=new int[size][size];
this.size=size;
}
if(size>start&&size>end){
node[start][end]=1;
node[end][start]=1;
}
}
public void graphNode(){
if(size>0 && node!=null){
for(int row=0;row<size;row++){
System.out.print("\nVertex "+row+" : ");
for(int col=0;col<size;col++){
System.out.print("  "+node[row][col]);
}
}
}
}
if(size>0 && node!=null){
for(int row=0;row<size;row++){
System.out.print("\nAdjacency Matrix of vertex "+row+" :");
for(int col=0;col<size;col++){
if(node[row][col]==1)
System.out.print(" "+col);
}
}
}
}
public void freeArray(){
if(node!=null){
node=null;
}
}
public static void main(String[] args) {
Graph g=new Graph(5);

//Display matrix
g.graphNode();

g.freeArray();
}
}``````

#### Output

``````Vertex 0 :   0  1  1  1  0
Vertex 1 :   1  0  0  0  1
Vertex 2 :   1  0  0  1  0
Vertex 3 :   1  0  1  0  1
Vertex 4 :   0  1  0  1  0
Adjacency Matrix of vertex 0 : 1 2 3
Adjacency Matrix of vertex 1 : 0 4
Adjacency Matrix of vertex 2 : 0 3
Adjacency Matrix of vertex 3 : 0 2 4
Adjacency Matrix of vertex 4 : 1 3``````
``````#Python implement Undirected graph using

class Graph:
"""Constructor for Graph"""
def __init__(self, size):
self.size=size
self.node=[] #empty list

def setData(self):
if(self.size>0 and self.node!=None):
#create list a new list and apped to
for i in range(self.size):
self.node.append([0]*self.size)

#S and E is two nodes
if(self.size>start and self.size>end):
self.node[start][end]=1
self.node[end][start]=1
else:
print("Invalid nodes")

def printGraph(self):
if(self.size>0 and self.node!=None):
index=0
while(index<self.size):
print("\nVertics  ",index,end=" : "),
print(self.node[index],end=" ")
index+=1

#  Display adjacency Node using vertex

if(self.size>0 and self.node!=None):
row=0
col=0
while(row<self.size):
print("\nAdjacency Matrix of vertex ",row,end=" : ")
col=0
while(col<self.size):
if(self.node[row][col]==1):
print(col,end="  ")

col+=1

row+=1

def main():
g=Graph(5)
g.setData()

#Connected two node with Edges

g.printGraph()

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

#### Output

``````Vertics   0 : [0, 1, 1, 1, 0]
Vertics   1 : [1, 0, 0, 0, 1]
Vertics   2 : [1, 0, 0, 1, 0]
Vertics   3 : [1, 0, 1, 0, 1]
Vertics   4 : [0, 1, 0, 1, 0]
Adjacency Matrix of vertex  0 : 1  2  3
Adjacency Matrix of vertex  1 : 0  4
Adjacency Matrix of vertex  2 : 0  3
Adjacency Matrix of vertex  3 : 0  2  4
Adjacency Matrix of vertex  4 : 1  3  ``````
``````//C# Graph represented by Adjacency Matrix
//Undirected graph
using System;

class MyGraph{
//2d array
public int[,]node;
public int size;
public MyGraph(int size){
this.size=size;
node=new int[size,size];
}

if(this.size>start && this.size>end){
node[start,end]=1;
node[end,start]=1;
}
}
public void printGraph(){
if(size>0 && node!=null){
for(int index=0;index<size;index++){
Console.Write("\nvertex {0} :",index);
for(int col=0;col<size;col++){
Console.Write(" {0}",node[index,col]);
}
}
}
}
public void freeNode(){
node=null;
}
if(size>0 && node!=null){
for(int row=0;row<size;row++){
Console.Write("\nAdjacency Matrix of vertex {0} : ",row);
for(int col=0;col<size;col++){
if(node[row,col]==1){
Console.Write(" {0}",col);
}
}
}
}

}
}
class Program{

static void Main(string[] args){
//create object
MyGraph g=new MyGraph(5);

//Connected two node with Edges

g.printGraph();
g.freeNode();
}
}``````

#### Output

``````vertex 0 : 0 1 1 1 0
vertex 1 : 1 0 0 0 1
vertex 2 : 1 0 0 1 0
vertex 3 : 1 0 1 0 1
vertex 4 : 0 1 0 1 0
Adjacency Matrix of vertex 0 :  1 2 3
Adjacency Matrix of vertex 1 :  0 4
Adjacency Matrix of vertex 2 :  0 3
Adjacency Matrix of vertex 3 :  0 2 4
Adjacency Matrix of vertex 4 :  1 3
``````
``````<?php
//Php Graph implemented by Adjacency Matrix
//Undirected graph

class MyGraph{

public \$node;
public \$size;
function __construct(\$size){
\$this->size=\$size;

}
public function setData(){
if(\$this->size>0 ){
for(\$row=0;\$row<\$this->size;\$row++){

for(\$col=0;\$col<\$this->size;\$col++){
\$this->node[\$row][\$col]=0;
}
}
}
}
if(\$this->size>\$start && \$this->size>\$end){
\$this->node[\$start][\$end]=1;
\$this->node[\$end][\$start]=1;
}
}
public function graphNode(){
if(\$this->size>0 && count(\$this->node)>0 ){
echo "\n Graph Element \n";
for(\$row=0;\$row<\$this->size;\$row++){
echo "\n Vertex \$row : ";
for(\$col=0;\$col<\$this->size;\$col++){

echo "  ". \$this->node[\$row][\$col];
}
echo "\n";
}
}
}

if(\$this->size>0 && count(\$this->node)>0){

for(\$row=0;\$row<\$this->size;\$row++){
echo "\nAdjacency Matrix of vertex \$row : " ;
for(\$col=0;\$col<\$this->size;\$col++){
if(\$this->node[\$row][\$col]==1){
echo "  ".\$col;
}

}
}
}
}

}

function main(){
//create object
\$g=new MyGraph(5);
\$g->setData();

//Connected two node with Edges

//Display matrix
\$g->graphNode();

}
main();
?>``````

#### Output

`````` Graph Element

Vertex 0 :   0  1  1  1  0

Vertex 1 :   1  0  0  0  1

Vertex 2 :   1  0  0  1  0

Vertex 3 :   1  0  1  0  1

Vertex 4 :   0  1  0  1  0

Adjacency Matrix of vertex 0 :   1  2  3
Adjacency Matrix of vertex 1 :   0  4
Adjacency Matrix of vertex 2 :   0  3
Adjacency Matrix of vertex 3 :   0  2  4
Adjacency Matrix of vertex 4 :   1  3``````
``````//C Graph represented by Adjacency List
//Directed graph
#include<stdio.h>
int size=6;
//Set initial values of vertex
void setData(int node[size][size]){
if(size>0){
int row=0,col=0;
for(row;row<size;row++){
for(col=0;col<size;col++){
node[row][col]=0;
}
}
}
}
//Add Edge of two nodes in graph

if(size>start && size>end){
node[start][end]=1;
}else{
printf("Invalid nodes location\n");
}
}

void printGraph(int node[size][size]){
if(size>0){
int row=0,col=0;
printf(" Graph Nodes");
for(row;row<size;row++){
printf("\n vertex %d: ",row );
for(col=0;col<size;col++){
printf("  %d",node[row][col]);
}
}
}
}
//printing all Adjacency Matrix of vertex by node
if(size>0){
int row=0,col=0;

for(row;row<size;row++){
printf("\n Adjacency Matrix of vertex %d: ",row );
for(col=0;col<size;col++){
if(node[row][col]==1){
printf("  %d",col);
}

}
}
}
}
int main(){
int node[size][size];
//First set node keys
setData(node);
//Connected two node with Edges

printGraph(node);

return 0;
}``````

#### Output

`````` Graph Nodes
vertex 0:   0  1  0  1  0  0
vertex 1:   0  0  0  0  0  0
vertex 2:   0  1  0  1  0  0
vertex 3:   0  0  0  0  1  0
vertex 4:   0  0  1  0  0  1
vertex 5:   0  0  1  0  0  0
Adjacency Matrix of vertex 0:   1  3
Adjacency Matrix of vertex 2:   1  3
Adjacency Matrix of vertex 3:   4
Adjacency Matrix of vertex 4:   2  5
Adjacency Matrix of vertex 5:   2``````
``````//C++ Graph represented by Adjacency Matrix
//Directed graph
#include<iostream>
using namespace std;

class Graph{
int **node;
int size;//number of nodes
public:
Graph(int);
void printGraph();

};
Graph::Graph(int size){
this->size=size;

//set number of nodes
node=new int*[size];//
for(int i=0;i<size;i++){
//create inner array
node[i]=new int [size];
//set default value
for(int j=0;j<size;j++){
node[i][j]=0;
}
}
}

//Add Edge from Two given Nodes
void Graph ::addEdge(int start ,int end){
//add edge form start to end
if(start<size && end <size){
node[start][end]=1;
}else{
//not valid Vertices
cout<<"Invalid Node Vertices "<< start<<" "<<end;
}
}
void Graph :: printGraph(){
if(size>0){
int row=0,col=0;
cout<<"\n Graph Nodes";
for(row;row<size;row++){
cout<<"\n vertex "<<row<< " : " ;
for(col=0;col<size;col++){
cout<<"  "<<node[row][col];
}
}
}
}
//printing all Adjacency Matrix of vertex by node
if(size>0){
int row=0,col=0;

for(row;row<size;row++){
cout<<"\nAdjacency Matrix of vertex "<<row <<" :";
for(col=0;col<size;col++){
if(node[row][col]==1){
cout<<"  "<<col;
}

}
}
}
}
int main(){
//Create Object
Graph g(6);

//Connected two node with Edges

g.printGraph();
return 0;
}``````

#### Output

`````` Graph Nodes
vertex 0 :   0  1  0  1  0  0
vertex 1 :   0  0  0  0  0  0
vertex 2 :   0  1  0  1  0  0
vertex 3 :   0  0  0  0  1  0
vertex 4 :   0  0  1  0  0  1
vertex 5 :   0  0  1  0  0  0
Adjacency Matrix of vertex 0 :  1  3
Adjacency Matrix of vertex 1 :
Adjacency Matrix of vertex 2 :  1  3
Adjacency Matrix of vertex 3 :  4
Adjacency Matrix of vertex 4 :  2  5
Adjacency Matrix of vertex 5 :  2``````
``````//Java Graph implemented by Adjacency Matrix
//Directed graph

public class Graph{
private int [][]node;
private int size;
Graph(int size){
node=new int[size][size];
this.size=size;
}
//Add directed edge in two node
if(size>start&&size>end){
node[start][end]=1;

}
}
public void graphNode(){
if(size>0 && node!=null){
for(int row=0;row<size;row++){
System.out.print("\nVertex "+row+" : ");
for(int col=0;col<size;col++){
System.out.print("  "+node[row][col]);
}
}
}
}
if(size>0 && node!=null){
for(int row=0;row<size;row++){
System.out.print("\nAdjacency Matrix of vertex "+row+" :");
for(int col=0;col<size;col++){
if(node[row][col]==1)
System.out.print(" "+col);
}
}
}
}
public void freeArray(){
if(node!=null){
node=null;
}
}
public static void main(String[] args) {
Graph g=new Graph(6);

//Display matrix
g.graphNode();

g.freeArray();
}
}``````

#### Output

`````` Graph Nodes
vertex 0:   0  1  0  1  0  0
vertex 1:   0  0  0  0  0  0
vertex 2:   0  1  0  1  0  0
vertex 3:   0  0  0  0  1  0
vertex 4:   0  0  1  0  0  1
vertex 5:   0  0  1  0  0  0
Adjacency Matrix of vertex 0:   1  3
Adjacency Matrix of vertex 2:   1  3
Adjacency Matrix of vertex 3:   4
Adjacency Matrix of vertex 4:   2  5
Adjacency Matrix of vertex 5:   2``````
``````#Python implement Directed graph using

class Graph:
"""Constructor for Graph"""
def __init__(self, size):
self.size=size
self.node=[] #empty list

def setData(self):
if(self.size>0 and self.node!=None):
#create list a new list and apped to
for i in range(self.size):
self.node.append([0]*self.size)

#S and E is two nodes
if(self.size>start and self.size>end):
self.node[start][end]=1

else:
print("Invalid nodes")

def printGraph(self):
if(self.size>0 and self.node!=None):
index=0
while(index<self.size):
print("\nVertices  ",index,end=" : "),
print(self.node[index],end=" ")
index+=1

#  Display adjacency Node using vertex

if(self.size>0 and self.node!=None):
row=0
col=0
while(row<self.size):
print("\nAdjacency Matrix of vertex ",row,end=" : ")
col=0
while(col<self.size):
if(self.node[row][col]==1):
print(col,end="  ")

col+=1

row+=1

def main():
g=Graph(6)
g.setData()

#Connected two node with Edges

g.printGraph()

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

#### Output

``````Vertices   0 : [0, 1, 0, 1, 0, 0]
Vertices   1 : [0, 0, 0, 0, 0, 0]
Vertices   2 : [0, 1, 0, 1, 0, 0]
Vertices   3 : [0, 0, 0, 0, 1, 0]
Vertices   4 : [0, 0, 1, 0, 0, 1]
Vertices   5 : [0, 0, 1, 0, 0, 0]
Adjacency Matrix of vertex  0 : 1  3
Adjacency Matrix of vertex  1 :
Adjacency Matrix of vertex  2 : 1  3
Adjacency Matrix of vertex  3 : 4
Adjacency Matrix of vertex  4 : 2  5
Adjacency Matrix of vertex  5 : 2 ``````
``````//C# Graph represented by Adjacency Matrix
//Directed graph
using System;

class MyGraph{
//2d array
public int[,]node;
public int size;
public MyGraph(int size){
this.size=size;
node=new int[size,size];
}

if(this.size>start && this.size>end){
node[start,end]=1;
}
}
public void printGraph(){
if(size>0 && node!=null){
for(int index=0;index<size;index++){
Console.Write("\nvertex {0} :",index);
for(int col=0;col<size;col++){
Console.Write(" {0}",node[index,col]);
}
}
}
}
public void freeNode(){
node=null;
}
if(size>0 && node!=null){
for(int row=0;row<size;row++){
Console.Write("\nAdjacency Matrix of vertex {0} : ",row);
for(int col=0;col<size;col++){
if(node[row,col]==1){
Console.Write(" {0}",col);
}
}
}
}

}
}
class Program{

static void Main(string[] args){
//create object
MyGraph g=new MyGraph(6);

//Connected two node with Edges

g.printGraph();
g.freeNode();
}
}``````

#### Output

``````vertex 0 : 0 1 0 1 0 0
vertex 1 : 0 0 0 0 0 0
vertex 2 : 0 1 0 1 0 0
vertex 3 : 0 0 0 0 1 0
vertex 4 : 0 0 1 0 0 1
vertex 5 : 0 0 1 0 0 0
Adjacency Matrix of vertex 0 :  1 3
Adjacency Matrix of vertex 1 :
Adjacency Matrix of vertex 2 :  1 3
Adjacency Matrix of vertex 3 :  4
Adjacency Matrix of vertex 4 :  2 5
Adjacency Matrix of vertex 5 :  2
``````
``````<?php
//PHP Graph implemented by Adjacency Matrix
//Directed graph

class MyGraph{

public \$node;
public \$size;
function __construct(\$size){
\$this->size=\$size;

}
public function setData(){
if(\$this->size>0 ){
for(\$row=0;\$row<\$this->size;\$row++){

for(\$col=0;\$col<\$this->size;\$col++){
\$this->node[\$row][\$col]=0;
}
}
}
}
if(\$this->size>\$start && \$this->size>\$end){
\$this->node[\$start][\$end]=1;
}
}
public function graphNode(){
if(\$this->size>0 && count(\$this->node)>0 ){
echo "<br> Graph Element <br>";
for(\$row=0;\$row<\$this->size;\$row++){
echo " Vertex \$row : ";
for(\$col=0;\$col<\$this->size;\$col++){

echo "  ". \$this->node[\$row][\$col];
}
echo "<br>";
}
}
}

if(\$this->size>0 && count(\$this->node)>0){

for(\$row=0;\$row<\$this->size;\$row++){
echo "<br>Adjacency Matrix of vertex \$row : " ;
for(\$col=0;\$col<\$this->size;\$col++){
if(\$this->node[\$row][\$col]==1){
echo "  ".\$col;
}

}
}
}
}

}

function main(){
//create object
\$g=new MyGraph(6);
\$g->setData();

//Connected two node with Edges

//Display matrix
\$g->graphNode();

}
main();
?>``````

#### Output

``````Graph Element
Vertex 0 : 0 1 0 1 0 0
Vertex 1 : 0 0 0 0 0 0
Vertex 2 : 0 1 0 1 0 0
Vertex 3 : 0 0 0 0 1 0
Vertex 4 : 0 0 1 0 0 1
Vertex 5 : 0 0 1 0 0 0

Adjacency Matrix of vertex 0 : 1 3
Adjacency Matrix of vertex 1 :
Adjacency Matrix of vertex 2 : 1 3
Adjacency Matrix of vertex 3 : 4
Adjacency Matrix of vertex 4 : 2 5
Adjacency Matrix of vertex 5 : 2``````

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.