I am looking at code to find complexity of deep first search (DFS). How does O(|V| + |E|) differ from O(V+E). What is the meaning of |·| around V and E? Does it mean the "sum of all vertices" is represented as |V| ?
V and E are a sets. So O(E) would be undefined. |·| gives you the number of elements in the set, it's called cardinality in Maths. For a graph G = (V,E) |E| is the number of edges, |V| is the number of vertices.
When people write O(V+E) they actually mean O(|V| + |E|) and most readers will understand it that way, because it is the only plausible explanation. Still it is unclean and I would not use it myself.
|V| typically means the cardinality (number of elements) in V.
So O(|V| + |E|) means "on the order of the number of elements in V plus the number of elements in E."
Related
I have an undirected graph G. Each time I want to pick a node v and remove all its neighbors from G. I want to pick the maximum number of vs until G becomes empty.
For example, given three-node graph A -- B -- C. The optimal strategy is to pick A and C. The worst strategy is to pick B only.
My first attempt is brute-force, but its time complexity is apparently 2 to the power of the number of vertices, too high. Is there any polynomial algorithm to reach the goal?
Constraint: the number of edges is less than 10% of the number of vertices.
Thanks in advance!
I wonder why graph algorithms time and space complexities are mostly represented using |E| and |V| instead of V and E. Every other algorithm's time and space complexities are represented using normal alphabets like
N or NlogN. Why the graph algorithms are represented in the mod(|E|) like way?
In graph algorithms, both E and V are sets, not numbers. E is the set of edges and V is the set of vertices.
The complexity function is of numerical values. There is no meaning in log(E) or E^2, unless you refer to the size of the set. This is exactly what the absolute value does for sets, |X| means the size of the set X. This means, |E| and |V| respectively are the number of edges and vertices in the graph.
When you usually see n, it means the size of the input. In graphs, we split it to |V| and |E|.
A common notation for graphs is using |V|=n and |E|=m, and then you use n,m, like you are used to in other places.
I am looking for a sequence of inputs for the Dijsktra algorigthm implemented with a regular heap, where Dijsktras actual complexity would be Θ((e+v)logv).
I know how to implement Dijsktra and how it works, I also understand that the most time consuming operations are adding a vertex to the heap and changing the distance of a vertex. However, I am not sure how to find a graph (sequence of graphs) that would be the worst case inputs for Dijkstra.
Also if you had any general tips on how to find a sequence of inputs for the worst case complexity, that would be helpful.
Let vertices be numbered from 1 to n and you want to find path from vertex 1 to vertex n. Let e[i][j] be length of edge, connecting i and j. Initially e[1][2] = e[2][3] = ... = e[n - 1][n] = 1. Now we iterate through the vertices from n - 2 to 1. In i-th vertex for each j in [i + 2, n] we make e[i][j] = e[i][i + 1] + e[i + 1][j] + 1.
Now we have full graph. In each iteration dijkstra will update O(n) vertices, so it's O(n ^ 2) = O(E) actions working in O(log n).
So final asymptotics will be O(n log(n) + E log(n))
Given a graph G,which are the sufficient and necessary conditions , so that this graph has a unique minimum spanning tree?In addition , how can I proove these conditions?
So far , I had found that those conditions are :
1)For every partition of V(G) into two subsets, the minimum weight edge with one endpoint in each subset is unique.
2)The maximum-weight edge in any cycle of G is unique.
But I am not sure if this is correct.Even in case this is correct,I cannot prove its correctness.
Actually, there is a necessary and sufficient condition for unique MST. In the book A First Course In Graph Theory, it is given as an exercise:
Exercise 4.30
Let G be a connected weighted graph and T a minimal spanning tree of G. Show that T is a unique minimal spanning tree of G if and only if the weight of each edge e of G that is not in T exceeds the weight of every other edge on the cycle in T+e.
I write my proof here.
This is false because at least the first condition is not necessary. The proof is by counterexample (source).
Take G to be any tree where all edge weights are 1. Then G has a
unique MST (itself), but any partition with more than one edge
crossing it has several minimum weight edges.
EDIT:
In response to your modified question...
There is a well-known sufficient (but not necessary) condition for the uniqueness of a MST:
If the weight of each edge in a connected graph is distinct, then the graph contains exactly one (unique) minimum spanning tree.
The proof is as follows (source):
For the sake of contradiction, suppose there are two different MSTs of
G, say T1 and T2. Let e = v-w be the min weight edge of G that is in
one of T1 or T2, but not both. Let's suppose e is in T1. Adding e to
T2 creates a cycle C. There is at least one edge, say f, in C that is
not in T1 (otherwise T1 would be cyclic). By our choice of e, w(e) ≤
w(f). Since all of the edge weights are distinct, w(e) < w(f). Now,
replacing f with e in T2 yields a new spanning tree with weight less
than that of T2 (contradicting the minimality of T2).
However, regarding "sufficient and necessary" conditions for the uniqueness of a MST, I do not believe any are known to exist.
I know there's a similar question in stack overflow, where one person has asked, why time complexity of BFS/DFS is not simply O(V).
The appropriate answer given was that E can be as large as V^2 in case of complete graph, and hence it is valid to include E in time complexity.
But, if V cannot be greater than E+1. So, in that case not having V in the time complexity, should work?
If it is given that E = kV + c, for some real constants k and c then,
O(E + V) = O(kV + c + V) = O(V) = O(E) and your argument is correct.
An example of this is trees.
In general (i.e., without any prior information), however, E = O(V^2), and thus we cannot do better than O(V^2).
Why not write just O(E) always?
EDIT: The primary reason for always writing O(E + V) is to avoid ambiguity.
For example, there might be cases when there are no edges in the graph at all (i.e. O(E) ~ O(1)). Even for such cases, we'll have to go to each of the vertex (O(V)), we cannot finish in O(1) time.
Thus, only writing O(E) won't do in general.
V has to be included because both BFS and DFS rely on arrays of size |V| to track which vertices have been processed/discovered/explored (whatever the case may be). If a graph has 0 edges and 100000 vertices, such arrays will still take more time to initialize than they would if there were only 5 vertices. Thus, the time complexities of BFS and DFS scale on |V|.