Number of cycles in a random graph - algorithm

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.

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

Shortest path in matrix with obstacles with cheat paths

First of all this is an assingment and I am not looking for direct answers but instead the complexity of the best solution as you might be thinking it .
This is the known problem of shortest path between 2 points in a matrix (Start and End) while having obstacles in the way. Moves acceptables is up,down,left and right . Lets say when moving i carry sth and the cost of each movement is 2 . There are points in the matrix (lets name them B points) where I can leave this sth in one B point and pick it up from another B point . Cost of dumping sth in B point is 1 and cost of picking sth up from a B point is 1 again . Whenever I move without this sth , my cost of moving now is 1 .
What I think of the solution is transform the matrix into a tree and have a BFS applied . However that works without the B points .
Whenever i take into account the B points complexity comes to a worst case scenario N^2.
Here is an example :
S - - -
- - - -
B - - B
- - O E
S = Start , E = End , B = B point to drop sth, O = obstacle
So i start with S move down down to the B point (2*2=4 points) leave sth in the B point (1 point ) move right right (2*1= 2 points ) , pick it up (1 point ) , move down 2 points = total of 10 points .
What i thought was build the tree with nodes every B point , however this would create a very dense cyclic graph of almost (V-1)*(V-1) edges which takes the algortithm in N^2 boundaries just to create the graph .
That is the worst case scenario as above :
S b b b
b b b b
b b b b
b b b E
Another option I thought was that of first calculating shortest paths withouth B points .
Then have iterations where at each iteration :
First have bfs on S and closest B
have BFS on E and closest B
Then see if there is a path between B of closest to S and B closest to E .
If there is then I would see if the path is smaller than that of regular shortest path with obstacles .
If that is bigger then there is no shortest path (no greedy test).
If there is no path between the 2 B points , try second closest to S and try again .
If no path again , the second closest to E and closest to S .
However I am not able to calculate the complexity in this one in the worst case scenario plus there is no greedy test that evaluates that.
Any help on calculating the complexity or even pointing out the best complexity solution (not the solution but just the complexity ) would be greatly appreciated
Your matrix is a representation of a graph. Without the cheat paths it is quite easy to implement a nice BFS. Implementing the cheat paths is not a big deal. Just add the same matrix as another 'layer' on top of the first one. bottom layer is 'carry', top layer is 'no carry'. You can move to the other layer only at B-points for the given cost. This is the same BFS with a third dimension.
You have n^2 nodes and (n-1)^2 edges per layer and additionally a maximum of n^2 eges connecting the layers. That's O(n^2).
You can just build a new graph with nodes labeled by (N, w) where N is a node in the original graph (so a position in your matrix), and w=0 or 1 is whether you're carrying a weight. It's then quite easy to add all possible edges in this graph
This new graph is of size 2*V, not V^2 (and the number of edges is around 4*V+number(B)).
Then you can use a shortest path algorithm, for instance Dijkstra's algorithm: complexity O(E + V log(V)) which is O(V log(V)) in your case.

How many different adjacency matrix does a graph with N vertices and E edges have?

The answer is N! and I don't understand how it is.
My view:
Assuming it is an undirected graph;
The dimension of each row in a adj. matrix of a vertex is N irrespective of the number of edges. Hence, the number of permutation possible for first row= N!.
Total permutation for second row= (N-1)! since one cell has already been taken care in the first row.
Similarly, third row= (N-2)!
.
.
.
For Nth row=1
Total permutation= N! + N-1!+...+1!
I am confused if considering an undirected or directed graph will yield different result or not. How will the answer change if we consider graph to be directed?
I'll take a shot at it, but please ask questions if it's unclear. For it to be N!, we are assuming an undirected graph.
For a graph with N vertices, it would be represented with an NxN matrix (2D array) with each value being either 0 (edge does not exist) or 1 (edge exists). In this, we are not considering other weights on edges.
Then, we consider all of the possible assignments. If there are N different choices on the first row, there must be N-1 choices on the second row (since we already know about the edge 1,2) and so on.
N * (N-1) * (N-2) * ... * 1 = N!

Separate areas 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.

Algorithm for polygon with weight on vertices and operations on edges

I am thinking about the algorithm for the following problem (found on carrercup):
Given a polygon with N vertexes and N edges. There is an int number(could be negative) on every vertex and an operation in set(*,+) on every edge. Every time, we remove an edge E from the polygon, merge the two vertexes linked by the edge(V1,V2) to a new vertex with value: V1 op(E) V2. The last case would be two vertexes with two edges, the result is the bigger one.
Return the max result value can be gotten from a given polygon.
I think we can use just greedy approach. I.e. for polygon with k edges find a pair (p, q) which produces the maximum number when collapsing: (p ,q) = max ({i operation j : i, j - adjacent edges)
Then just call a recursion on polygons:
1. Let function CollapseMaxPair( P(k) ) - gets polygon with k edges and returns 'collapsed' polygon with k-1 edges
2. Then our recursion:
P = P(N);
Releat until two edges left
P = CollapseMaxPair( P )
maxvalue = max ( two remained values)
What do you think?
I have answered this question here: Google Interview : Find the maximum sum of a polygon and it was pointed out to me that that question is a duplicate of this one. Since no one has answered this question fully yet, I have decided to add this answer here as well.
As you have identified (tagged) correctly, this indeed is very similar to the matrix multiplication problem (in what order do I multiply matrixes in order to do it quickly).
This can be solved polynomially using a dynamic algorithm.
I'm going to instead solve a similar, more classic (and identical) problem, given a formula with numbers, addition and multiplications, what way of parenthesizing it gives the maximal value, for example
6+1 * 2 becomes (6+1)*2 which is more than 6+(1*2).
Let us denote our input a1 to an real numbers and o(1),...o(n-1) either * or +. Our approach will work as follows, we will observe the subproblem F(i,j) which represents the maximal formula (after parenthasizing) for a1,...aj. We will create a table of such subproblems and observe that F(1,n) is exactly the result we were looking for.
Define
F(i,j)
- If i>j return 0 //no sub-formula of negative length
- If i=j return ai // the maximal formula for one number is the number
- If i<j return the maximal value for all m between i (including) and j (not included) of:
F(i,m) (o(m)) F(m+1,j) //check all places for possible parenthasis insertion
This goes through all possible options. TProof of correctness is done by induction on the size n=j-i and is pretty trivial.
Lets go through runtime analysis:
If we do not save the values dynamically for smaller subproblems this runs pretty slow, however we can make this algorithm perform relatively fast in O(n^3)
We create a n*n table T in which the cell at index i,j contains F(i,j) filling F(i,i) and F(i,j) for j smaller than i is done in O(1) for each cell since we can calculate these values directly, then we go diagonally and fill F(i+1,i+1) (which we can do quickly since we already know all the previous values in the recursive formula), we repeat this n times for n diagonals (all the diagonals in the table really) and filling each cell takes (O(n)), since each cell has O(n) cells we fill each diagonals in O(n^2) meaning we fill all the table in O(n^3). After filling the table we obviously know F(1,n) which is the solution to your problem.
Now back to your problem
If you translate the polygon into n different formulas (one for starting at each vertex) and run the algorithm for formula values on it, you get exactly the value you want.
Here's a case where your greedy algorithm fails:
Imagine your polygon is a square with vertices A, B, C, D (top left, top right, bottom right, bottom left). This gives us edges (A,B), (A,D), (B,C), and (C, D).
Let the weights be A=-1, B=-1, C=-1, and D=1,000,000.
A (-1) ------ B (-1)
| |
| |
| |
| |
D(1000000) ---C (-1)
Clearly, the best strategy is to collapse (A,B), and then (B,C), so that you may end up with D by itself. Your algorithm, however, will start with either (A,D) or (D,C), which will not be optimal.
A greedy algorithm that combines the min sums has a similar weakness, so we need to think of something else.
I'm starting to see how we want to try to get all positive numbers together on one side and all negatives on the other.
If we think about the initial polygon entirely as a state, then we can imagine all the possible child states to be the subsequent graphs were an edge is collapsed. This creates a tree-like structure. A BFS or DFS would eventually give us an optimal solution, but at the cost of traversing the entire tree in the worst case, which is probably not as efficient as you'd like.
What you are looking for is a greedy best-first approach to search down this tree that is provably optimal. Perhaps you could create an A*-like search through it, although I'm not sure what your admissable heuristic would be.
I don't think the greedy algorithm works. Let the vertices be A = 0, B = 1, C = 2, and the edges be AB = a - 5b, BC = b + c, CA = -20. The greedy algorithm selects BC to evaluate first, value 3. Then AB, value, -15. However, there is a better sequence to use. Evaluate AB first, value -5. Then evaluate BC, value -3. I don't know of a better algorithm though.

Resources