After I learned how to determine if a directed graph has 1 topological ordering, I am sort of curious if there is a way to determine if there are graphs with exactly 2. First of all is it true that there are graphs with 2 topological sorts?
I learned to use a Hamiltonian path to determine if a DAG has a unique topological sort. Does that somehow apply to this?
Thanks
If at the line (*) you can select from 2 different nodes once - there are two topological orderings
L ← Empty list that will contain the sorted elements
S ← Set of all nodes with no incoming edges
while S is non-empty do
remove a node n from S (*)
add n to tail of L
for each node m with an edge e from n to m do
remove edge e from the graph
if m has no other incoming edges then
insert m into S
if graph has edges then
return error (graph has at least one cycle)
else
return L (a topologically sorted order)
More precisely quoting R. Sedgewick:
A digraph has a unique topological ordering if and only if there is a
directed edge between each pair of consecutive vertices in the
topological order (i.e., the digraph has a Hamiltonian path). If the
digraph has multiple topological orderings, then a second topological
order can be obtained by swapping a pair of consecutive vertices.
Related
How can I know all possible ways that the edges are connected if I know the topological sort?
Here is the original problem:
Now little C has topologically sorted a simple (no heavy edges) directed acyclic graph, but accidentally lost the original. Except for topological sequences, little C only remembers that the original graph had the number of edges k, and that there is one vertex u in the graph that can reach all the other vertices. He wants to know how many simple directed acyclic graphs there are that satisfy the above requirements. Since the answer may be large, you only need to output the remainder of the answer module m.
I have just learned the topological sort. I wonder how I can use it in an upside down way? I know the final toposorted way as (1 2 3 4) and there is one vertex that connects all other vertexes, and there are 4 edges in all, but I need the number of all possible ways that edges are linked.
I think this problem has something to deal with permutation number,and the specific u has to be the first in the toposorted list.
NOTICE the max of m can be up to 200'000,so definitely you can not brute force this problem!!!
Let the topological order be u = 1, 2, …, n. Since 1 can reach all other
vertices, the topological order begins with 1. Each node v > 1, being
reachable from u, must have arcs from one or more nodes < v. These
choices are linked only by the constraint on the number of arcs.
We end up computing Count[v][m] (modulo whatever the modulus is) as
the number of reconstructions on 1, 2, …, v with exactly m arcs. The
answer is Count[n][k].
Count[1][0] = 1 if m == 0 else 0
for v > 1, Count[v][m] = sum for j = 1 to min(m, v-1) of (v-1 choose j)*Count[v-1][m-j]
Given an adjacency list representation for an undirected graph. Write a function to count the number of edges in the undirected graph.
I know the number of edges in an undirected graph is n(n-1)/2 but I don't know how to write a function for that.
Considering that I've a list and using that I'll be counting the number of edges. How should I start?
n(n-1)/2 is the maximum number of edges in a simple undirected graph, not the number of edges for every such graph.
Given that you have an adjacency list representation, let it be the case that vertices u and v have an edge between them. Then, v will appear in the adjacency list of u and u will appear in the adjacency list of v. This is true for any u and v.
Hence, if you count the total number of entries of all the elements in the adjacency list of each vertex, the result will be twice the number of edges in the graph. Dividing this value by two will give the desired result.
I know the number of edges in an undirected graph is n(n-1)/2 but I don't know how to write a function for that.
The maximum number of edges in undirected graph is n(n-1)/2.
not only number of edges.
Suppose the povided array is.
0[1,2,3]
1[0,2]
2[0,1,4]
3[0]
4[2]
then number of edges will be. #java
for (ArrayList<Integer> verts : adj) {
sumOfVert += verts.size();
}
return sumOfVert/2;// Output will be 5;
Given an undirected graph. How do I check if it can be divided into two sets where every node of one set is connected to every other node of its own set (complete graph). A set can be empty or of only one node. No node should be remaining.
Thanks.
EDIT: Edges between two sets is not forbidden.
Basically we have to check if the graph can be divided into two cliques
As commented by #Damien, checking whether vertices of a given graph can be partitioned into two cliques is actually the decision problem of clique cover with k = 2. For general k (even for k = 3), the clique cover problem is known to be NP-complete. For k = 2, there exists a O(n2) algorithm, based on the observation below.
Given a graph G = (V, E), denote its complement as G'. Then V can be partitioned into two cliques if and only if G' is 2-colorable.
The proof is simple and thus omitted here. The sketch of the algorithm is shown below.
01. construct G' from G;
02. if G' is bipartite
03. return true;
04. else
05. return false;
Note that the first line requires O(n2) time, while testing whether G' is bipartite requires only O(n + m) time using BFS, where n is the # of vertices and m is the # of edges. Therefore, the total complexity is O(n2).
The Question is from Code jam.
Question:
Is there any way to divide the nodes of a graph into two group such that any two nodes which can't remain in the same group should be in different group.
Is there any standard algorithm for this?
How should I tackle this problem when each group should have equal element.
First, the feasibility problem (is there such set/ doesn't exist such set) is 2-coloring problem, where:
G = (V,E)
V = { all nodes }
E = { (u,v) | u and v are "troubling each other" }
This problem is solved by checking if the graph is bi-partite, and can be done using BFS.
How to tackle the problem when each group should have equal element.
first, let's assume the graph is bi-partite, so there is some solution.
Split the graph into set of connected components: (S1,S2,S3,...,Sk).
Each connected component is actually a subgraph (Si = Li,Ri) - where Li,Ri are the two sides of the bipartite graph (there is only one such splitting in each connected component, if ignoring the order of Li and Ri).
Create a new array:
arr[i] = |Li| - |Ri|
where |X| is the cardinality of X (number of elements in the set)
Now, solving this problem is same as solving the partition problem, which can be done in pseudo-polynomial time (which is polynomial in the number of nodes).
The solution to partition problem splits each arr[i] to be in A or in B, such that sum{A} is closest as possible to sum{B}. If arr[i] is in A, in your solution, color Li with "1", and Ri with "2". Otherwise - do the opposite.
Solution will be O(k*n+m), where k is number of connected components, n is number of nodes in the graph, and m is number of edges in the graph.
You build a graph from the given nodes (using a hash-table to map names to nodes) and then you use BFS or DFS to traverse the graph and determine if its bipartite (that is, divisibe into two disjoint sets such that a node in one set is only in "trouble" with nodes in the other set, but not with any node in its own set). This is done by assigning a boolean value to each node as its visited by the BFS/DFS and then checking if any of its visited neighbors has the same value, which means the graph is not bipartite (not divisible into two groups).
c > b
d > b
a > d
c > a
Given such transitive relationships, what is the most efficient algorithm to calculate the relation between all of them?
Build a graph, and use topological sort to retrieve the nodes in sorted order.
If you use am adjacency list representation and a hash map for the vertices of your graph, the overall timing will be O(N), where N is the number of relationships that you have. Building the graph is linear in the number of relationships that you have. The topological sort is O(|V|+|E|), where |V| is the number of vertices, and |E| is the number of edges. The number of edges is equal to the number of relationships, i.e. N, and V has the upper bound of 2*N, so it we have O(3*N), which is O(N).
What you are actually describing in your case is a topological sort problem: http://en.wikipedia.org/wiki/Topological_sorting
Where instead of a graph, you have orderings of letters. Each letter represents a node and each comparison operator represents a directed edge. The standard algorithm for computing the topological sort of a pairwise orderings is as follows:
L ← Empty list that will contain the sorted elements
S ← Set of all nodes with no incoming edges
while S is non-empty do
remove a node n from S
add n to tail of L
for each node m with an edge e from n to m do
remove edge e from the graph
if m has no other incoming edges then
insert m into S
if graph has edges then
return error (graph has at least one cycle)
else
return L (a topologically sorted order)
Keep in mind that for you to find an ordering, there can be no cycles. i.e.:
a < b //This
b < c //contains
c < a //a cycle!
This is called a DAG. Directed acyclic graph. See: http://en.wikipedia.org/wiki/Directed_acyclic_graph
Here is an example of how to do it in Python: Topological sort python