Prim's MST
This algorithm is another one approach to find the MST - minimum spanning tree. The main idea is to partition a set of vertices into two: included to MST already and excluded. Step by step you’ll replace vertices from the second set to the first by picking the less weighted edge that connects both sets.
So algorithm is next:
- Put the start vertex to the
visited
, the rest will beunvisited
, i. e. in the second set. - Start adding vertices. Visit all neighbors for all visited vertices and select the edge with least weight, that does not create a cycle. Mark the adjacent vertex as
visited
. - Do the 2) until all vertices are visited.
You can find a lot of different implementations, but the main idea is to put all vertices into the visited
set step by step.
Swipe to start coding
Complete the primMST()
algorithm inside Graph
class. Init the graph and call this method. To detect a cycle, use BFS() or DFS() algorithms. You can implement it inside the class.
Рішення
Дякуємо за ваш відгук!
single
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Сумаризуйте цей розділ
Пояснити код у file
Пояснити, чому file не вирішує завдання
Awesome!
Completion rate improved to 7.69
Prim's MST
Свайпніть щоб показати меню
This algorithm is another one approach to find the MST - minimum spanning tree. The main idea is to partition a set of vertices into two: included to MST already and excluded. Step by step you’ll replace vertices from the second set to the first by picking the less weighted edge that connects both sets.
So algorithm is next:
- Put the start vertex to the
visited
, the rest will beunvisited
, i. e. in the second set. - Start adding vertices. Visit all neighbors for all visited vertices and select the edge with least weight, that does not create a cycle. Mark the adjacent vertex as
visited
. - Do the 2) until all vertices are visited.
You can find a lot of different implementations, but the main idea is to put all vertices into the visited
set step by step.
Swipe to start coding
Complete the primMST()
algorithm inside Graph
class. Init the graph and call this method. To detect a cycle, use BFS() or DFS() algorithms. You can implement it inside the class.
Рішення
Дякуємо за ваш відгук!
Awesome!
Completion rate improved to 7.69single