I am aware that the problem has been discussed here more than once. However, I need to find the maximum number K of vertex-disjoint paths in a directed graph with a running time of |V| x |E|.
I know the algorithm of transforming each vertex into v_in, v_out and adding an edge with capacity 1 from v_in to v_out and for each pair of vertices (u,v) add an edge with capacity 1 from u_out to v_in and then compute the max flow in this network. However, after my calculations this algorithm takes O(E) preprocessing + O(VE^2) or O(V^2E) for max flow. Am I doing something wrong?
Use James B Orlin's + KRT (King, Rao, Tarjan)'s algorithm instead of Ford Fulkerson to calculate flow and it'll be O(V*E). (See https://en.wikipedia.org/wiki/Maximum_flow_problem)
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 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).
I have a random graph G(n, p) with n = 5000 vertices and an edge probability of p = 0.004.
I wonder what would be the expected number of edges in the graph but I have not much knowledge in probability-theory.
Can anyone help me?
Thank you so much!
EDIT:
If pE is the number of possible edges in the Graph, wouldn't I have to calculate 0.004 * pE to get the expected number of edges in the graph?
First, ask yourself the maximum number of possible edges in the graph. This is when every vertex is connected to every single other vertex (nC2 = n * (n-1)/2), assuming this is an undirected graph without self-loops).
If each possible edge has a likelihood of 0.004, and the # of possible edges is n(n-1)/2, then the expected number of edges will be 0.004*(n(n-1)/2).
The number of expected vertices depend on the number of nodes and the edge probability as in E = p(n(n-1)/2).
The total number of possible edges in your graph is n(n-1) if any i is allowed to be linked to any j as both i->j and j->i. I am your friend, you are mine. If the graph is undirected (and an edge only means that we are friends) the total number of edges drop by half: n(n-1)/2 since i->j and j->i are the same.
The multiplication with p gives the expected number of edges, since every possible edge has become real or not depending on the probability. p=1 gives n(n-1)/2 edges since every possible edge actually happened. For graphs with p<1, the actual edge count might (obviously) differ from time to time if you were to actually generate a random graph using the p and n of your choice. Expected edge count will however be the most common observed edge count if you were to generate an infinite number of random graphs. NetLogo is a very pedagogical tool if you want to generate random graphs and get a feel for how network measurements arise from random graphs of different structures.
The graph is positive weighted and might be acyclic or not.
input file consists of
vertex number, edge number, begining vertex, ending vertex
edge1(from, to, weight)
edge2(from, to, weight)
and so on.
the length of the path will be infinite if there is cycle in the graph and will be 0 if there is no way
the way I do is that I remove the same edges with less lengths and use bellman ford or dijkstra's algorithm in adjecent list or matrix and both work fine.
however, program should find the path at most 2 seconds and some input files contain 10000 vertices and 100000 edges
what should I do?
The time limit is 2 sec, it means the program should have near about ~10^6 iterations. See the limits of V = 10000 and e = 100000. This means an algorithm of O(V) or O(E) or O(V + E) or even O(E + VlogV)will easily compute your requirement well in given time.
Note E + Vlogv ~ (100000 + ~132877) which is less than 10^6
// This 10^6 limit is quit good for processors with 10^9 Hz frequency. So, even if your algo has 1000 instructions for each iteration, you will be in safe zone.
So, here is the proposed algorithm:
You will build the graph in O(E). Use Adjacency list data structure to represent the graph.
-> While building this data structure, store indegree of each vertex and also store count of vertices.
countV := V;
foreach edge_i
inDegree[to] := inDegree[to] + 1;
Check if there is cycle in the graph in O(V + E).
if no vertex with indegree = 0 and countV = 0
graph has no cycle
else if no vertex with indegree = 0 and countV != 0
graph has cycle
else select any vertex having 0 indegree
countV := countV - 1;
decrease all its directed neighbours' inDegree by 1.
So if you get a cycle, your answer is directly infinite.
Make a BFS or DFS to get whether the ending vertex is reachable from beginning vertex or not. This can be done in O(V + E) or even O(E). Let us take O(V + E). If not reacable your answer is directly 0.
Now, apply dijkstra but in relax condition, just check the opposite. i.e in the pseudocode given here, instead of doing
if alt < dist[v]:
dist[v] := alt;
do
if alt > dist[v]:
dist[v] := alt;
This can be done in O(E + VlogV). Hence overall complexity of the solution will be O(E + VlogV) which is well in constraints.
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