Question: I really need help with this assingment in C++. Please see the .ccp file below to utilize. With the cpp file provided, do the following:
I really need help with this assingment in C++. Please see the .ccp file below to utilize. With the cpp file provided, do the following:
Add to the program so that it outputs the nodes of the graph in a depth first
traversal. I also need the output in breadth first traversal.
// Graphs.cpp : Defines the entry point for the console application.
//
// Taken from http://www.algolist.net/Data_structures/Graph/Internal_representation on 11/10/2017
//#include "stdafx.h"
#include
#include
using namespace std;
class Graph {
private:
bool** adjacencyMatrix;
int vertexCount;
public:
Graph(int vertexCount) {
this->vertexCount = vertexCount;
adjacencyMatrix = new bool*[vertexCount];
for (int i = 0; i < vertexCount; i++) {
adjacencyMatrix[i] = new bool[vertexCount];
for (int j = 0; j < vertexCount; j++)
adjacencyMatrix[i][j] = false;
}
}
void addEdge(int i, int j) {
if (i >= 0 && i < vertexCount && j >= 0 && j < vertexCount) { // bdd - changed from j > 0 to j >= 0
adjacencyMatrix[i][j] = true;
adjacencyMatrix[j][i] = true;
}
}
void removeEdge(int i, int j) {
if (i >= 0 && i < vertexCount && j >= 0 && j < vertexCount) { // bdd - changed from j > 0 to j >= 0
adjacencyMatrix[i][j] = false;
adjacencyMatrix[j][i] = false;
}
}
bool isEdge(int i, int j) {
if (i >= 0 && i < vertexCount && j >= 0 && j < vertexCount) // bdd - changed from j > 0 to j >= 0
return adjacencyMatrix[i][j];
else
return false;
}
~Graph() {
for (int i = 0; i < vertexCount; i++)
delete[] adjacencyMatrix[i];
delete[] adjacencyMatrix;
}
};
int main() {
Graph graph(7);
graph.addEdge(0, 1);
graph.addEdge(0, 2);
graph.addEdge(0, 3);
graph.addEdge(1, 0);
graph.addEdge(1, 4);
graph.addEdge(1, 6);
graph.addEdge(2, 0);
graph.addEdge(2, 5);
graph.addEdge(2, 6);
graph.addEdge(3, 0);
graph.addEdge(3, 4);
graph.addEdge(4, 1);
graph.addEdge(4, 3);
graph.addEdge(4, 5);
graph.addEdge(5, 2);
graph.addEdge(5, 4);
graph.addEdge(6, 1);
graph.addEdge(6, 2);
// verify edges
for (int i = 0; i < 7; i++)
for (int j = 0; j < 7; j++)
cout << "Edge " << i << " " << j << " == " << graph.isEdge(i, j) << endl;
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
