Question: Graph.h #pragma once #include #include #include #include #ifndef COP 3 5 3 0 _ PROJECT _ 3 _ GRAPH _ H #define COP 3 5

Graph.h
#pragma once
#include
#include
#include
#include
#ifndef COP3530_PROJECT_3_GRAPH_H
#define COP3530_PROJECT_3_GRAPH_H
using namespace std;
class AdjacencyList
{
private:
//graph structure
unordered_multimap>> graph;
//store lat and long of nodes as pair
unordered_multimap> nodeLocations;
public:
void insertNode(int nodeID, double latitude, double longitude)
{
nodeLocations.insert({ nodeID, {latitude, longitude}});
}
void insertEdge(int fromID, int toID, double weight)
{
graph.insert({ fromID, {{toID, weight}}});
}
//code referenced from Lecture Slides - Mod 8a - Graphs Terminology and Implementation -- slide 63
vector bfs(int startID, int endID)
{
//create containers for bfs
set visited;
queue queue;
unordered_map previous;
//insert and push startID into visited and queue
visited.insert(startID);
queue.push(startID);
//set node previous to start id as -1
previous[startID]=-1;
while (!queue.empty())
{
int u = queue.front();
//checks if u is the same as the endID, add it to the path and keep adding the previous nodes until it reaches the previous[start] which is -1
if (u == endID)
{
vector pathTrace;
for (int i = endID; i !=-1; i = previous[i])
{
pathTrace.push_back(i);
}
//reverse order of path
reverse(pathTrace.begin(), pathTrace.end());
return pathTrace;
}
queue.pop();
//check if element is in the visited set, if its not, add it to vector and queue
vector neighbours;
auto it = graph.equal_range(u);
for (auto& i = it.first; i != it.second; ++i)
{
for (const auto& neighbour : i->second)
{
int v = neighbour.first;
if (visited.count(v)==0)
{
visited.insert(v);
queue.push(v);
}
}
}
}
//no path found return empty
return {};
}
vector dijkstras(int startID, int endID);
unordered_multimap>>& getAdjacencyList()
{
return graph;
}
unordered_multimap> getNodeLoc()
{
return nodeLocations;
}
};
#endif
-----------------------------------------------
Main.cpp
#include
#include "graph.h"
#include
#include
#include
#include
#include
#include
#define pi 3.14159265358979323846
//Earth Radius in Kilometers found online
const double EarthR =6378.0;
//formula to calculate weights based on longitude and latitude
double Formula(double Latitude_1, double Longitude_1, double Latitude_2, double Longitude_2){
double Longitude =(Latitude_2- Latitude_1)* pi /180.0;
double Latitude =(Longitude_2- Longitude_1)* pi /180.0;
Latitude_1= Latitude_1* pi /180.0;
Latitude_2= Latitude_2* pi /180.0;
double Result = sin(Latitude /2)* sin(Latitude /2)+ cos(Latitude_1)* cos(Latitude_2)* sin(Longitude /2)* sin(Longitude /2);
double Result_1=2* atan2(sqrt(Result), sqrt(1- Result));
double final = EarthR * Result_1;
return final;
}
class Drawer : public osmium::handler::Handler {
public:
AdjacencyList& graph;
int Node_Total =0;
const int Node_Max =100000;
Drawer(AdjacencyList& list) : graph(list){}
//override node function
void node(const osmium::Node& nodes){
if (Node_Total < Node_Max){
graph.insertNode(nodes.id(), nodes.location().lat(), nodes.location().lon());
Node_Total++;
cout << "Node ID: "<< nodes.id()<<"\t"<< Node_Total <<"\t";
}
}
//override way function
void way(const osmium::Way& Direct){
const auto& Direction_of_Node = Direct.nodes();
for (size_t x =1; x < Direction_of_Node.size(); ++x){
auto Node_1= Direction_of_Node[x -1].ref();
auto Node_2= Direction_of_Node[x].ref();
if (graph.getNodeLoc().count(Node_1) && graph.getNodeLoc().count(Node_2))
{
auto it1= graph.getNodeLoc().equal_range(Node_1);
auto it2= graph.getNodeLoc().equal_range(Node_2);
for (auto i = it1.first; i != it1.second; i++)
{
for (auto j = it2.first; j != it2.second; j++)
{
double Latitude_1= i->second.first;
double Longitude_1= i->second.second; }}}}};
i am getting a derefrencing error at the end two lines in the main.cpp file for some reason. Everything else works

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!