Prim's Algorithm

์šฐ์„ ์ˆœ์œ„ ํ์˜ ๋ฐฉ๋ฒ•์„ ์ด์šฉํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

vertex๋ฅผ ํ•œ๊ฐœ์”ฉ ์„ ํƒํ•˜๋ฉฐ ์ตœ์†Œ ๋น„์šฉ์˜ edge๋ฅผ ์ฐพ๋Š” ๋ฐฉ๋ฒ•.

decrease-key์˜ ๊ฐœ๋…์„ ์ด์šฉํ•˜๋ฉฐ decrease-key๋Š” ํ˜„์žฌ ๊ณ„์‚ฐ๋œ v๋…ธ๋“œ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ณด๋‹ค ํ˜„์žฌ ๋…ธ๋“œ u๋ถ€ํ„ฐ v๊นŒ์ง€์˜ ๊ฒฝ๋กœ๊ฐ€ ๋” ์ž‘๋‹ค๋ฉด ๊ฐ’์„ ๊ฐฑ์‹ ํ•ด์ฃผ๋Š” ๊ฒƒ.

ํŠน์ง•

  • ์ •์  ์„ ํƒ ๊ธฐ๋ฐ˜

  • ์‹œ์ž‘ ์ •์ ๋ถ€ํ„ฐ ์ถœ๋ฐœํ•˜์—ฌ ํ•ด๋‹น ๋…ธ๋“œ๊นŒ์ง€์˜ ์ตœ์†Œ ๋น„์šฉ์„ ๊ธฐ๋กํ•˜๋Š” ๋ฐฐ์—ด์„ ์ด์šฉํ•˜์—ฌ ๊ตฌํ•˜๋Š” ๋ฐฉ์‹

  • ์ž๋ฃŒ๊ตฌ์กฐ์ค‘ ํ•˜๋‚˜์ธ ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์ด์šฉํ•˜๋ฉฐ, ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ–ˆ๋Š”๊ฐ€๊ฐ€ ์‹œ๊ฐ„๋ณต์žก๋„์— ์˜ํ–ฅ

Pesudo Code

MST-Prim(G, w, r)
    Q = V[G];
    for each u โˆˆ Q
        key[u] = INFINITY;
    key[r] = 0;
    p[r] = NULL;
    while (Q not empty)
        u = ExtractMin(Q);
        for each v โˆˆ Adj[u]
            if (v โˆˆ Q and w(u,v) < key[v])  //Decrease-key
                p[v] = u;
                key[v] = w(u,v);

๊ตฌํ˜„ ๋ฐฉ๋ฒ•

  1. vertex๋“ค์˜ key๊ฐ’์„ Infinity๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.

  2. start vertex์˜ key๊ฐ’์„ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. (์–ด๋–ค vertex๋ฅผ ์„ ํƒํ•˜๋”๋ผ๊ณ  MST๊ฐ€ ๋‚˜์˜จ๋‹ค.)

  3. ํ˜„์žฌ vertex์— ์ธ์ ‘ํ•œ vertex๋“ค ์ค‘ ์„ ํƒํ•˜์ง€ ์•Š์•˜๊ณ , ๊ฐ€์žฅ vertex์˜ key๊ฐ’์ด ์ž‘์€ vertex์„ ์ฐพ๋Š”๋‹ค. (exract-min = ์ตœ์†Œ๊ฐ’ ์ถ”์ถœ)

  4. ํ˜„์žฌ vertex๋ฅผ ์„ ํƒํ•œ๋‹ค.

  5. ์ธ์ ‘ํ•œ vertex์ค‘ vertex์˜ key๊ฐ’๋ณด๋‹ค ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ๋” ์ž‘๋‹ค๋ฉด key๊ฐ’์„ ๊ฐ€์ค‘์น˜๋กœ ๊ฐฑ์‹  ํ•œ๋‹ค. (decrease -key)

  6. ์ธ์ ‘ํ•œ vertex์ค‘ ์„ ํƒํ•˜์ง€ ์•Š์•˜๊ณ , ๊ฐ€์žฅ vertex์˜ key๊ฐ’์ด ์ž‘์€ vertex๋ฅผ ๊ธฐ์ค€์œผ๋กœ 3๋ฒˆ๋ถ€ํ„ฐ ๋‹ค์‹œ ๋ฐ˜๋ณตํ•œ๋‹ค.

  7. ๋ชจ๋“  vertex๊ฐ€ ์„ ํƒ๋˜์—ˆ๋‹ค๋ฉด ๋๋‚œ๋‹ค.

์ธ์ ‘ ํ–‰๋ ฌ


์œ„์— ์„ค๋ช…ํ•œ 3๋ฒˆ ๋ฐฉ๋ฒ•์˜ extract-min์„ ์•„๋ž˜์™€ ๊ฐ™์ด ๋ฐฐ์—ด๋กœ ๊ตฌํ˜„ํ•˜๋ฉฐ, ๋งค๋ฒˆ VํšŒ ๋ฐ˜๋ณตํ•œ๋‹ค.

    int v = -1;              //์ธ์ ‘ vertex์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ–๋Š” vertex
    int min_key = INFINITY;  //์ธ์ ‘ vertex์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ€์ค‘์น˜

    /* ์ธ์ ‘ vertex์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ–๋Š” vertex ์ฐพ๊ธฐ*/
    for (int j = 0; j < V; j++) {
        if (!selected[j] && (min_key > vertex_key[j])) {
            v = j;
            min_key = vertex_key[j];
        }
    }

์•„๋ž˜๋Š” 5๋ฒˆ ๋ฐฉ๋ฒ•์œผ๋กœ ์ธ์ ‘ํ•œ vertex์ค‘ vertex์˜ key๊ฐ’๋ณด๋‹ค ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ๋” ์ž‘๋‹ค๋ฉด key๊ฐ’์„ ๊ฐ€์ค‘์น˜๋กœ ๊ฐฑ์‹  ํ•œ๋‹ค.

    for (int j = 0; j < V; j++) {
        if (vertex_key[j] > adjMatrix[v][j]) {
            vertex_key[j] = adjMatrix[v][j];
        }
    }

์ธ์ ‘ ๋ฆฌ์ŠคํŠธ


์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋Š” ์ธ์ ‘ํ•œ vertex์˜ ๊ฐ€์ค‘์น˜๋ฅผ priority queue๋ฅผ ํ†ตํ•ด ์ €์žฅํ•œ๋‹ค.

๋•Œ๋ฌธ์—, 3๋ฒˆ ๋ฐฉ๋ฒ•์˜ exract-min์€ ๋งจ์•ž์—์„œ pop์„ ์‹œ์ผœ ์ฐพ์•„์ฃผ๋ฉด ๋œ๋‹ค.

std::set<II> q;  //์ด์ง„ํž™์œผ๋กœ queue ๋งŒ๋“ค๊ธฐ ( set์€ red-black tree๋กœ ๋งŒ๋“ค์–ด์ง )
auto u = q.begin();  // extract-min

5๋ฒˆ ๋ฐฉ๋ฒ•์œผ๋กœ ์ธ์ ‘ํ•œ vertex์ค‘ vertex์˜ key๊ฐ’๋ณด๋‹ค ๊ฐ„์„ ์˜ ๊ฐ€์ค‘์น˜๊ฐ€ ๋” ์ž‘๋‹ค๋ฉด key๊ฐ’์„ ๊ฐ€์ค‘์น˜๋กœ ๊ฐฑ์‹  ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์ด ์ธ์ ‘ํ•œ ๊ฐ„์„ ์˜ ๊ฐœ์ˆ˜๋งŒํผ ์ˆ˜ํ–‰ํ•œ๋‹ค.

    /*selectํ•œ vertex์™€ ์ธ์ ‘ํ•œ ๊ฐ„์„ ์ธ e*/
        for (auto e : adjList[ํ˜„์žฌ vertex]) {
            /* ์„ ํƒ๋˜์ง€ ์•Š์€ vertex์ด๊ณ  ํ•ด๋‹น vertex์˜ key๊ฐ’๊ณผ edge์˜ cost๋ฅผ ๋น„๊ตํ•ด cost๊ฐ€ ๋” ์ž‘๋‹ค๋ฉด*/
            if (!selected[์ธ์ ‘ํ•œ vertex] && vertex_key[์ธ์ ‘ํ•œ vertex] > ๊ฐ€์ค‘์น˜) {
                q.erase({vertex_key[์ธ์ ‘ํ•œ vertex], e.second});  //๊ฐ™์€ vertex๋กœ ํ–ฅํ•˜๋Š” ๊ฐ„์„ ์ค‘ weight๊ฐ€ ๋” ์ž‘์€ ๊ฐ„์„ ์ด ์žˆ๋‹ค๋ฉด ๊ทธ ์ „ ๊ฐ„์„ ์€ ์‚ญ์ œ
                vertex_key[์ธ์ ‘ํ•œ vertex] = ๊ฐ€์ค‘์น˜;  // vertex key๊ฐ’ ๊ฐฑ์‹ 
                q.insert({๊ฐ€์ค‘์น˜, ์ธ์ ‘ํ•œ vertex});   //ํ์— ์‚ฝ์ž…
            }
        }

์‹œ๊ฐ„ ๋ณต์žก๋„

์‹œ๊ฐ„๋ณต์žก๋„๋Š” ์ดˆ๊ธฐํ™”ํ•˜๋Š”๋ฐ O(|V|), MST๊ณ„์‚ฐํ•˜๋Š”๋ฐ O(|V|) * T(extract-min) (๊ฐ€์žฅ ์ ์€ ๊ฐ’ ์ถ”์ถœํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฐ์‹œ๊ฐ„) + O(|E|) * T(decrease-key) ( key๊ฐ’ ๋ณ€๊ฒฝํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„ )์™€ ๊ฐ™๋‹ค.

๋”ฐ๋ผ์„œ, priority-queue๋ฅผ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ–ˆ๋Š”์ง€์— ๋”ฐ๋ผ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋‹ฌ๋ผ์ง„๋‹ค.

์ผ๋ฐ˜ ๋ฐฐ์—ด๋กœ ๊ตฌํ˜„ํ–ˆ์„ ๊ฒฝ์šฐ T(extract)๊ฐ€ O(|V|), T(decrease)๋Š” O(1)๋งŒํผ ๊ฑธ๋ ค ์ด O(|V^2|)์ด ๊ฑธ๋ฆฐ๋‹ค.

binary heap(์ด์ง„ ํž™)์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด T(extract)๊ฐ€ O(lgV), T(decrease)๋Š” O(lgV)๋งŒํผ ๊ฑธ๋ ค ์ด O(VlgV) + O(ElgV) ์ด๊ธฐ ๋•Œ๋ฌธ์— O((E+V)lgV)๋งŒํผ ๊ฑธ๋ฆฐ๋‹ค. ๋ฌด๋ฐฉํ–ฅ ๊ทธ๋ž˜ํ”„์ผ๋•Œ E์˜ ์ตœ์†Œ๊ฐ’์€ V-1๋กœ ๊ฑฐ์˜ ๋Œ€๋ถ€๋ถ„์ด |E| > |V| ์ด๋ฏ€๋กœ O(|E|lg|V|)๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

priority queue๋ฅผ ์ด์ง„ ํž™์ด ์•„๋‹Œ fibonacci heap์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด decrease-key์˜ ์‹œ๊ฐ„์„ ์ข€๋” ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค. decrease-key์‹œ๊ฐ„์ด O(1)๋งŒํผ ๊ฑธ๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— O(E+VlgV)๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. O(E) or O(VlgV) ์ธ ์ด์œ ๋Š” ์ตœ์•…์˜ ๊ฒฝ์šฐ์— E๋Š” O(V^2)์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๊ตฌํ˜„ ์ฝ”๋“œ

์•„๋ž˜ ์ฝ”๋“œ๋Š” ์‚ฌ์ดํด์ด์—†๋Š” ๋ฌด๋ฐฉํ–ฅ์˜ ๊ทธ๋ž˜ํ”„์ด๊ณ , ๊ฐ€์ค‘์น˜๋ฅผ ๋ฌด์ž‘์œ„๋กœ ์ƒ์„ฑํ•œ ๊ทธ๋ž˜ํ”„์ด๋‹ค.

์†Œ์Šค์ฝ”๋“œ c++ ( ์ ‘๊ธฐ / ํŽผ์น˜๊ธฐ )

#include <time.h>  //์‹œ๊ฐ„ ์ธก์ •

#include <algorithm>  //for_each
#include <cstdlib>    //rand
#include <ctime>      //time
#include <iostream>
#include <set>
#include <vector>

#define INFINITY 2147483647
#define II std::pair<int, int>  // first = weight, second = dest

typedef struct edge {
    int src;     //์ถœ๋ฐœ vertex
    int dest;    //๋„์ฐฉ vertex
    int weight;  //๊ฐ€์ค‘์น˜(๋น„์šฉ)
} edge;

class Graph {
   private:
    edge e;

   public:
    Graph(int src = 0, int dest = 0, int weight = 0) {
        this->e.src = src;
        this->e.dest = dest;
        this->e.weight = weight;
    }
    int getSrc() { return this->e.src; }
    int getDest() { return this->e.dest; }
    int getWeight() { return this->e.weight; }
};

void CalcTime();
void randomPush(std::vector<Graph> &);     // graph์— ์‚ฌ์ดํด ์—†๋Š” ์—ฐ๊ฒฐ๊ทธ๋ž˜ํ”„ cost๊ฐ’ ๋ฌด์ž‘์œ„ ์ƒ์„ฑ
void print_edge_info(std::vector<Graph>);  // graph ๊ฐ„์„ ๋“ค ๋ณด๊ธฐ

int prim_adjList_heap(std::vector<Graph> &, std::vector<std::vector<II>>,
                      int);  // Adj list์™€ priority queue ์ด์šฉํ•ด ๊ตฌํ˜„ --> set์€ red-black-tree
void make_adj_list(std::vector<Graph>, std::vector<std::vector<II>> &);  //์ฃผ์–ด์ง„ ๊ทธ๋ž˜ํ”„๋ฅผ ์ธ์ ‘๋ฆฌ์ŠคํŠธ๋กœ ํ‘œํ˜„

int prim_adjMatrix(std::vector<Graph> &, std::vector<std::vector<int>>, int);  // Adj matrix๋กœ ๊ตฌํ˜„
void make_adj_matrix(std::vector<Graph>, std::vector<std::vector<int>> &);     //์ฃผ์–ด์ง„ ๊ทธ๋ž˜ํ”„๋ฅผ ์ธ์ ‘ํ–‰๋ ฌ๋กœ ํ‘œํ˜„

int V;                                 // vertex ๊ฐœ์ˆ˜
clock_t start, finish, used_time = 0;  //์‹คํ–‰ ์‹œ๊ฐ„ ์ธก์ •์„ ์œ„ํ•œ ๋ณ€์ˆ˜

int main() {
    std::vector<Graph> g;    // graph g
    int minimum_weight = 0;  // minimum cost
    std::vector<std::vector<II>> adjList;
    std::vector<std::vector<int>> adjMatrix;

    randomPush(g);       //๊ฐ„์„  random ์‚ฝ์ž…
    print_edge_info(g);  // edge info print

    make_adj_list(g, adjList);      //์ฃผ์–ด์ง„ ๊ทธ๋ž˜ํ”„๋ฅผ ์ธ์ ‘๋ฆฌ์ŠคํŠธ๋กœ ๋งŒ๋“ค๊ธฐ
    make_adj_matrix(g, adjMatrix);  //์ฃผ์–ด์ง„ ๊ทธ๋ž˜ํ”„๋ฅผ ์ธ์ ‘ํ–‰๋ ฌ๋กœ ๋งŒ๋“ค๊ธฐ

    start = clock();
    minimum_weight = prim_adjMatrix(g, adjMatrix, 0);  //์ธ์ ‘ํ–‰๋ ฌ์„ ์ด์šฉํ•œ prim's algorithm (0๋ฒˆ๋…ธ๋“œ๋ฅผ ์ฒซ ๋…ธ๋“œ๋กœ ์‹œ์ž‘)
    // minimum_weight = prim_adjList_heap(g, adjList, 0);  //์ธ์ ‘๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•œ prim's algorithm (0๋ฒˆ๋…ธ๋“œ๋ฅผ ์ฒซ ๋…ธ๋“œ๋กœ ์‹œ์ž‘)
    finish = clock();
    std::cout << "\nminimum cost : " << minimum_weight << std::endl;
    CalcTime();

    return 0;
}

int prim_adjList_heap(std::vector<Graph> &g, std::vector<std::vector<II>> adjList, int start) {
    int sum = 0;
    std::set<II> q;                               //์ด์ง„ํž™์œผ๋กœ queue ๋งŒ๋“ค๊ธฐ ( set์€ red-black tree๋กœ ๋งŒ๋“ค์–ด์ง )
    std::vector<int> vertex_key(V, INFINITY);     // vertex์˜ ์ตœ์†Œ weight๊ฐ’ ๊ณ„์‚ฐ
    std::vector<bool> selected(g.size(), false);  //์„ ํƒ๋œ vertex์ธ๊ฐ€

    vertex_key[start] = 0;
    q.insert(II(0, start));  //์‹œ์ž‘ ๋…ธ๋“œ ๊ฐ€์ค‘์น˜ 0์œผ๋กœ ์‹œ์ž‘
    std::cout << "\nroute";

    /*vertex ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•œ๋‹ค
     while๋Œ€์‹  for(int i=0; i < V ; i++)๋กœ ํ•ด๋„ ๋ฌด๋ฐฉ
    */
    while (!q.empty()) {
        /*extract min*/
        int select_key = q.begin()->second;
        int min_of_key = q.begin()->first;
        q.erase(q.begin());

        if (selected[select_key]) {
            std::cout << " NOT MST" << std::endl;
            exit(1);
        }

        sum += min_of_key;
        selected[select_key] = true;
        std::cout << "dest : " << select_key << " (dis : " << vertex_key[select_key] << ")" << std::endl;

        /*decrease key*/
        for (auto e : adjList[select_key]) {
            if (!selected[e.second] && vertex_key[e.second] > e.first + vertex_key[select_key]) {
                q.erase({vertex_key[e.second], e.second});  //๊ฐ™์€ ๋…ธ๋“œ๋กœ ํ–ฅํ•˜๋Š” ๊ฐ„์„ ์ค‘ weight๊ฐ€ ๋” ์ž‘์€ ๊ฐ„์„ ์ด ์žˆ๋‹ค๋ฉด ๊ทธ ์ „ ๊ฐ„์„ ์€ ์‚ญ์ œ
                q.insert({e.first, e.second});  //ํ์— ์‚ฝ์ž…
                vertex_key[e.second] = e.first + vertex_key[select_key];
            }
        }
    }

    std::cout << std::endl;
    return sum;
}

void make_adj_list(std::vector<Graph> g, std::vector<std::vector<II>> &adj) {
    adj.resize(V);
    bool isEdge;
    for (int i = 0; i < g.size(); i++) {
        isEdge = false;
        int src = g[i].getSrc();
        int dest = g[i].getDest();
        int weight = g[i].getWeight();

        /*๋™์ผ vertex๋กœ ํ–ฅํ•˜๋Š” ๊ฐ„์„ ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’๋งŒ๊ฐ€์ง€๊ณ  ์ธ์ ‘ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ*/
        if (adj[src].empty()) {
            adj[src].push_back({weight, dest});
        } else {
            for (int j = 0; j < adj[src].size(); j++) {
                if (adj[src][j].second == dest) {
                    isEdge = true;
                    if (adj[src][j].first > weight) {
                        adj[src][j].first = weight;
                    }
                }
            }
            if (!isEdge) adj[src].push_back({weight, dest});
        }

        isEdge = false;
        if (adj[dest].empty()) {
            adj[dest].push_back({weight, src});
        } else {
            for (int j = 0; j < adj[dest].size(); j++) {
                if (adj[dest][j].second == src) {
                    isEdge = true;
                    if (adj[dest][j].first > weight) {
                        adj[dest][j].first = weight;
                    }
                }
            }
            if (!isEdge) adj[dest].push_back({weight, src});
        }
    }
}

int prim_adjMatrix(std::vector<Graph> &g, std::vector<std::vector<int>> adjMatrix, int start) {
    int sum = 0;
    std::vector<int> vertex_key(V, INFINITY);     // vertex์˜ ์ตœ์†Œ weight๊ฐ’ ๊ณ„์‚ฐ
    std::vector<bool> selected(g.size(), false);  //์„ ํƒ๋œ vertex์ธ๊ฐ€

    vertex_key[start] = 0;  //์‹œ์ž‘๋…ธ๋“œ key๊ฐ’ 0์œผ๋กœ ์‹œ์ž‘
    std::cout << "\nroute";
    /*vertex ์ˆ˜๋งŒํผ ๋ฐ˜๋ณตํ•œ๋‹ค*/
    for (int i = 0; i < V; i++) {
        int select_idx = -1;     //์ธ์ ‘ vertex์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ–๋Š” vertex
        int min_key = INFINITY;  //์ธ์ ‘ vertex์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ€์ค‘์น˜

        /* ์ธ์ ‘ vertex์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ–๋Š” vertex ์ฐพ๊ธฐ*/
        for (int j = 0; j < V; j++) {
            if (!selected[j] && (min_key > vertex_key[j])) {
                select_idx = j;
                min_key = vertex_key[j];
            }
        }

        /*ํ˜„์žฌ ์ฝ”๋“œ์—์„œ๋Š” ์—ฐ๊ฒฐ์•ˆ๋œ ๊ทธ๋ž˜ํ”„๋Š” ์ฃผ์–ด์ง€์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—
          ์—†์–ด๋„ ๋ฌด๋ฐฉํ•˜์ง€๋งŒ ๋งŒ์•ฝ์„ ์œ„ํ•œ ์—๋Ÿฌ์ฒ˜๋ฆฌ*/
        if (select_idx == -1) {
            std::cout << "Not MST" << std::endl;
            exit(1);
        }

        selected[select_idx] = true;
        sum += min_key;
        std::cout << " -> " << select_idx << "(cost : " << min_key << ")";

        /*์ธ์ ‘ vertex์˜ weight๊ฐ€ vertex_key๊ฐ’๋ณด๋‹ค ์ž‘๋‹ค๋ฉด key๊ฐ’ ๊ฐฑ์‹  */
        for (int j = 0; j < V; j++) {
            if (!selected[j] && vertex_key[j] > adjMatrix[select_idx][j]) {
                vertex_key[j] = adjMatrix[select_idx][j];
            }
        }
    }
    std::cout << std::endl;
    return sum;
}

void make_adj_matrix(std::vector<Graph> g, std::vector<std::vector<int>> &adj) {
    adj.assign(V, std::vector<int>(V, INFINITY));
    for (int i = 0; i < g.size(); i++) {
        int src = g[i].getSrc();
        int dest = g[i].getDest();
        int weight = g[i].getWeight();

        if (adj[src][dest] > weight) {
            adj[src][dest] = weight;
        }
        if (adj[dest][src] > weight) {
            adj[dest][src] = weight;
        }
    }
}

/*vertex์ˆ˜ ์ž…๋ ฅ๋ฐ›์€ ํ›„ ๊ทธ๋ž˜ํ”„ ๊ฐ„์„  ๊ฐ€์ค‘์น˜ random ์‚ฝ์ž…*/
void randomPush(std::vector<Graph> &g) {
    std::cout << "create number of Vertex : ";
    std::cin >> V;

    srand((unsigned int)time(NULL));
    for (int i = 0; i < V - 1; i++) {
        g.push_back(Graph(i, i + 1, rand() % 1000));
        for (int j = i + 1; j < V; j++) {
            g.push_back(Graph(i, j, rand() % 1000));
        }
    }
    for (int i = (rand() % 3); i < V - 1; i += (rand() % 10)) {
        g.push_back(Graph(i, i + 1, rand() % 1000));
        for (int j = i + 1; j < V; j += (rand() % 10)) {
            g.push_back(Graph(i, j, rand() % 1000));
        }
    }
}

void print_edge_info(std::vector<Graph> g) {
    std::cout << "edge info : \n";
    std::for_each(g.begin(), g.end(), [](Graph a) {
        std::cout << "src : " << a.getSrc() << " desc : " << a.getDest() << " weight : " << a.getWeight() << std::endl;
    });
}

//์‹คํ–‰ ์‹œ๊ฐ„์„ ์ธก์ • ๋ฐ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
void CalcTime() {
    used_time = finish - start;
    printf("\n*********** result **********\n     time : %lf sec\n", (double)(used_time) / CLOCKS_PER_SEC);
}

Last updated