Calculate the area of the shape created by multiple paths

I’m trying to write an algorithm to calculate the area created by multiple paths that can be overlapping or not. Here is an example:

Example Paths


  • 4 separate paths (A,B,C,D) which are a collection of vertices (A1,A2,…)
  • Area desired is represented by green

Edge Cases

  • As shown with B, a path might have segments that don’t contribute to a filled shape
  • As shown with C, a path might be completely enclosed by other paths and therefore should basically be ignored.
  • As shown with D, paths may create independent shapes
  • As shown with A and B, it should be a union of all the shapes

My first question is if an algorithm for this already exists. If it does, it would save me a lot of effort :). I tried searching around but I don’t even know how to describe this problem concisely.

Assuming one doesn’t exist for this exact purpose I have to move on to figuring it out myself. I’m assuming the right data structure for the job is a graph. I’m thinking I will add points for each intersection (highlighted in red) as I insert paths into the graph.

Then "all I need" is an algorithm for tracing around the outside of each shape because calculating the area of those irregular polygons will be simple. Does something like that already exist? My primary hangups when I think about how to do this are:

  • What vertex do I "start" at?
  • How do I account for multiple shapes (D as well as A,B,C)?
  • How do I account for the parts of shapes like formed by A1,A5,A5 where I’ll be visiting that intersection point multiple times?

I’m not necessarily looking for a complete solution, I’d love thoughts on if you think I’m approaching this the best way so far and if you have any ideas/suggestions on how to achieve this.

Thanks in advance!

Algorithm to figure our deps in a graph that can resolve deps from cache (dictionary) of walked paths

I have a graph like this that starts from one top node and has cycles:

enter image description here

I need to write an algorithm to figure if node1 depends on node2. The most primitive algorithm I’ve written simply starts with node1 and recursively follows all available edges looking for node2 starting from node1. It’s very inefficient because I traverse the graph over and over again. I’m wondering if there’s an algorithm I can look at that caches nodes and dependencies it already walked through so that I don’t go through paths I’ve walked once and can figure if there’s a dependency or not from cache?

For example, if I’m given node D and the question is whether it depends on node F, I’ll walk D->E->F, and when next time I get the question if node E depends on F, I’ll get that from cache without walking the graph.

Thanks for any ideas and suggestions!

Where in the Sargauth Level are the paths into Skullport?

In Waterdeep: Dungeon of the Mad Mage and its Maps and Miscellany, there is a map of the Sargauth level and a map of the levels of Skullport.

Skullport’s maps show the five paths in and out: Shadow Pass, Beggar’s Rest Pass, Taglath’s Gap, Steamfall Pass and Whisperhaunt Pass.

However, the Sargauth Level’s maps only shows two paths labelled "To Skullport" how are these paths connected?

Directed Grid Graphs; All Possible Paths Through Nodes

I have a problem in which I am interested in taking a matrix of positive integer weights, including zero, where the matrix has dimensions nrow x ncol and the columns always sum to the same arbitrary number. I want to search for a list of paths (sets of edges essentially) that traverse through the grid space of the same dimension & create the paths such that the # of edges in a path going through a node is equal to the nodes weight (from the matrix). Ie: if a particular index in my matrix was "3", there would be 3 edges that would run into (and out of) this node.

Some important restrictions. Firstly, The only direction the edges can move is rightward (so vertical edges are disallowed) but only one column distance at a time. I do allow for the edges to go from any row_j (in column_i) to any other possible row_j in column_i+1, so diagonal edges (either upwards or downwards are allowed). Here’s an example of such a solution (it is non singular which is why Im interested in ALL possible paths)

enter image description here

Most importantly, I am interested in two things. First I want all possible paths from this process, and even more critically, I want to minimise the number of possible diagonal edges that my resulting paths will contain.

Any sort of algorithm here to solve this would be hugely helpful.

I have managed to solve the case when I don’t care about the number of diagonal edges, and just want a set of paths that match with the weights. To do that, I used the weights at adjacent columns to generate a Markov transition process. This gave me a series of transition matrices (of length ncol-1) which from there I was able to construct probabilistically what my paths through my weights were.

Let me know if anyone needs any more details/explanations here.

Fastest Algorithm for finding All Pairs Shortest Paths on Sparse Non-Negative Graph

As discussed here Johnson’s Algorithm can be used to solve the APSP-Problem in $ O(V^2\log V + VE)$ in stead of $ O(V^3)$ for Floyd-Warshall. However, Johnsons Algorithm does quite a bit of work for the case that some weights are negative. Is there a faster way to compute this given that all weights are positive?

Are there context free grammars for all restricted Dyck paths?

A Dyck path is a finite list of $ 1$ ‘s and $ -1$ ‘s whose partial sums are nonnegative and whose total sum is $ 0$ . For example, [1, 1, -1, -1] is a Dyck path. Rather than use $ 1$ and $ -1$ , it is common to use "U" and "D" for $ 1$ and $ -1$ , respectively, so we might write UUDD instead. (These might be more familiar as Dyck words.)

It is well-known that Dyck paths have a standard "grammar." The "Dyck grammar" for a path $ P$ is

$ $ P = \epsilon \quad | \quad U P D P.$ $

This grammar is very useful because it lets us quickly compute the generating function which enumerates the number of Dyck paths of given lengths.

I am interested not in all Dyck paths, but restricted sets of Dyck paths. For example, consider the Dyck paths which avoid "peaks" (the bigram UD) at positive even heights; UUUDDD is such a path, while UUDD is not. If we could devise an unambiguous grammar for such paths, then we could write down an equation for the generating function which counts them. (There is such a grammar. It requires two production rules – one for the paths which avoid peaks at even heights, and one for paths which avoid peaks at odd heights.)

This idea leads to a natural question:

Given a (possibly infinite) set of positive integers $ A$ , does there exist a finite, unambiguous context-free grammar which generates precisely the Dyck paths whose peaks avoid $ A$ ?

The answer (I think) is "yes" when $ A$ is finite or an arithmetic progression. I suspect that the answer is "no" in every other case, but I have no idea how to begin proving this.

For example, here is a special case that I cannot decide:

Is the set of Dyck paths which avoid peaks at positive squares a context-free language?

Finding paths of length $n$ on a directed graph

Suppose I have the following directed graph

g = Graph[{DirectedEdge[v1, v1, a], DirectedEdge[v1, v1, OverBar[a]],     DirectedEdge[v1, v2, b], DirectedEdge[v2, v2, c],     DirectedEdge[v1, v3, OverBar[b]],     DirectedEdge[v3, v3, OverBar[c]]}, VertexLabels -> "Name",    EdgeLabels -> "Index"] 

First of all, I would prefer to label the edges with the tag of the directed edge, but I’m not sure how to do this.

The main question is, can Mathematica give me all possible paths of length $ n$ on this graph starting from, say, $ v1$ ?

Pack Paths [Concave and Convex]

I would like to design an algorithm to pack closed paths into a rectangle. An example of one of these paths is below:

The rectangle will have a fixed width, but the height will expand to accommodate the paths.

A Simple Approach I Already Tried:

Calculate the smallest rectangle that inscribes the path. (Results were later improved by rotating the path until its width was as small as possible). Pack the paths left to right, and if the remaining width is too small, expand the height and pack on the next row. This approach, however, loses optimizations with concavity. I want optimizations like this to be made:

But with the current approach I get this:

What type of algorithms can be used to optimize both concave and convex shape packing?

How to find all paths of length up to k between two nodes in a directed graph?

Let’s say I have a directed graph represented as an adjacency matrix, and I’m given two nodes $ u,v$ and a parameter $ k$ . Let’s also say that the maximal degree of a node in the graph is $ d$ .

What is the most efficient algorithm to find all paths of length $ <=k$ from $ u$ to $ v$ ?

I know the number of such paths can be calculated efficiently using Dynamic Programming approach, but I’m looking for an algorithm that allows to get a set of actual paths.

It’s clear that such algorithm would have a factor of the number of paths in its time complexity, which has $ k$ in the exponent, but still, I would like to know what is the best approach for such a problem.