DFS on undirected graph complexity - graph

Let's say I have an undirected graph with V nodes and E edges.If I represent the graph with adjacency lists,if I have a representation of an edge between x and y,I must also have a representation of the edge between y and x in the adjacency list.
I know that DFS for directed graphs has V+E complexity.For undirected graphs doesn't it have v+2*e complexity ,because you visit each edge 2 times ?Sorry if it's a noobish question..I really want to understand this think.Thank you,

The complexity is normally expressed O(|V| + |E|) and this isn't affected by the factor of 2.
But the factor of 2 is actually there. One undirected edge behaves just line 2 directed edges. E.g. the algorithm (for a connected undirected graph) is
visit(v) {
mark(v)
for each unmarked w adjacent to v, visit(w)
}
The for loop will consider each edge incident to each vertex once. Since each undirected edge is incident to 2 vertices, it will clearly be considered twice!
Note the undirected DFS doesn't have to worry about restarting from all the sources. You can pick any vertex.

Related

Is there a way to find the minimum complete subgraph in a complete graph?

Given a undirected weighted complete graph G=(V,E) of N vertices, I want to know that if finding the smallest complete subgraph (with minimum edge weights sum) with M vertices (M <= N) is NP-hard or not.
This problem is NP-hard, because the decision form of the clique problem can be reduced to it.
The decision form of the clique problem asks, given an undirected graph and an integer k, whether there is a complete subgraph with k vertices. This problem is NP-hard. Given an instance of this problem, construct a complete weighted graph on the same number of vertices by assigning a weight of 1 to any edge present in the original graph, and a weight of 2 to any edge not present in the original graph. Then a minimum-weight complete subgraph will have every edge weight equal to 1 if and only if there is a complete subgraph of size k in the original graph.

Finding vertices of a undirected graph that are connected in second graph

If I have an undirected graph, G = (V, E), and I want to find the subset S of vertices in G such that all the vertices in S have at least n connections to other vertices in S. What would be the best way to do this?
At first, this task sounds similar to the clique problem. Which might suggest your task will be NP-hard as well.
At second thought, it's actually much simpler and here's an algorithm:
remove all v of degree less than n
repeat (1) on the
restricted G until it's empty or all remaining vertices have degree
n or more
the remaining vertices are your subset S

What is the minimum number of edges that an undirected graph with n vertices must have for it to always be connected?

I know that for an undirected graph with n vertices to be connected it must have n - 1 edges. However, my question is what is the minimum number of edges that it can have for it to always be connected. For example, does a graph with n vertices and n + 2 edges have to be always be connected? If not, what is the number of edges it must have for it to always be connected?
If you allow for repeated connections then there is no maximum number. (For example you have 3 vertices a,b,c and the edge (a,b) appears infinity many times but there is no edge that connects with c). So to make this interesting lets say you can't have repeated connections.
For your case of n+2 edges consider if you had a graph with 10 vertices and its edges form two disjoint copies of k5. k5 has 10 edges so we have a a graph with 10 vertices and 20 edges which works as a counter example to your claim. However if you notice in my example if we don't disconnect any edges you cannot add one without connecting the graph.
Another example we can consider (again with 10 vertices) is k9 and a single vertex. k9 has 36 edges (more than my previous example) and the single vertex makes the graph disjoint. In general your maximal example will be k(n-1) and a single vertex.
km has m(m-1)/2 edges so the maximum number of edges you can have and still have a disjoint graph is (n-1)(n-2)/2. Meaning the minimal number of edges to guarantee a n vertex graph (with no self loops or multiple connections) is (n-1)(n-2)/2 + 1.
the maximum number of edges that a n vertices graph can have to not be connected is n-2.
for a graph having 3 vertices you need atleast 2 edges to make it connected which is n-1 so one edge lesser than that will give you the maximum edges with which graph will be disconnected.
does a graph with n vertices and n + 2 edges have to be always be connected : depends whether self loops are allowed or not for example consider a case of 3 vertices and 5 edges so it will be connected by 2 edges and 3 self loops but fir the case of 4 vertices and 6 edges it's possible also and not possible too.

igraph nonreciprocal edges after converting to undirected graph using mutual

I'm working on a directed graph in igraph for R. I'm trying to convert it to an undirected graph where just reciprocal edges of the former persist. Should be easy but I'm getting strange results.
first I did it like that
library(igraph)
load("dmNet.Rdata")
#http://www.unet.univie.ac.at/~a0406222/dmNet.Rdata
recNet <- as.undirected(net, mode = "mutual",edge.attr.comb="sum")
when I check E(recNet)$weight there are a lot of edges with a weight of 1, which should not be possible since the sum of two reciprocal edges has to be at least 2. Then I did it like that
recNet <- as.undirected(net, mode = "mutual",edge.attr.comb="c")
now I can see that there are actually some edges containing just one value. My new graph recNet seems to contain nonreciprocal edges of net. What am I doing wrong, or am I missunderstandig the "mutual option"?
This happens because some edges are self-loops in your graph. All the edges that end up with a weight of 1 are self-loops:
all(which(E(recNet)$weight == 1) %in% which(is.loop(recNet)))
# [1] TRUE
Apparently, a self-loop is considered as a mutual edge in a directed graph. If you want to consider self-loops as non-mutual, then you can just remove them from the graph. Be careful, though, because some vertices have multiple self-loops, and you might not want to remove these.

What is the maximum number of edges in a directed graph with n nodes? [closed]

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 1 year ago.
Improve this question
What is the maximum number of edges in a directed graph with n nodes? Is there any upper bound?
If you have N nodes, there are N - 1 directed edges than can lead from it (going to every other node). Therefore, the maximum number of edges is N * (N - 1).
Directed graph:
Question: What's the maximum number of edges in a directed graph with n vertices?
Assume there are no self-loops.
Assume there there is at most one edge from a given start vertex to a given end vertex.
Each edge is specified by its start vertex and end vertex. There are n
choices for the start vertex. Since there are no self-loops, there are
n-1 choices for the end vertex. Multiplying these together counts all
possible choices.
Answer: n(n−1)
Undirected graph
Question: What's the maximum number of edges in an undirected graph with n vertices?
Assume there are no self-loops.
Assume there there is at most one edge from a given start vertex to a given end vertex.
In an undirected graph, each edge is specified by its two endpoints
and order doesn't matter. The number of edges is therefore the number
of subsets of size 2 chosen from the set of vertices. Since the set of
vertices has size n, the number of such subsets is given by the
binomial coefficient C(n,2) (also known as "n choose 2"). Using the
formula for binomial coefficients, C(n,2) = n(n-1)/2.
Answer: (n*(n-1))/2
In an undirected graph (excluding multigraphs), the answer is n*(n-1)/2. In a directed graph an edge may occur in both directions between two nodes, then the answer is n*(n-1).
In addition to the intuitive explanation Chris Smith has provided, we can consider why this is the case from a different perspective: considering undirected graphs.
To see why in a DIRECTED graph the answer is n*(n-1), consider an undirected graph (which simply means that if there is a link between two nodes (A and B) then you can go in both ways: from A to B and from B to A). The maximum number of edges in an undirected graph is n(n-1)/2 and obviously in a directed graph there are twice as many.
Good, you might ask, but why are there a maximum of n(n-1)/2 edges in an undirected graph?
For that, Consider n points (nodes) and ask how many edges can one make from the first point. Obviously, n-1 edges. Now how many edges can one draw from the second point, given that you connected the first point? Since the first and the second point are already connected, there are n-2 edges that can be done. And so on. So the sum of all edges is:
Sum = (n-1)+(n-2)+(n-3)+...+3+2+1
Since there are (n-1) terms in the Sum, and the average of Sum in such a series is ((n-1)+0)/2 {(last + first)/2}, Sum = n(n-1)/2
If the graph is not a multi graph then it is clearly n * (n - 1), as each node can at most have edges to every other node. If this is a multigraph, then there is no max limit.
Putting it another way:
A complete graph is an undirected graph where each distinct pair of vertices has an unique edge connecting them. This is intuitive in the sense that, you are basically choosing 2 vertices from a collection of n vertices.
nC2 = n!/(n-2)!*2! = n(n-1)/2
This is the maximum number of edges an undirected graph can have. Now, for directed graph, each edge converts into two directed edges. So just multiply the previous result with two. That gives you the result: n(n-1)
In a directed graph having N vertices, each vertex can connect to N-1 other vertices in the graph(Assuming, no self loop). Hence, the total number of edges can be are N(N-1).
In the graph with self loop
max edges= n*n
such as we have 4 nodes(vertex)
4 nodes = 16 edges= 4*4
There can be as many as n(n-1)/2 edges in the graph if not multi-edge is allowed.
And this is achievable if we label the vertices 1,2,...,n and there's an edge from i to j iff i>j.
See here.
The correct answer is n*(n-1)/2. Each edge has been counted twice, hence the division by 2. A complete graph has the maximum number of edges, which is given by n choose 2 = n*(n-1)/2.
Can also be thought of as the number of ways of choosing pairs of nodes n choose 2 = n(n-1)/2. True if only any pair can have only one edge. Multiply by 2 otherwise
Undirected is N^2. Simple - every node has N options of edges (himself included), total of N nodes thus N*N

Resources