SQL Server數(shù)據(jù)庫(kù)工程師培訓(xùn)實(shí)戰(zhàn)教程
2022-10-17 13:17 作者:666好聽(tīng)的名字 | 我要投稿
?// 1. check input
? ? ? ?if (adjacencyMatrix == null || adjacencyMatrix.length == 0 || adjacencyMatrix[0] == null || adjacencyMatrix[0].length == 0) {
? ? ? ? ? ?return;
? ? ? ?}
? ? ? ?// 2. 邊的數(shù)組 => 構(gòu)建鄰接表 => 非必須
? ? ? ?Map<Integer, List<Integer>> adjacencyList = new HashMap<>();
? ? ? ?for (int i = 0; i < nodeNum; i++) {
? ? ? ? ? ?adjacencyList.put(i, new ArrayList<>());
? ? ? ?}
? ? ? ?for (int i = 0; i < edges.length; i++) {
? ? ? ? ? ?int u = edges[i][0];
? ? ? ? ? ?int v = edges[i][1];
? ? ? ? ? ?adjacencyList.get(u).add(v);
? ? ? ? ? ?adjacencyList.get(v).add(u);
? ? ? ?}
標(biāo)簽: