java实现判断图的连通性
图的连通性的概念:
连通无向图:如果无向图任意两个顶点都连通,则称为连通无向图
连通有向图:如果有向图任意两个顶点vi,vj,从vi到vj和从vj到vi都有路径,则称有向图是强连通有向图
public class Connect { public static void main(String[] args) { Set<String> nodes = ImmutableSet.of("A", "B", "C", "D"); List<String> hop = Lists.newArrayList("C->A", "C->D"); List<Pair<String, String>> objects = Lists.newArrayList(); for (String s : hop) { String[] split = s.split("->"); String from = split[0]; String to = split[1]; Pair<String, String> pair = new Pair<>(from, to); objects.add(pair); } Boolean isConnect = unDirectGraphConnect(nodes,objects); System.out.println("无向图连通性"); System.out.println(isConnect); System.out.println("有向图连通性"); Boolean isConnect2 = directGraphConnect(nodes, objects); System.out.println(isConnect2); } /** * 连通无向图判断,最后应该只有一个连通分量,而且可以连通所有节点 * * @param pairList * @return */ public static Boolean unDirectGraphConnect(Set<String> nodes, List<Pair<String, String>> pairList) { Multimap<String, String> map = HashMultimap.create(); for (Pair<String, String> pair : pairList) { String from = pair.getFrom(); String to = pair.getTo(); //默认from、to都不存在 boolean fromPresent = false; boolean toPresent = false; if (isPresent(map, from)) { fromPresent = true; } if (isPresent(map, to)) { toPresent = true; } //from/to都不存在,最简单,from做key,将from和to放value里 if (!fromPresent && !toPresent) { map.put(from, from); map.put(from, to); //from存在,to不存在,要区分from是key还是value } else if (!toPresent) { boolean inKey = map.containsKey(from); if (inKey) { map.put(from, to); } else { String valKey = getKeyByValue(map, from); map.put(valKey, to); } //to存在,from不存在,也要区分to是key还是value } else if (!fromPresent) { boolean toInKey = map.containsKey(to); if (toInKey) { map.put(to, from); } else { String valKey = getKeyByValue(map, to); map.put(valKey, from); } } //剩下最后一种可能,from/to都存在,那就不需要处理了 } System.out.println(map); //只有一个连通分量,且能连通所有节点 return map.keySet().size() == 1 && map.values().containsAll(nodes); } /** * 有向图连通性判断 * 理论上有多少顶点就有多少key,且value都等于顶点数 * * @param pairList * @return */ public static Boolean directGraphConnect(Set<String> nodes, List<Pair<String, String>> pairList) { Multimap<String, String> map = HashMultimap.create(); //对map初始化 for (String node : nodes) { map.put(node, node); } for (Pair<String, String> pair : pairList) { String from = pair.getFrom(); String to = pair.getTo(); Collection<String> values = map.get(from); //把to加入from连通map if (!values.contains(to)) { map.put(from, to); } //所有之前能到from的,现在也能到to了 for (String key : map.keySet()) { Collection<String> values2 = map.get(key); if (values2.contains(from) && !values2.contains(to)) { values2.add(to); } } //所有之前to能到的,现在from也能到了 Collection<String> value5 = map.get(to); for (String s : value5) { if (!map.get(from).contains(s)) { map.put(from, s); } } } boolean connect = true; int nodeSize = nodes.size(); for (String key : map.keySet()) { Collection<String> values3 = map.get(key); if (values3.size() != nodeSize) { connect = false; } } System.out.println(map); return connect; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗