码迷,mamicode.com
首页 > 其他好文 > 详细

最短路径

时间:2015-08-05 22:00:34      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

 

package 最短路径;  
02./**  
03. * 邻接矩阵  
04. * @author 小宇  
05. */  
06.public class MGraph {  
07.    //顶点为空  
08.    public static final int NULL = 1000;      
09.    //邻接矩阵  
10.    int[][] edges = new int[9][9];       
11.    //顶点数和边数  
12.    int n,e;     
13.}  

 

/** 
02. * 此代码用于生成邻接矩阵 
03. * 并显示 
04. * @author 小宇 
05. */  
06.  
07.public class CreateMgraph {  
08.    //根据二维数组,生成邻接矩阵  
09.    public void createMat(MGraph g, int A[][], int n)  
10.    {                
11.        int i, j;  
12.        g.n = n;  
13.        g.e = 0;  
14.        for(i = 0; i < n; i++)  
15.            for(j = 0; j < n; j++)  
16.            {  
17.                g.edges[i][j] = A[i][j];  
18.                if(g.edges[i][j] != MGraph.NULL)  
19.                    g.e++;  
20.            }  
21.    }  
22.    //输出邻接矩阵  
23.    public void DispMat(MGraph g)  
24.    {  
25.        int i, j;  
26.        for(i = 0; i < g.n; i++)  
27.        {  
28.            for(j = 0; j < g.n; j++)  
29.            {  
30.                if(g.edges[i][j] == MGraph.NULL)  
31.                    System.out.print("-" + " ");  
32.                else  
33.                    System.out.print(g.edges[i][j] + " ");  
34.            }  
35.            System.out.println();  
36.        }  
37.    }  
38.}  

 

 

/** 
02. * 核心代码:生成最短路径 
03. * @author 小宇 
04. */  
05.public class GetShortestPath {  
06.      
07.    //Dijkstra算法  
08.    public static void Dijkstra(MGraph mgraph,int v0)  
09.    {  
10.        final int INFINITY = 65535;  
11.        //用于存储最短路径下标数组  
12.        int[] pathMatirx = new int[9];  
13.        //用于存储到各点最短路径权值  
14.        int[] shortPathTable = new int[9];  
15.        int min,v,w,k = 0;  
16.        //finalGot[n] = 1表示求得v0 - vn最短路径  
17.        int[] finalGot = new int[9];  
18.        //初始化数据  
19.        for(v = 0; v  < mgraph.n; v++)  
20.        {  
21.            //全部顶点初始化为未知最短路径状态  
22.            finalGot[v] = 0;  
23.            //将与与v0有连线的点加上权值  
24.            shortPathTable[v] = mgraph.edges[v0][v];  
25.            //初始化路径数组为0  
26.            pathMatirx[v] = 0;  
27.        }  
28.        //v0至v0路径为0  
29.        shortPathTable[v0] = 0;  
30.        //v0至v0不须求路径  
31.        finalGot[v0] = 1;  
32.        for(v= 1; v < mgraph.n; v++)  
33.        {  
34.            min = INFINITY;  
35.            //寻找距v0最近顶点  
36.            for(w = 0; w < mgraph.n; w++)  
37.            {  
38.                if(finalGot[w] == 0 && shortPathTable[w] < min)  
39.                {  
40.                    k = w;  
41.                    min = shortPathTable[w];  
42.                }  
43.            }  
44.            //将找到的顶点标记为1  
45.            finalGot[k] = 1;  
46.            //修正当前最短路径及距离  
47.            for(w = 0; w < mgraph.n; w++)  
48.            {  
49.                //如果经过v顶点的路径比现在这条路径距离短的话  
50.                if(finalGot[w] == 0 && (min + mgraph.edges[k][w] < shortPathTable[w]))  
51.                {  
52.                    shortPathTable[w] = min + mgraph.edges[k][w];  
53.                    pathMatirx[w] = k;  
54.                }  
55.            }  
56.        }  
57.        //输出  
58.        System.out.println("Dijkstra算法求得最短路径:  ");  
59.        for(v = 1; v < mgraph.n; v++)  
60.        {  
61.            k = v;  
62.            System.out.print(k + "->");  
63.            while(k != v0)  
64.            {  
65.                k = pathMatirx[k];  
66.                System.out.print(k + "->");  
67.            }  
68.            System.out.println();  
69.        }  
70.    }  
71.      
72.    //-----------------------------------------------------  
73.      
74.    //弗洛伊德算法算法基本思想与Dijkstra算法相似  
75.    public static void Floyd(MGraph mgraph)  
76.    {  
77.        int[][] shortPathTable = new int[9][9];  
78.        int[][] pathMatirx = new int[9][9];  
79.        int v, w, k;  
80.        for(v = 0; v < mgraph.n; v++)  
81.        {  
82.            for(w = 0; w < mgraph.n; w++)  
83.            {  
84.                shortPathTable[v][w] = mgraph.edges[v][w];  
85.                pathMatirx[v][w] = w;  
86.            }  
87.        }  
88.        for(k = 0; k < mgraph.n; k++)  
89.        {//1  
90.            for(v = 0; v < mgraph.n; v++)  
91.            {//2  
92.                for(w = 0; w < mgraph.n; w++)  
93.                {//3  
94.                    if(shortPathTable[v][w] > shortPathTable[v][k] + shortPathTable[k][w])  
95.                    {  
96.                        shortPathTable[v][w] = shortPathTable[v][k] + shortPathTable[k][w];  
97.                        pathMatirx[v][w] = pathMatirx[v][k];  
98.                    }                         
99.                }//3  
100.            }//2  
101.        }//1  
102.        //显示  
103.        System.out.println("Floyd算法求得最短路径:");  
104.        for(v = 0; v < mgraph.n; v++)  
105.        {  
106.            for(w = v + 1; w < mgraph.n; w++)  
107.            {  
108.                System.out.print("v" + v + "->v" + w + "weight:" + shortPathTable[v][w] + "  ");  
109.                k = pathMatirx[v][w];  
110.                System.out.print("path:" + v);  
111.                while(k != w )  
112.                {  
113.                    System.out.print("->" + k);  
114.                    k = pathMatirx[k][w];  
115.                }  
116.                System.out.println("->" + w);  
117.            }  
118.            System.out.println();  
119.        }  
120.    }  
121.}  

 

 

/** 
02. * 测试代码 
03. * @author 小宇 
04. * 
05. */  
06.public class Test   
07.{  
08.    public static void main(String[] args)  
09.    {  
10.        MGraph mg = new MGraph();  
11.          
12.        int[][] array = new int[9][9];  
13.        for(int i = 0;i < 9; i++)  
14.            for(int j = 0;j < 9; j++)  
15.                array[i][j] = MGraph.NULL;  
16.        array[0][1] = 1;  
17.        array[1][0] = 1;  
18.        array[0][2] = 5;  
19.        array[2][0] = 5;  
20.        array[1][2] = 3;  
21.        array[2][1] = 3;  
22.        array[1][3] = 7;  
23.        array[3][1] = 7;  
24.        array[3][4] = 2;  
25.        array[4][3] = 2;  
26.        array[1][4] = 5;  
27.        array[4][1] = 5;  
28.        array[2][4] = 1;  
29.        array[4][2] = 1;  
30.        array[2][5] = 7;  
31.        array[5][2] = 7;  
32.        array[4][5] = 3;  
33.        array[5][4] = 3;  
34.        array[3][6] = 3;  
35.        array[6][3] = 3;  
36.        array[4][6] = 6;  
37.        array[6][4] = 6;  
38.        array[4][7] = 9;  
39.        array[7][4] = 9;  
40.        array[5][7] = 5;  
41.        array[7][5] = 5;  
42.        array[6][7] = 2;  
43.        array[7][6] = 2;  
44.        array[6][8] = 7;  
45.        array[8][6] = 7;  
46.        array[7][8] = 4;  
47.        array[8][7] = 4;  
48.        CreateMgraph cm = new CreateMgraph();  
49.        cm.createMat(mg, array, 9);  
50.        cm.DispMat(mg);  
51.          
52.        GetShortestPath.Dijkstra(mg, 0);  
53.        GetShortestPath.Floyd(mg);  
54.        }  
55.}  

 

最短路径

标签:

原文地址:http://www.cnblogs.com/rixiang/p/4705853.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!