0

实际上,我想知道 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()
    {
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                System.out.print("  "+w[i][j]+"  ");
            }
        System.out.println();
        }
    }
}
4

2 回答 2

6

Java中的Prims算法实现-最小成本生成树

public class Prim {
    int weightArray[][] = new int[20][20];
    int visited[] = new int [20];
    int d[] = new int[20];
    int p[] = new int[20];
    int verticeCount, edgeCount;
    int nodeA, nodeB, weight;
    int current, total, mincost;


    public static void main(String args[]) throws IOException {

        // Instantiate the graph based on input
        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("\nEnter number of vertices: ");
        verticeCount = Integer.parseInt(buf.readLine());
        System.out.print("\nEnter number of edges: ");
        edgeCount = Integer.parseInt(buf.readLine());
        for (int i = 1; i <= verticeCount; i++) {
            for(int j = 1; j <= verticeCount; j++) {
                weightArray[i][j] = 0;
            }
        }

       for (int i = 1; i <= verticeCount; i++) {
           p[i] = visited[i] = 0;
           d[i] = 32767;
        }

        for (int i = 1; i <= edgeCount; i++) {
            System.out.print("\nEnter edge nodeA, nodeB and weightArray weight: ");
            nodeA=Integer.parseInt(in.readLine());
            nodeB=Integer.parseInt(in.readLine());
            weight=Integer.parseInt(in.readLine());
            weightArray[nodeA][nodeB] = weightArray[nodeB][nodeA] = weight;
        }
        // End of graph instantiation

        current = 1;
        d[current] = 0;
        total = 1;
        visited[current] = 1;
        while( total != verticeCount) {
            for (int i = 1; i <= verticeCount; i++) {
                if ( weightArray[current][i] != 0) {
                    if( visited[i] == 0) { 
                        if (d[i] > weightArray[current][i]) {
                            d[i] = weightArray[current][i];
                            p[i] = current;
                        }
                    }
                }
            }
            mincost=32767;
            for (int i = 1 ; i <= verticeCount; i++) {
                if (visited[i] == 0) {
                    if (d[i] < mincost) {
                        mincost = d[i];
                        current = i;
                    }
                }
            }
            visited[current]=1;
            total++;
        }

        mincost=0;
        for(i=1;i<=verticeCount;i++)
        mincost=mincost+d[i];

        System.out.print("\n Minimum cost="+mincost);
        System.out.print("\n Minimum Spanning tree is");

        for(i=1;i<=verticeCount;i++)
        System.out.print("\n vertex" +i+"is connected to"+p[i]);
    }
}
于 2013-08-02T05:20:41.897 回答
0

Prim的算法:

Prim 算法是一种贪心算法,它为连接的加权无向图找到最小生成树。这意味着它找到了形成包含每个顶点的树的边的子集,其中树中所有边的总权重最小化。

定义:

  • 创建一个包含单个顶点的树,从图中任意选择
  • 创建一个包含图中所有边的集合
  • 循环直到集合中的每条边连接树中的两个顶点

    • 从集合中移除连接树中的顶点与树中不存在的顶点的最小权重的边
  • 将该边添加到树中

维基链接 - http://en.wikipedia.org/wiki/Prim 's_algorithm

示例链接:

http://github.com/phishman3579/java-algorithms-implementation/blob/master/src/com/jwetherell/algorithms/graph/Prim.java

http://sourcecodesforfree.blogspot.in/2013/05/10-prims-algorithm.html

于 2013-08-02T05:20:27.740 回答