## path finding – Pathfinding/waypoint nodes for world map screen

I would like to make a world map similar to the one in Fire Emblem- a bunch of destinations linked by paths. Those being console, it’s fairly easy to set which destination is north, east, south, or west based on the direction pressed on the input. However I want to make it so I can click on a destination with the mouse, no matter how far, and the player plots a route to it, taking into account split paths.

I’ve used A* for my battle code, but would that apply here? Or is there a simpler way to achieve what I need?

I’ve tried googling ‘nodes’, ‘waypoints’ etc but not found anything specific. Is there a term for this style of pathing?

Posted on

## graphs – Finding the smallest-cost way to deliver goods

I want to deliver products from various sources to various destinations such that the overall cost is minimized. However, each supplier (“source”) has a contractual obligation: they specify a minimum and maximum quantity of the product that should be taken from them (i.e. the “minimum” indicates that one must purchase at least that amount, whereas the “maximum” indicates that we must take no more than that amount).

My manager tells me that they have computer code that can solve the capacitated minimum spanning tree problem (see: https://en.wikipedia.org/wiki/Capacitated_minimum_spanning_tree). Using this as a oracle, how can you determine a method to determine which supplier(s) should deliver their products to which destination(s) at a lowest total cost? I’m provided with the costs $$c_{i, j}$$ of transporting from source $$i$$ to destination $$j$$, and I’m also provided with the minimum and maximum quantities of product to be taken from each supplier.

This is a practice problem that I encountered while studying NP completeness and reductions.

I know that I need to somehow construct a graph to pass into the blackbox but coming up with the graph is the tricky part. I thought about constructing a graph with all of the $$c_{i, j}’s$$ as edge weights, but then I can’t get the maximum and minimum quantities to come into play. I’ve thought about this problem for a while now, but I still can’t figure it out. Does anyone have any suggestions? This reduction seems a lot more complicated than the other ones I’ve seen, or maybe I’m just overthinking it?

Posted on

## testing – An effective way of finding regressions

There is a rather large data flow engine – more than 2000 different flow definitions of “what to do with inbound data”. The engine deals with various data formats (flat-file, CSV, JSON, XML, or even binary), performs filtering, transforms data formats, etc.

The flow engine consists of a number of libraries and tools involved in the processing. There are 3rd party ones like Saxon performing XML transformations or Jackson for JSON parsing and a variety of in-house converters, filters, etc.

Naturally, there is a need for deploying new versions of the libraries and tools (new features, security fixes, etc.). This is a risk since correctness of the processing is business-critical.

A simple tests like – unit testing the new feature, integration testing with a fixed set of input data – is not enough. There have been cases of regressions which first appeared after several days after deploy. For example, a very rare combination of numeric values triggered a formatting in a way it triggered a Saxon optimization bug which caused the formatting being omitted.

The currently employed method of testing is to compare new and current versions “online” for several weeks. It’s like adding a “copy & divert” stage resulting in processing the inbound data using both the current and the new version of a tool/utility and then comparing results. This is a very cumbersome, time consuming and potentially risky.

I’ve been thinking about a more effective way of doing this. Any ideas?

Posted on

## unreal – C++ map isn’t finding key even though it exists

I am in the process of learning C++ for the Unreal engine.
I am trying to figure out why when I use the () operator on a map<Vector2, ControlNode> with a value that I know is in the map, it returns nullptr.

Here are the relevant pieces of code:

Vector2

``````**Vector2.h**
class MAGICGAME2D_API Vector2
{
public:
Vector2(float x, float z);
Vector2();
float X;
float Z;

public:
friend bool operator< (const Vector2& c1, const Vector2& c2);

};

**Vector.cpp**
bool operator< (const Vector2& c1, const Vector2& c2)
{
return (c1.X < c2.X ||
c1.Z < c2.Z);
}
``````

Creating the map:

``````for(auto values : *data->getAutomata()) {
auto pos = values.first;
shared_ptr<ControlNode> node = make_shared<ControlNode>(pos,values.second);
controlNodes.emplace(pos,node);
}
``````

And this is the place I make the call to check if the value is in the map:

``````shared_ptr<ControlNode> node = (*(*data).getControlNodes())(pos);
``````

The data variable is a pointer, that points to an object that holds a shared_ptrstd::map<Vector2,shared_ptr<ControlNode>>. So that is why I am double dereferencing that part of the code.

This call to retrieve from the map acts a bit odd. Sometimes it will work. For example, where `pos = Vector2(0,0)`. But then it will fail where `pos = Vector2(0,25)`, and it will return a nullptr. The weird thing is that I have confirmed that `pos = Vector2(0,25)` is already in the map.

Is there something I am doing wrong with the call to retrieve the `shared_ptr<ControlNode>`?

## c++ – Finding sum in an array

I have this program finding 2 numbers in the array (unsorted) so that its sum equal to the target sum. Here is my program:

``````vector<int> findNumbers(vector<int>& nums, int target)
{
vector<int> result;
for(int i = 0; i < nums.size() - 1; i++)
for(int j = i + 1; j < nums.size(); j++)
if(nums(i) + nums(j) == target)
{
result.push_back(i);
result.push_back(j);
return result;
}
return result;
}
``````

But in the last line, the return result doesn’t seem to do anything, because when the numbers are found it is already return before it, but if I remove it, it will cause error. Does the last line slow my program down?

## calculus and analysis – Finding the volume of a graphed function using disks/washers method

The problem I’m working on is the following:

1. Graph the function and then using the method of Disks/Washers find the volume of rotation y = Sin^2(x),y =0,0 <= x <= pi about y=-1.

2. Graph the function and then using the method of Disks/Washers find the volume of the rotation for y = x, y =xe^(1-(x/2)), about y = 3. Use NSolve to find the points of intersection.

I’m not sure where to start using the disks/washers method at all. I’m familiar with graphing the first equation, but not the 2nd. Thanks in advanced guys!

Posted on

## c – Finding the first null character in a file, if I am going to save everything before it in a malloc() block?

I need to know which is a better option, provided they both work as intended. I need to open a file, and load everything up to the first null character into a `malloc()`ed block. There is going to be other data after the first null terminator. The file would look like `<text I need to read><random binary>`. Which option would be better?

First of here’s a `struct`, because actually I need to record both the data and the size.

``````struct DataBlock {
char *Buffer;
unsigned long Size;
};
``````

So this?

``````struct DataBlock MakePreNulltermData(FILE *File) {
unsigned int Len = 0;
while (fgetc(File)) {
Len++;
}
char *const Buffer = malloc(Len+1);
if (!Buffer) {
abort();
}
return (struct DataBlock){Buffer, Len};
}
``````

Or this?

``````struct DataBlock MakePreNulltermData(FILE* File) {
fseek(File, 0, SEEK_END);
unsigned long Size = ftell(File);
rewind(File);
char *Buffer = malloc(Size+1);
if (!Buffer) {
abort();
}
const unsigned long Len = memchr(Buffer, 0, Size)-Buffer;
if (!Buffer = realloc(Buffer, Len)) {
abort();
}
return (struct DataBlock){Buffer, Len};
}
``````

The first one reads the data twice from the file. I don’t know how slow that is, but the second one reads any given byte from the file once. But the second needs a bigger `malloc()` block up front, and then `realloc()`ed to match the data. The size of the pre-nullterm data is completely arbitrary and estimating the size for the purpose of this question is, well, out of the question. Which should I use given the circumstances, can I make either of them more efficient?

Should I count the number of bytes until the first null char, and allocate once or should I read the entire file into a data block, then count the bytes until the first null char IN RAM in the `malloc()` block, then resize the malloc block? Read twice and allocate once, or read once and allocate twice?

## combinatorics – Finding an expression for the number of trees in a full graph with \$n\$ nodes and \$k\$ branches

A full graph is defined as an interconnection of nodes with edges such that all pairs of nodes are joined by exactly $$k$$ edges. The special case of $$k = 1$$ represents a complete graph. Let $$T(k, n)$$ denote the number of trees in a full graph on $$n$$ nodes and $$k$$ edges. How can I find an expression for $$T(k, n)$$?

I think that $$T(1, n) = {{nchoose 2} choose n – 1},$$ but I’m not entirely sure. I think I should try to come up with a recurrence and try to solve the recurrence, but I’ve been having trouble doing so.

Posted on

## algorithms – Finding the most frequent element, given that it’s Theta(n)-frequent?

Very partial answer: At least for $$alpha > 0.5$$, yes.

1. $$text{candidate}$$ <- (null value), $$text{count}$$ <- 0

2. For each element $$x$$ in the array

1. If $$x = text{candidate}$$ then

1. increment $$text{count}$$
2. else

1. If $$text{count} = 0$$

1. $$text{candidate} leftarrow x$$, $$text{count} leftarrow 1$$
2. else

1. decrement $$text{count}$$

The candidate remaining at the end of the array is the majority element. A potential-function argument can show this to be the case (I was taught this in a teaser for an online algorithms class).

This can be extended to $$alpha = 0.5$$ by first finding two distinct elements of the array, then running the above on the array without one of them, then on the array without the other, then finally checking the frequency of the values you get from those two runs.

But – such a trick will probably not work for lower $$alpha$$ values.

Posted on

## natural language – Finding position of specific substring within string

Is there a simple way of finding the position of a specific instance of a substring within a string? For instance, in the sentence “Bob bought his car at the car dealership”. Something like `StringPosition` would give me the position of both instances of car, namely `{{16, 18}, {27, 29}}`, but what if I only wanted the first or the second instance of the word?

If I do a `StringPosition` on every word of the sentece, both instances of the word “car” would yield `{{16, 18}, {27, 29}}`, as opposed to `{16, 18}` for the first instance and `{27, 29}` for the second instance, which is not very useful.

This seems to be a recurrent theme for a lot of Mathematica’s NLP functions when they are applied to text: The functions act on each word, without considering the context of the word within the text. Mathematica just gives you all possible answers for each word.

Posted on