Is there any Algorithm exists for counting unique pair (pair of vertices) in undirected graph (vertices repetition not allowed). I think it could be a variation of bipartite graph but if there is any better way to find out .. please comment.
[I think Problem Belongs to Perfect Matching Algorithm]
Problem Statement:
I have an undirected graph which consists of n vertexes and m edges. I can delete edges from the graph. Now I'm interested in one question : is it possible to delete edges in the graph so that the degree of each vertex in the graph will be equal 1.. There can be multiple edges in the graph, but can not be any loops
Example: n = #vertices, m = #edges
n = 4, m = 6
1 2
1 3
1 4
2 3
2 4
3 4
Unique sequence could be (1 2, 3 4) (1 4, 2 3) (1 3, 2 4)
The set of edges that covers the entire graph without using the same vertex multiple times is called a matching or independent edge set, see wikipedia.
In that article is also mentioned that the number of distinct matchings in a graph (which is the number you are after) is called the Hosoya index, see this wikipedia article.
Algorithms to compute this number are not trivial and Stack Overflow wouldn't be the right place to try to explain them, but I at least I hope you have enough pointers to investigate further.
Here is pseudo code, it should run in O(|E|) time, i.e. linear of number of edges :
Suppose G = (V, E) is your initial graph, with E - initial set of all edges
count = 0;
while(E is not empty) {
//1. pick up any edge e = (n1, n2) from E
//2. remove e from G
E = E - e;
//3. calculate number of edges in G remaining if nodes n1 and n2 were removed
// -> these are edges making pair with e
edges_not_connected_to_e = |E| - |n1| - |n2|;
// where |n1| - degree of n1 in updated G (already without edge e)
//4. update the count
count += edges_not_connected_to_e;
}
return count;
Let me know if you need more clarifications. And probably someone could fix my Graph math notations, in case they are incorrect.
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]
There is a directed graph (which might contain cycles), and each node has a value on it, how could we get the sum of reachable value for each node. For example, in the following graph:
the reachable sum for node 1 is: 2 + 3 + 4 + 5 + 6 + 7 = 27
the reachable sum for node 2 is: 4 + 5 + 6 + 7 = 22
.....
My solution: To get the sum for all nodes, I think the time complexity is O(n + m), the n is the number of nodes, and m stands for the number of edges. DFS should be used,for each node we should use a method recursively to find its sub node, and save the sum of sub node when finishing the calculation for it, so that in the future we don't need to calculate it again. A set is needed to be created for each node to avoid endless calculation caused by loop.
Does it work? I don't think it is elegant enough, especially many sets have to be created. Is there any better solution? Thanks.
This can be done by first finding Strongly Connected Components (SCC), which can be done in O(|V|+|E|). Then, build a new graph, G', for the SCCs (each SCC is a node in the graph), where each node has value which is the sum of the nodes in that SCC.
Formally,
G' = (V',E')
Where V' = {U1, U2, ..., Uk | U_i is a SCC of the graph G}
E' = {(U_i,U_j) | there is node u_i in U_i and u_j in U_j such that (u_i,u_j) is in E }
Then, this graph (G') is a DAG, and the question becomes simpler, and seems to be a variant of question linked in comments.
EDIT previous answer (striked out) is a mistake from this point, editing with a new answer. Sorry about that.
Now, a DFS can be used from each node to find the sum of values:
DFS(v):
if v.visited:
return 0
if v is leaf:
return v.value
v.visited = true
return sum([DFS(u) for u in v.children])
This is O(V^2 + VE) worst vase, but since the graph has less nodes, V
and E are now significantly lower.
Some local optimizations can be made, for example, if a node has a single child, you can reuse the pre-calculated value and not apply DFS on the child again, since there is no fear of counting twice in this case.
A DP solution for this problem (DAG) can be:
D[i] = value(i) + sum {D[j] | (i,j) is an edge in G' }
This can be calculated in linear time (after topological sort of the DAG).
Pseudo code:
Find SCCs
Build G'
Topological sort G'
Find D[i] for each node in G'
apply value for all node u_i in U_i, for each U_i.
Total time is O(|V|+|E|).
You can use DFS or BFS algorithms for solving Your problem.
Both have complexity O(V + E)
You dont have to count all values for all nodes. And you dont need recursion.
Just make something like this.
Typically DFS looks like this.
unmark all vertices
choose some starting vertex x
mark x
list L = x
while L nonempty
choose some vertex v from front of list
visit v
for each unmarked neighbor w
mark w
add it to end of list
In Your case You have to add some lines
unmark all vertices
choose some starting vertex x
mark x
list L = x
float sum = 0
while L nonempty
choose some vertex v from front of list
visit v
sum += v->value
for each unmarked neighbor w
mark w
add it to end of list
I'm trying to solve pretty complex problem with graphs, namely we have given undirected graph with N(N <= 10)nodes and M (M <= 25)edges.
Let's say we have two sets of edges A and B, we can't have two same edges in both A and B, also there can be edges that wont be used in the any of those sets, each edge is assigned value to it. We want to minimize the total sum of the two trees.
Please note that in both sets A and B the edges should form connected graph with all N nodes.
Example
N = 2, M = 3
Edges: 1 - 2, value = 10, 1 - 2, value: 20, 2 - 1, value 30, we want to return the result 30, in the set A we take the first edge and in set B the second edge.
N = 5
M = 8
Edges: {
(1,2,10),
(1,3,10),
(1,4,10),
(1,4,20),
(1,5,20),
(2,3,20),
(3,4,20),
(4,5,30),
}
set A contains edges {(1,2,10), (1,3,10), (1,4,10), (1,5,20)}
while set B contains {(1,4,20), (2,3,20), (3,4,20), (4,5,30)}
What I tried
Firstly I coded greedy solution, I first generated the first minimum spanning tree and then I generated with the other edges the second one, but it fails on some test cases. So I started thinking about this solution:
We can see that we want to split the edges in two groups, also we can see that in each group we want to have N - 1 edges to make sure the graph doesn't contain not-wanted edges, Now we see that in worse-case we will use (N-1) + (N-1) edges, that is 18 edges. This is small numbers, so we can run backtracking algorithm with some optimizations to solve this problem.
I still haven't coded the backtracking because I'm not sure if it will work, please write what do you think. Thanks in advance.
So I need to solve this graph, I have a general idea on how to solve it but if I am doing this wrong please correct me.
So to find the MST I need to perform Kruskals algorithm on the graph
here is my psuedocode for this Kruskals algorithm
Kruskal(V,E)
A = null;
for each v contains in V
make disjoint set(v)
Sort E by weight increasingly
for each(v1, v2) contains in E
if
Kruskal(V,E)
A = null;
for each v contains in V
make disjoint set(v)
Sort E by weight increasingly
for each(v1, v2) contains in E
if Find(v1) is not equal to Find(v2)
A = A Union {(v1,v2)}
Union(v1,v2)
Return A
So first thing I do is find the nodes with the shortest distances right?
1)
I assume S to H has the shortest distance since the d(h,s) = -3
so A = {(h,s)}
so now we follow this pattern
2) A = {(h,s),(s,f)}
3) A = {(h,s),(s,f)(s,n)}
4) A = {(h,s)(s,f)(s,n)(f,k)}
5) A = {(h,s)(s,f)(s,n)(f,k)(s,m)} (we skip H to N because a path is already made from h to n which is through s )
6) A = {(h,s)(s,f)(s,n)(f,k)(s,m)(d,b)}
7) A = {(h,s)(s,f)(s,n)(f,k)(s,m)(d,b)(b,m)}
so now since there is a path that connects to all edges we are good right?
but the thing i dont understand is there there are distance[u,v] which is shorter than the path[u,v] through multiple vertices. For example d[d,m] is shorter then p[d,m] which goes through B first. Am I doing something wrong?
You're not doing anything wrong. There's no guarantee that a MST preserves shortest distances between nodes. Eg: the three node complete graph ABC with edge weights 3, 2, 2 (with apologies for my ascii art):
A --- 2 --- B
| |
2 /
| /
C----3---/
The minimal spanning tree is C-A-B but the distance between B and C in the original graph is 3, and in the MST 4.
Let's call an undirected graph of n vertices p-interesting, if the following conditions fulfill:
the graph contains exactly 2n + p edges;
the graph doesn't contain self-loops and multiple edges;
for any integer k (1 ≤ k ≤ n), any subgraph consisting of k vertices contains at most 2k + p edges.
A subgraph of a graph is some set of the graph vertices and some set of the graph edges. At that, the set of edges must meet the condition: both ends of each edge from the set must belong to the chosen set of vertices.
The task is to find a p-interesting graph consisting of n vertices.
To see the problem statement click here
I can not even understand the tutorial explained here.
If anyone can point me to the theory required for the background or some obscure theorem related to this problem. I'd be very glad.
That's a somewhat muddled editorial. Let's focus on creating 0-interesting graphs first. The key fact from graph theory is the following formula.
sum_{vertices v} degree(v) = 2 #edges
In a graph where every vertex has degree 4 (4-regular graph), the left-hand side is 4n, so the number of edges is exactly 2n. Every n'-vertex subgraph of a 4-regular graph has vertices of degree at most 4, so the left-hand side is at most 4n', and the number of edges is at most 2n'. Thus, every 4-regular graph is 0-interesting. There are many ways to obtain a 4-regular graph; one is to connect vertex i to vertices i - 2, i - 1, i + 1, i + 2 modulo n.
Assuming that n >= 5, the editorial aims to prove that a graph comprised of edges (1, v) and (2, v) for all v from 3 to n and (1, 2) is "(-3)-interesting", which technically doesn't work out because each 1-vertex subgraph should have at most 2(1) - 3 = -1 edges (oops). Since the actual p of interest are nonnegative and there are no self-loops, though, this problem will resolve itself when we add the additional edges as below. For n'-vertex subgraphs with n' >= 2, we consider four cases, two of which are symmetric. The first case is that the subgraph includes neither 1 nor 2. This subgraph has no edges, and n' >= 2 implies that 0 < 2n' - 3. The second case is that the subgraph includes 1 but not 2. This subgraph can have edges from 1 to each of its other vertices, at most n' - 1 <= 2n' - 3 edges. The third case is that the subgraph includes 2 but not 1; it is symmetric to the second case. The fourth case is that the subgraph includes both 1 and 2, in which case it has at most 1 edge between 1 and 2, n' - 2 edges from 1 to other vertices, and n' - 2 edges from 2 to other vertices, for a total of at most 2n' - 3 edges.
For p-interesting graphs, the observation is that, by adding p new edges to a 0-interesting graph, the number of edges in the new graph is 2n + p, as required. The number of edges in each n'-vertex subgraph is the number of old edges plus the number of new edges. The number of old edges is at most 2n', as before. The number of new edges is at most p.