## Intro

As I mentioned in my previous post, the final approach I want to try is a stack-based DFS implementation. This will certainly be faster than any of the graph library-based approaches, and based on Python’s performance issues around recursive algorithms, might be the fastest approach of all.

## The Code

First things first, here’s the code:

```""" A recursive implementation of the surveying problem
find_reservoirs: Determine the number and location of contiguous reservoirs in a grid
get_neighbors: Find the number of active neighbor elements for a given element
"""
from tools import get_neighbors

METHOD_NAME = "Stack Method (List)"

def find_reservoirs(this_grid):
""" Recursively determines how many wells are needed, making the assumption that
only one well is needed per contiguous field

this_grid: This is the list of locations to be checked for the current reservoir
reservoir: If being called recursively, this contains the current reservoir that is being built
original_grid: If being called recursively, this is the full grid to find neighbor elements
"""

checked_elements = set()
stack = list()
reservoirs = []

remaining_nodes = this_grid

while remaining_nodes:
reservoir = set()
stack.append(remaining_nodes.pop())

while stack:
location = stack.pop()

if location in checked_elements:
continue

stack.extend(get_neighbors(location, this_grid))

reservoirs.append(reservoir)
remaining_nodes -= reservoir

return reservoirs
```

It’s pleasingly concise, especially considering it’s not calling any external libraries. It’s essentially a standard DFS, but since I’m working with a forest instead of a single tree I wrap the main loop iterating through the stack (the tree) with another while loop that iterates through all remaining nodes (the forest). This outer loop ‘seeds’ the stack with an arbitrary item (I just pick the first one in the set), and then when the stack is empty I remove the entire tree from the forest with a set subtraction. Once I’ve processed the final tree, it is subtracted from the forest and the outer `while` loop ends.

One design decision I made at this stage was to use a list as my stack. Python does have a deque1https://docs.python.org/3.8/library/collections.html#collections.deque object that I could have used instead, which is optimized for fast appends and pops and has O(1) performance compared to O(n) performance for lists. I’ll look at that in the next article though.

## Results

As I said last time, I wanted to dive into matplotlib to generate some plots of the data I have been generating. So here’s a bar chart that shows the relative performance of all the different implementations for a range of grid sizes and probability thresholds.

The takeaway from this chart is that the stack method is the fastest implementation for all scenarios, but it’s very close between the stack and recursive approaches. This isn’t surprising, we already know that the time taken to figure out which sites are near which other sites is the dominant factor in all methods. Any performance improvement on the recursive side of things is going to be limited.

## Next Steps

The chart above is a sneak preview of some of the investigations I have started doing into the relative performance of the different DFS algorithms based on grid size and probability threshold. Grid size is a pretty simple scalar, in that beyond a certain point the number of trees scales linearly with the number of available locations. However, the probability threshold is a bit more interesting since it interacts with the clustering algorithm to very rapidly generate very dense forests at still relatively low thresholds.

The next article will be the final one in the series, and will dive into more detail on some of the interesting mathematical features of this problem, and come up with a final determination on the best solution, including looking into list vs deque implmentations of the stack algorithm.

## Update

So I started looking at graph-tools, but unfortunately, I fell at the first hurdle; I couldn’t even get it to compile. Kept running out of memory at the same point, I suppose I should have gone for more than 16 Gb when I upgraded my computer earlier this year.

The solution is to use a docker image that is helpfully provided by the graph-tools maintainer, but in order to do that, I need to learn docker. Making some great process, but it might be another week or so before I can get my Python code running against containerized Python. So while I do that, here’s a post I wrote a few weeks ago but was saving for a rainy day…

## Background

Shortly after the interview where I came up with the DFS solution to the surveying problem I had an amusing thought. The panel said I could use any language I wanted, so I thought to myself, what would the panel have thought if I decided to try and solve the problem in XSLT1https://en.wikipedia.org/wiki/XSLT?

Anyone who knows me knows I have a strange fondness for XSLT. That strange fondness goes back to my first job after my Ph.D., where XSLT was used extensively to transform XML documents. The majority of my colleagues hated that language since it has a trifecta of attributes against it:

1. It’s functional, as opposed to imperative
2. It’s very rarely used
3. It is itself written in XML

I personally always enjoyed writing XSLT; I saw the need to learn a functional programming language as an interesting challenge, and once I got used to thinking in a functional way, the code came naturally. The fact that it was written in XML wasn’t too much of an issue thanks to a decent IDE, the only real issue was that with a (comparatively) small user community, it was often up to me to figure out the best way to do things.

Another important thing to know about XSLT, besides the fact that it’s a functional language, is that it’s a transformation language, i.e. it works by transforming an input document into an output document. An XSLT file can’t do anything by itself, it must be applied to something.

## The Solution

Anyway, enough background. I’ll now go through the code I wrote to solve this problem in XSLT. As I mentioned above, an XSLT file must be applied to an input document, so here’s my (abridged) input document:

```<grid>
<el>
<x>0</x>
<y>16</y>
</el>
<el>
<x>0</x>
<y>17</y>
</el>
<el>
<x>0</x>
<y>19</y>
</el>
<el>
<x>1</x>
<y>3</y>
</el>
<el>
<x>1</x>
<y>4</y>
</el>
</grid>```

It’s a pretty simple document format and required some pretty trivial modifications to the `build_graph()` code from a previous article to spit out XML instead of a Python `set` object. Each `<el>` element represents a location in the set, and is defined by it’s `<x>` and `<y>` child elements.

Next is the XSLT itself:

```<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="xs exsl"
version="1.0">

<xsl:include href="firstPass.xslt" />
<xsl:include href="secondPass.xslt" />

<xsl:output method="xml" indent="yes"/>

<xsl:template match="grid">
<!-- Actually implement the logic -->
<xsl:variable name="firstPass">
<xsl:apply-templates select="el" mode="first" />
</xsl:variable>

<!-- Strip duplicate locations and empty reservoirs -->
<xsl:variable name="reservoirs">
<xsl:apply-templates select="exsl:node-set(\$firstPass)/reservoir" mode="secondPass"/>
</xsl:variable>

<!-- Generate summary results -->
<xsl:element name="results">
<xsl:element name="numberOfReservoirs">
<xsl:value-of select="count(exsl:node-set(\$reservoirs)/reservoir)" />
</xsl:element>
<xsl:element name="reservoirs">
<xsl:apply-templates select="exsl:node-set(\$reservoirs)/reservoir" mode="results" />
</xsl:element>
</xsl:element>
</xsl:template>

<!-- Results wrapper template -->
<xsl:template match="reservoir" mode="results">
<xsl:copy>
<xsl:attribute name="size">
<xsl:value-of select="count(location)" />
</xsl:attribute>
<xsl:copy-of select="location"/>
</xsl:copy>
</xsl:template>

</xsl:stylesheet>```
```<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="xs exsl"
version="1.0">

<!-- First time through we need to create the reservoir element -->
<xsl:template match="el" mode="first">
<xsl:element name="reservoir">

<xsl:element name="location">
<xsl:copy-of select="*" />
</xsl:element>

<xsl:variable name="this_x" select="number(x/text())"/>
<xsl:variable name="this_y" select="number(y/text())"/>

<!-- Register that we have visited this element before -->
<xsl:variable name="visited">
<xsl:copy-of select="." />
</xsl:variable>

<!-- We rely on the grid being sorted in increasing x and y. -->
<!-- The first time through, we will only find a neighbor with increasing or same x -->
<xsl:apply-templates select="following::el[
(x = \$this_x and y = \$this_y + 1) or
(x = \$this_x + 1 and y = \$this_y - 1) or
(x = \$this_x + 1 and y = \$this_y) or
(x = \$this_x + 1 and y = \$this_y + 1)]" mode="recursive">
<xsl:with-param name="visited" select="\$visited" />
</xsl:apply-templates>

</xsl:element>
</xsl:template>

<!-- Subsequent times through we don't, but we do need to check we haven't been here before -->
<xsl:template match="el" mode="recursive">
<xsl:param name="visited" />

<xsl:variable name="currentElement" select="." />

<!-- Check if we have been here before, which stops infinite recursion -->
<xsl:if test="not(exsl:node-set(\$visited)/el[x = \$currentElement/x][y = \$currentElement/y])">

<xsl:variable name="this_x" select="number(x/text())"/>
<xsl:variable name="this_y" select="number(y/text())"/>

<!-- Add the current location to the list of visited locations -->
<xsl:variable name="newVisited">
<xsl:copy-of select="exsl:node-set(\$visited)/el" />
<xsl:copy-of select="\$currentElement" />
</xsl:variable>

<xsl:element name="location">
<xsl:copy-of select="*" />
</xsl:element>

<!-- Apply this template over all neighbor locations -->
<!-- Here we might need to go 'up' and 'left', so we need to check negative offsets -->
<xsl:apply-templates select="../el[
(x = \$this_x - 1 and y = \$this_y - 1) or
(x = \$this_x - 1 and y = \$this_y) or
(x = \$this_x - 1 and y = \$this_y + 1) or
(x = \$this_x and y = \$this_y - 1) or
(x = \$this_x and y = \$this_y + 1) or
(x = \$this_x + 1 and y = \$this_y - 1) or
(x = \$this_x + 1 and y = \$this_y) or
(x = \$this_x + 1 and y = \$this_y + 1)]" mode="recursive">
<xsl:with-param name="visited" select="\$newVisited" />
</xsl:apply-templates>
</xsl:if>
</xsl:template>

</xsl:stylesheet>```
```<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="1.0">

<!-- We need a separator here, otherwise we can't tell the difference between e.g. 1,20 and 12,0 -->
<xsl:key name="locations-by-coords" match="location" use="concat(x, ',', y)" />

<!-- Only match reservoirs that have at least one location after stripping duplicates -->
<xsl:template match="reservoir[count(location[generate-id() = generate-id(key('locations-by-coords', concat(x, ',', y)))]) != 0]" mode="secondPass">
<xsl:copy>
<!-- Iterate over the unique locations in a reservoir -->
<xsl:apply-templates select="location[generate-id() = generate-id(key('locations-by-coords', concat(x, ',', y)))]" mode="secondPass" />
</xsl:copy>
</xsl:template>

<!-- Actually output the location -->
<xsl:template match="location" mode="secondPass">
<xsl:copy-of select="." />
</xsl:template>

</xsl:stylesheet>```

You can see one of the things I hate about XSLT, which is how wordy and bloated the source is! In case you didn’t notice, it’s in three different files just to avoid hundreds and hundreds of lines of scrolling.

The code is executed in two parts. The first is extremely similar to the Python DFS solution, although because XSLT is a functional language and all variables are immutable, there’s no way of determining if a location has already been visited. There are some optimizations that can be made by ensuring the locations are visited in a well-defined order using the `<xsl:sort />` function, but it doesn’t allow the elimination of visiting the same location twice. Consider the following situation:

Even if the list of locations is sorted by increasing x and then increasing y, or even increasing distance from the origin, you still wouldn’t necessarily identify (3,0) as being a part of the well unless you go all the way down to (3,2), and then back up to (3,0). At this point, you may already have started a new well at (3,0) which is a duplicate of the first well. The global ‘visited’ set in the Python implementation solves this problem, but that’s not possible in a functional implementation.

The solution here is to add a second pass through which does two things. The first is that it removes duplicate nodes within the same well, caused when there are multiple possible ways of getting from one active location to another. The second is to remove all reservoirs that are a subset of another reservoir. Through some clever sorting, we can ensure that we will always have the full reservoir before any subsets, which means we can perform this filtering by doing a global filter on unique locations. The code implements Muenchian Grouping2https://en.wikipedia.org/wiki/XSLT/Muenchian_grouping to enable this, which is a neat way of removing duplicate nodes based on arbitrary but definable rules for what constitutes a duplicate within an element.

The final step is then a template that wraps the reservoir results with a summary of how many reservoirs were found in total and adds the size of each reservoir to each reservoir element. An excerpt of the output is given below.

```<?xml version="1.0" encoding="UTF-8"?>
<results>
<numberOfReservoirs>21</numberOfReservoirs>
<reservoirs>
<reservoir size="3">
<location>
<x>0</x>
<y>16</y>
</location>
<location>
<x>0</x>
<y>17</y>
</location>
<location>
<x>1</x>
<y>17</y>
</location>
</reservoir>
<reservoir size="2">
<location>
<x>0</x>
<y>19</y>
</location>
<location>
<x>1</x>
<y>20</y>
</location>
</reservoir>```

## Thoughts

First things first, I know this is a terrible solution. It’s inefficient in that it requires multiple passes to generate the right output, and it fails completely at pretty modestly-sized grids (anything above 50×50 takes too long to complete). It might be possible to perform some improvements, but it’s not something I really care to do.

I realize that XSLT was never designed to perform this kind of work, but then I thought to myself: “how you would go about any kind of efficient BFS implementation without being able to rely on a global ‘visited’ tracker to avoid hitting the same location twice?” Is it even possible to implement an O(n) BFS algorithm in a functional language?

The answer seems to be yes! An article on Stack Overflow3https://stackoverflow.com/questions/30464163/functional-breadth-first-search has a solution in Haskell that seems to do exactly what’s required. I have no experience writing in Haskell, but it seems like an extremely interesting approach. If I get time, it’s something I’ll play around with to see how it works, and whether it makes sense for a problem like this!

Until then, back to docker…

## The Surveying Problem Part 4: Graph Solution

As I mentioned in my previous post, the recursive solution to the Surveying Problem is pretty performant, but I wanted to see if a graph-based solution would be faster or easier to implement.

I started with the NetworkX package1https://networkx.github.io/, which I have used before in some other projects. In my experience, it’s reasonably quick, easy to install, and is released under a BSD license, which is important when you are writing in a commercial environment. Without further ado, here’s the code:

```import networkx as nx

METHOD_NAME = "NetworkX Method"

def get_neighbors(node, graph):
""" Returns a list of neighbor location objects

node: A networkX node. The name is a 2-tuple representing the x and y coordinates of an element
graph: A networkX graph of locations
"""
x_coord, y_coord = node
x_offsets = [-1, 0, 1]
y_offsets = [-1, 0, 1]

neighbors = list()
for x_offset in x_offsets:
for y_offset in y_offsets:
if x_offset == 0 and y_offset == 0:
continue

coord = (x_coord + x_offset, y_coord + y_offset)
if coord in graph:
neighbors.append(coord)
return neighbors

def find_reservoirs(grid):
""" Uses a graph approach to find how many wells are needed, making the assumption that
only one well is needed per contiguous field

grid: Set containing all locations with oil
"""

locations_graph = nx.Graph()

edges_to_create = set()
for node in locations_graph:
neighbors = get_neighbors(node, locations_graph)
_ = [edges_to_create.add((node, neighbor)) for neighbor in neighbors]

connected_subgraphs = nx.connected_components(locations_graph)
wells = [{vertex for vertex in subgraph} for subgraph in connected_subgraphs]

return wells
```

## Pros: Simplicity

First, the pros of this approach. Firstly, the code is conceptually a lot simpler than the recursive approach:

1. Create nodes for every location that has oil
2. Connect nodes together if they are adjacent
3. Call `nx.connected_components()` on our graph
• This generates the set of connected subgraphs
• i.e. the set of subgraphs where every node is connected to every other node in that subgraph
4. List comprehension to get the correct output summary

Another pro is that a lot of the algorithmic design has been done by the NetworkX folks, as opposed to by me. As far as an interview question goes that’s obviously a bad thing since the point of the exercise was to test my problem-solving abilities, not to have an encyclopedic knowledge of which libraries are available. However, in a professional environment, I think it’s always better to use a library and let someone else do the thinking for you.

## Cons: Performance

As for the cons, performance is significantly worse, almost by an order of magnitude. Here’s a summary of the comparison between the recursive solution and the NetworkX-based graph solution:

The obvious question to ask is, why? To answer this question, I looked at the source for the `nx.connected_components()` method.

## Investigating the Slowdown

Behind the scenes, NetworkX is doing something very similar to what I implement previously 2https://github.com/networkx/networkx/blob/master/networkx/algorithms/components/connected.py. `nx.connected_components()` implements a Breadth-First Search3https://en.wikipedia.org/wiki/Breadth-first_search, whereas my recursive code implements a Depth-First Search4https://en.wikipedia.org/wiki/Depth-first_search. A BFS might make sense if we just wanted to find the existence of every well, but since we need to find out every well’s size and complete location, the decision between BFS vs DFS really doesn’t make a difference; the optimal solution is just that we should each location exactly once. The Wikipedia page on connected components5https://en.wikipedia.org/wiki/Connected_component_(graph_theory) states the following:

“It is straightforward to compute the components of a graph in linear time (in terms of the numbers of the vertices and edges of the graph) using either breadth-first search or depth-first search. In either case, a search that begins at some particular vertex v will find the entire component containing v (and no more) before returning. To find all the components of a graph, loop through its vertices, starting a new breadth-first or depth-first search whenever the loop reaches a vertex that has not already been included in a previously found component. Hopcroft & Tarjan (1973) describe essentially this algorithm, and state that at that point it was “well known”.”

https://en.wikipedia.org/wiki/Connected_component_(graph_theory)

In other words, my solution that I gave during my interview was literally the textbook solution to the problem!

But if the algorithms are effectively equivalent, then why the performance difference? I strongly suspect the reason is because of the additional overhead in the NetworkX approach, probably related to the number of additional function calls. Python is notoriously bad with function call latency https://ilovesymposia.com/2015/12/10/the-cost-of-a-python-function-call/, and the poor performance of NetworkX is well documented6https://graph-tool.skewed.de/performance.

Moving to a library that does more of the heavy lifting in a more performant language should enable some significant performance improvements. I’m going to take a look at graph-tool and see how that goes. I have been compiling it while writing this article and it’s still going, so it might be a while before I can start and the code and write the next article. Don’t hold your breath!

## Next Steps

• Implement the code in graph-tools
• Pre-requisite: Compile graph-tools!
• Try and get the NetworkX implementation to work on a grid of 10,000×10,000 elements. I did try this for the table above, but my system ran out of RAM. I might have to switch to a disk-based copy of the grid to generate the graph as I do. This will have a pretty significant performance hit, but might be the only way to get the memory usage down