当前位置: 首页 > article >正文

【Java】图论笔记

已含dfs和bfs,相关算法正在研究

代码

import java.util.*;

public class Prim{
    public static void main(String[] args) {
        Graph graph=new Graph();
        graph.addVertex("A");
        graph.addVertex("B");
        graph.addVertex("C");
        graph.addVertex("D");
        graph.addVertex("E");

        graph.addEdge("A","B",1);
        graph.addEdge("A","E",2);
        graph.addEdge("B","C",2);
        graph.addEdge("B","F",2);
        graph.addEdge("C","D",2);
        graph.addEdge("D","F",2);
        graph.addEdge("E","F",2);

        System.out.println(graph.dfs());
        graph.addVertex("1");
        System.out.println(graph.dfs());
    }

}
class Graph{
    public Boolean isConnected(){
        List<String> visited=new ArrayList<>();
        for(String v1:vertexs.keySet()){
            visited.add(v1);
            dfs(v1,visited);
            break;
        }

        return visited.size()==vertexs.size();
    }
    public void prim(){
        List<String> list=new ArrayList();
        putFirst(list);
        int size=vertexs.size();
        for(int i=1;i<size;i++){
            int min=Integer.MAX_VALUE;
            String next=null;
            for(String key:list){
                Map<String,Integer>neighbors=vertexs.get(key);
                for(Map.Entry<String,Integer> nbsentry:neighbors.entrySet()){
                    if(!list.contains(nbsentry.getKey())){
                        if(nbsentry.getValue()<min){
                            min= nbsentry.getValue();
                            next=nbsentry.getKey();
                        }
                    }
                }
            }
            if(next!=null){
                list.add(next);
            }

        }
        System.out.println(list);
    }
    public void putFirst(List<String> list){
        for(Map.Entry<String,Map<String,Integer>>entry:vertexs.entrySet()){
            String key=entry.getKey();
            Map<String,Integer> value=entry.getValue();
            list.add(key);
            break;
        }
    }
    private Map<String,Map<String,Integer>>vertexs;
    public Graph(){
        vertexs=new HashMap<>();
    }
    public void addVertex(String v1){
        if(!vertexs.containsKey(v1)){
            vertexs.put(v1,new HashMap<>());
        }
    }
    public void addEdge(String v1,String v2,int weight){
        if(!vertexs.containsKey(v1)){
            vertexs.put(v1,new HashMap<>());
        }
        vertexs.get(v1).put(v2,weight);
        if(!vertexs.containsKey(v2)){
            vertexs.put(v2,new HashMap<>());
        }
        vertexs.get(v2).put(v1,weight);
    }
    public Map<String,Integer> getNeighbors(String v1){
        return vertexs.get(v1);
    }
    public static void my_traverseNeighbors(Map<String, Integer> neighbors) {
        for (Map.Entry<String, Integer> entry : neighbors.entrySet()) {
            String vertex = entry.getKey();
            Integer weight = entry.getValue();
            System.out.println("顶点: " + vertex + ", 权重: " + weight);
        }
    }
    public void bfs(){
        List<String> visited=new ArrayList<>();
        for(String v1:vertexs.keySet()){
            bfs(v1,visited);
        }
        System.out.println("null");
    }

    private void bfs(String v1,List visited) {
        if(!visited.contains(v1)){
            System.out.print(v1+"->");
            visited.add(v1);
        }
        Map<String,Integer> map=vertexs.get(v1);
        for(Map.Entry<String,Integer>entry:map.entrySet()){
            String v2 = entry.getKey();
            if(!visited.contains(v2)){
                System.out.print(v2+"->");
                visited.add(v2);
            }
        }
    }

    public List dfs(){
        List<String> visited=new ArrayList<>();
        for(String v1:vertexs.keySet()){
            if(!visited.contains(v1)){
                visited.add(v1);
                dfs(v1,visited);
            }

        }
        return visited;
    }

    private List dfs(String v1, List<String> visited) {

        Map<String,Integer> map =vertexs.get(v1);
        for(Map.Entry<String,Integer>entry:map.entrySet()){
            String key=entry.getKey();
            if(!visited.contains(key)){
                visited.add(key);
                dfs(key,visited);
            }
        }
        return visited;
    }
}

http://www.kler.cn/a/161672.html

相关文章:

  • 将Excel文件的两个表格经过验证后分别读取到Excel表和数据库
  • 2024-11-13 学习人工智能的Day26 sklearn(2)
  • 【Linux】TCP原理
  • 时序数据库TimescaleDB安装部署以及常见使用
  • 闯关leetcode——3174. Clear Digits
  • 红帽认证和华为认证哪个好?看完这4点你就明白了
  • 2023-12-03 C语言最小二乘法备忘
  • 零基础自学C语言|深入理解指针 ④
  • 2002-2021年全国各地级市环境规制18个相关指标数据
  • vue项目中如何引入zip压缩包之解决方案
  • html和css写去哪儿导航条
  • CefSharp 获取POST(AJAX)、GET消息返回值(request)
  • “低代码开发:快餐大厨还是魔术棒?探寻软件开发的诙谐世界“
  • ZooKeeper学习一
  • Verilog学习 | 用initial语句写出固定的波形
  • 《使用ThinkPHP6开发项目》 - 安装ThinkPHP框架
  • Amazon CodeWhisperer 正式发布可免费供个人使用
  • 【Flink系列五】Checkpoint及Barrier原理
  • CCF刷题记录 -- 202305-2:矩阵运算 --python解法
  • 【每日一题】—— D. Divide and Equalize(Codeforces Round 903 (Div. 3))(数学、数论)
  • 12.07
  • Hadoop学习笔记(HDP)-Part.19 安装Kafka
  • Win10 安装.NET Framework 3.5 报错0x80240438
  • 利用 Python 进行数据分析实验(四)
  • log4j日志框架的使用
  • 【redis笔记】分布式锁