2016-10-24 2 views
-2

Я пытаюсь изучить алгоритм Прима, и я использую 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. Пожалуйста, найдите время, чтобы посетить [помощь], а также прочитать [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) { 

должен вероятная быть перемещен в конец файла.

Смежные вопросы