2016-10-24 4 views
-2

私はPrimのアルゴリズムを学習しようとしていますが、this websiteを使用していますが、そのコード部分を実行するのに問題があります。私はpublic static int Prims(Vector<Vector<node>> adjList)にあるものと混同され、コードをコンパイルして実行する方法を知りました。 (Javaのために新しいので、その愚かな質問の場合は私に言い訳)。適合コードを取得できません

編集:

class node implements Comparable<node> { 
    int weight, index; 
    public node(int weight, int index) { 
    this.weight = weight; 
    this.index = index; 
    } 
    public int compareTo(node e) { 
    return weight - e.weight; 
    } 
}public static int Prims(Vector<Vector<node>> adjList) { 
    // Current cost of MST. 
    int cost = 0; 
    int n = adjList.size(); 

    PriorityQueue<node> pq = new PriorityQueue<node>(); 

    // Keep track if each node is visited. 
    boolean visited[] = new boolean[n]; 
    for (int i = 0; i < n; i++) { 
    visited[i] = false; 
    } 

    // Number of nodes visited. 
    int inTree = 1; 

    // Mark starting node as visited. 
    visited[0] = true; 

    // Add all edges of starting node. 
    for (int i = 0; i < adjList.get(0).size(); i++) { 
    pq.add(adjList.get(0).get(i)); 
    } 
    // Keep going until all nodes visited. 
    while (!pq.isEmpty() && inTree < n) { 
    // Get the edge with the smallest weight. 
    node cur = pq.poll(); 
    // Skip if node already used. 
    if (visited[cur.index]) { 
     continue; 
    } 
    inTree++; 
    visited[cur.index] = true; 
    cost += cur.weight; 
    // Add all the edges of the new node to the priority queue. 
    for (int i = 0; i < adjList.get(cur.index).size(); i++) { 
     pq.add(adjList.get(cur.index).get(i)); 
    } 
    } 
    // Graph not connected if number of nodes used is less than total nodes. 
    if (inTree < n) { 
    return -1; 
    } 

    return cost; 
} 
+0

ようこそStackOverflow。 [help]にアクセスして[ask]を読むのに時間をかけてください。より多くのコードを表示し、問題の内容をより明確に説明する必要があります。 –

答えて

-1

をあなたは、以下のコマンドを発行する必要があるコードをコンパイルして実行するためにIDEを使用しない場合:これは私が実行しようとしているコードである

javac MyCode.java 
java MyCode 

あなたのコードはMyCode.javaという名前のファイルにあり、定義されたパッケージはないと思います。

0

あなたの方法public static int Prims(Vector<Vector<node>> adjList)は、クラスのメンバーではありません。それが必要です。先頭の}の行

}public static int Prims(Vector<Vector<node>> adjList) { 

は、ファイルの最後に移動する必要があります。

関連する問題