Skip to content

Commit

Permalink
Don't store graph pointer in every node
Browse files Browse the repository at this point in the history
Instead we just pass it through to all functions that need it.
  • Loading branch information
aaronpuchert committed Oct 20, 2018
1 parent d7c239e commit ebfb7a1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
7 changes: 3 additions & 4 deletions include/floodit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ class State

/**
* Do a move.
* @param graph Graph to be based on.
* @param next Color for move.
* @return True, if the move makes sense.
*/
bool move(color_t next);
bool move(const Graph &graph, color_t next);

/**
* Get valuation of the state.
Expand All @@ -120,11 +121,9 @@ class State
bool done() const;

private:
int computeValuation() const;
int computeValuation(const Graph &graph) const;

private:
const Graph *graph;

std::vector<bool> filled;
std::vector<color_t> moves;
int valuation;
Expand Down
28 changes: 14 additions & 14 deletions src/floodit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ void Graph::reduce()
}

State::State(const Graph &graph)
: graph(&graph), filled(graph.getNumNodes(), false),
: filled(graph.getNumNodes(), false),
moves(1, graph.getNode(graph.getRootIndex()).color)
{
// Check that the graph is reduced. We are going to assume that later.
Expand All @@ -110,10 +110,10 @@ State::State(const Graph &graph)
}

filled[graph.getRootIndex()] = true;
valuation = computeValuation();
valuation = computeValuation(graph);
}

bool State::move(color_t next)
bool State::move(const Graph &graph, color_t next)
{
assert(next != moves.back());

Expand All @@ -125,8 +125,8 @@ bool State::move(color_t next)
// Does the move change anything?
bool expansion = false;
for (unsigned node = 0; node < filled.size(); ++node)
if (graph->getNode(node).color == next && !filled[node])
for(unsigned neighbor : graph->getNode(node).neighbors)
if (graph.getNode(node).color == next && !filled[node])
for(unsigned neighbor : graph.getNode(node).neighbors)
if (filled[neighbor])
{
filled[node] = true;
Expand All @@ -141,15 +141,15 @@ bool State::move(color_t next)
// Does the move change anything that couldn't have happened before?
bool additionalExpansion = false;
for (unsigned node = 0; node < filled.size(); ++node)
if (graph->getNode(node).color == next && !filled[node])
if (graph.getNode(node).color == next && !filled[node])
{
// Was any of the neighbors filled before the last move?
bool prev = false;
for(unsigned neighbor : graph->getNode(node).neighbors)
for(unsigned neighbor : graph.getNode(node).neighbors)
if (filled[neighbor])
{
filled[node] = true;
if (graph->getNode(neighbor).color != last)
if (graph.getNode(neighbor).color != last)
prev = true;
}
if (filled[node] && !prev)
Expand All @@ -160,11 +160,11 @@ bool State::move(color_t next)
return false;
}

valuation = computeValuation();
valuation = computeValuation(graph);
return true;
}

int State::computeValuation() const
int State::computeValuation(const Graph &graph) const
{
// Bitfield to mark visited nodes (to avoid visiting a node more than once).
std::vector<bool> visited = filled;
Expand All @@ -188,7 +188,7 @@ int State::computeValuation() const
// obviously still a lower bound, hence admissible. It is also consistent.

// The remaining number of nodes for each color.
std::vector<unsigned> colorCounts = graph->getColorCounts();
std::vector<unsigned> colorCounts = graph.getColorCounts();
// The remaining number of colors.
unsigned numColors = colorCounts.size();
unsigned max = 0;
Expand All @@ -197,9 +197,9 @@ int State::computeValuation() const
// Expand current layer of nodes.
for (unsigned node : current)
{
if (--colorCounts[graph->getNode(node).color] == 0)
if (--colorCounts[graph.getNode(node).color] == 0)
--numColors;
for (unsigned neighbor : graph->getNode(node).neighbors)
for (unsigned neighbor : graph.getNode(node).neighbors)
{
// If we didn't visit the node yet, it has distance = r+1.
if (!visited[neighbor])
Expand Down Expand Up @@ -252,7 +252,7 @@ std::vector<color_t> computeBestSequence(const Graph &graph)
continue;

State nextState = state;
if (nextState.move(next))
if (nextState.move(graph, next))
queue.push(std::move(nextState));
}
}
Expand Down

0 comments on commit ebfb7a1

Please sign in to comment.