Minimum edge weight along a path - graph

How would I find the maximum of a set of minimum edge weights along all possibles path between arbitrary vertices (u,v)?
I was thinking a modification of Floyd-Warshall?
i.e. Path 1: s - a - b - c - d - t with weights 1 - 5 - 6 - 10 - 9
Minimum edge weight is 1
Path 2: s - x - y - z - w - t with weights 3 - 9 - 8 - 6 - 7
Minimum edge weight is 3
Thus the result is max(1, 3) = 3

Yes, a modification of Floyd-Warshall would work - instead of shortest path length, you'll keep track of maximum path "width".
If you're only interested in two vertices, you may take a simpler approach: start with an empty graph and add edges ordered by their weight (from high to low). When the nodes in question get connected, the last edge added gives you the maximal "width". Done properly (i.e. using disjoint set to check the connectedness), this will be quicker then Floyd-Warshall.
Note: I'm only considering positive weights.

Related

Shortest path from source to destination with constraints on the order of vertices traversed

Suppose that the we have a bidirectional graph with V amount of vertices and E amount of edges, where all of the edges' weight are 1. Now, every vertices bar the source has its own level and suppose that we want to reach level L. Suppose that we start from level 1. When our level is i, we can only pass through the vertices that are either level 0 or level i. After passing a vertex that has level i, we level up by 1.
Now, given a starting point and a target level, how do we compute the minimum steps (costs) needed to reach the target level?
I believe that this problem can be solved with modified BFS algorithm. As of now, I can determine whether such path(s) exist(s) or not but I haven't been successful on computing the costs.
Any help and suggestions would be greatly appreciated.
For example, let's have a graph with V = 5, E = 4, L = 3, and starting point at vertex 0.
The following lines are the level of each vertex:
0 NO-LEVEL
1 0
2 0
3 2
4 1
and these following E lines represent the edges:
0 1
1 2
2 3
0 4
With this input, the output should be: 5 (0->4->0->1->2->3).
However, my code's output is 3 (0->1->2->3).
I am still confused on how to include the 0->4->0 process when counting the distance.
I'd suggest making L copies of every vertex. Vertex (v,i) means vertex v "at" level i. Then set the edge costs equal to infinity if we cannot go from (v,i) to (w,j). For example, if vertex v has level 3 and vertex w has level 2, then the cost from (v,3) to (w,2) is infinity.
Then solve a standard shortest path problem on the resulting network.

How do I calculate the minimum and maximum amount of nodes and leaves in a rooted tree?

I'm looking to calculate the minimum and maximum amount of nodes and leaves in a rooted tree with the height h and the degree d.
I'm guessing the minimum amount of leaves is always 1 (if h >= 2).
The maximum amount of nodes should be G^(h-2), the leaves should be G^(h-1).
For the minimum amount of nodes I'm clueless.
Am I correct or am I missing something?
Given that the tree has height h and degree d the following applies.
Minimum numbers of nodes
To construct a tree of height h with as few nodes as possible, you'll want that each node only has one child. Consequently you'll need h nodes.
Maximum numbers of nodes
To use as many nodes as possible you'll want that every node (except the leafs) has as many childern as possible, i.e. d childern. This will look like:
level nodes at level
1 1 (d^0)
2 d (d^1)
3 d^2
4 d^3
So the number of nodes is a sum like
num_nodes = d^0 + d^1 + d^2 + d^3 + ....
This is a geometric sum that can be calculated as:
num_nodes = (1 - d^h)/(1 - d)

Shortest Paths with One negative edge

Let G(V,E) be a directed connected graph with no negative cycles in it. All the edges have non-negative weight, except ONE edge. Find a simple shortest path from s,t in V.
My idea -
Do a BFS on the graph, find the edge with the negative weight.
Add this negative weight to all the edges, so we eliminate the negative weight.
Do Dijkstra algorithm.
My idea doesn't work.
Can you please help me find out why?
Thank you.
The reason that your approach doesn't work is that it unfairly penalizes paths with more edges.
Imagine two paths from a source node to a destination, one with more edges, but lower weight, and another with a fewer edges with higher weight. Let's assume that the weight added to each edge is 3.
Original paths:
S -> 1 -> 1 -> 1 -> 1 -> 1 -> T wt = 5
S -> 4 -> 3 -> T wt = 7
Paths after adding weight:
S -> 4 -> 4 -> 4 -> 4 -> 4 -> T wt = 20
S -> 7 -> 6 -> T wt = 13
As you can see, the second path is now incorrectly identified as the shorter one.
The problem with your approach is that it may create more negative edges if the one negative edge has a large negative value.
You can look into Bellman-Ford shortest path algorithm to solve this problem:
1) The first step is to initialize distances from source to all vertices as infinite and distance to source itself as 0. Create an array dist[] of size |V| with all values as infinite except dist[src] where src is source vertex.
2) This step calculates shortest distances. Do following |V|-1 times where |V| is the number of vertices in given graph.
…..a) Do following for each edge u-v
………………If dist[v] > dist[u] + weight of edge uv, then update dist[v]
………………….dist[v] = dist[u] + weight of edge uv
3) This step reports if there is a negative weight cycle in graph. Do following for each edge u-v
……If dist[v] > dist[u] + weight of edge uv, then “Graph contains negative weight cycle”
The idea of step 3 is, step 2 guarantees shortest distances if graph doesn’t contain negative weight cycle. If we iterate through all edges one more time and get a shorter path for any vertex, then there is a negative weight cycle

Maximum number of edges in undirected graph with n vertices with k connected components?

I couldn't answer the question myself because i don't see any similar behavior for all the examples i tried.
The question again:
Maximum number of edges in undirected graph with n vertices with k connected components?
Thanks.
This answer depends on whether your graphs are allowed to have self-loops or not. For simplicity, I'm going to assume they aren't.
If you have a connected component with x nodes in it, the maximum number of edges you can have in that connected component is x(x - 1) / 2. Therefore, if you have n total nodes and k different connected components, you can imagine distributing the nodes into the connected components in a way that maximizes the sum of x(x - 1) / 2 across the connected components.
Specifically, suppose your CC's have n1, ..., nk nodes each. You want to solve the following quadratic program:
Maximize: n1(n1 - 1) / 2 + ... + nk(nk - 1) / 2
Subject to: n1 + ... + nk = n
I'm going to claim that this is maximized when k - 1 of the connected components have 1 node and the last connected component has n - k + 1 nodes in it. Intuitively, this is true because any node you remove from the huge CC will cause a large drop in the number of possible edges that will not be offset by the meager increase in the number of possible edges in the other connected component the node was added to.
Under this setup, the maximum number of possible edges in the k - 1 singleton CC's will be 0 and the maximum number of possible edges in the other CC will be (n - k + 1)(n - k) / 2. Therefore, the total should be (n - k + 1)(n - k) / 2.
Hope this helps!
when graph do not contain self loops and is undirected then the maximum no. of edges are-
(n-k+1)(n-k)/2
It is because maximum number of edges with n vertices is n(n-1)/2. Now for example, if we are making an undirected graph with n=2 (4 vertices) and there are 2 connected components i.e, k=2, then first connected component contains either 3 vertices or 2 vertices, for simplicity we take 3 vertices (Because connected component containing 2 vertices each will not results in maximum number of edges). These 3 vertices must be connected so maximum number of edges between these 3 vertices are 3 i.e, (1->2->3->1) and the second connected component contains only 1 vertex which has no edge. So the maximum number of edges in this case are 3. This implies that replacing n with n-k+1 in the formula for maximum number of edges i.e, n(n-1)/2 will results in (n-k+1)(n-k)/2 which is maximum number of edges that a graph of n vertices with k connected component can have.see image for better understanding
hope it will be helpful!!

3D Trilateration using given distances of unknown fixed points

I am new to this forum and not a native english speaker, so please be nice! :)
Here is the challenge I face at the moment:
I want to calculate the (approximate) relative coordinates of yet unknown points in a 3D euclidean space based on a set of given distances between 2 points.
In my first approach I want to ignore possible multiple solutions, just taking the first one by random.
e.g.:
given set of distances: (I think its creating a pyramid with a right-angled triangle as a base)
P1-P2-Distance
1-2-30
2-3-40
1-3-50
1-4-60
2-4-60
3-4-60
Step1:
Now, how do I calculate the relative coordinates for those points?
I figured that the first point goes to 0,0,0 so the second one is 30,0,0.
After that the third points can be calculated by finding the crossing of the 2 circles from points 1 and 2 with their distances to point 3 (50 and 40 respectively). How do I do that mathematically? (though I took these simple numbers for an easy representation of the situation in my mind). Besides I do not know how to get to the answer in a correct mathematical way the third point is at 30,40,0 (or 30,0,40 but i will ignore that).
But getting the fourth point is not as easy as that. I thought I have to use 3 spheres in calculate the crossing to get the point, but how do I do that?
Step2:
After I figured out how to calculate this "simple" example I want to use more unknown points... For each point there is minimum 1 given distance to another point to "link" it to the others. If the coords can not be calculated because of its degrees of freedom I want to ignore all possibilities except one I choose randomly, but with respect to the known distances.
Step3:
Now the final stage should be this: Each measured distance is a bit incorrect due to real life situation. So if there are more then 1 distances for a given pair of points the distances are averaged. But due to the imprecise distances there can be a difficulty when determining the exact (relative) location of a point. So I want to average the different possible locations to the "optimal" one.
Can you help me going through my challenge step by step?
You need to use trigonometry - specifically, the 'cosine rule'. This will give you the angles of the triangle, which lets you solve the 3rd and 4th points.
The rules states that
c^2 = a^2 + b^2 - 2abCosC
where a, b and c are the lengths of the sides, and C is the angle opposite side c.
In your case, we want the angle between 1-2 and 1-3 - the angle between the two lines crossing at (0,0,0). It's going to be 90 degrees because you have the 3-4-5 triangle, but let's prove:
50^2 = 30^2 + 40^2 - 2*30*40*CosC
CosC = 0
C = 90 degrees
This is the angle between the lines (0,0,0)-(30,0,0) and (0,0,0)- point 3; extend along that line the length of side 1-3 (which is 50) and you'll get your second point (0,50,0).
Finding your 4th point is slightly trickier. The most straightforward algorithm that I can think of is to firstly find the (x,y) component of the point, and from there the z component is straightforward using Pythagoras'.
Consider that there is a point on the (x,y,0) plane which sits directly 'below' your point 4 - call this point 5. You can now create 3 right-angled triangles 1-5-4, 2-5-4, and 3-5-4.
You know the lengths of 1-4, 2-4 and 3-4. Because these are right triangles, the ratio 1-4 : 2-4 : 3-4 is equal to 1-5 : 2-5 : 3-5. Find the point 5 using trigonometric methods - the 'sine rule' will give you the angles between 1-2 & 1-4, 2-1 and 2-4 etc.
The 'sine rule' states that (in a right triangle)
a / SinA = b / SinB = c / SinC
So for triangle 1-2-4, although you don't know lengths 1-4 and 2-4, you do know the ratio 1-4 : 2-4. Similarly you know the ratios 2-4 : 3-4 and 1-4 : 3-4 in the other triangles.
I'll leave you to solve point 4. Once you have this point, you can easily solve the z component of 4 using pythagoras' - you'll have the sides 1-4, 1-5 and the length 4-5 will be the z component.
I'll initially assume you know the distances between all pairs of points.
As you say, you can choose one point (A) as the origin, orient a second point (B) along the x-axis, and place a third point (C) along the xy-plane. You can solve for the coordinates of C as follows:
given: distances ab, ac, bc
assume
A = (0,0)
B = (ab,0)
C = (x,y) <- solve for x and y, where:
ac^2 = (A-C)^2 = (0-x)^2 + (0-y)^2 = x^2 + y^2
bc^2 = (B-C)^2 = (ab-x)^2 + (0-y)^2 = ab^2 - 2*ab*x + x^2 + y^2
-> bc^2 - ac^2 = ab^2 - 2*ab*x
-> x = (ab^2 + ac^2 - bc^2)/2*ab
-> y = +/- sqrt(ac^2 - x^2)
For this to work accurately, you will want to avoid cases where the points {A,B,C} are in a straight line, or close to it.
Solving for additional points in 3-space is similar -- you can expand the Pythagorean formula for the distance, cancel the quadratic elements, and solve the resulting linear system. However, this does not directly help you with your steps 2 and 3...
Unfortunately, I don't know a well-behaved exact solution for steps 2 and 3, either. Your overall problem will generally be both over-constrained (due to conflicting noisy distances) and under-constrained (due to missing distances).
You could try an iterative solver: start with a random placement of all your points, compare the current distances with the given ones, and use that to adjust your points in such a way as to improve the match. This is an optimization technique, so I would look up books on numerical optimization.
If you know the distance between the nodes (fixed part of system) and the distance to the tag (mobile) you can use trilateration to find the x,y postion.
I have done this using the Nanotron radio modules which have a ranging capability.

Resources