您能否給出我可以學(xué)習(xí)如何通過某種模式在圖形中搜索的方向。我有一些具有唯一 ID 和類型的單向圖,例如 A、B、C。我需要根據(jù)模式搜索所有 ID。例如如果節(jié)點(diǎn)類型在此方向連接,搜索 ( A, B, ) 應(yīng)返回所有 ID。C返回可以是 as[1,4,6] [4,6,8]等。當(dāng)我在尋找某種特定類型的節(jié)點(diǎn)時(shí),Bfs 和 dfs 可能會(huì)有所幫助,但是如果我有這些節(jié)點(diǎn)之間的連接模式,我該如何搜索。我的測試如下所示:Vertex<Integer, String> vertex1 = new Vertex<Integer, String>(1, "A");Vertex<Integer, String> vertex2 = new Vertex<Integer, String>(2, "B");Vertex<Integer, String> vertex3 = new Vertex<Integer, String>(3, "D");Vertex<Integer, String> vertex4 = new Vertex<Integer, String>(4, "A");Vertex<Integer, String> vertex5 = new Vertex<Integer, String>(5, "C");Vertex<Integer, String> vertex6 = new Vertex<Integer, String>(6, "B");Vertex<Integer, String> vertex7 = new Vertex<Integer, String>(7, "E");Vertex<Integer, String> vertex8 = new Vertex<Integer, String>(8, "C");vertex1.setNeighbors(Collections.singletonList(vertex2));vertex2.setNeighbors(Arrays.asList(vertex3, vertex5));vertex3.setNeighbors(Collections.singletonList(vertex2));vertex4.setNeighbors(Arrays.asList(vertex5, vertex6, vertex7));vertex5.setNeighbors(Arrays.asList(vertex2, vertex4, vertex6));// vertex5.setNeighbors(Arrays.asList(vertex2, vertex3, vertex4, vertex6));vertex6.setNeighbors(Arrays.asList(vertex4, vertex5, vertex8));vertex7.setNeighbors(Collections.singletonList(vertex4));vertex8.setNeighbors(Collections.singletonList(vertex6));List<List<String>> expectedAnswers = new ArrayList<List<String>>();List<String> answer = new ArrayList<String>();answer.add("1");answer.add("2");answer.add("5");expectedAnswers.add(answer);answer = new ArrayList<String>();answer.add("4");answer.add("6");answer.add("5");expectedAnswers.add(answer);answer = new ArrayList<String>();answer.add("4");answer.add("6");answer.add("8");expectedAnswers.add(answer);
按模式搜索圖表
人到中年有點(diǎn)甜
2022-07-14 16:59:49