Please finish the codes in Graph.h class.
#################### Vertex.h :
#ifndef VERTEX_H
#define VERTEX_H
#include
#include
#include
using namespace std;
class Vertex
{
private:
int _id;
static int _id_counter;
unordered_map _edges;
//cheater method for tracking path weight
int _path_weight = 0;
public:
Vertex()
{
_id_counter++;
_id = _id_counter;
}
Vertex(int id)
{
if (id >= _id_counter)
{
_id_counter = id + 1;
}
_id = id;
}
int getPathWeight() const
{
return _path_weight;
}
void setPathWeight(int weight)
{
_path_weight = weight;
}
int getId() const
{
return _id;
}
void addEdge(Vertex *vertex, int weight)
{
_edges[vertex] = weight;
}
int getEdgeWeight(Vertex *edge)
{
return _edges[edge];
}
unordered_map &getEdges()
{
return _edges;
}
void removeEdge(Vertex *vertex)
{
_edges.erase(vertex);
}
};
int operator==(const Vertex &lhs, const Vertex &rhs)
{
return lhs.getId() == rhs.getId();
}
bool operator<(const Vertex &lhs, const Vertex &rhs)
{
return lhs < rhs;
}
bool operator>(const Vertex &lhs, const Vertex &rhs)
{
return lhs > rhs;
}
class PathWeightComparer
{
public:
bool operator()(const Vertex lhs, const Vertex rhs)
{
return (lhs.getPathWeight() > rhs.getPathWeight());
}
};
//hashing algorithm must exist in STD namespace
namespace std {
template <>
struct hash
{
//provide a hash (convert grocery item into integer)
std::size_t operator()(const Vertex& item) const
{
size_t hash_val = 0;
//to hash INTs using the STL
hash int_hash{};
//define hashing algorithm. Ours is pretty easy...
hash_val = int_hash(item.getId());
//add others as necessary
return hash_val;
}
};
}
int Vertex::_id_counter = 0;
#endif
#################### Graph.h :
#ifndef GRAPH_H
#define GRAPH_H
#include
#include
#include
#include \"Vertex.h\"
using namespace std;
class Graph
{
unordered_map _vertices;
public:
void addVertex(Vertex vertex)
{
_vertices[vertex.getId()] = vertex;
}
//MA #12 TODO: IMPLEMENT!
unordered_map computeShortestPath(Vertex *start)
{
//holds known distances
unordered_map distances;
//underlying heap
priority_queue, PathWeightComparer> dijkstra_queue{};
//reset start\'s path weight
start->setPathWeight(0);
//make sure that the starting vertex is in the graph
//push on starting vertex
//while queue not empty
//Top of heap not known (in distances)?
//make known
//push on outgoing edges
return distances;
}
};
#endif
######################### main.cpp
#include
#include
#include
#include
#include \"Graph.h\"
#include \"Vertex.h\"
using namespace std;
void graphTest()
{
//populate graph
Graph graph{};
vector vertices{};
//generate vertices
for (int i = 0; i < 4; i++)
{
vertices.push_back(Vertex{});
}
/*
Graph: 0 -> 1 (weight 4)
0 -> 3 (weight 10)
1 -> 2 (weight 4)
1 -> 3 (weight 8)
2 -> 3 (weight 15)
*/
vertices[0].addEdge(&vertices[1], 4);
vertices[0].addEdge(&vertices[3], 20);
vertices[1].addEdge(&vertices[2], 4);
vertices[1].addEdge(&vertices[3], 8);
vertices[2].addEdge(&vertices[3], 15);
//add vertices to graph
for (auto vertex : vertices)
{
graph.addVertex.