• 使用java实现最小生成树prim算法


    实体类:

    package com.caicai.prim.entity;
    /**
     * 
     * @author caizy
     *
     */
    public class ShortEdge {
        private int lowCost;//到连通树的最小权值
        private int adjvex;//使它到连通树的最小权值的那个节点编号
        public int getLowCost() {
            return lowCost;
        }
        public void setLowCost(int lowCost) {
            this.lowCost = lowCost;
        }
        public int getAdjvex() {
            return adjvex;
        }
        public void setAdjvex(int adjvex) {
            this.adjvex = adjvex;
        }
        
    
    }
    package com.caicai.prim.entity;
    
    public class Graph<T> {
        private int vertexNum;//顶点数
        private T vertex[];//存放顶点信息的数组
        private int arcNum;//存放边数;
        private int arc[][];//存放边关系和边的权值:当为0时不存在边,大于零时有边且边为权值。
        public int getVertexNum() {
            return vertexNum;
        }
        public void setVertexNum(int vertexNum) {
            this.vertexNum = vertexNum;
        }
        public T[] getVertex() {
            return vertex;
        }
        public void setVertex(T[] vertex) {
            this.vertex = vertex;
        }
        public int getArcNum() {
            return arcNum;
        }
        public void setArcNum(int arcNum) {
            this.arcNum = arcNum;
        }
        public int[][] getArc() {
            return arc;
        }
        public void setArc(int[][] arc) {
            this.arc = arc;
        }
        
        
    
    }

    prim算法实现:

    package com.caicai.prim;
    
    import java.util.HashMap;
    import java.util.LinkedHashMap;
    import java.util.Map;
    
    import com.caicai.prim.entity.Graph;
    import com.caicai.prim.entity.ShortEdge;
    
    /**
     * 
     * @author caizy
     *prim算法实现
     */
    public class Prim<T> {
        private Map<Integer,ShortEdge> map = new HashMap<Integer, ShortEdge>();
        private Graph<T> graph;
        public Graph<T> getGraph() {
            return graph;
        }
        public void setGraph(Graph<T> graph) {
            this.graph = graph;
        }
        public Map<Integer,ShortEdge> prim() {
            initial();
            int arc[][]=graph.getArc();
            Map<Integer,ShortEdge> resultMap=new LinkedHashMap<Integer,ShortEdge>();
            while(map.size()>0) {
                int k = minEdge(map);
                resultMap.put(k,map.get(k) );
                map.remove(k);
                if(map.size()==0)
                    break;
                for (int key:map.keySet()) {    
                    if (arc[key][k] < map.get(key).getLowCost())
                        map.get(key).setLowCost(arc[k][key]);
                        map.get(key).setAdjvex(k);
                }    
            }
            return resultMap;
        }
        private void initial() {
            int arc[][]=graph.getArc();
            for(int i = 1; i < graph.getVertexNum(); i++) {
                ShortEdge shortEdge = new ShortEdge();
                shortEdge.setAdjvex(0);//开始的时候集合中只有一个顶点0
                if(arc[0][i]!=0)
                    shortEdge.setLowCost(arc[0][i]);
                else
                    shortEdge.setLowCost(Integer.MAX_VALUE);
                map.put(i,shortEdge);
            }
        }
        private int minEdge(Map<Integer,ShortEdge> map) {
            int min=Integer.MAX_VALUE;
            int found =0;
            for(Integer key: map.keySet()) {
                if(map.get(key).getLowCost()<min) {
                    min=map.get(key).getLowCost();
                    found=key;
                }
            }
            return found;
        }
    
    }

    测试类

    package prim.test;
    
    import java.util.Map;
    
    import com.caicai.prim.Prim;
    import com.caicai.prim.entity.Graph;
    import com.caicai.prim.entity.ShortEdge;
    
    public class PrimTest {
        
        public static void main(String args[]) {
            Graph<String> graph = new Graph<String>();
            graph.setVertexNum(4);
            graph.setArcNum(5);
            int arc[][] = new int[5][5];
            arc[0][1]=19;
            arc[1][0]=19;
            arc[1][2]=25;
            arc[2][1]=25;
            arc[1][3]=25;
            arc[3][1]=25;
            arc[2][3]=17;
            arc[3][2]=17;
            arc[2][0]=46;
            arc[0][2]=46;
            graph.setArc(arc);
            String vertex[]=new String[4];
            for (int i= 0;i<4;i++) {
                vertex[i]="顶点"+i;
            }
            graph.setVertex(vertex);
            Prim<String> prim =new Prim<String>();
            prim.setGraph(graph);
            Map<Integer,ShortEdge> resultMap=prim.prim();
            for(int key:resultMap.keySet()) {
                System.out.format("边为%d"+"-"+"%d,"+"权值为:%d
    ",key,resultMap.get(key).getAdjvex(),
                        resultMap.get(key).getLowCost());
            }
            
        }
    
    }

     测试结果:

    参考书籍:数据结构(c++版)第2版 王红梅等编著

  • 相关阅读:
    C语言之分支语句
    C语言之运算符与表达式
    C语言之数据类型④——中文字符
    独特的对象引用:this
    理解赋值“=”的含义
    编写类的 “模板”
    类的定义
    Java语言规范
    第一周总结
    定义常量
  • 原文地址:https://www.cnblogs.com/cai-cai777/p/11111993.html
Copyright © 2020-2023  润新知