Two Dimensional Array Representing a Weight Matrix - c++

I'm a bit stumped in how a 2d array(Matrix) such as this
0.0 1.8 9.1 4.0 3.5
1.8 0.0 8.1 5.2 8.6
9.1 8.1 0.0 2.9 8.1
4.0 5.2 2.9 0.0 2.0
3.5 8.6 8.1 2.0 0.0
is supposed to represent a graph. The values represent the weights but then what represents the nodes and edges. I'm trying to brute force find all possible tree's (Which I'm not asking for help on that, just trying to understand how this is supposed to represent this

If you have a (weighted, undirected) graph with 5 vertices---call them v1, v2, v3, v4, v5---the graph can be represented by your matrix.
v1 v2 v3 v4 v5
v1 0.0 1.8 9.1 4.0 3.5
v2 1.8 0.0 8.1 5.2 8.6
v3 9.1 8.1 0.0 2.9 8.1
v4 4.0 5.2 2.9 0.0 2.0
v5 3.5 8.6 8.1 2.0 0.0
The number in, say, (v2, v4), represents an edge connecting v2 and v4 with a weight of 5.2. The zero entry could represent non-edges, or edges with zero weight. Non-weighted graphs are usually represented with a boolean value in each entry, 1 representing an edge, 0 representing no edge. The graph is (well, can be) undirected if the matrix is symmetric.
NB: the picture in your question cannot be represented by the given matrix: the matrix represents a graph with 5 vertices, and the graph represented by the picture has 8 vertices.

A 2D array (=matrix) is a common way to represent a graph. It is also called Adjacency Matrix.
In graph theory, an adjacency matrix is a square
matrix used to represent a finite graph. The elements of the matrix
indicate whether pairs of vertices are adjacent or not in the graph.
A matrix M that is NxN represents a graph with N vertices (nodes).
When M[i][j] = 0 there is no edge between vertices i and j.
When M[i][j] = 1 there exist an edge between vertices i and j.
Sometimes it is easy to have a different number than 1 to represent the weight of this specific edge (same as in your case).

Related

Programmatically change the speed of an audio file in real-time

Environment
Hardware: Raspberry Pi x
O.S.: Raspbian Jessie Lite
Language: Qt5 / C++
Goal
Execute an audio file (wav or better mp3) changing its speed smoothly and countinuosly. The pitch should change according to the speed (playback rate).
My application updates several times per second a variable that contains the desired speed: i.e. 1.0 = normal speed. Required range is about 0.2 .. 3.0, with a resolution of 0.01.
The audio is likely music, expected format: mono, 16-bit, 11.025 Hz.
No specific constraints about latency: below 500 ms is acceptable.
Some thougths
QMediaPlayer in QtMultimedia has the playbackRate property that should do exactly this. Unfortunately I have never be able to make QtMultimedia work in my systems.
It's ok to use also an external player, and send data using pipes or any IPC.
How would you achieve this?
I don't know how much of this translates to C++. The work I did on this problem uses Java. Still, something of the algorithm should be of help.
Example data (made up):
sample value
0 0.0
1 0.3
2 0.5
3 0.6
4 0.2
5 -0.1
6 -0.4
With normal speed, we send the output line a series of values where the sample number increments by 1 per output frame.
If we were going slower, say half speed, we should output twice as many values before reaching the same point in the media data. In other words, we need to include, in our output, values that are at the non-existent, intermediate sample frame locations 0.5, 1.5, 2.5, ...
To do this, it turns out that linear interpolation works quite well for audio. It is possible to use a more sophisticated curve fitting algorithm but the increase in fidelity is not considered to be worth the trouble.
So, we end up with a stream as follows (for half speed):
sample value
0 0.0
0.5 0.15
1 0.3
1.5 0.4
2 0.5
2.5 0.55
3 0.6
etc.
If you want to play back 3/4 speed, then the positions and values used in the output would be this:
sample value
0 0.0
0.75 0.225
1.5 0.4
2.25 0.525
3 0.6
3.75 0.525
etc.
I code this via a "cursor" that is incremented each sample frame, where the increment amount determines the "speed" of the playback. The cursor points into an array, like an integer index would, but instead, is a float (or double). If there is a fractional part to the cursor's value, the fraction is used to interpolate between sample values pointed to by the integer part and the integer part plus one.
For example, if the cursor was 6.25, and the value of soundData[6] was A and the value of soundData[6+1] was B, the sound value would be:
audioValue = A * 0.75 + B * 0.25
The degree of precision with which you can define your speed increment is quite high. I think Java's floats are considered sufficient for this purpose.
As for keeping a dynamically changing speed increment smooth, I am spreading out the changes to new speeds over a series of 4096 steps (roughly 1/10th of a second, at 44100 fps). Change requests are often asynchronous, e.g., from a GUI, and are spread out over time in a somewhat unpredictable way. The smoothing algorithm should be able to recalculate and update itself with each new speed request.
Following is a link that demonstrates both strategies, where a sound's playback speed is altered in real time via a slider control.
SlidersTest.jar
This is a runnable copy of the jar file that also contains the source code, and executes via Java 8. You can also rename the file SlidersTest.zip and then drill in to view the source code, in context.
But links to the source files can also be navigated to directly in the two following sections of a page I posted for this code I recently wrote and made open source:
see AudioCue.java
see SlidersTest.java
AudioCue.java is a long file. The relevant parts are in the inner class at the end of the file: class AudioCuePlayer, and for the smoothing algorithm, check the setter method setSpeed which is about 3/4's of the way down. Sorry I don't have line numbers.

CGAL - Boost create undirected graph

I've created a 3D Volume mesh using Cgal's documentation and I was succesfully able to visualize my c3t3 (Complex 3 in triangulation 3) object. The mesh is composed of several connected components the number of which i want to find using boost.
Dealing with c2t3 (cimplex 2 in triangulation 3) in the past i iterated over the vertices of the, make pairs with a vertex_descriptor and then again iterate over the edges finding the vertices and by using add_edge i created the undirected graph and finally return the number of c.c.
Now, the c3t3 object only provides iterators about vertices nad cells (not edges - only can be implicitly found). Can you help me pass the c3t3 object to a graph structure of Boost?
So far i did:
for (Cell_iterator c_it=c3t3.cells_in_complex_begin(); c_it != c3t3.cells_in_complex_end(); ++c_it)
{
Vertex_descriptor vd1 = boost::add_vertex(graph);
Vertex_descriptor vd2 = boost::add_vertex(graph);
Vertex_descriptor vd3 = boost::add_vertex(graph);
Vertex_descriptor vd4 = boost::add_vertex(graph);
C3t3::Vertex_handle v0 = c_it->vertex(0);
C3t3::Vertex_handle v1 = c_it->vertex(1);
C3t3::Vertex_handle v2 = c_it->vertex(2);
C3t3::Vertex_handle v3 = c_it->vertex(3);
vertex_id_map.insert(std::make_pair(v0, vd1));
vertex_id_map.insert(std::make_pair(v1, vd2));
vertex_id_map.insert(std::make_pair(v2, vd3));
vertex_id_map.insert(std::make_pair(v3, vd4));
}
Now i have to create the edges but i don;t know where to find the correct edges correspond to my c3t3 object.. Thank you for your help in advance
CGAL provides "Boost Graph adapters" for many of its classes (at least for triangulations, arrangements and for polyhedral surfaces). See more details in CGAL BGL page.
Essentially all DCEL- or similar structures can be viewed as a combination of two graphs, "primary" for vertex-edge relationships and "dual" for cell-edge relationships; CGAL provides Boost graph adapters for both.
If this out-of-box graphs are not good for you, than you have to answer your central question: how do you iterate over edges of a given cell (i.e. over edges of the "dual" graph) or over edges of a given vertex (i.e. over edges of the "primal" graph).

Calculation of accumulation area

I'm looking for a GIS/Geometric algorithm:
I have 1000 points randomly distributed in a large area(such as a city), How can I find out all the small areas which have more than 15 points? Like this picture below:
Each point has its own latitude and longitude coordinates. The small area less than 200m x 200m.
You should take a look at RTREE structures.
See http://en.wikipedia.org/wiki/R-tree
You've such algorithms implemented e.g. in the SQlite3 engine.
See http://www.sqlite.org/rtree.html
Our Open Source version already includes the RTREE extension for Delphi 6 up to XE, compiled by default since rev. 1.8.
Not sure what your performance requirements are. But a naive implementation would be, for each point, to sum up the inverse of the distance to all other points:
for i := 0 to 999 do
for j := 0 to 999 do
if i<>j then
Point[i].Score := Point[i].Score + ( 1 / Distance(Point[i], Point[j]) );
The points near the center of each accumulation area will have the highest score.

How to generate half-edge structure representation for a polygonal mesh?

I would like to generate output to display the numeric data of the Half-Edge Structure that is based from an input of polygonal mesh data (in numeric data form).
The concept to read the polygonal model basically is like this:
For the INPUT, the file is in OFF format and include datas like
(a) First part: the number of vertex, number of face, and number of edge.
(b) Second part: line of each vertex,
(c) Last part: line of each polygonial face.
Example: (based from above)
First part:
4 4 6
Second part:
-1.7 0.0 1.0
1.7 0.0 1.0
0.0 0.0 -2.0
0.0 3.0 0.0
Last part:
3 0 1 2
3 0 3 2
3 3 2 1
3 0 2 3
And, the program must be able to generate following data:
(a) Vertices:
1. vertex index (Total number of vertex input)
2. each x-coordinate, y-coordinate, z-coordinate, and half edge
(b) Half-Edges:
1. half edge index (Total number of half edge to be generated from a.2. above)
2. starting vertex, face, next half edge, and adjacent half edge.
(c) Faces:
1. face index (Total number of face as shown on b.2. starting from 0)
2. half edge.
The concept is like that, no need to display visual graphic but need to use algorithm and generate simple Half-Edge Structure data, then read the OUTPUT file by using Notepad, etc.
This question consists of two parts:
Reading mesh in OFF file format, which includes reading vertex coordinates
-1.7 0.0 1.0
1.7 0.0 1.0
0.0 0.0 -2.0
0.0 3.0 0.0
and triples of vertex indices per each triangle
3 0 1 2
3 0 3 2
3 3 2 1
3 0 2 3
Half-edge data structure construction from these vertex triples. It is the topic of the question Initializing Half-edge data structure from vertices, where you can find both the explanation and links to exemplar implementations.

How can I generate random samples from bivariate normal and student T distibutions in C++?

what is the best approach to generate random samples from bivariate normal and student T distributions? In both cases sigma is one, mean 0 - so the only parameter I am really interested in is correlation (and degrees of freedom for student t). I need to have the solution in C++, so I can't unfortunately use already implemented functions from MatLab or Mathematica.
You can use the GNU GSL libraries. See here for Bivariate normal:
http://www.gnu.org/software/gsl/manual/html_node/The-Bivariate-Gaussian-Distribution.html
and Student's t-distribution here:
http://www.gnu.org/software/gsl/manual/html_node/The-t_002ddistribution.html
They are straight forward to use.
For a bivariate normal with covariance unity and zero mean, just draw two univariate normals.
If you want to draw a bivariate normal with means (m1, m2), standard deviations (s1, s2) and correlation rho, then draw two unit univariate normals X and Y and set
u = m1 + s1 * X
v = m2 + s2 * (rho X + sqrt(1 - rho^2) Y)
Then u and v are distributed as you wish.
For the Student T, you have to draw a normal variate N and a chi^2 variate V. Then, N / sqrt(V) has T distribution.
To draw the chi^2, you should use a package. Or have a look at Numerical Recipes chapter 7 for how to draw from a Gamma distribution (xhi^2 is a special case of Gamma).
You should take a look at the Boost libraries random distributions - see http://www.boost.org/doc/libs/1_41_0/libs/random/random-distributions.html. I've found them very easy to use, once you wrap your head around their basic concepts. Unfortunately, I don't know enough about statistics to tell you whether they will exactly meet your needs.