Adjacency matrix representation of graph

Undirected Graph Adjacency Matrix

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
void addEdge(int node[size][size],int start,int end){

    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
void adjacencyNode(int node[size][size]){
    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
        addEdge(node,0,1);
        addEdge(node,0,2);
        addEdge(node,0,3);
        addEdge(node,1,4);
        addEdge(node,2,3);
        addEdge(node,3,4);

        printGraph(node);
        adjacencyNode(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 addEdge(int,int);
    void printGraph();
    void adjacencyNode();
};
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
void Graph::adjacencyNode(){
    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
    g.addEdge(0,1);
    g.addEdge(0,2);
    g.addEdge(0,3);
    g.addEdge(1,4);
    g.addEdge(2,3);
    g.addEdge(3,4);
    g.adjacencyNode();
    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;
    }
    public void addEdge(int start,int end){
        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]);
                }
            }
        }
    }
    public void adjacencyNode(){
        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);
        //Add Edge
        g.addEdge(0,1);
        g.addEdge(0,2);
        g.addEdge(0,3);
        g.addEdge(1,4);
        g.addEdge(2,3);
        g.addEdge(3,4);

        //Display matrix
        g.graphNode();

        g.adjacencyNode();
        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
Java adjacency matrix Undirected Graph
#Python implement Undirected graph using 
#Adjacency Matrix

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)

  def addEdge(self,start,end):
    #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
  def adjacencyNode(self):

    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.addEdge(0,1)
  g.addEdge(0,2)
  g.addEdge(0,3)
  g.addEdge(1,4)
  g.addEdge(2,3)
  g.addEdge(3,4)

  g.printGraph()
  g.adjacencyNode()

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];
	}

	public void addEdge(int start,int end){
		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;
	}
	public void adjacencyNode(){
		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.addEdge(0,1);
		g.addEdge(0,2);
		g.addEdge(0,3);
		g.addEdge(1,4);
		g.addEdge(2,3);
		g.addEdge(3,4);

		g.printGraph();
		g.adjacencyNode();
		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;
          }
      }
    }
  }
  public function addEdge($start,$end){
    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";
      }
    }
  }

  public function adjacencyNode(){
    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
  $g->addEdge(0,1);
  $g->addEdge(0,2);
  $g->addEdge(0,3);
  $g->addEdge(1,4);
  $g->addEdge(2,3);
  $g->addEdge(3,4);

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

  $g->adjacencyNode();


}
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
Directed Graph Adjacency Matrix
//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
void addEdge(int node[size][size],int start,int end){

    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
void adjacencyNode(int node[size][size]){
    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
    
        addEdge(node,0,1);
        addEdge(node,0,3);
        addEdge(node,2,1);
        addEdge(node,2,3);
        addEdge(node,3,4);
        addEdge(node,4,2);
        addEdge(node,4,5);
        addEdge(node,5,2);
        printGraph(node);
        adjacencyNode(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 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
#include<iostream>
using namespace std;

class Graph{
    int **node;
  int size;//number of nodes
public:
    Graph(int);
    void addEdge(int,int);
    void printGraph();
    void adjacencyNode();
    
};
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
void Graph::adjacencyNode(){
    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.addEdge(0,1);
    g.addEdge(0,3);
    g.addEdge(2,1);
    g.addEdge(2,3);
    g.addEdge(3,4);
    g.addEdge(4,2);
    g.addEdge(4,5);

    g.addEdge(5,2);
    
    g.printGraph();
    g.adjacencyNode();
    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
    public void addEdge(int start,int end){
        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]);
                }
            }
        }
    }
    public void adjacencyNode(){
        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);
        //Add Edge
        g.addEdge(0,1);
        g.addEdge(0,3);
        g.addEdge(2,1);
        g.addEdge(2,3);
        g.addEdge(3,4);
        g.addEdge(4,2);
        g.addEdge(4,5);

        g.addEdge(5,2);

        //Display matrix
        g.graphNode();

        g.adjacencyNode();
        g.freeArray();
    }
}
Java adjacency matrix directed Graph

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
#Python implement Directed graph using 
#Adjacency Matrix

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)

  def addEdge(self,start,end):
    #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
  def adjacencyNode(self):

    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.addEdge(0,1);
  g.addEdge(0,3);
  g.addEdge(2,1);
  g.addEdge(2,3);
  g.addEdge(3,4);
  g.addEdge(4,2);
  g.addEdge(4,5);
  g.addEdge(5,2);

  g.printGraph()
  g.adjacencyNode()

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];
	}

	public void addEdge(int start,int end){
		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;
	}
	public void adjacencyNode(){
		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.addEdge(0,1);
		g.addEdge(0,3);
		g.addEdge(2,1);
		g.addEdge(2,3);
		g.addEdge(3,4);
		g.addEdge(4,2);
		g.addEdge(4,5);
		g.addEdge(5,2);

		g.printGraph();
		g.adjacencyNode();
		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;
          }
      }
    }
  }
  public function addEdge($start,$end){
    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>";
      }
    }
  }

  public function adjacencyNode(){
    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
  $g->addEdge(0,1);
  $g->addEdge(0,3);
  $g->addEdge(2,1);
  $g->addEdge(2,3);
  $g->addEdge(3,4);
  $g->addEdge(4,2);
  $g->addEdge(4,5);
  $g->addEdge(5,2);

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

  $g->adjacencyNode();


}
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.

New Comment







© 2021, kalkicode.com, All rights reserved