An algorithm description for determining if there is a loop (loop) in the non-graph
If there is a loop, there must be a sub-graph, which is a loop. the degree >=2 of all vertices in the loop.
Algorithm:
First step: Delete the vertices of all degrees <=1 and the associated edges, and subtract one additional vertex associated with those edges.
The second step is to queue the vertex with degrees to 1 and remove one vertex from the queue to repeat step one.
If there is no vertex removed at the end, there is a ring, otherwise there is no ring.
Algorithm Analysis:
Because there is a M-bar, n vertices. If m>=n, the existence loop can be judged directly according to the knowledge of graph theory .
(Proof: If there is no loop, then the graph must be K tree k>=1.) Depending on the nature of the tree, the number of edges M = n-k. K>=1, so: m<n)
If m<n, follow the algorithm above for each deletion of a 0 vertex operation (up to n times), or each deletion of a vertex with a degree of 1 (at the same time delete one edge) operation once (up to M times). The total number of these two operations does not exceed m+n. Because of m<n, the algorithm complexity is O (n)
==============================================================================
==============================================================================
"Is there a ring to the graph" "http://blog.csdn.net/insistgogo/article/details/6978718"
Detailed and specific code
==============================================================================
The main have depth first and topological sort 2 method
1, topological sorting, if you can use the topology to sort all the nodes in the graph, it shows that there is no ring in this diagram, and if not complete, then there is a ring.
2, you can use strongly Connected components to do, we can recall the concept of strong connectivity sub-graph, that is, for a certain sub-map of a graph, any u->v in the sub-chart, there must be v->u, then this is a strong connectivity sub-graph. This qualification is exactly the concept of a ring. So I think that by looking for a graph of strongly connected sub-graphs the method should be able to find out whether there is a ring in the graph, there are several rings.
3, is to use an improved DFS
When I first saw this problem, I wanted to use DFS alone to solve the problem. But to think about it, is not able to. If the topic gives an OK,DFS graph, then the problem can be solved. But there is no correct result for the graph. For example: a->b,a->c->b, we use DFS to deal with this graph, we will draw it to have a ring, but actually did not.
We can fix this problem by slightly changing the DFS. The workaround is as follows:
A node in the figure, according to its c[n] value, has three states:
0, this node has not been accessed
-1, has been visited at least 1 times, and its descendant nodes are being accessed
1, its descendant nodes are accessed.
According to this assumption, when searching by DFS, there are three possible ways to encounter a node:
1, if c[v]=0, this is a new node, do not do the processing
2, if c[v]=-1, the description is access to the node's descendants in the process of access to the node itself, the diagram has a ring.
3, if c[v]=1, similar to the derivation of 2, no ring. In the program, add some special processing, that is, you can find a few rings in the diagram, and record the path of each ring
I don't understand the above algorithm. So it was not implemented, but I implemented the loop detection with DFS.
- //DFS, Discovery Loop (returns True) is not serializable, returns false
- For (int i = 1; i <= n; i++) {
- if (Dfscheckcircuit (i))
- return false;
- }
- Returns True if the loop is found, otherwise the traversal end returns false
- Private boolean dfscheckcircuit (int current ) {
- if (Walked[current]) {
- return true;
- }
- Walked[current] = true;
- For (int i = 1; i <= n; i++)
- if (Digraph[current][i]) {
- if (Dfscheckcircuit (i)) {
- return true;
- }
- }
- Walked[current] = false;
- return false;
- }
Whether there is a ring in the non-direction graph