Minimal count of the disjoint set partitioning - graph

I am looking for an efficient algorithm (if there is one) to solve the following problem:
Given a set S, whose elements are sets with only two elements. For simplicity, let' s say "two elements" are integers from 1 on. As an example, S can be decribed like: S = {{1, 2}, {2, 3}}. We define R as a radix, which means the integers in the set can not be greater than or equal to R, a bit different from integer 10 in the decimalism. We now define a group G which is merged by the disjoint sets in S with the total count of integers in G less than or equal to R. For example, S = {{1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4}}, R = 4, and then G1 = {{1, 2}, {3, 4}}, G2 = {{1, 3}, {2, 4}}, G3 = {{1, 4}, {2, 3}}. Therefore, in this example, the minimal count of groups is 3.
I want to know if there is any algorithms can efficiently solve this problem with minimal groups. Before posting this problem, I was thinking to transform it into a graph clustering problems, however I find it hard to do with it. I hope I can get some help here, thank you!

I think I must have misunderstood this question, it seems so trivial.
Let
N be the number of sets in S
M be the minimal number of groups in G1, G2, ... GM
then
M = ( 2 * N ) / R, rounded UP to the nearest whole integer
The rounding up is needed because if 2 * N is not divisible by R, then there will one G group with less than R elements.
e.g for your example: M = 2 * 6 / 4 = 3

Related

How to make a diagonal tensor and why doesn't Tensorflow linalg.tensor_diag do that?

What I would consider a diagonal tensor is a tensor t of shape (d1, ..., dr) which is all zero except when the components are equal.
So t[i,j,k,l] = 0 unless i == j == k == l.
A function to create such a tensor should take in a shape (d1, ..., dr) and a vector [a1, ..., ak] of length min(d1, ..., dr), placing these values along the diagonal.
I would like to do this in Tensorflow, and the most relevant function I could find was tf.linalg.tensor_diag, but it doesn't do what I want. For instance, the diagonal input is a tensor, and the output tensor always has twice the rank, and so it can never output tensors of odd rank.
The documentation says "Given a diagonal, this operation returns a tensor with the diagonal and everything else padded with zeros", but I don't know how to square that with its actual behavior.
My question is two parts:
What is the best way in TF to do create what I am calling a diagonal tensor. Is there another name for this?
Why does linalg.tensor_diag work like this? What is the intended use?
Here is an example output:
>>> tf.linalg.tensor_diag([1,2],[3,4]])
<tf.Tensor: shape=(2, 2, 2, 2), dtype=int32, numpy=
array([[[[1, 0],
[0, 0]],
[[0, 2],
[0, 0]]],
[[[0, 0],
[3, 0]],
[[0, 0],
[0, 4]]]], dtype=int32)>```
So this is a little tricky to think about but I'll try to explain the thinking.
If you do tf.linalg.tensor_diag([1,2,3,4]) this is intuitively gives a matrix with that diagonal:
[[1, 0, 0, 0],
[0, 2, 0, 0],
[0, 0, 3, 0],
[0, 0, 0, 4]]
Notice you went from rank 1 to rank 2 doing this, the rank doubled. So to "diagonalize" it's going to end up doubling the rank.
Now your question, tf.linalg.tensor_diag([[1,2],[3,4]]) What you're passing in is a matrix so rank 2
[[1, 2],
[3, 4]]
But now, how should this be diagonalized? So it's rank 2 and following the pattern means we'll end up with something of rank 4. In the previous example diagonalize sort of "pulled up" the vector into the higher rank. And each step of "pulling up" took a single value from the diagonal and put it there.
So this matrix will also be "pulled up" and each step of the way leaving a value. So it's going to make 4 squares of [[0,0],[0,0]] and drop the value in each one. This would give us
[[1,0],
[0,0]]
[[0,2],
[0,0]]
[[0,0],
[3,0]]
[[0,0],
[0,4]]
Lastly things will be "grouped" if they were originally (like [1,2] idk how better to say this) so that gives the final result of
[
[
[[1,0],
[0,0]] ,
[[0,2],
[0,0]]
],
[
[[0,0],
[3,0]] ,
[[0,0],
[0,4]]
]
]
Which indeed gives us a rank 4 result 👍
Note: You may want to look into the other diag function for more of you're trying to do

Plotting incomplete 3D data in contour plot

I have a set of incomplete 3D data. The data looks like the following:
X Y Z
1 1 3
1 2 4
1 3 5
2 2 8
2 3 7
2 4 1
3 1 0
3 4 3
Kindly note that the data is incomplete, since the data points with (1,4), (2,1), (3,2) and (3,3) is missing if I consider my X range as (1,2,3) and Y range as (1,2,3,4). Due to scientific reasons, the data is those points cannot be estimated.
As a result the data cannot be made to a grid which is required by most of the programs for 3D plotting. However, I need to go for a contour plot of the same.
Hence, can you kindly let me know of any software which will enable me to do the same, and also how to get it done? I would personally prefer a contour plot with colours. I do not mind leaving empty boxes in the plot, since my data is incomplete.
Mathematica
data = {
{1, 1, 3}, {1, 2, 4}, {1, 3, 5},
{2, 2, 8}, {2, 3, 7}, {2, 4, 1},
{3, 1, 0}, {3, 4, 3}
};
ListContourPlot[data]

Duplicate structured surface/mesh in gmsh

I'm trying to build a large structure from a simple geometric shape in gmsh and I'd like to use a structured (quadrilateral) grid. I start by creating that shape and then duplicating and translating it as often as needed to build my final structure.
The problem is that even if I define the lines and surfaces of the original shape to be transfinite, this property is lost once I duplicate and translate it. Check this sample code for a square:
Point(1) = {0, 0, 0, 1};
Point(2) = {0, 1, 0, 1};
Point(3) = {1, 1, 0, 1};
Point(4) = {1, 0, 0, 1};
Line(1) = {1, 2};
Line(2) = {2, 3};
Line(3) = {3, 4};
Line(4) = {4, 1};
Line Loop(5) = {1, 2, 3, 4};
Plane Surface(6) = {5};
Transfinite Line {1, 2, 3, 4} = 10 Using Progression 1;
Transfinite Surface {6};
Recombine Surface {6};
Translate {0, 1, 0} {
Duplicata { Surface{6}; }
}
I obtain the original square with a structured grid but the duplicated one does not have this property.
Is there a possibility to retain the structured grid when I copy the surface?
EDIT: It seems that there is indeed no possibility to duplicate a structured volume or surface. The problem is that these properties are directly related to the mesh itself and not the geometry. And the mesh cannot be duplicated.
It is possible.
You can use the GMSH Geometry.CopyMeshingMethod property that is responsible for copying the meshing method for duplicated or translated geometric entities. By default, it is turned off. To turn it on, you can simply add the following line to the beginning of your GEO file.
Geometry.CopyMeshingMethod = 1;
Now, compare:
Tested on GMSH 3.0.5, but should work with any modern version.
This fix (using "Geometry.CopyMeshingMethod = 1;") works unless you use OpenCASCADE to define your geometry.
Try simply to include "SetFactory("OpenCASCADE");" in the beginning of your script and you will see it fails.

Solving a matrix differential equation with Mathematica

I need to solve this equation in Mathematica:
d/dx v(x) = A . v(x)
here v is the column vector {v1(x),v2(x),v3(x),v4(x)} and
A is a 4 x 4 matrix.
I want to solve for the functions v1, v2, v3, v4 with any initial conditions.
The range of x is from 0 to 1000.
How do I write Mathematica code for this type of differential equation using NDSolve?
So, if you have some horrible matrix
A = RandomReal[0.1, {4, 4}]; (* A horrible matrix *)
which we make anti-symmetric (so the solution is oscillatory)
A = A - Transpose#A;
Define the vector of functions and their initial conditions
v[x_] := {v1[x], v2[x], v3[x], v4[x]};
init = v[0] == RandomReal[1, 4]
Then the NDSolve command looks like
sol = NDSolve[LogicalExpand[v'[x] == A.v[x] && init],
{v1, v2, v3, v4}, {x, 0, 1000}]
And the solutions can be plotted with
Plot[Evaluate[v[x] /. sol], {x, 0, 1000}]
Note that that the above differential equation is a linear, first order equation with constant coefficients, so is simply solved using a matrix exponential.
However, if the matrix A was a function of x, then analytic solutions become hard, but the numerical code stays the same.
For example, try:
A = RandomReal[1/10, {4, 4}] - Exp[-RandomReal[1/100, {4, 4}] x^2];
A = A - Transpose#A;
Which can produce solutions like
I wanted to do the same with a matrix instead of a vector v. As long as equation for it can be read correctly without knowing that this symbol represents a vector or a matrix, NDSolve deduced its character from initial condition, however in case when dimensionality of variable is explicit:
M'[t]==a[t]*IdentityMatrix[2]+M[t]
it fails.
An "ordinary" solution is to define matrix explicitly and flatten it when giving as a list of variables.
However I omitted this issue (and many relatex syntax problems) just introducing a reduntant variable which only role is to be the identity matrix but without introducing a list (matrix is 2d list, so Mathematica acts as while adding lists to each other, generating the error):
eqn = {w'[t] == a[t]*identity[t] + w[t], a'[t] == 2, identity'[t] == {{0, 0}, {0, 0}}}
init={ w[0] == {{1, 2}, {2, 1}}, a[0] == 1, identity[0] == {{1, 0}, {0, 1}}}
sol = NDSolve[eqn&&init, {w, a, identity}, {t, 0, 1}]
Some evidence of work:
Plot[{Evaluate[w[t] /. sol][[1, 1, 1]], Evaluate[w[t] /. sol][[1, 1, 2]]}, {t, 0, 1}]
Try something like this (I do not have Mathematica on my home notebook :))
NDSolve[Transpose[{v1[x],v2[x],v3[x],v4[x]}']=={{a11,a12,a13,a14},{a21,a22,a23,a24},{a31,a32,a33,a34},{a41,a42,a43,a44}}.Transpose[{v1[x],v2[x],v3[x],v4[x]}], {v1,v2,v3,v4},{x,0,1000}]
ps: you can rewrite it in a different way, replacing your record as a set of equations
{v1'[x]==a11*v1[x]+a12*v2[x]+a13*v3[x]+a14*v4[x],v2'[x]==a21*v1[x]+a22*v2[x]+a23*v3[x]+a24*v4[x], and so on..} if you want )

Sketching solution curves for differential equations

I have a few differential equations that I'd like to draw solutions for, for a variety of start values N_0
Here are the equations:
dN\dt= bN^2 - aN
dN\dt = bN^2 (1 - N\K) - aN
How would I go about it?
I don't really care about the language is used. In terms of dedicated math I have mathematica and matlab on my computer. I've got access to maple. I have to do more of this stuff, and I'd like to have examples from any language, as it'll help me figure out which one I want to use and learn it.
I'll pretend the first one cannot be solved analytically so as to show how one would go about playing with a general ODE in mathematica.
Define
p1[n0_, a_, b_, uplim_: 10] :=(n /. First#NDSolve[
{n'[t] == b*n[t]^2 - a*n[t], n[0] == n0},n, {t, 0, uplim}]
which returns the solution of the ODE, i.e., a = p1[.1, 2., 3.] and then e.g. a[.3] tells you n(.3). One can then do something like
Show[Table[ans = p1[n0, 1, 1];
Plot[ans[t], {t, 0, 10}, PlotRange \[Rule] Full],
{n0, 0, 1, .05}], PlotRange \[Rule] {{0, 5}, {0, 1}}]
which plots a few solutions with different initial values:
or, to gain some insight into the solutions, one can interactively manipulate the values of a, b and n0:
Manipulate[
ans = p1[n0, a, b];
Plot[ans[t], {t, 0, 10},PlotRange -> {0, 1}],
{{n0, .1}, 0, 1},
{{a, 1}, 0, 2},
{{b, 1}, 0, 2}]
which gives something like
with the controls active (i.e. you move them and the plot changes; try it live to see what I mean; note that you can set parameters for which the initial conditions gives diverging solutions).
Of course this can be made arbitrarily more complicated. Also in this particular case this ODE is easy enough to integrate analytically, but this numerical approach can be applied to generic ODEs (and many PDEs, too).
Adding to the several good answers, if you just want a quick sketch of an ODE's solutions for many starting values, for guidance, you can always do a one-line StreamPlot. Suppose a==1 and b==1, and dy/dx == x^2 - x.
StreamPlot[{1, x^2 - x}, {x, -3, 3}, {y, -3, 3}]
StreamStyle -> "Line" will give you just lines, no arrows.
In Mathematica you use NDSolve (unless it can be solved analytically, in which case you use DSolve. So for your first equation I tried:
b = 1.1; a = 2;
s = NDSolve[{n'[t] == b n[t]^2 - a n[t], n[0] == 1}, n, {t, 0, 10}];
Plot[Evaluate[n[t] /. s], {t, 1, 10}, PlotRange -> All]
I didn't know what to use for a, b or N0, but I got this result:
If you're happy to solve the equations numerically, MATLAB has a set of ODE solvers that might be useful. Check out the documentation for the ode45 function here.
The general approach is to define an "ode function" that describes the right-hand-side of the differential equations. You then pass this function, along with initial conditions and an integration range to the ode solvers.
One attractive feature of this type of approach is that it extends in a straight-forward way to complex systems of coupled ODE's.
Hope this helps.

Resources