Separate areas algorithm - algorithm

We are given an N x M rectangular area with K lines in it. Every line has (x0,y0) - (x1,y1), beginning and end coordinates. Are there some well known algorithms or resources to learn that can help me to write a program to find how many separate areas those lines form in the rectangular area?
If this is the original rectangular area : http://prntscr.com/6p9m2c
Then there are 4 separate areas: http://prntscr.com/6p9mo5

All segments with intersections form planar graph. You have to count thoroughly vertices and edges of this graph, then apply Euler's formula
F = E - V + 2
where
V is vertice count - number of intersections (and corners and free segment ends)
E is edge count - number of segments, formed after intersections
F is number of facets.
Your needed region count is
R = F - 1
because F takes into account outer facet.
For your example - there are 16 vertices, 10 horizontal edges and 9 vertical, so
R = 10 + 9 - 16 + 2 - 1 = 4
Note that you can either count vertices with degree 1,2 (corners and free ends) or ignore them together with one neighbour segment (simplify graph) - this doesn't influence to result.

Imagine that the NxM grid is a graph where each '.' is a vertex, and two vertex is connected by an edge if they are side by side (above, below, on the side). Now each separate area corresponds to a connected component of the graph, and you can count the number of connected components in O(N*M) using BFS or DFS algorithms.

Related

Find minimum number of triangles enclosing all points in the point cloud

Input
You have a points list which represents a 2D point cloud.
Output
You have to generate a list of triangles (should be as less as possible triangles) so the following restrictions are fulfilled:
Each point from the cloud should be a vertex of a triangle or be
inside a triangle.
Triangles can be build only on the points from
the original point cloud.
Triangles should not intersect with each
other.
One point of the cloud can be a vertex for several triangles.
If triangle vertex lies on the side of another triangle we assume such triangles do not intersect.
If point lies on the side of triangle we assume the point is inside a triangle.
For example
Investigation
I invented the way to find a convex hull of given set of points and divide that convex hull into triangles but it is not right solution.
Any guesses how to solve it?
Here is my opinion.
Create a Delaunay Triangulation of the point cloud.
Do a Mesh Simplification by Half Edge Collapse.
For step 1, the boundary of the triangulation will be the convex hull. You can also use a Constrained Delaunay Triangulation (CDT) if you need to honor a non-convex boundary.
For step 2 half-edge collapse operation is going to preserve existing vertices, so no new vertices will be added. Note that in your case the collapses are not removing vertices, they are only removing edges. Before applying an edge collapse you should check that you are not introducing triangle inversions (which produce self intersection) and that no point is outside a triangle. The order of collapses matter but you can follow the usual rule that measures the "cost" of collapses in terms of introducing poor quality triangles (i.e. triangles with acute angles). So you should choose collapses that produce the most isometric triangles as possible.
Edit:
The order of collapses guide the simplification to different results. It can be guided by other criteria than minimize acute angles. I think the most empty triangles can be minimized by choosing collapses that produce triangles most filled vs triangles most empty. Still all criteria are euristics.
Some musings about triangles and convex hulls
Ignoring any set with 2 or less points and 3 points gives always gives 1 triangle.
Make a convex hull.
Select any random internal point.
unless all points are in hull ...
All point in the hull must be part of an triangle as they per definition of convex hull can't be internal.
Now we have an upper bound of triangles, namely the number of points in the hull.
An upper bound is also number of points / 3 rounded up as you can make that many independent triangles.
so the upper bound is the minimum of the two above
We can also guess at the lower bound roundup(hull points / 3) as each 3 neighboring points can make a triangle and any surplus can reuse 1-2.
Now the difficult part reducing the upper bound
walk through the inner points using them as center for all triangles.
If any triangle is empty we can save a triangle by removing the hull edge.
if two or more adjacent triangles are empty we will have to keep every other triangle or join the 3 points to a new triangle, as the middle point can be left out.
note the best result.
Is this prof that no better result exist? no.
If there exist a triangle that envelop all remaining points that would this be better.
N = number of points
U = upper bound
L = lower bound
T = set of triangles
R = set of remaining points
A = set of all points
B = best solution
BestSolution(A)
if A < 3 return NoSolution
if A == 3 return A
if not Sorted(A) // O(N)
SortByX(A) // O(n lg n) or radex if possible O(N)
H = ConvexHull(A)
noneHull = A - H
B = HullTriangles(H, noneHull) // removing empty triangles
U = size B
if noneHull == 0
return U // make triangles of 3 successive points in H and add the remaining to the last
if U > Roundup(N/3)
U = Roundup(N/3)
B = MakeIndepenTriangles(A)
AddTriangle(empty, A)
return // B is best solution, size B is number of triangles.
AddTriangle(T, R)
if size T+1 >= U return // no reason to test if we just end up with another U solution
ForEach r in R // O(N)
ForEach p2 in A-r // O(N)
ForEach p3 in A-r-p2 // O(N)
t = Triangle(r, p2, p3)
c = Candidate(t, T, R)
if c < 0
return c+1 // found better solution
return 0
Candidate(t, T, R)
if not Overlap(t, T) // pt. 3, O(T), T < U
left = R-t
left -= ContainedPoints(t) // O(R) -> O(N)
if left is empty
u = U
U = size T + 1
B = T+t
return U-u // found better solution
return AddTriangle(T+t, left)
return 0
So ... total runtime ...
Candidate O(N)
AddTriangle O(N^3)
recursion is limited to the current best solution U
O((N N^3)^U) -> O((N^4)^U)
space is O(U N)
So reducing U before we go to brute force is essential.
- Reducing R quickly should reduce recursion
- so starting with bigger and hopefully more enclosing triangles would be good
- any 3 points in the hull should make some good candidates
- these split the remaining points in 3 parts which can be investigated independently
- treat each part as a hull where its 2 base points are part of a triangle but the 3rd is not in the set.
- if possible make this a BFS so we can select the most enclosing first
- space migth be a problem
- O(H U N)
- else start with points that are a 1/3 around the hull relative to each other first.
AddTriangle really sucks performance so how many triangles can we really make
Selecting 3 out of N is
N!/(N-3)!
And we don't care about order so
N!/(3!(N-3)!)
N!/(6(N-3)!)
N (N-1) (n-2) / 6
Which is still O(N^3) for the loops, but it makes us feel better. The loops might still be faster if the permutation takes too long.
AddTriangle(T, R)
if size T+1 >= U return // no reason to test if we just end up with another U solution
while t = LazySelectUnordered(3, R, A) // always select one from R first O(R (N-1)(N-2) / 6) aka O(N^3)
c = Candidate(t, T, R)
if c < 0
return c+1 // found better solution
return 0

An efficient solution to find if n vertex disjoint path exist

You have been given an r x c grid. The vertices in i row and j column is denoted by (i,j). All vertices in grid have exactly four neighbors except boundary ones which are denoted by (i,j) if i = 1, i = r , j = 1 or j = c. You are given n starting points. Determine whether there are n vertex disjoint paths from starting points to n boundary points.
My Solution
This can be modeled as a max-flow problem. The starting points will be sources, boundary targets and each edge and vertex will have capacity of 1. This can be further reduced to generic max flow problem by making each vertex split in two, with capacity of 1 in edge between them, and having a supersource and a supersink connected with sources and targets be edge of capacity one respectively.
After this I can simply check whether there exists a flow in each edge (s , si) where s is supersource and si is ith source in i = 1 to n. If it does then the method returns True otherwise False.
Problem
But it seems like using max-flow in this is kind of overkill. It would take some time in preprocessing the graph and the max-flow takes about O(V(E1/2)).
So I was wondering if there exists an more efficient solution to compute it?

Number of cycles in a random graph

In an undirected random graph of 8 vertices, the probability of an edge being present between a pair of vertices in 1/2. What is the expected number of unordered cycles of length 3?
Here's how I thought of going about it:
Method 1
Basically cycles ("unordered" i am assuming to mean that the vertices can be taken in any order) of length 3 would be triangles.
Letting number of vertices = v, and no of such cycles being C
For n=3, C = 1
For n = 4, c = 4. (a square with 2 diagonal lines. 4 unique triangles).
....
For n>3, C(n) = (n-2) + (n-2)(n-3) + (n-4), generalizing.
This is because: let us start with an outside edge, and the triangles possible with that as a base. For the first edge we choose (2 vertices gone there), there are (n-2) other vertices to choose the 3rd point of triangle. So (n-2) in the first term.
Next, the last term is because the very last side we choose to base our triangles on would have its leftmost and rightmost triangles taken already by the first side we chose and its immediate predecessor.
The middle term is the product of the remaining number of edges and possible triangles.
.--------.
. .
. .
. .
With the above set of 8 vertices one can trivially think it out visually. (If better diagrams are needed, I shall do the needful!). So for v=8, C(8) = 40. So, as probability of an edge is 1/2 . . .
Method 2 The number of triangles from n points is nC3, where C is "combination". But as half of these edges are not expected to exist . . .
I am not sure how to proceed beyond this point. Any hints in the right direction would be great. Btw its not a homework question.
You have nC3 possible triangles. For a triangle to appear all three edges must exist - so the probability of a specific triangle to appear is 1/8.
The expected number of triangles is then (nC3) / 8.
In your case, 8C3 / 8 or 7.
So we have a graph with 8 vertices, now we want all three length cycles possible, given that probability of having an edge between any two vertices =1/2.
Suppose a b and c are any three vertices of given graph, now to make a cycle between a, b and c there should be an edge between a------b(probability of this=1/2) and there should be an edge between b------c(probability of this=1/2) and there should be an edge between c------a(probability of this=1/2) ,
So probability of getting one cycle = (1/2)(1/2)(1/2)*1 =1/8
And probability of getting all cycles=
1/8 *(total no of 3 length cycles possible with 8 vertices)
= 1/8*(8C3) =7.
so you have given n vertices now you are having nC1 way to select the first edge.
probability of existing that edge would be 1/2 .now in the left n-1 edge we can choose (n-1)C1 ways with probability as 1/2 similarly third edge with (n-2)C1 with probability 1/2 so simultaneously this can done as {nC1*(1/2)(n-1)C1(1/2)*(n-2)C1*1/2}/3!
we have to divide by 3!due to fact that it is asking for unordered.

k means clustering sample data

I am writing program to implement k-means clustering.
consider a simple input with 4 vertices a,b,c and d with following edge costs
[vertex1] [vertex2] [edge cost]
a b 1
a c 2
a d 3
b d 4
c d 5
Now I need to make the program run until i get 2 clusters.
My doubt is, in the first step when calculate the minimum distance it is a->b (edge cost 1). Now I should consider ab as a single cluster. If that is the case, what will be the distance of ab from c and d?
The K-means algorithm works as follows:
choose k points as initial centroids (hence, K-*);
calculate the distance from all vertices to the k centroids choosen;
assign each vertex to the closest centroid;
recalculate the position of the centroids by generating the mean between all the vertices that belong to the centroid (hence, k-means, one mean calculation for each of the k centroids);
go to step 2 and stop when, in step 3, no vertex get assigned to another centroid -- or until your error condition gets satisfied.
In your case, as you have an undirected graph, it'd be better for you to generate the coordinates of each vertex considering the edge distances, and then, apply the algorithm.
If you don't want to do this initial process, you may calculate the distance from a vertex to all other reachable vertices, but you'd have to do this for every iteration -- which is quite an unnecessary overhead.
For your undirected graph:
[vertex1] [vertex2] [edge cost]
a b 1
a c 2
a d 3
b d 4
c d 5
The table of distances would be something like:
a b c d
a 0 1 2 3
b 1 0 (1) 4
c 2 (1) 0 5
d 3 4 5 0
(1) - b to c = (b to a, a to c) = 3
If this should be your table, simply apply the Dijkstra algorithm on your graph, for each vertex, and consider the resultant table your table of distances.
The table would have the minimal distances, but, if you have any other policy to calculate it, it's totally up to you saying how to calculate it.
Notice also that, if your graph is directed, the matrix will not be symmetric, as it is, in this case.

How to solve the following graph game

Consider the following game on an undirected graph G. There are two players, a red color player R and a blue color player B. Initially all edges of G are uncolored. The two players alternately color an uncolored edge of G with their color until all edges are colored. The goal of B is that in the end, the blue-colored edges form a connected spanning subgraph of G. A connected spanning subgraph of G is a connected subgraph that contains all the vertexes of graph G. The goal of R is to prevent B from achieving his goal.
Assume that R starts the game. Suppose that both players play in the smartest way. Your task is to find out whether B will win the game.
Input:
Each test case begins with a line of two integers n ( 1 <= n <= 10) and m (0 <= m <= 30), indicating the number of vertexes and edges in the graph. All vertexes are numbered from 0 to n-1.
Then m lines follow. Each line contains two integers p and q ( 0 <= p, q < n) , indicating there is an edge between vertex p and vertex q.
Output:
For each test case print a line which is either "YES" or "NO" indicating B will win the game or not.
Example:
3 4
0 1
1 2
2 0
0 2
Output: Yes
My idea:
If we can find two disjoint spanning trees of the graph, then player B wins the game. Otherwise, A wins.
'Two disjoint spanning trees' means the edge sets of the two trees are disjoint
I wonder if you can prove or disprove my idea
Your idea is correct. Find a proof here:
http://www.cadmo.ethz.ch/education/lectures/FS08/graph_algo/solution01.pdf
If you search for "connectivity game" or "maker breaker games" you should find some more interesting problems and algorithms.
So I think R should follow the following strategy:Find the node with least degree (uncolored edges) (which does not have any Blue colored Edge)
call it N
if degree of N (uncolored edges) is 1 then R wins, bye bye
Find its adjacent nodes {N1,...,Nk}
Pick up M from {N1,...,Nk} such that degree (uncolored) of M (and M does not have any blue colored edge) is the least among the set
Color the edge Connecting from M to N
Repeat this.

Resources