奈學(xué)教育-p8-百萬(wàn)業(yè)務(wù)架構(gòu)師5期
圖中 BFS
public class BFSInGraph {
? ?public void bfsInGraph(int nodeNum, int[] edges, int[][] adjacencyMatrix) {
? ? ? ?// check input
? ? ? ?if (nodeNum < 1 || adjacencyMatrix == null || adjacencyMatrix.length == 0 || adjacencyMatrix[0] == null || adjacencyMatrix[0].length == 0) {
? ? ? ? ? ?return;
? ? ? ?}
? ? ? ?// construct adjacencyList => Map<Node, List<Node>> => node -> adjacency node
? ? ? ?Map<Integer, List<Integer>> adjacencyList = new HashMap<>();
? ? ? ?for (int i = 0; i < nodeNum; i++) {
? ? ? ? ? ?adjacencyList.add(i, new ArrayList<>());
? ? ? ?}
標(biāo)簽: