I am trying to write an algorithm to check if a graph is connected, (building a board game in which the map is contained as a graph where Regions are Vertices and Borders are edges).
Each region contains a vector of regions that are its neighbors (vector neighbors).
I build the map and check if its connected in the main() function here:
int main()
{
Map map;
Region r1("R1");
Region r2("R2");
Region r3("R3");
r1.addNeighbor(r2);
r2.addNeighbor(r1);
r2.addNeighbor(r3);
r3.addNeighbor(r2);
map.addRegion(r1);
map.addRegion(r2);
map.addRegion(r3);
map.traversal(r1);
map.isConnected();
return 0;
}
And here is my traversal() and isConnected() method implementation:
void Map::traversal(Region currentNode)
{
visited.push_back(currentNode.getRegionName());
Region* current = ¤tNode;
cout << (*current).getRegionName() << " loc: " << current << endl;
for (auto const & neighbor : (currentNode).getNeighbors())
{
if (std::find(visited.begin(), visited.end(), neighbor.getRegionName()) != visited.end()) {
}
else {
cout << (neighbor).getRegionName() << " neighbors: " << (neighbor).getNeighbors().size() << " location: " << &(neighbor) << endl;
traversal(neighbor);
}
}
}
bool Map::isConnected()
{
cout << visited.size() << endl;
cout << regions.size() << endl;
vector<string> regionList;
for (int i = 0; i < regions.size(); i++)
{
regionList.push_back(regions[i].getRegionName());
}
if (visited.size() == regionList.size())
{
return true;
}
else
{
return false;
}
}
The issue I have here is that for some reason, when I get the neighbors of nodes other than the starting node during the recursion of the traversal function, the function for some reason sometimes no longer remembers the neighbors of the current node being traversed (the output of the (neighbor).getNeighbors().size() will sometimes be equal to 0). Also, the address of the currentNode is not always the same as the address of the original object being referenced, leading me to believe that it is copying the object rather than directly pointing to its memory location.
Any help would be appreciated. I am still very new to C++ and the concept of pointers.
Here's the code for my Region class by request: Region.h
#pragma once
#include <string>
#include <vector>
using namespace std;
class Region
{
private:
string owner;
string regionName;
int numTokens;
public:
vector<Region> neighbors;
void setOwner(string playerName);
void setRegionName(string name);
void setNumTokens(int num);
void addNeighbor(Region r);
vector<Region> getNeighbors() const;
string getOwner() const;
string getRegionName() const;
int getNumTokens() const;
Region();
Region(string regionName);
~Region();
};
Region.cpp
#include "stdafx.h"
#include "Region.h"
Region::Region()
{
}
Region::Region(string name)
{
regionName = name;
owner = "none";
numTokens = 0;
}
Region::~Region()
{
}
void Region::setOwner(string playerName)
{
playerName = owner;
}
string Region::getRegionName() const
{
return regionName;
}
int Region::getNumTokens() const
{
return numTokens;
}
void Region::setRegionName(string name)
{
regionName = name;
}
void Region::setNumTokens(int num)
{
numTokens = num;
}
void Region::addNeighbor(Region r)
{
neighbors.push_back(r);
}
vector<Region> Region::getNeighbors() const
{
return neighbors;
}
string Region::getOwner() const
{
return owner;
}