Question 2 Number of connected Components in an Undirected Graph

Summary

Step 1: State the Problem 归大类

  • this is a graph problem

  • I would like to model the problem to a graph problem

Step 2: Build Graph

  • vertex: node

  • edge: undirected edges

  • assume that no duplicate edges will appear in edges ==> Map<Integer, List<Integer>>

Step 3 & 4: what problem--> really solve which problem?

  • so the problem is asking me to find out how many connected components are there in the graph built

  • 每一次从一个没访问过的点出发都能遍历完一个新的联通分量的所有点:

  • 我从几个点出发了,我就有几个联通分量

  • numbersOfConnectedComponent有几个联通分量

  • Therefore this is a traversal(reachable) problem in graph

Step 5: propose 算法对边选择最优

  • dfs & bfs

Last updated