C++, Can someone go over a portion of my code for an undirected graph. Without c
ID: 3837500 • Letter: C
Question
C++, Can someone go over a portion of my code for an undirected graph. Without changing the bold, those three functions need to stay as is. The existing functions should stay the same. Only the code that goes inside can be changed.
typedef int Vertex;
class Graph {
public:
Graph(int n); // TO DO
int size(); // TO DO
void addLabel(Vertex i, string s); // TO DO
void addEdge(Vertex i, Vertex j); // TO DO
vector<Vertex> getAdjacentVertices(Vertex); // TO DO
private:
// TO DO
// member variables and functions to implement the public member functions
map<string, Vertex> labelsToInt;
map<Vertex, string> intToLabels;
vector<vector<bool>> adj;
}
// TO DO
Graph::Graph(int n) {
// initialize an undirected graph that can store at most n vertices
// How do you initialize a <vector<vector<bool>> NON DYNAMICALLY?
//The variable is set in private as <vector<vector<bool>> adj;
}
// TO DO
int Graph::size() {
// return the maximum number of vertices
// I think it is:
return adj.size(); //since adj is the matrix of bool <vector<vector<bool>>
}
// TO DO
void Graph::addLabel(Vertex i, string s) {
//give a string label to vertex
//Given an index from the file, it should take the Vertex(which is an int) i and should map it to string s. The values are read from a file already. so it needs to map i and s together.
}
void Graph::addEdge(Vertex i, Vertex j) {
// add an edge between vertices i and j given an instance of Graph class (g)
}
vector<Vertex> Graph::getAdjacentVertices(Vertex n) {
// return a vector of vertices adjacent to vertex n
}
Explanation / Answer
class Graph {
public:
Graph(int n); // TO DO
int size(); // TO DO
void addLabel(Vertex i, string s); // TO DO
void addEdge(Vertex i, Vertex j); // TO DO
vector<Vertex> getAdjacentVertices(Vertex); // TO DO
private:
// TO DO
// member variables and functions to implement the public member functions
int vertex;// store number of vertices in graph
map<string, Vertex> labelsToInt;
map<Vertex, string> intToLabels;
vector<vector<bool>> adj;
}
// TO DO
Graph::Graph(int n) {
vertex = n;
int i,j;
for (int i = 0; i < n; i++) {
vector<int> row; // Create an empty row
for (int j = 0; j < n; j++) {
row.push_back(false); // Add an element (column) to the row
}
adj.push_back(row); // Add the row to the main vector
}
// initialize an undirected graph that can store at most n vertices
// How do you initialize a <vector<vector<bool>> NON DYNAMICALLY?
//The variable is set in private as <vector<vector<bool>> adj;
}
// TO DO
int Graph::size() {
// return the maximum number of vertices
// I think it is:
return vertex;
//return adj.size(); //since adj is the matrix of bool <vector<vector<bool>>
}
// TO DO
void Graph::addLabel(Vertex i, string s) {
//give a string label to vertex
intToLabels[i] = s;
//Given an index from the file, it should take the Vertex(which is an int) i and should map it to string s. The values are read from a file already. so it needs to map i and s together.
}
void Graph::addEdge(Vertex i, Vertex j) {
labelsToInt[s] = i;
// add an edge between vertices i and j given an instance of Graph class (g)
}
vector<Vertex> Graph::getAdjacentVertices(Vertex n) {
std::vector<Vertex> v; // create new vecote of type Vertex
for(i=0;i<vertex;i++){
if(adj[n][i]) // if adjacent of n equal to true it gets inside if block
v.push_back(i); // store it in vector v
}
return v; // return v
// return a vector of vertices adjacent to vertex n
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.