小编Jus*_*tSt的帖子

我想在java中编写一个prim的算法

实际上,我想知道prim和Dijkstra算法的含义.如果有人能教如何在JAVA中编写它,我感激不尽.我试着去理解某人的prim算法代码,但我在某个地方停留了.

下面显示的代码是随机矩阵.我想继续写prim的算法.有人可以帮忙吗?

 import java.util.*;
 class RandomGraph
 {
    public static Scanner br = new Scanner(System.in);
    static int w [][];
    static int n;
    static int i, j;

    public static void main (String[] args)
    {
        System.out.println("Find the shortest edge");
        System.out.println("\nEnter the number of the vertices: ");
        n = br.nextInt();
        w = new int[n+1][n+1];
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
            {
                if((i!=j))
                {
                w[i][j] = w[j][i]= 1+(int)(Math.random()*9);
                }
                else if(i == j)
                {
                    w[i][j] = w[j][i] = 0;
                }
            }
        Graph();
    }

    static void Graph()
    { …
Run Code Online (Sandbox Code Playgroud)

java

0
推荐指数
1
解决办法
3万
查看次数

标签 统计

java ×1