Given the following driver file, implement the adjacency matrix functions to pri
ID: 3882914 • Letter: G
Question
Given the following driver file, implement the adjacency matrix functions to print out the following adjacency matrix graphs:
#include <iostream>
using namespace std;
struct Edge
{
int start, end;
};
const int N = 5;
class AdjMat
{
int Vcnt, Ecnt;
int adj[N][N];
bool digraph;
public:
AdjMat(int, bool); //constructor – all values in the adjacency matrix are zero
// and undirected graph
void insert(Edge); //insert edge
void remove(Edge); //remove edge
bool edge(int, int); //check whether the edge is in the graph
bool const directed(); //check whether the graph is directed
void showGraph(); //print adjacent matrix
int V() const; //number of vertices
int E() const; //number of edges
bool directed() const; //check whether digraph
};
int main()
{
int countEdges, countNodes;
AdjMat mat(N, 0);
Edge e1, e2, e3, e4, e5, e6, e7;
//implement all edges
cout << "Before inserting edges into the adjacecy matrix: " << endl;
mat.showGraph();
countNodes = mat.V();
countEdges = mat.E();
cout << "There are " << countNodes << " vertices in the graph. " << endl;
cout << "There are " << countEdges << " edges in the graph. " << endl;
cout << endl;
mat.insert(e1);
mat.insert(e2);
mat.insert(e3);
mat.insert(e4);
mat.insert(e5);
mat.insert(e6);
mat.insert(e7);
cout << "After inserting edges into the adjacecy matrix: " << endl;
mat.showGraph();
countNodes = mat.V();
countEdges = mat.E();
cout << "There are " << countNodes << " vertices in the graph. " << endl;
cout << "There are " << countEdges << " edges in the graph. " << endl;
cout << endl;
mat.remove(e2);
mat.remove(e3);
cout << "After removing edges into the adjacecy matrix: " << endl;
mat.showGraph();
countNodes = mat.V();
countEdges = mat.E();
cout << "There are " << countNodes << " vertices in the graph. " << endl;
cout << "There are " << countEdges << " edges in the graph. " << endl;
cout << endl;
system("pause");
return 0;
}
The output should look as follows:
I need the source code for the implementation of this program. Please and thank you.
CUsers eleel Desktop Test Debugl TestLee.exe Before inserting edges into the adjacecy matrix: 4 0 [01 0101 G1 01 1 [0101 01 01 01 2 [01 0101 G1 01 3 [0101 01 01 01 4 01 0101 G1 01 There are 5 ertices in the graph. There are edges in the graph- After inserting edges into the adjacecy matrix: 4 0 [01 11 01 01 [11 1 1 01 L1 1 L11 2 001 11 01 1 [01 3 01 11 L1 1 L11 4 1 1 01 1 01 There are 5 ertices in the graph. There are edges in the graph. After removing edges into the adjacecy matrix: 4 0 [01 11 01 01 [01 1 1 01 01 1 L11 2 001 1 01 1 [01 3 01 11 L1 1 L11 4 01 1 01 1 01 There are 5 ertices in the graph. There are 5 eges in the graph. Press any key to continue .. .Explanation / Answer
AdjMat :: AdjMat(int a, bool val)
{
int i,j;
for(i=0;i<a;i++)
{
for(j=0;j<a;j++)
adj[i][j] = 0;
}
digraph = val;
Vcnt = N;
Ecnt = 0;
}
void AdjMat:: showGraph()
{
int i,j,k=0,l=0;
for(i=0;i<N;i++)
{
if(i==0)
{
while(k<5)
{
cout<<" ";
cout<<k;
k++;
}
cout<<endl;
}
for(int j=0;j<N;j++)
{
if(j==0)
{
cout<<l<<" ";
l++;
}
cout<<adj[i][j]<<" ";
/* cout<<"N="<<N<<endl;
cout<<"j="<<j<<endl;
cout<<"pass"<<endl;*/
}
//cout<<"pass2"<<endl;
cout<<endl;
}
}
void AdjMat::insert(Edge e1)
{
int p = e1.start;
int q = e1.end;
adj[p][q] = 1;
adj[q][p] = 1;
Ecnt++;
}
void AdjMat :: remove(Edge e1)
{
int p = e1.start;
int q = e1.end;
adj[p][q] = 0;
adj[q][p] = 0;
Ecnt--;
}
int AdjMat:: V() const
{
return Vcnt;
}
int AdjMat :: E() const
{
return Ecnt;
}
bool AdjMat :: directed() const
{
return digraph;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.