Wednesday 22 November 2017

C++ Program to Represent Graph Using Incidence Matrix


Code:

#include   iostream
#include   iomanip

using namespace std;

// A function to print the incidence matrix.
void PrintMat(int mat[][20], int n, int count)
{
int i, j;

cout<<"\n\n"<
for(i = 0; i < n; i++)
cout<
cout<<"\n\n";

// Print the incidence matrix.
for(i = 0; i < count; i++)
{
cout<

for(j = 0; j < n; j++)
{
cout<
}

cout<<"\n\n";
}
}

int main()
{
int i, j, v, k, count = 0, n, incimat[100][20];

cout<<"Enter the number of vertexes: ";
cin>>v;

// Take input of the adjacency of each pair of vertexes.
for(i = 0; i < v; i++)
{
for(j = i+1; j < v; j++)
{
cout<<"\nEnter 1 if the vertex "<
cin>>n;
if(n == 1)
{
for(k = 0; k < v; k++)
{
if(k == i || k == j)
incimat[count][k] = 1;
else
incimat[count][k] = 0;
}
count++;
cout<<"\t\tIt is edge E("<
}
}
}

PrintMat(incimat, v, count);
}



Output:

Case 1:
Enter the number of vertexes: 3

Enter 1 if the vertex 1 is adjacent to 2, otherwise 0: 1
                It is edge E(1).
Enter 1 if the vertex 1 is adjacent to 3, otherwise 0: 0

Enter 1 if the vertex 2 is adjacent to 3, otherwise 0: 1
                It is edge E(2).

          V(1)    V(2)    V(3)

    E(1)     1     1     0

    E(2)     0     1     1

Case 2:
Enter the number of vertexes: 4

Enter 1 if the vertex 1 is adjacent to 2, otherwise 0: 1
                It is edge E(1).
Enter 1 if the vertex 1 is adjacent to 3, otherwise 0: 1
                It is edge E(2).
Enter 1 if the vertex 1 is adjacent to 4, otherwise 0: 0

Enter 1 if the vertex 2 is adjacent to 3, otherwise 0: 1
                It is edge E(3).
Enter 1 if the vertex 2 is adjacent to 4, otherwise 0: 0

Enter 1 if the vertex 3 is adjacent to 4, otherwise 0: 1
                It is edge E(4).

          V(1)    V(2)    V(3)    V(4)

    E(1)     1     1     0     0

    E(2)     1     0     1     0

    E(3)     0     1     1     0

    E(4)     0     0     1     1



More C++ Programs:















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...