0% found this document useful (0 votes)
25 views3 pages

Graph

Uploaded by

Rithin D Almeida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views3 pages

Graph

Uploaded by

Rithin D Almeida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

//Author Name: huda fathima

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

You might also like