美文网首页
最短路径算法

最短路径算法

作者: 綿綿_ | 来源:发表于2019-04-04 17:23 被阅读0次
import java.util.Scanner;
class GraphMatrix2
{
    static final int MaxNum=20;
    char[] Vertex=new char[MaxNum];
    int GType;
    int VertexNum;
    int EdgeNum;
    int[][] EdgeWeight=new int [MaxNum][MaxNum];
    int[] isTrav=new int[MaxNum];
}
public class ShortestPath {
    
        static final int MaxValue=67777;
        static int[] path=new int [GraphMatrix2.MaxNum];
        static int[] tmpvertex=new int [GraphMatrix2.MaxNum];
        static Scanner input= new Scanner(System.in);
        static void CreateGraph(GraphMatrix2 GM)
        {
            int i,j,k;
            int weight;
            char EstartV,EendV; //  start and end vertex of of edge;
            
            System.out.println("enter the info. of graph");
            for(i=0;i<GM.VertexNum;i++)  //enter the vertex
            {
                System.out.printf("the %d vertex", i+1);
                GM.Vertex[i]=(input.next().toCharArray())[0]; //save in array
            }
            System.out.println("enter the edge and the weight");
            for(k=0;k<GM.EdgeNum;k++)
            {
                System.out.printf("the %d edge", k+1);
                EstartV=input.next().charAt(0);
                EendV=input.next().charAt(0);   
                weight=input.nextInt();
                for(i=0;EstartV!=GM.Vertex[i];i++); //search start vertex in 
                                                    //those vertexes that we already have
                for(j=0;EendV!=GM.Vertex[j];j++);   //search end vertex in ...
                GM.EdgeWeight[i][j]=weight;    //save weight in corresponding position
                if(GM.GType==0) //if graph is undirected 
                {
                    GM.EdgeWeight[j][i]=weight; //save data diagonally
                }
            }
        }
        static void ClearGraph(GraphMatrix2 GM)
        {
            int i,j;
            for(i=0;i<GM.VertexNum;i++)
            {
                for(j=0;j<GM.VertexNum;j++)
                {
                    GM.EdgeWeight[i][j]=MaxValue;
                }
            }
        }
        static void DisplayGraph(GraphMatrix2 GM)
        {
            int i,j;
            for(j=0;j<GM.VertexNum;j++)
            {
                System.out.printf("\t%c",GM.Vertex[j]); //print vertex at the first line
            }
            System.out.printf("\n");
            for(i=0;i<GM.VertexNum;i++)
            {
                System.out.printf("%c", GM.Vertex[i]);
                for(j=0;j<GM.VertexNum;j++) 
                {
                    if(GM.EdgeWeight[i][j]==MaxValue)
                    {
                        System.out.printf("\tz");//z means infinity here
                    }else
                    {
                        System.out.printf("\t%d", GM.EdgeWeight[i][j]);
                    }
                }System.out.printf("\n");
            }
        }
        static void DistMin(GraphMatrix2 GM,int vend)
        {
            int[] weight = new int[GraphMatrix2.MaxNum];
            int i,j,k,min;
            vend--;
            
            for(i=0;i<GM.VertexNum;i++)
            {
                weight[i]=GM.EdgeWeight[vend][i];
            }
            for(i=0;i<GM.VertexNum;i++)
            {
                if(weight[i]<MaxValue && weight[i]>0)
                {
                    path[i]=vend;
                }
            }
            for(i=0;i<GM.VertexNum;i++)
            {
                tmpvertex[i]=0;
            }
            tmpvertex[vend]=1;
            weight[vend]=0;
            for(i=0;i<GM.VertexNum;i++)
            {
                min=MaxValue;
                k=vend;
                for(j=0;j<GM.VertexNum;j++)
                {
                    if(tmpvertex[j]==0 && weight[j]<min)
                    {
                        min=weight[j];
                        k=j;
                    }
                }
                tmpvertex[k]=1;
                for(j=0;j<GM.VertexNum;j++)
                {
                    if(tmpvertex[j]==0 && weight[k]+GM.EdgeWeight[k][j]<weight[j])
                    {
                        weight[j]=weight[k]+GM.EdgeWeight[k][j];
                        path[j]=k;
                    }
                }
            }
        }
        public static void main(String[] args)
        {
            GraphMatrix2 GM = new GraphMatrix2();
            int vend;
            int i,k;
            System.out.println("enter the type of graph:");
            GM.GType=input.nextInt();
            System.out.println("enter the number of vertex:");
            GM.VertexNum=input.nextInt();
            System.out.println("enter the number of edge:");
            GM.EdgeNum=input.nextInt();
            ClearGraph(GM);
            CreateGraph(GM);
            System.out.println("the adjacency matrix of this graph is below:");
            DisplayGraph(GM);
            
            System.out.println("enter the destination vertex");
            vend = input.nextInt();
            
            DistMin(GM,vend);
            
            vend--;
            System.out.printf("the shortest path to vertex %c is:", GM.Vertex[vend]);
            for(i=0;i<GM.VertexNum;i++)
            {
                if(tmpvertex[i]==1)
                {
                    k=i;
                    while(k!=vend)
                    {
                        System.out.printf("vertex %c -", GM.Vertex[k]);
                        k=path[k];
                    }
                    System.out.printf("vertex %c \n", GM.Vertex[k]);
                }
                else
                {
                    System.out.printf("%c - %c:no path\n",GM.Vertex[i],GM.Vertex[vend]);
                    
                }
            }
                
        }
}

相关文章

  • 最短路径 之 Dijkstra 算法

    • 最短路径 之 Floyd 算法• 最短路径 之 Bellman 算法 Dijkstra算法是用于求解单源最短路...

  • 最短路径 之 Bellman 算法

    • 最短路径 之 Floyd 算法• 最短路径 之 Dijkstra 算法 Bellman算法差不多是Floyd算...

  • 最短路径 之 Floyd 算法

    • 最短路径 之 Dijkstra 算法• 最短路径 之 Bellman 算法 Floyd算法是基于一种动态规划的...

  • 7.8图的应用:最短路径问题

    最短路径问题:Dijkstra算法 ❖解决带权最短路径问题的经典算法是以发明者命名的“Dijkstra算法”❖这是...

  • 图的应用-最短路径求解

    图的最短路径   图的最短路径是一个起点到一个终点之间最短的路径。  用于解决最短路径问题的算法被称做“最短路径算...

  • turtle Floyd-Warshall(Graph)

    最短路径算法 Floyd-Warshall(打开新窗口)的算法是用来寻找具有正负边权重的加权图中的最短路径。该算法...

  • 数据结构与算法--最短路径之Floyd算法

    数据结构与算法--最短路径之Floyd算法 我们知道Dijkstra算法只能解决单源最短路径问题,且要求边上的权重...

  • 据结构与算法学习-最短路径

    最短路径顾名思义就是两个点之间所需花费最短的那个路径。在算法中计算最短路径有两个比较著名的算法:Dijkstra算...

  • A星寻路算法-过程可视化

    A*是啥? A*搜索算法,俗称A星算法。通过全局路径节点,求解起始点到目标点的最短路径 ,如果存在最短路径,无论在...

  • 19-图的最短路径

    图的最短路径 迪杰斯特拉算法 贝尔曼-福特算法 弗洛伊德算法 SPFA算法(中国西南交通大学段凡丁发明) 最短路径...

网友评论

      本文标题:最短路径算法

      本文链接:https://www.haomeiwen.com/subject/vnzbbqtx.html