An undirected graph having n edges, then find out no. of vertices that graph have? - data-structures

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.

Related

How to get the maximum sub-complete-graph from a complete graph?

Suppose we have a N-complete graph G(V, E), which is undirected and weighted. Given K, how could we find the sub-graph G'(V', E'), which is also a complete graph and |V'| = K and the sum of all edges in E' is maximum.
Since the original graph is complete, so you can just sum the weights for all the nodes, and keeps only the maximum K nodes and their edges.
Is this right? If not, you can delete the node with minimal weight sum each time, calculate weights sums again, this is more time consume.

Do connected graphs with more than N-1 edges always contain connected graph with N-1 edges?

We know that:
If we have N vertices
To build a connected undirected graph, you'll need at least N-1 edges.
Let M be the set of possible connected undirected graphs with N-1 edges.
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Can we prove or disprove that if there's an undirected connected graph with more than N-1 edges, it must contain one of the graphs in M? In other words, can we take one of the graphs in M and add edges to create this new graph?
(by "containing", I mean that it has all the edges of the other graph plus some more.)
Can we prove or disprove that if there's an undirected connected graph with more than N-1 edges, it must contain one of the graphs in M?
Assuming that undirected connected graph g with more than N-1 edges has N vertices, the answer is "yes".
You can prove it by constructing a Spanning Tree of g, which is a subgraph with N vertices and N-1 edges. The problem statea that M contains all such graphs, a spanning tree of g is a member of M. Since a spanning tree is constructed by removing edges from g, you can add these edges back, thus going from a member of M back to the original graph g.
No, this isn't necessarily the case. As an example, imagine a path graph with 2n nodes (and, therefore, 2n - 1 edges). Cut out the middle edge, splitting the graph into two connected components that are each path graphs. Both of these paths have n - 1 edges, but neither connected component is a connected graph with 2n - 2 edges.

Algorithm to Maximize Degree Centrality of Subgraph

Say I have some graph with nodes and undirected edges (the edges may have a weight associated to them).
I want to find all (or at least one) connected subgraphs that maximize the sum of the degree centrality of all nodes in the subgraph (the degree centrality is based on the original graph) under the constraint that the sum of the weighted edges is < X.
Is there an algorithm that will do this?
A quick search took me to this description of degree centrality. It turns out that the "degree centrality" of a vertex is simply its degree (neighbour count).
Unfortunately your problem is NP-hard, so it's very unlikely that any algorithm exists that can solve every instance quickly. First notice that, assuming edge weights are positive, the edges in any optimal solution necessarily form a tree, since in any non-tree you can delete at least 1 edge without destroying connectivity, and doing so will decrease the total edge weight of the subgraph. (So, as a positive spinoff: If you compute the minimum spanning tree of your input graph and find that it happens to have total weight < X, then you can simply include every vertex in the graph in your solution.)
Let's formulate a decision version of your problem. Given a graph G = (V, E) with positive (I'll assume) weights on the edges, a number X and a number Y, we want to know: Does there exist a connected subgraph G' = (V', E') of G such that the sum of the edge weights in E' is at most X, and the sum of the degrees of V' (w.r.t. G) is at least Y? (Clearly this is no harder than your original problem: If you had an algorithm to solve your problem, then you could just run it, add up the degrees of the vertices in the subgraph it found and compare this to Y to answer "my" problem.)
Here's a reduction from the NP-hard Steiner Tree in Graphs problem, where we are given a graph G = (V, E) with positive weights on the edges, a subset S of its vertices, and a number k, and the task is to determine whether it's possible to connect the vertices in S using a subset of edges with total weight at most k. (As I showed above, the solution will necessarily be a tree.) If the sum of all degrees in G is d, then all we need to do to transform G into an input for your problem is the following: For each vertex s_i in S we add enough new "ballast" vertices that are each connected only to s_i, via edges with weight X+1, to bring the degree of s_i up to d+1. We set X to k, and set Y to |S|(d+1).
Now suppose that the solution to the Steiner Tree problem is YES -- that is, there exists a subset of edges having total weight <= k that does connect all the vertices in S. In that case, it's clear that the same subgraph in the instance of your problem constructed above connects (possibly among others) all the vertices in S, and since each vertex in S has degree d+1, the total degree is at least |S|(d+1), so the answer to your decision problem is also YES.
In the other direction, suppose that the answer to your decision problem is YES -- that is, there exists a subset of edges having total weight <= X ( = k) that connects a set of vertices having total degree at least |S|(d+1). We need to show that this implies a YES answer to the original Steiner Tree problem. Clearly it suffices to show that the vertex set V' of any subgraph satisfying the conditions above (i.e. edges have total weight <= k and vertices have total degree >= |S|(d+1)) contains S (possibly among other vertices). So let V' be the vertex set of such a solution, and suppose to the contrary that there is some vertex u in S that is not in V'. But then the largest sum of degrees that we could possibly make would be to include all other non-ballast vertices in the graph in V', which would give a degree total of at most (|S|-1)(d+1) + d (the first term is the degree sum for the other vertices in S; the second is an upper bound on the degree sum of all non-S vertices in G; note that none of the ballast vertices we added in could be in the subgraph, because the only way to include any of them is to use an edge of weight X+1, which we obviously can't do). But clearly (|S|-1)(d+1) + d = |S|(d+1) - 1, which is strictly less than |S|(d+1), contradicting our assumption that V' has a degree total at least |S|(d+1). So it follows that S is a subset of V', and thus that it is possible to use the same subset of edges to connect the vertices in S for a total weight of at most k, i.e. that the answer to the Steiner Tree problem is also YES.
So a YES answer to either problem implies a YES answer to the other one, in turn implying that a NO answer to either implies a NO answer to the other. Thus if it were possible to solve the decision version of your problem in polynomial time, it would imply a polynomial-time solution to the NP-hard Steiner Tree in Graphs problem. This means the decision version of your problem is itself NP-hard, and so is the optimisation version (which as I said above is at least as hard). (The decision form is also NP-complete, since a YES answer can be easily verified in polynomial time.)
Sidenote: At first I thought I had a very straightforward reduction from the NP-hard Knapsack problem: Given a list of n weights w_1, ..., w_n and a list of n profits p_1, ..., p_n, make a single central vertex c, and n other vertices v_1, ..., v_n. For each v_i, attach it to c with an edge of weight w_i, and add p_i other leaf vertices, each attached only to v_i with an edge of weight X+1. However this reduction doesn't actually work, because the profits can be exponential in the input size n, meaning that the constructed instance of your problem might need to have an exponential number of vertices, which isn't allowed for a polynomial-time reduction.

Total cycles in a Undirected graph

Given an undirected graph with N vertices and M edges I need to find the number of cycles in the graph. But there is a constraint.
Here is an example of it:
Consider this graph with 6 vertices and 7 edge pairs :- A-B , B-C , C-F , A-D , D-E , E-F , B-E.
Here is the image for better understanding :
Then here 2 cycles should be counted that are A-B-E-D-A and B-C-F-E-B but not A-B-C-F-E-D-A
So I need to find the count of the total cycles in the graph.
I think you are looking for a cycle basis of your graph. You do that by finding any spanning tree of the graph (for example a DFS or BFS tree). The non-tree edges of the graph represent a cycle basis: If you connect the endpoints by the unique path through the tree, you get an element of the basis.
So if the graph is connected, the number of basis elements is m - n + 1 (m = number of edges, n = number of nodes). If it's not connected, just decompose it into connected components and sum up the number of basis elements of the components. You get something like m - n + c where c is the number of connected components. Thus, if you're not interested in the actual cycles and only in their count, you just need to find the number of connected components. You can use DFS or BFS for that as well.

Find the maximum number of edges in the graph

There are 'n' vertices and 0 edges of an undirected graph. What can be the maximum number of edges that we can draw such that the graph remains disconnected.
I have made the solution that we can exclude one vertex and can find the maximum number of edges between n-1 vertices of undirected graph, so that the graph still remains disconnected.
which is n(n-1)/2 for n vertices and will be (n-1)(n-2)/2 for n-1 vertices.
Can there be a better solution?
You can resolve this using analysis. Take your idea and generalize it. You divide the n vertices in two groups , of size x and n-x.
Now the number of edges is a function of x, expressed by
f(x)= x(x-1)/2 + (n-x)(n-x-1)/2
f(x) = 1/2(2x^2 - 2nx +n^2 - n)
The value which maximize this function is the partition size you want. If you make calculation you find that it decrease from x=0 to x=n/2, then increase to x=n. As x = 0 or x = n means the graph is collected, you take the next greatest value which is x=1. So your intuition is optimal.
Your solution should be the best solution.
Because any new edge added must have the nth vertex at one end.
If graph can have multi edges, answer is infinity for n>=3.
If it can also contain self-loops, answer is infinity for n>=2,
If none of those holds your solution is correct.

Resources