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 cases. Currently it only tests 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;
matrixGraph.add;
matrixGraph.add;
matrixGraph.add;
matrixGraph.add;
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;
listGraph.add;
listGraph.add;
listGraph.add;
listGraph.add;
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 ;
GraphInterface.h:
#ifndef GRAPHINTERFACE
#define GRAPHINTERFACE
template
class GraphInterface
public:
virtual int getNumVertices const ;
virtual int getNumEdges const ;
virtual void addLabelType start, LabelType end, int weight ;
virtual bool removeLabelType start, LabelType end;
virtual int getEdgeWeightLabelType start, LabelType end const ;
virtual void display;
;
#endif
AdjacencyListGraph.h:
#ifndef ADJACENCYLISTGRAPH
#define ADJACENCYLISTGRAPH
#include
#include
#include
#include "GraphInterface.h
template
class AdjacencyListGraph : public GraphInterface
private:
int numVertices;
std::unorderedmap adjacencyList;
public:
AdjacencyListGraphint numVertices : numVerticesnumVertices
int getNumVertices const override
return numVertices;
int getNumEdges const override
int count ;
for const auto& pair : adjacencyList
count pair.second.size;
return count;
void addLabelType start, LabelType end, int weight override
adjacencyListstartpushbackstd::makepairend weight;
To only use directed graphs replace line with the following: adjacencyListendpushbackstd::makepairstart weight;
bool removeLabelType start, LabelType end override
auto& neighbors adjacencyListstart;
for auto it neighbors.begin; it neighbors.end; it
if itfirst end
neighbors.eraseit;
return true;
return false;
int getEdgeWeightLabelType start, LabelType end const override
for const auto& neighbor : adjacencyList.atstart
if neighborfirst end
return neighbor.second;
return std::numericlimits::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 ADJACENCYMATRIXGRAPH
#define ADJACENCYMATRIXGRAPH
#include
#include
#include
#include "GraphInterface.h
template
class AdjacencyMatrixGraph : public GraphInterface
private:
int numVertices;
std::vector matrix;
public:
AdjacencyMatrixGraphint numVertices : numVerticesnumVertices
matrix.assignnumVertices std::vectornumVertices std::numericlimits::max;
int getNumVertices const override
return numVertices;
int getNumEdges const override
int count ;
for int i ; i numVertices; i
for int j ; j numVertices; j
if matrixij std::numericlimits::max
count;
return count;
void addLabelType start, LabelType end, int weight override
matrixstartend weight;
To use undirected graphs uncomment the following: matrixendstart weight;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
