Java programming depth first traversal and connected component code example

Depth first traversal

Depth first traversal is similar to a person walking a maze:

As shown in the figure, select an edge from the starting point to go to the next vertex, and mark that the vertex has arrived before reaching a vertex.

When you come to a marked vertex, go back to the previous vertex and select a vertex that has not been reached.

When there is no way to go at the intersection, continue to retreat.

The concept of connected component: the polar connected subgraph of undirected graph G is called the connected component of G. There is only one connected component of any connected graph, that is, itself. An unconnected undirected graph has multiple connected components.

Let's take a look at specific examples:

The number of pass components is 3

summary

The above is all about the code example of depth first traversal and connected components in Java programming. I hope it will be helpful to you. If there are deficiencies, please leave a message to point out. Pay attention to programming tips and you will gain more.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>