简体   繁体   中英

Undirected Graph Java

I'm currently working on graphs. I have an example: you enter 8 edges (1-2,2-4,4-1,3-6,3-7,3-5,6-7,6-5). Should be printed the count of components (2) and the number of nodes the largest component have (3). I dont understand how it is 3.
I tried to draw it, I got the right amout of components - 2, but the bigger one have 4 nodes.

A component is a maximal subgraph such that a path exists between each pair of nodes in this subgraph. If the graph is directed, as in your case, you can distinguish between weak and strong components. For the former, you look for paths while ignoring the link directions, whereas for the latter, you take them into account.

In your graph, the first part (1->2->4->1) is a directed cycle, so it is both a weak and a strong component. The second part (3,5,6,7) forms a weak component, but not a strong one. Indeed, one cannot go from 7 or 5 to 3 or 6 by following links using the directions. Moreover, one cannot go from 6 to 3.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM