Determine the number of Unicom sub-graphs

Source: Internet
Author: User

[Leetcode] Number of Connected parts in an undirected graph undirected graph

Given n nodes labeled from 0 to N-1 and a list of undirected edges (each edge was a pair of nodes), write a function to F IND the number of connected in an undirected graph.

Example 1:

0 3

| |

1---2 4

Given n = 5 and edges = [[0, 1], [1, 2], [3, 4]], return 2.

Example 2:

0 4

| |

1---2---3

Given n = 5 and edges = [[0, 1], [1, 2], [2, 3], [3, 4]], return 1.

Note:

You can assume that no duplicate edges would appear in edges. Since all edges was undirected, [0, 1] is the same as [1, 0] and thus would not appear together in edges

This problem allows us to find the number of connected areas in the undirected graph, there are few problems with graph graph in Leetcode, and the solution has similar characteristics, it is necessary to construct the adjacency linked list adjacency list. A solution to this problem is to use DFS to do, the idea is to each node has a flag flag it has been visited, for a node that has not been visited, we will increase the result by 1, because this is definitely a new connected area, and then we through the adjacency list to traverse its adjacent nodes, And they are marked as visited, after traversing all the connected nodes we continue to look for the next non-visited node, and so on until all the nodes have been accessed, then we also find out the number of connected areas.

Solution One:

1 classSolution {2  Public:3     intCountcomponents (intN, vector<pair<int,int> >&edges) {4         intres =0;5vector<vector<int> >g (n);6vector<BOOL> V (N,false);7          for(Auto a:edges) {8 G[a.first].push_back (a.second);9 G[a.second].push_back (a.first);Ten         } One          for(inti =0; I < n; ++i) { A             if(!V[i]) { -++Res; - Dfs (g, V, i); the             } -         } -         returnRes; -     } +     voidDFS (vector<vector<int> > &g, vector<BOOL> &v,inti) { -         if(V[i])return; +V[i] =true; A          for(intj =0; J < G[i].size (); ++j) { at Dfs (g, V, G[i][j]); -         } -     } -};

This problem also has a more ingenious method, do not have to set up adjacent linked list, nor DFS, the idea is to establish a root array, subscript and node value is the same, at this time root[i] is the node I belongs to group I, we initialized n parts (res = n), Assuming that each node belongs to a separate interval at the beginning, and then we start traversing all the edges, for the two points of an edge, they start with a different value in root, and we subtract 1 from the result, which means one less interval and then update the root value of one of the nodes. The root value of the two nodes is identical, so we can mark the root value of all nodes of the connecting interval as the same value, and the root value of the different connecting interval is not the same, so we can find the number of the connecting interval.

Solution Two:

1 classSolution {2  Public:3     intCountcomponents (intN, vector<pair<int,int> >&edges) {4         intres =N;5vector<int>root (n);6          for(inti =0; I < n; ++i) Root[i] =i;7          for(Auto a:edges) {8             intx = Find (root, a.first), y =find (root, a.second);9             if(X! =y) {Ten--Res; OneRoot[y] =x; A             } -         } -         returnRes; the     } -     intFind (vector<int> &root,inti) { -          while(Root[i]! = i) i =Root[i]; -         returni; +     } -};

Determine the number of Unicom sub-graphs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.