Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
In an undirected graph with n vertices and no edges, what is the maximum number of edges that can be added so that the graph remains unconnected? This is an interview question.
NC2
(N-1)C2
N!
(N-1)!
The maximum number of edges in a graph with N vertices is NC2 (link).
Note that, to remain unconnected, one of the vertices should not have any edges. More formally, there has to be a cut (across which there won't be any edges) with one side having only one vertex. Why not more than one vertex? Proof by induction:
The cases for 0, 1 and 2 vertices are trivial.
Consider a graph with 3 vertices. The best cut will be one with 2 vertices on one side and 1 vertex on the other side.
Now assume the best cut is one with N-1 vertices on one side and 1 vertex on the other with N >= 3. Now try to add a vertex. Adding the vertex to the side with one vertex will result in one edge that can be added. Adding the vertex to the other side will result in N-1 possible edges. Clearly N-1 > 1 for N >= 3. Thus it's always better to add the vertex to the side with N-1 vertices.
Now there are two ways to go from here:
Consider the graph without one of the edges. The maximum number of edges of this sub-graph is (N-1)C2.
Consider the maximum number of edges of the graph as is and subtract the number of edges from one vertex. This gives NC2 - (N-1) = N(N-1)/2 - 2(N-1)/2 = (N-2)(N-1)/2 = (N-1)C2.
So the answer is (N-1)C2, i.e. option 2.
b (n-1)C2
An example of such a graph is a complete graph of n-1 vertices and one isolated vertex.
In this example, the complement graph would have nC2 - (n-1)C2 = n-1 edges.
And either given graph or its complement is connected (proof).
Hence, if we constucted a graph with more than (n-1)C2 edges, then the complement would have less than n-1 edges and couldn't be connected, so our graph would be.
Related
An undirected graph having 'n' number of edges, then find out number of vertices that graph have?
Since an edge is a connection between to vertices, the amount of vertices is at max 2n.
The amount of vertices is at minimum n+1. (This is pretty logical if you imagine that you have 2 edges - then you will at minimum have 3 vertices, because each edge must connect 2 vertices)
So if e = n, then n+1 <= v <= 2n
There is no exact formula for the number of vertices in terms of number of edges in the general case. However, if you take special cases, you can say more: if the graph is a tree, then the number of vertices is one more than the number of edges.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Given a Directed Weighted Graph which is Strongly Connected. I need to find a strongly connected sub-graph out of this graph such that the difference between the maximum and minimum weight edges is minimum.
To be more clearly, I need to get rid of edges in such a way that after removing them, the graph will still be strongly connected and the difference between the maximum and minimum weight edges is minimum.
Here is an example:
The first line is the number of N nodes and M edges of this graph. The next M lines represent the edges of this graph.
3 6
1 2 8
2 3 32
3 1 16
1 3 81
3 2 243
2 1 27
The chosen N-nodes sub-graph would contain the edges:
1 2 8
2 3 32
3 1 16
The difference between the maximum and minimum weight edges is: 32 - 8 = 24.
Which is minimum among all choices.
I'm looking for an optimal solution. There are at most 3000 nodes and 5000 edges.
Given an algorithm that tests whether a given digraph G = (V, E) is strongly connected in O(f(|V|, |E|)) time, this problem can be solved in time O(|E|*f(|V|, |E|)) -- and better than that if testing for strong connectivity can be done more quickly after adding or removing a single edge to an already-tested digraph.
Sort edges in increasing order by weight, and number them in this order. Initially, add the first (lowest-weight) edge to the set E' of selected edges; for as long E' is not strongly connected, add the next edge to it. If this loop does not terminate then G is not strongly connected. Otherwise, when it stops, after adding, say, edge j, we have found a minimum-difference solution given that we include edge 1. Record this (1, j) solution as the incumbent.
Now remove edge 1 from E', so that edge 2 is the lowest-weight edge remaining in E'. Leave all other already-decided edges in place, and begin adding the next-lowest-weight edges again, starting at edge j+1, until an SCG forms. This can be repeated to compute the minimum-difference solution given that the lowest-weight edge included is edge i, for each i <= |V|. Keep the best overall.
Note that when solving for the starting point i+1, it isn't necessary to get rid of the edges decided for the previous starting point i: If edges i, i+1, ..., j-1 do not form an SCG, then edges i+1, i+2, ..., j-1 do not form an SCG either. Exploiting this means the overall outer loop runs just |E| times, instead of O(|E|^2) times.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Suppose you are given a graph where each edge represents the path cost and each vertex has also a cost which represents that, if you select a path using this node, the cost will be added with the path cost. How can it be solved using Dijkstra’s algorithm?
First solution: duplicate the nodes to add an "internal" edge
Replace the non-oriented edges with oriented edges, and duplicate every node N into one "incoming" node Ni and one "outgoing" node No so that going through a node N in the original graph is equivalent to going through both nodes Ni and No in the new graph, and thus through the extra edge from Ni to No.
A B Ao Ai
\ / \ /
N -----------> Bo - Ni - No - Bi in the new graph, every edge is oriented
| / \ all the edges on this drawing are oriented left-to-right
C Co Ci
Make sure edges between non-twin nodes are always oriented out-->in, and edges between twin nodes are always oriented in-->out. For instance there is an edge from Ni to No (twin nodes), and an edge from Bo to Ni (non twin nodes).
Second solution: add one half of the code of the node to all its edges
Add the cost of the source node to the cost of every one of its edges;
Add the cost of the target node to the cost of every one of its edges;
Add half of the cost of every other node to the cost of its edges.
Now you can check than in any path S-A-B-C-T, the costs of S and T are added once edge, and the half-costs of A, B and C are added twice.
The cost that Dijkstra's algorithm assigns to each vertex is the cost to reach that vertex. You just need to incorporate the vertex costs into that cost calculation.
If only edges have costs, then the cost to reach a vertex is the cost to reach the previous vertex plus the edge cost.
If the vertices also have costs, then the cost to reach a vertex is the cost to reach previous vertex, plus the cost of the edge and the new vertex.
Otherwise, it's exactly the same algorithm.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
suppose we have a weighted grid graph and our problem is to find maximum independent set. There is a greedy algorithm that each time choose the heaviest node and remove it and its neighbors until all nodes of G have been chosen or removed. we want to prove that W(s) >= 1/4 W(T) where S is our greedy result and T is the OPT solution.
let S be the result of our greedy algorithm and T be an arbitrary independent set which can be the OPT. We know that for any T for any node v which belongs to T-S there exist a node v' in S which is neighbor of v and w(v) <= w(v').
Is there any idea to how prove that?
Just use your last statement, and consider T as the maximum independent set and you have this two results :
for every node in T-S like v there exist a node like u in S were W(v) <= W(u).
each node like u in S is at most neighbor of 4 nodes in T.
now use them :)
The desired result can be obtained with the following proof.
Let S be the set generated by the greedy algorithm, let T be an independent set of maximal weight. We will stepwise transform T into S and bound the loss for each transformation step.
Choose v in T\S with maximal weight. By the statement included in the question above, there exists v' in S such that w(v') >= w(v); choose such a v'. Let N be the neighbourhood of v' in T; N contains v and at most 4 vertices (as we have a grid graph). As v was chosen with maximal weight and w(v')>=w(v), we obtain w(v')>=w(N)/4. We set T':=(T\N) and add v' to it. By construction, T' remains an independent set and we have w(T') >= w(T) - (3/4)w(N).
In total, for each exchange step, vertices from T\S get eliminated, but a node from S is added such that the added total weight is at least one quarter of the lost total weight.
Furthermore, the constructed sets N in each step are disjoint, which means that in each step, at least one quarter of w(N) is preserved. In total, as we have constructed S, andS has weight at least (1/4)w(T).
Note that the input graph is not required to be a grid graph but maximum degree 4 is sufficient; furthermore, the proof can be generalized by permitting an abitrary graph, replacing 4 by the maximum degree Δ yielding an approximation ratio of 1/Δ.
I created a program that adds edges between vertices. The goal is to add as many edges as possible without crossing them(ie Planar graph). What is the complexity?
Attempt: Since I used depth first search I think it is O(n+m) where n is node and m is edge.
Also, if we plot the number of edges as a function of n what is it going to look like?
Your first question is impossible to answer, since you have not described the algorithm.
For your second question, any maximal planar graph with v ≥ 3 vertices has exactly 3v - 6 edges.