Algorithm 1 we know that for the ring 1-2-3-4-1, the degree of each node is 2. Based on this, we have the following algorithm (which is similar to the topological sorting of Directed Graphs ): calculate the degree of all vertices in the graph, and delete the vertices whose degree is <= 1 and the edges related to the vertex, subtract the degree of vertices related to these edges. If there are still vertices with degrees <= 1, repeat Step 2, and if there are still undeleted vertices, there is a ring; otherwise, the cycle time complexity is O (E + V). E and V are the number of vertices in the graph respectively. This algorithm will be analyzed later when algorithm 3 is analyzed. Algorithm 2 traverses the graph in depth first. If an edge of a node is found to point to an accessed Node during the traversal process, in addition, if the accessed node is not the parent node of the current node (the parent node here indicates the parent node in the dfs traversal sequence), the ring exists. However, we cannot just use a bool array to identify whether the node has been accessed. For example, if the image traverses from node 1-Traverse 2-then traverse 3, and then finds that 3 has an edge pointing to 1 that has been traversed, there is a ring. However, when the first node is returned, the other side points to the accessed 3, and the ring is calculated again. According to section 22.3 of Introduction to algorithms, deep Priority Search divides each node into three states: white, gray, and black. At the beginning, all nodes are white. When a node is accessed, the node turns gray. When all adjacent contacts of the node are accessed, the color of the node turns black. Then our algorithm is: if a node has an edge pointing to a gray Node during traversal, there is a ring. In the preceding example, when we trace back to node 1, although an edge points to 3 that has been accessed, 3 is already black, so the ring will not be duplicated. In the following code, the value of the visit array is divided into 0, 1, and 2 States, which represent white, gray, and black respectively. By calling the dfs function, all the loops in the graph can be output, and the graph is represented by the adjacent matrix, if there is no edge between two nodes, the corresponding value is INT_MAX void dfsVisit (vector <int> & graph, int node, vector <int> & visit, vector <int> & father) {int n = graph. size (); visit [node] = 1; // cout <node <"-\ n"; for (int I = 0; I <n; I ++) if (I! = Node & graph [node] [I]! = INT_MAX) {if (visit [I] = 1 & I! = Father [node]) // find a ring {int tmp = node; cout <"cycle:"; while (tmp! = I) {cout <tmp <"->"; tmp = father [tmp];} cout <tmp <endl ;} else if (visit [I] = 0) {father [I] = node; dfsVisit (graph, I, visit, father) ;}} visit [node] = 2 ;} void dfs (vector <int> & graph) {int n = graph. size (); vector <int> visit (n, 0); // visit is divided into three states: vector <int> father (n,-1) according to section 22.3 of Introduction to algorithms ); // father [I] records the parent node of I during traversal (int I = 0; I <n; I ++) if (visit [I] = 0) dfsVisit (graph, I, visit, fath Er);} the time complexity of the algorithm is also O (E + V). For directed graph algorithm 3, we all know that Topology Sorting for Directed Graphs can determine whether a ring exists. For the topological sorting of Directed Graphs, we all know the kahn algorithm: Calculate the inbound degrees of all vertices in the graph and add the vertices with an inbound degree of 0 to the stack. If the stack is not empty: retrieve the top Vertex a of the stack, output the vertex value and delete all edges starting from vertex a. If the entry of the other vertex of the deleted edges is 0, import the edges to the stack. If there are still vertices in the graph, it indicates that the graph has a ring; otherwise, the output vertex is a topological sorting sequence. If the above topological sorting algorithm is used to find the ring, you can determine whether there is a ring, but it is a bit difficult to output the ring. Not all the remaining vertices are the vertices in the ring. For example, if the image runs the above algorithm on the graph, all the nodes will not be deleted, however, only 1 2 3 is the point in the ring, and 4 is not the node in the ring. The idea of algorithm 1 above is the same as that of algorithm 3, so this problem also exists. Algorithm 4 in fact, algorithm 2 can be moved intact to detect and output all Directed Graphs in the ring. This address algorithm 5 According to the directed graph's Strongly Connected Component Algorithm, each strongly connected component must have a ring, according to the definition of the strongly connected component, there is a path from vertex I to j, and there is also a path from j to I. For algorithms that require strongly connected components, refer to Wikipedia here. Supplement: to use dfs for topological sorting, you only need to slightly modify algorithm 2 to output the topological sorting results of Directed Graphs, that is, according to the time when the node is marked as black, the more marked as black, the more backward in the topological sequence. We add a stack on the basis of algorithm 2 to save the topological sorting result. Only the last row of dfsVisit has been changed. This algorithm can complete the topological sorting, it can also check whether a ring exists in the graph. (The idea of this algorithm is the same as that in section 22.4 of Introduction to algorithms.) stack <int> tuopu; void dfsVisit (vector <int> & graph, int node, vector <int> & visit, vector <int> & father) {int n = graph. size (); visit [node] = 1; // cout <node <"-\ n"; for (int I = 0; I <n; I ++) if (I! = Node & graph [node] [I]! = INT_MAX) {if (visit [I] = 1 & I! = Father [node]) // find a ring {int tmp = node; cout <"cycle:"; while (tmp! = I) {cout <tmp <"->"; tmp = father [tmp];} cout <tmp <endl ;} else if (visit [I] = 0) {father [I] = node; dfsVisit (graph, I, visit, father) ;}} visit [node] = 2; tuopu. push (node);} void dfs (vector <int> & graph) {int n = graph. size (); vector <int> visit (n, 0); // visit is divided into three states: vector <int> father (n,-1) according to section 22.3 of Introduction to algorithms ); // father [I] records the parent node of I during traversal (int I = 0; I <n; I ++) if (visit [I] = 0) dfsVisit (graph, I, visit, father );}