Minimum cost to connect components of a disconnected graph? - graph-algorithm

We are initially given a fully connected graph by means of an adjacency matrix. Then, some edges are removed such that the graph becomes disconnected and we now have multiple components of this disconnected graph. What is the minimum cost needed to connect all the components?

Let G = (V, E_1 ∪ E_2) be the original (weighted, fully connected) graph and G' = (V, E_1) the graph obtained by removing the edges in the set E_2.
Consider the graph G'' that is obtained by contracting the connected components of G' (i.e., each connected component becomes a single vertex), where two vertices of G'' are neighbours if and only if the corresponding connected components in G' were connected by an edge in E_2. Essentially, this means that the edges of G'' are the edges in the set E_2 (the edges that were removed from the original graph).
Observe that adding a subset of the edges from E_2 to G' restores (full) connectivity of G' if and only if these edges connect all vertices from G''. The cheapest way to do this is by selecting a min-cost spanning tree on G'' (with respect to the weights of the edges). From your comments, I assume you know what a minimum spanning tree is and how it can be computed.
One-sentence-summary:
A cost-minimal set of edges that is needed to restore connectivity can be found by computing a minimum (cost) spanning tree on the graph that is obtained by contracting each of the connected components into a single vertex and that contains, as its edge set, the edges that were removed from the original graph.

Related

Detection of vortex and numbering on basis of rotation

Problem Statement: I have a video of ANSYS Simulation of vortices formed due to flat plate plunging. The video contains vortices (in simpler terms blobs), which are distinguished according to their rotation (based on red or blue colour).
Objective: The vortices are needed to be identified and labelled according to their rotation. For example, vortices having clockwise rotation (red coloured) are labelled as R1, R2, R3, and so on and the anticlockwise rotation (blue coloured) as B1, B2, B3, etc.
Problem: I have been able to identify vortices as either clockwise and anticlockwise using contour detection, but the major issue is of numbering. The identified blob when it goes out of frame the label gets destroyed ie. if the vortex had a label A1 and now goes out of frame this label gets assigned to another vortex in the frame thus loosing the uniqueness.
so your problem is tracking?
keep track of "objects" and their positions. I could call them vortices but I'll keep it general.
in a new frame, find blobs
then assign each of those blobs to the spatially nearest object (within some distance). check to make sure you don't match multiple blobs to the same object... or be aware of the possibility at least. if there's no existing object within some distance, create a new object for this blob.
update each object's position from its assigned blob's current position
take care to cull objects that disappear (i.e. had no assignment).
this is tracking by assignment.
this can be made arbitrarily complicated.
you should keep track of the colors of your objects so you know not to match a red blob to a blue object (vortex).
you could represent your objects using just a centroid. or you could represent them using a contour. then "distance" might involve calculating overlap and maybe even shape (look up fourier descriptors, they're the fourier transform of a polar representation of the contour).
you could assign (and update) a velocity to an object, and predict its next position linearly, and use that to maybe get a more stable prediction.

Find explicitly islands in matrix

I am given a matrix of 0 and 1's and have to find the islands formed by one's. If found a reference :
https://www.careercup.com/question?id=14948781
About who to compute the number of island but don't know at all how to adapt the algorithm to at the end obtain the list of islands given a lists of cells of the matrix belonging to them.
This problem is essentially asking you to find the connected components of an undirected graph. Here, a connected component is a group of 1's surrounded by 0s and none of the 1s in the group are connected to another separate group of 1s surrounded by 0s.
To solve this, you can start by performing a Depth First Search (DFS) on each of the elements in the 2D matrix.
If the algorithm encounters an unvisited 1, increment the count of the islands
Recursively perform a DFS on all the adjacent vertices (up, down, left, right)
Keep track of the visited 1s so that they are not visited again (can be done
using a set or by marking the node in the graph visited with a boolean flag)
As soon as a 0 is found during this recursive DFS, stop the DFS
Continue the algorithm by moving onto the next element in the 2D matrix and
repeat steps 1-4
When you reach the end of the 2D matrix return the count of islands

r-tree how to calculate minimum bounding rectangles of non leaf nodes

I have many building footprints and want to store them in a r-tree structure.I want to understand that in a r-tree structure leaf nodes are minimum bounding rectangles (MBR) of real objects, in my case building footprints. But I could not understand how MBRs of non leaf nodes can be calculated and I want to know how can it be done (In the picture Green boxes). I suppose there are many possible solution but I just want to know only one of them.
The bounding boxes of inner nodes are computed exactly the same way as for leaf nodes.
You need the minimum and maximum in each axis.
The MBR of a non-leaf node is the union of its children nodes (can be leaf or non-leaf nodes) so that it is the bounding box of offspring data.
Take the two-dimensional example in your picture, suppose the children nodes A(X_amin, X_amax, Y_amin, Y_amax) and B(X_bmin, X_bmax, Y_bmin, Y_bmax), the non-leaf parent node is N(min(X_amin, X_bmin), max(X_amax, X_bmax), min(Y_amin, Y_bmin), max(Y_amax, Y_bmax)).

Pathfinding On a huge Map

I am in need of some type of pathfinding, so I searched the Internet and found some algorithms.
It seems like they all need some type of map also.
This map can be represented by:
Grid
Nodes
As my map is currently quite huge (20.000 x 20.000 px), a grid map of 1 x 1 px tiles would lead to 400.000.000 unique points on the Grid and also the best Quality I would think. But thats way to much points for me so I could either
increase the tile size (e.g. 50 x 50 px = 160.000 unique points)
switch to Nodes
As the 160.000 unique points are also to much for me, or I would say, not the quality I would like to have, as some units are bigger as 50 px, I think Nodes are the better way to go.
I found this on the Internet 2D Nodal Pathfinding without a Grid and did some calculations:
local radius = 75 -- this varies for some units so i stick to the biggest value
local DistanceBetweenNodes = radius * 2 -- to pass tiles diagonaly
local grids = 166 -- how many col/row
local MapSize = grids * DistanceBetweenNodes -- around 25.000
local walkable = 0 -- used later
local Map = {}
function even(a)
return ((a / radius) % 2 == 0)
end
for x = 0, MapSize, radius do
Map[x] = {}
for y = 0, MapSize, radius do
if (even(x) and even(y)) or (not even(x) and not even(y)) then
Map[x][y] = walkable
end
end
end
Without removing the unpassable Nodes and a unit size of 75 i would end up with ~55445 unique Nodes. The Nodes will drastically shrink if i remove the unpassable Nodes, but as my units have different sizes i need to make the radius to the smallest unit i got. I dont know if this will work with bigger units later then.
So i searched the Internet again and found this Nav Meshes.
This will reduce the Nodes to only "a few" in my eyes and would work with any unit size.
UPDATE 28.09
I have created a nodal Map of all passable Areas now ~30.000 nodes.
Here is an totally random example of a map and the points i have:
Example Map
This calls for some optimization, and reduce the amount of nodes you have.
Almost any pathfinding algorithm can take a node list that is not a grid. You will need to adjust for distance between nodes, though.
You could also increase your grid size so that it does not have as many squares. You will need to compensate for small, narrow paths, in some sort of way, though.
At the end of the day, i would suggest you reduce your node count by simply placing nodes in an arranged path, where you know it is possible to get from point A to B, specifying the neighbors. You will need to manually make a node path for every level, though. Take my test as an example (There are no walls, just the node path):
For your provided map, you would end up with a path node similar to this:
Which has around 50 nodes, compared to the hundreds a grid can have.
This can work on any scale, since your node count is dramatically cut, compared to the grid approach. You will need to make some adjustments, like calculating the distance between nodes, now that they are not in a grid. For this test i am using dijkstra algorithm, in Corona SDK (Lua), but you can try using any other like A-star (A*), which is used in many games and can be faster.
I found a Unity example that takes a similar approach using nodes, and you can see that the approach works in 3D as well:

Calculating the neighborhood distance

What method would you use to compute a distance that represents the number of "jumps" one has to do to go from one area to another area in a given 2D map?
Let's take the following map for instance:
(source: free.fr)
The end result of the computation would be a triangle like this:
A B C D E F
A
B 1
C 2 1
D 2 1 1
E . . . .
F 3 2 2 1 .
Which means that going from A to D, it takes 2 jumps.
However, to go from anywhere to E, it's impossible because the "gap" is too big, and so the value is "infinite", represented here as a dot for simplification.
As you can see on the example, the polygons may share points, but most often they are simply close together and so a maximum gap should be allowed to consider two polygons to be adjacent.
This, obviously, is a simplified example, but in the real case I'm faced with about 60000 polygons and am only interested by jump values up to 4.
As input data, I have the polygon vertices as an array of coordinates, from which I already know how to calculate the centroid.
My initial approach would be to "paint" the polygons on a white background canvas, each with their own color and then walk the line between two candidate polygons centroid. Counting the colors I encounter could give me the number of jumps.
However, this is not really reliable as it does not take into account concave arrangements where one has to walk around the "notch" to go from one polygon to the other as can be seen when going from A to F.
I have tried looking for reference material on this subject but could not find any because I have a hard time figuring what the proper terms are for describing this kind of problem.
My target language is Delphi XE2, but any example would be most welcome.
You can create inflated polygon with small offset for every initial polygon, then check for intersection with neighbouring (inflated) polygons. Offseting is useful to compensate small gaps between polygons.
Both inflating and intersection problems might be solved with Clipper library.
Solution of the potential neighbours problem depends on real conditions - for example, simple method - divide plane to square cells, and check for neighbours that have vertices in the same cell and in the nearest cells.
Every pair of intersecting polygons gives an edge in (unweighted, undirected) graph. You want to find all the path with length <=4 - just execute depth-limited BFS from every vertice (polygon) - assuming that graph is sparse
You can try a single link clustering or some voronoi diagrams. You can also brute-force or try Density-based spatial clustering of applications with noise (DBSCAN) or K-means clustering.
I would try that:
1) Do a Delaunay triangulation of all the points of all polygons
2) Remove from Delaunay graph all triangles that have their 3 points in the same polygon
Two polygons are neightbor by point if at least one triangle have at least one points in both polygons (or obviously if polygons have a common point)
Two polygons are neightbor by side if each polygon have at least two adjacents points in the same quad = two adjacent triangles (or obviously two common and adjacent points)
Once the gaps are filled with new polygons (triangles eventually combined) use Djikistra Algorithm ponderated with distance from nearest points (or polygons centroid) to compute the pathes.

Resources