Time complexity of DFS in undirected graph - graph

Lets say we have 4 nodes and all nodes are connected to all other nodes, and I need to go from node1 to node4. Most resources i check the time complexity is O(V+E), but I am kind of confused.
node1
|
node2 / node3 / node4 level2 3
|
node1 / node3 / node4 .................. level3 3 to power of 2
so complexity is N to the power of N if all node will be visited. Even using a hashset to keep track of which nodes have been visited using backtracking, it should not impact the overall time complexity?

The complexity would not be NN if you visit all nodes from each node, it would be N2, if N here is your number of nodes.
Now it so happens that a complete graph (a graph where all nodes are connected to all nodes, like in your example) has a number of edges |E| = O(|V|2). If you don't know why, just note that each node is connected to (N - 1) other nodes so the exact number of edges is (N - 1)2 = N2 - 2N + 1 = O(N2).
The time complexity of Depth-first search is, like you say, O(|V| + |E|). In big-O notation, this is just the same as O(max(|V|, |E|)), because only the dominant term will matter. So for our complete graph, |E| dominates and the complexity is just O(|E|).
As we previously stated, |E| = O(|V|2), so our complexity is O(|E|) = O(|V|2).
Does this provide some clarity to your confusion?

Related

Graph Theory (degree of the vertices of a graph when it can be partitioned into two trees)

Show that if the edge set of a graph G(V,E) with n nodes
can be partitioned into 2 trees,
then there is at least one vertex of degree less than 4 in G.
...................................................................................
I have tried to prove this problem with the help of the method of contradiction.
Assume that all vertices of the graph G has degree >= 4.
Assume the graph G is partitioned into two trees T1 and T2.
With the help of the above assumptions the only observation I could make is that for every vertex v in G
degree of v must be greater than or equal to 2 in either T1 or T2.
I don't know how proceed with this. Please help.
If my approach for solving this problem is wrong then please provide a different solution.
You started with a good approach. Lets assume all vertices in G has degree of 4 (or above) and sssume the graph G is partitioned into two trees T1 and T2.
We know that number of edge in tree is n-1 (when n is number of vertices). Therefor in each of T1 and T2 we have n-1 edges (consider n to be |V|) -> combine we have 2n-2 edges in G -> |E| = 2n-2
From the other hand we know that each v in G -> d(v) > 4 . And we know that sum of degree in graph equal to 2|E|. therefor, 2*|E| >= 4*n (I took the minimum degree for each vertex and each edge contribute 2 to the sum of the degree). So we got |E| >= 2*n.
Contradiction -> There is have to be one vertex with degree less then 4

How to design a pseudocode to detect if an non-directed graph has a C4 cycle with runtime O (| V³ |)

This case is specific at runtime O (|V³|) and to find a C4 cycle. Can someone help me? Thanks!
Find all cycles in the graph.
For each cycle:
Generate a forest of edge distinct depth- (or breadth-) first search trees rooted from vertices on the cycle and terminating when it reaches any vertex on the cycle.
The paths connecting points on the cycle are given by the roots and leaves of the DFS trees.
If there exists two (vertex and edge distinct) paths, u1->v1 and u2->v2, such that the vertices are ordered u1 < u2 < v1 < v2 around the cycle (in either direction) then the graph has a complete K4 minor.
Johnson's algorithm to find elementary cycles is O((v+e)(c+1)) for v vertices, e edges and c elementary cycles.
A DFS (or BFS) is O(v+e) so repeating it for each cycle is also O((v+e)c).
You then need to order the paths and find distinct paths with alternating end-points (for each cycle) which may be possible in the complexity you require.

Number of broadcasts to allow 95% of node pairs to exchange in directed graph

I have a directed unweighted graph with N nodes and E edges. Nodes are of an average degree 2E/N.
In the first round, nodes each broadcast their information to all their neighbours. In subsequent rounds, nodes broadcast the information received from their neighbours during the previous round to all other neighbours, and so forth.
The graph is not guaranteed to be acyclic.
My question is: how many consecutive rounds of broadcast are required, on average, for 95% of node pairs to have reached one another? Is it possible to calculate an approximate figure based on the average degree of the graph?
By average, I assume you mean average over all possible (N,E) directed graphs with no multiple edges.
Theorem 1
If E <= (N-1)^2, there will be at least one graph where information won't propagate.
Proof
A directed graph with N nodes has up to N(N-1) edges. Consider a complete graph, select a node, and remove all its outgoing edges (Alternatively, we can remove all its incoming edges). Information from this node cannot propagate, and we are left with N(N-1)-(N-1) = (N-1)^2 edges.
Corollary 1
When E <= (N-1)^2, there is at least one graph where information cannot propagate, therefore the average number of rounds is infinite.
Theorem 2a
If E > (N-1)^2 the maximal number of rounds is 2.
Proof
A directed graph with N nodes and E > (N-1)^2 edges is a complete graph where up to (N-2) edges removed.
If we want to remove edges from a complete graph such the the number of rounds will be 3 (e.g. from node A to node B), we'll need to make sure there is no node B and edges A->B and B->C. This means that we need to remove at least one edge (either A->B or B->C) for each of the (N-2) possible 'B' nodes. We also need to remove the direct A->C edge. In total we need to remove (N-3) edges.
Theorem 2b
If E > (N-1)^2 the minimal number of rounds is 2.
Proof
Trivial. The graph is incomplete, therefore there is at least one path of length 2.
Corollary 2
if (N-1)^2 < E < N(N-1), the number of rounds is 2.
Theorem 3
If E = N(N-1), the number of rounds is 1
Proof
Trivial. Complete graph.
Now, you are asking about more than 95% of the node pairs.
Of course we can build some (N-1)^2 < E < N(N-1) graphs, where >= 95% of ordered-node-pairs can communicate in 1 round, but the other ordered-node-pairs communicate in 2 rounds.
This is trivial if you consider a complete directed graph of 6 nodes where only one edge is removed. (6*5-1) / (6*5) = 96.66% of the ordered-node-pairs can communicate in one round.
Why do you ask specifically about 95%? Is it important to derive calculations for exactly this number? Let us know. I don't think that you can derive a simple accurate generic formula, especially when N and E are small. Maybe we can formulate something asymptotically (for very large N).

Acyclic Directed Graphs and the edges

Okay, I know that a directed acyclic graph (DAG) has E=V-1 edges. E = number of edges. V = number of vertices.
So the question is, "In a directed graph G, the number of edges is always less than the number of vertices." True or false?
Thanks for the help.
Assume N vertices/nodes, and let's explore building up a DAG with maximum edges. Consider any given node, say N1. The maximum # of nodes it can point to, or edges, at this early stage is N-1. Let's choose a second node N2: it can point to all nodes except itself and N1 - that's N-2 additional edges. Continue for remaining nodes, each can point to one less edge than the node before. The last node can point to 0 other nodes.
Sum of all edges: (N-1) + (N-2) + .. + 1 + 0 == (N-1)(N)/2

What is the number of all possible non-cyclic simple paths in a fully-connected directed graph?

Let's say we have a fully connected digraph G with N vertices and M edges.
How many edges does the graph have? Is it M = N^2?
If we take one vertex and start visiting its neighbors in a 'depth-first search' manner and avoiding loops, how many non-cyclic simple paths will we get?
For example, if we start from vertex 1 in a graph of 4 vertices, here are the paths:
- 1
- 1,2
- 1,3
- 1,4
- 1,2,3
- 1,2,4
- 1,3,2
- 1,3,4
- 1,4,2
- 1,4,3
Is it N! or more for a graph with N vertices? I could not find a way to generalize this and to derive a usable formula.
If your graph is full, there are n! simple paths for each vertex, so total of n*n! simple paths in the graph.
let a starting vertex be v_1.
There are |V| possibilities what to do next: move to one of each V\{v_1}, or stop.
next you have |V|-1 possibilities: move to one of each V\{v_1,v_2} [where v_2 is the node chosen as second] or stop.
... [do induction to formally prove it here]
after you have a path of n nodes, there is one only possibility: stop.
giving you total of n*(n-1)*...*1 = n! possible simple paths for each vertex, and n*n! total possible simple paths in the graph

Resources