Graph
Graph
//registration number:200970020
//program description: representation of undirected and directed graph
//date-of-creation:23-06-2021
#include<iostream>
using namespace std;
#define size 5
class graph
{
int arr[size][size];
int n;
public:
graph(int v)
{
n=v;
}
void init()
{
for(char i='A';i<'A'+n;i++) {
for(char j='A';j<'A'+n;j++) {
arr[i][j]=0;
}
}
}
void addEdge(int src,int dest)
{
arr[src][dest]=1;
}
void printAdjMatrix()
{
cout<<"The Adjacency Matrix is : "<<endl;
for(char i='A';i<'A'+n;i++) {
for(char j='A';j<'A'+n;j++) {
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
}
void D_graph()
{
char s,d;
char op;
init();
cout<<"\nAvailable Vertices are : ";
for(char i='A';i<'A'+n;i++) {
cout<<i<<" ";
do
{
cout<<"\nEnter the source vertex : ";
cin>>s;
cout<<"Enter the destination vertex : ";
cin>>d;
addEdge(s,d);
cout<<"Edge added from "<<s<<" to "<<d;
cout<<"\nDo you want to continue (y/n) : ";
cin>>op;
}while(op=='y' || op=='Y');
printAdjMatrix();
}
void UD_graph()
{
char s,d;
char op;
init();
cout<<"Available Vertices are : ";
for(char i='A';i<'A'+n;i++) {
cout<<i<<" ";
do
{
cout<<"\nEnter the source vertex : ";
cin>>s;
cout<<"Enter the destination vertex : ";
cin>>d;
addEdge(s,d);
addEdge(d,s);
cout<<"Edge added from "<<s<<" to "<<d;
cout<<"\nDo you want to continue (y/n) : ";
cin>>op;
}while(op=='y' || op=='Y');
printAdjMatrix();
}
};
int main()
{
int v,ch;
char op;
cout<<"\nEnter the number of vertices : ";
cin>>v;
graph g(v);
do
{
cout<<"1.Directed Graph\n2.Undirected Graph";
cout<<"\nEnter your choice : ";
cin>>ch;
switch(ch)
{
case 1: g.D_graph();
break;
case 2: g.UD_graph();
break;
}
cout<<"\nDo you want to continue (y/n): ";
cin>>op;
}while(op=='y' || op=='Y');
return 0;
}