C++ FORMAT PLEASE: I need a code that does the following as below directions and
ID: 3848271 • Letter: C
Question
C++ FORMAT PLEASE: I need a code that does the following as below
directions and guidelines:
- Copy the code below into a new header file called Graph.h
- Then, create a new source code file called Graph.cpp.
- Since we are not using templates, we are going to write our function definitions inside of the Graph.cpp.
We will test them in GraphTest.cpp
- We will be using the adjacency list representation of a Graph for this data structure.
- The heart of the Graph data structure will be a vector of linked lists, to store the adjacent vertices of each vertex in the Graph
fill in the code in the comments please!
#include <iostream>
#include <cstdlib>
#include "List.h"
#include <vector>
using namespace std;
class Graph {
public:
/**Constructors and Destructors*/
Graph(int n);
//initializes an empty graph to have n vertices and no edges
/*** Access Functions ***/
int getNumEdges();
//returns the number of edges in the graph
int getNumVertices();
//returns the number of vertices in the graph
bool isEmpty();
//returns whether the graph is empty
/*** Manipulation Procedures ***/
void addEdge(int u, int v);
//Pre: u <= getNumVertices() && v <=getNumVertices()
//inserts vertex v into the adjacency list of vertex u (i.e. inserts v into the list at index u)
//and inserts u into v.
void removeEdge(int u, int v);
//Pre: u <= getNumVertices() && v <=getNumVertices()
//removes vertex v from the adjacency list of vertex u
//and removes u from v.
/*** Additional Operations ***/
void printGraph(ostream& output);
//Prints the adjacency list of each vertex in the graph,
//vertex: <space separated list of adjacent vertices>
//Prints to the console or to an output file given the ostream parameter
private:
int vertices, edges; //number of edges and vertices
vector<List<int> > adj;
};
Explanation / Answer
/*
* C++ Program to Implement Adjacency Matrix
*/
#include <iostream>
#include <cstdlib>
using namespace std;
#define MAX 20
/*
* Adjacency Matrix Class
*/
class AdjacencyMatrix
{
private:
int n;
int **adj;
bool *visited;
public:
AdjacencyMatrix(int n)
{
this->n = n;
visited = new bool [n];
adj = new int* [n];
for (int i = 0; i < n; i++)
{
adj[i] = new int [n];
for(int j = 0; j < n; j++)
{
adj[i][j] = 0;
}
}
}
/*
* Adding Edge to Graph
*/
void add_edge(int origin, int destin)
{
if( origin > n || destin > n || origin < 0 || destin < 0)
{
cout<<"Invalid edge! ";
}
else
{
adj[origin - 1][destin - 1] = 1;
}
}
/*
* Print the graph
*/
void display()
{
int i,j;
for(i = 0;i < n;i++)
{
for(j = 0; j < n; j++)
cout<<adj[i][j]<<" ";
cout<<endl;
}
}
};
/*
* Main
*/
int main()
{
int nodes, max_edges, origin, destin;
cout<<"Enter number of nodes: ";
cin>>nodes;
AdjacencyMatrix am(nodes);
max_edges = nodes * (nodes - 1);
for (int i = 0; i < max_edges; i++)
{
cout<<"Enter edge (-1 -1 to exit): ";
cin>>origin>>destin;
if((origin == -1) && (destin == -1))
break;
am.add_edge(origin, destin);
}
am.display();
return 0;
}
Output :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.