......................

Program to Display Adjacency Matrix (using Boolean Matrix)

Posted by Unknown On Friday, October 15, 2010 0 comments

/***************************************************************************
--> Suppose G is a simple directed graph with m nodes, and suppose the nodes of G have been ordered and are called v1,v2,...,vm. Then the adjacency matrix, A = (aij) of the graph G is the m*m matrix defined as follows:

                  aij = {1 if vi is adjacent to vj, that is, if there is an edge (vi,vj)
                        {0 otherwise

such a matrix A , which contains entries of only 0 and 1, is called a Bit or Boolean Matrix.
****************************************************************************/

#include "iostream.h"
#include "conio.h"

struct Adjacency_Matrix
{
char Vertices[30]; //Name of Vertices
int Adjacent[30][30]; //Adjacency Matrix
};

int main()
{
int m;
Adjacency_Matrix a;
cout << "How Many Vertices? "; //Number of Nodes on a Graph
cin >> m;

cout << "\nEnter Vertices Name : "; //Input the name of Vertices
for(int i = 0;i < m;i++)
  cin >> a.Vertices[i];

cout << "\nEnter Boolean Matrix :\n"; //Input Boolean Matrix
for(int i = 0;i < m;i++)
  for(int j = 0;j < m;j++)
   cin >> a.Adjacent[i][j];

cout << endl << "\nThe Adjacency List\n"; //Display Adjacency Matrix
cout << "------------------\n";
for(int i = 0;i < m;i++)
  {
   cout << a.Vertices[i];
   for(int j = 0;j < m;j++)
    {
     if(a.Adjacent[i][j] == 1)
      cout << ' ' << a.Vertices[j];
    }
   cout << endl;
  }

getch();
}

 
OUTPUT

How Many Vertices? 4

Enter Vertices Name : X Y Z W

Enter Boolean Matrix :
0 0 0 1
1 0 1 1
1 0 0 1
0 0 1 0

The Adjacency List
----------------------
X W
Y X Z W
Z X W
W Z

Download Original File

No comments:

Post a Comment

Leave Feedback about this BLOG