iterate through a 3d matrix, finding all solutions - c++

iterate through a 3d matrix
i need to check every possible solution to a certain predicament.
i have a matrix[x][y][z] that represents possible nodes to travel through. I already finished a method that should give me a set of solutions (it disables a single path every iteration and recalculates the entire solution, disable priority is based on travel capacity of the last solution)
but i need to see how effective my method is in terms of total time taken to calculate a set. For this i require a method calculate the solution on every permutation of these paths.
currently it only has a single layer in between 2 main layers (L1) where 0 is a free path and 1 is a non accessible path.
This here is the starting layout where i can toggle the values on layer L1 from 0 to 1 to disable a path and the basis of my shortest path search algorithm.
L0 0 0 0 0 0 L1 0 1 0 1 0 L2 0 0 0 0 0
0 1 0 1 0 1 1 1 1 1 0 1 0 1 0
0 0 0 0 0 0 1 0 1 0 0 0 0 0 0
0 1 0 1 0 1 1 1 1 1 0 1 0 1 0
0 0 0 0 0 0 1 0 1 0 0 0 0 0 0
how can i iterate through every possible combination of disabling the free paths when the dimensions of the matrix are non constant (meaning they are already user defined on compile time and can be changed whenever)? there are 2^n solutions where n is the number of free path on all mediary layers.
(a quick explanation in C or C++ would be best, even pseudo code is good) since there currently 9 free path to make combinations with there should be about 2^9 solutions which i need to test with. I havent done any brute force algorithms before so i have no idea how to make one.

Related

Retrieve multiple ArrayFire subarrays from min/max data points

I have an array with sections of touching values in it. For example:
0 0 1 0 0 0 0 0 0 0
0 1 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 2 2 2 0 0
0 0 0 0 0 0 0 2 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 3 0 0 0 0 0 0 0
0 0 3 0 0 0 0 0 0 0
0 0 3 0 0 0 0 0 0 0
from this, I created a set of af::arrays: minX, maxX, minY, maxY. These define the box that encloses each group.
so for this example:
minX would be: [1,5,2] // 1 for label(1), 5 for label(2) and 2 for label(3)
maxX would be: [3,7,2] // 3 for label(1), 7 for label(2) and 2 for label(3)
minY would be: [0,3,7] // 0 for label(1), 3 for label(2) and 7 for label(3)
maxY would be: [1,4,9] // 1 for label(1), 4 for label(2) and 9 for label(3)
So if you take the i'th element from each of those arrays, you can get the upperleft/lowerright bounds of a box that encloses the corresponding label.
I would like use these values to pull out subarrays from this larger array. My goal is to put these values enclosed in the boxes into a flat list. In GPU memory, I also have calculated how many entries I would need for each box using the max/min X/Y values. So in this example - the result of the flat list should be:
result=[0 1 0 1 1 1 2 2 2 0 0 2 3 3 3]
where the first 6 entries are from the box
______
|0 1 0 |
|1 1 1 |
------
the second 6 entries are from the box
______
|2 2 2 |
|0 0 2 |
------
and the final three entries are from the box
___
| 3 |
| 3 |
| 3 |
---
I cannot figure out how to index into this af::array with min/max values in memory that resides on the GPU (and do not want to transfer them to the CPU). I was trying to see if gfor/seq would work for me, but it appears that af::seq cannot use array data, and everything I have tried with using af::index i could not get to work for me either.
I am able to change how I represent min/max (I could store indices for upper left/lower right) but my main goal is to do this efficiently on the GPU without moving data back and forth between the GPU and CPU.
How can this be achieved efficiently with ArrayFire?
Thank you for your help
How did you get there so far? which language are you using?
I guess you could be tiling the results to 3rd dimensions to handle each regions separately and end up with min/max vectors in GPU memory.

effective calculate method for RCPSP(resource constrained project scheduling problem)

The problem I am solving is scheduling tasks from limited resources.
The way I thought about it is to use a two-dimensional array to identify resources.
I wonder how I can calculate efficiently because the operation speed is too long.
Using a binary tree is likely to be difficult. After calculation, there is a process of randomly exchanging indexes for the search process.
For example)
Factory's capacity : 4
A(2,2) B(3,2) C(1,1) \\\\task(processing time , required area)
Schedule : A-B-C ,1 means that there is space left, and 0 means that there is no space.
A task can only be allocated if the space required is continuously present.
The x-axis represents time and the y-axis represents capacity.
1 1 1 ... ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ 1 1 1 ㅡㅡㅡㅡㅡㅡㅡ 0 0 0 1 ㅡㅡㅡㅡㅡ 0 0 0 1
1 1 1 ... ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ 1 1 1 ㅡㅡㅡㅡㅡㅡㅡ 0 0 0 1 ㅡㅡㅡㅡㅡ 0 0 0 1
1 1 1 ... ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ 0 0 1 ㅡㅡㅡㅡㅡㅡㅡ 0 0 1 1 ㅡㅡㅡㅡㅡ 0 0 1 1
1 1 1 ... ㅡㅡㅡㅡㅡㅡㅡㅡㅡㅡ 0 0 1 ㅡㅡㅡㅡㅡㅡㅡ 0 0 1 1 ㅡㅡㅡㅡㅡ 0 0 0 1
The good new is your problem looks very similar to well known Job shop scheduling problem. The bad new is Job shop scheduling is NP-hard.

The longest cycle in an undirected graph

How i can get the longest cycle in a undirected Graph (without BackTracking, it takes too long).
Example:
0 3 0 1 0
3 0 0 1 0
0 0 0 0 0
1 1 0 0 0
0 0 0 0 0
Solve: 3 + 3 + 1 => Out: 1 - 2 - 3 - 1.
If you can find the longest cycle, you can detect whether the graph has a Hamiltonian Cycle, which is an NP-complete problem, thus making your problem NP-hard.
That means no solution will be fundamentally better than backtracking unless P=NP.

Setting the number of edges in igraph_erdos_renyi_game c++

I would like to create a directed network graph using igraph c++ in which each node is randomly connected to exactly n other distinct nodes in the network (i.e. excluding connections to itself, and loops/multiple edges to the same loop). I was thinking of using the method igraph_erdos_renyi_game, but for some reason I do not get the desired degree distribution. In particular, if I set the arguments like this:
igraph_erdos_renyi_game(&g, IGRAPH_ERDOS_RENYI_GNM, n, m, true, false);
with n = 5, m = 1, I get this Adjacency Matrix (e.g.):
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 0 0 0 0
0 0 0 0 0
If I use igraph_k_regular_game(&g, n, m, true, false) instead, I get exactly the desired outcome, namely (e.g.):
0 0 0 0 1
1 0 0 0 0
0 0 0 0 1
0 1 0 0 0
0 0 0 1 0
So to summarize, I would like for each node to have 1 edge to n randomly selected agents. Am I misinterpreting the way the Erdos-Renyi method works, or am I passing the wrong arguments?
m is the total number of edges in erdos_renyi_game(). So just use k_regular_game(), it does exactly what you want.

Distance span between two binary strings

Is there any way of finding efficiently (bitwise operations) the distance (not Hamming Distance!) of two 8-bit binary strings?
Each byte is guaranteed to have only one bit set.
Like:
a=0 0 0 0 0 0 0 1
b=0 0 0 1 0 0 0 0
0 0 0 1 0 0 0 1 -> distance = 3
^^^^^
------
a=0 0 0 0 0 1 0 0
b=0 1 0 0 0 0 0 0
0 1 0 0 0 1 0 0 -> distance = 3
^^^^^
------
a=0 1 0 0 0 0 0 0
b=0 0 0 0 0 0 1 0
0 1 0 0 0 0 1 0 -> distance = 4
^^^^^^^
I could work with something like logarithms but that is not very efficient
"Efficient" can mean a different things here: e.g., asymptotic vs performance for a known range of inputs; time vs space; etc.
I'll assume you care about raw speed for the small bounded inputs you describe.
Baseline approach. Take the smaller bit, and left shift it until it's equal to the larger bit, counting shifts. While this is O(n), that sort of analysis doesn't matter here since n is bounded.
You might compare that baseline to either of the following approaches, which have better time complexity but may or may not be faster for your inputs.
Alternative 1. Put all the distances in a lookup matrix. O(1) time complexity, but O(n^2) space complexity.
Alternative 2. Have a lookup table for the logarithms, and return the difference log2(a) - log2(b), where a >= b. O(1) time complexity, O(n) space complexity. (Note that I'm assuming that dist(a, a) = 0, which is a off-by-one from what you describe above.)
I don't know in practice which of those will be faster, but the main point is not to assume that O(n) means that the algorithm is slower in absolute terms for your inputs.
You can use OR operation (logical summing) and then find a maximum amount of zeros, which goes one by one. Hope i get your question right.