Question: C + + , Visual Studio. I have a project with three headers and a . cpp file. The project is supposed to implement and

C++, Visual Studio. I have a project with three headers and a .cpp file. The project is supposed to implement and test 8 cases. Currently it only tests 4. It needs to test the distinction between directed and undirected adjacency lists and adjacency matrices. After the remaining tests are implemented I would appreciate if you could look at two loops or iterations and prove the correctness of them by commenting a invariant statement and showing where the invariant is true initially, the execution of the loop preserves the invariant, the invariant captures the correctness of the algorithm, and the loop terminates. I would appreciate any help, especially a practical example, and I would greatly appreciate comments so I can learn from your answer.
Driver.cpp:
#include
#include "AdjacencyMatrixGraph.h"
#include "AdjacencyListGraph.h"
int main(){
AdjacencyMatrixGraph matrixGraph(5);
matrixGraph.add(0,1,1);
matrixGraph.add(0,2,2);
matrixGraph.add(1,3,3);
matrixGraph.add(2,4,4);
std::cout << "Adjacency Matrix Graph: "<< std::endl;
matrixGraph.display();
std::cout << "Number of vertices: "<< matrixGraph.getNumVertices()<< std::endl;
std::cout << "Number of edges: "<< matrixGraph.getNumEdges()<< std::endl;
AdjacencyListGraph listGraph(5);
listGraph.add(0,1,1);
listGraph.add(0,2,2);
listGraph.add(1,3,3);
listGraph.add(2,4,4);
std::cout <<"
Adjacency List Graph:" << std::endl;
listGraph.display();
std::cout << "Number of vertices: "<< listGraph.getNumVertices()<< std::endl;
std::cout << "Number of edges: "<< listGraph.getNumEdges()<< std::endl;
return 0;
}
GraphInterface.h:
#ifndef _GRAPH_INTERFACE
#define _GRAPH_INTERFACE
template
class GraphInterface {
public:
virtual int getNumVertices() const =0;
virtual int getNumEdges() const =0;
virtual void add(LabelType start, LabelType end, int weight =1)=0;
virtual bool remove(LabelType start, LabelType end)=0;
virtual int getEdgeWeight(LabelType start, LabelType end) const =0;
virtual void display()=0;
};
#endif
AdjacencyListGraph.h:
#ifndef _ADJACENCY_LIST_GRAPH
#define _ADJACENCY_LIST_GRAPH
#include
#include
#include
#include "GraphInterface.h"
template
class AdjacencyListGraph : public GraphInterface {
private:
int numVertices;
std::unordered_map>> adjacencyList;
public:
AdjacencyListGraph(int numVertices) : numVertices(numVertices){}
int getNumVertices() const override {
return numVertices;
}
int getNumEdges() const override {
int count =0;
for (const auto& pair : adjacencyList){
count += pair.second.size();
}
return count;
}
void add(LabelType start, LabelType end, int weight =1) override {
adjacencyList[start].push_back(std::make_pair(end, weight));
//To only use directed graphs replace line 39 with the following: adjacencyList[end].push_back(std::make_pair(start, weight));
}
bool remove(LabelType start, LabelType end) override {
auto& neighbors = adjacencyList[start];
for (auto it = neighbors.begin(); it != neighbors.end(); ++it){
if (it->first == end){
neighbors.erase(it);
return true;
}
}
return false;
}
int getEdgeWeight(LabelType start, LabelType end) const override {
for (const auto& neighbor : adjacencyList.at(start)){
if (neighbor.first == end){
return neighbor.second;
}
}
return std::numeric_limits::max(); // Edge doesn't exist
}
void display() override {
std::cout << "Adjacency List:" << std::endl;
for (const auto& pair : adjacencyList){
std::cout << pair.first <<": ";
for (const auto& neighbor : pair.second){
std::cout <<"("<< neighbor.first <<","<< neighbor.second <<")";
}
std::cout << std::endl;
}
}
};
#endif
AdjacencyMatrixGraph.h:
#ifndef _ADJACENCY_MATRIX_GRAPH
#define _ADJACENCY_MATRIX_GRAPH
#include
#include
#include
#include "GraphInterface.h"
template
class AdjacencyMatrixGraph : public GraphInterface {
private:
int numVertices;
std::vector> matrix;
public:
AdjacencyMatrixGraph(int numVertices) : numVertices(numVertices){
matrix.assign(numVertices, std::vector(numVertices, std::numeric_limits::max()));
}
int getNumVertices() const override {
return numVertices;
}
int getNumEdges() const override {
int count =0;
for (int i =0; i < numVertices; ++i){
for (int j =0; j < numVertices; ++j){
if (matrix[i][j]!= std::numeric_limits::max()){
count++;
}
}
}
return count;
}
void add(LabelType start, LabelType end, int weight =1) override {
matrix[start][end]= weight;
//To use undirected graphs uncomment the following: matrix[end][start]= weight;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!