Dijkstra's Algorithm

๊ทธ๋ž˜ํ”„ ์ค‘์—์„œ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ค‘์— ํ•˜๋‚˜๋กœ ํ•˜๋‚˜์˜ ์ •์ ์—์„œ ๋‹ค๋ฅธ ๋ชจ๋“  ์ •์ ๊นŒ์ง€์˜ ์ตœ๋‹จ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜ (single-source shortest path algorithmm)

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

vertex๋ฅผ ํ•œ๊ฐœ์”ฉ ์„ ํƒํ•˜๋ฉฐ ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๋ฐฉ๋ฒ•.

relax์˜ ๊ฐœ๋…์„ ์ด์šฉํ•˜๋ฉฐ relax๋Š” ํ˜„์žฌ ๊ณ„์‚ฐ๋œ v๋…ธ๋“œ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋ณด๋‹ค ํ˜„์žฌ ๋…ธ๋“œ u๊นŒ์ง€์˜ ๊ฒฝ๋กœ์™€ u์—์„œ v์˜ ๊ฐ€์ค‘์น˜ ( e(u,v) ) ๊ฐ€ ๋” ์ž‘๋‹ค๋ฉด ๊ฐ’์„ ๊ฐฑ์‹ ํ•ด์ฃผ๋Š” ๊ฒƒ.

relax๋Š” prim ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ decrease-key์™€ ์œ ์‚ฌํ•˜๋ฉฐ dp๋ฅผ ์ถ”๊ฐ€ํ•œ ๊ฐœ๋…

ํŠน์ง•

  • ํƒ์š•์ ์ธ ๋ฐฉ๋ฒ•( Greedy Method )

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

  • ์Œ์˜ ๊ฐ€์ค‘์น˜ ํ—ˆ์šฉ x

  • ์‹œ์ž‘ ์ •์ ๋ถ€ํ„ฐ ์ถœ๋ฐœํ•˜์—ฌ ์ตœ์†Œ ๋น„์šฉ์˜ ๊ฐ„์„ ์„ ๊ฐ–๋Š” ์ •์ ์„ ์„ ํƒํ•˜์—ฌ ์‹ ์žฅ ํŠธ๋ฆฌ ์ง‘ํ•ฉ์„ ๋‹จ๊ณ„์ ์œผ๋กœ ํ™•์žฅ

  • ๋ฐ€์ง‘ ๊ทธ๋ž˜ํ”„์— ์ ํ•ฉ

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

Pesudo Code

Dijkstra(G, w , s){
    INITIALIZE-SINGLE-SOURCE(G, s)
    S <- โˆ… Q <- V[G]
    while Q != โˆ…
        do u <- EXTRACT-MIN(โˆ…)
        s <- S โˆช {u}
        for each vertex v โˆˆ Adj[u]
            do RELAX(u, v, w)
}
RELAX(u,v,w)
    if v.d > u.d + w(u,v)
        v.d = u.d + w(u,v)
        v.ฯ€ = u

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

Prim's ์•Œ๊ณ ๋ฆฌ์ฆ˜์—์„œ decrease key ๋ถ€๋ถ„์„ dp๋ฅผ ์ด์šฉํ•œ๋‹ค๋Š” ๊ฒƒ ๋นผ๊ณ  ๋‹ค๋ฅธ ๊ฒƒ์ด ์—†๋‹ค.

  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๊ฐ’์„ ๊ฐ€์ค‘์น˜์™€ ํ˜„์žฌ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ์˜ ํ•ฉ์œผ๋กœ ๊ฐฑ์‹  ํ•œ๋‹ค. (RELAX)

  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๊ฐ’์„ ๊ฐ€์ค‘์น˜์™€ ํ˜„์žฌ๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ์˜ ํ•ฉ์œผ๋กœ ๊ฐฑ์‹  ํ•œ๋‹ค.

์ž˜ ๋ณด๋ฉด ์•Œ๊ฒ ์ง€๋งŒ Prim's ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๋น„๊ตํ•ด์„œ vertex_key[selct_idx]์ถ”๊ฐ€ ๋˜์—ˆ๋‹ค.

    for (int j = 0; j < V; j++) {
        if (!selected[j] && vertex_key[j] > adjMatrix[select_idx][j] + vertex_key[select_idx]) {
            vertex_key[j] = adjMatrix[select_idx][j] + vertex_key[select_idx];
        }
    }

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


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

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

    int select_key = q.begin()->second;
    int min_of_key = q.begin()->first;
    q.erase(q.begin());

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

    /*selectํ•œ vertex์™€ ์ธ์ ‘ํ•œ ๊ฐ„์„ ์ธ e*/
    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];
        }
    ใ…‹}

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

Prim's ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๋™์ผํ•˜๋‹ค.

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

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

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

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

priority queue๋ฅผ ์ด์ง„ ํž™์ด ์•„๋‹Œ fibonacci heap์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด RELAX์˜ ์‹œ๊ฐ„์„ ์ข€๋” ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค. RELAX์‹œ๊ฐ„์ด 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 ๊ฐ„์„ ๋“ค ๋ณด๊ธฐ
void make_adj_list(std::vector<Graph>, std::vector<std::vector<II>> &);     //์ฃผ์–ด์ง„ ๊ทธ๋ž˜ํ”„๋ฅผ ์ธ์ ‘๋ฆฌ์ŠคํŠธ๋กœ ํ‘œํ˜„
void make_adj_matrix(std::vector<Graph>, std::vector<std::vector<int>> &);  //์ฃผ์–ด์ง„ ๊ทธ๋ž˜ํ”„๋ฅผ ์ธ์ ‘ํ–‰๋ ค๋กœ ํ‘œํ˜„

int dijkstra_heap(std::vector<Graph> &, std::vector<std::vector<II>>, int);
int dijkstra_array(std::vector<Graph> &, std::vector<std::vector<int>>, 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<int>> adjMatrix;
    std::vector<std::vector<II>> adjList;

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

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

    start = clock();
    // minimum_weight = dijkstra_heap(g, adjList, 0); //binary heap์„ ์ด์šฉํ•œ ๊ตฌํ˜„
    minimum_weight = dijkstra_array(g, adjMatrix, 0);  // array ์ด์šฉํ•œ ๊ตฌํ˜„
    finish = clock();
    std::cout << "\nall route dis : " << minimum_weight << std::endl;
    CalcTime();

    return 0;
}

int dijkstra_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(V, false);      //์„ ํƒ๋œ vertex์ธ๊ฐ€

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

    /*Vertex๋งŒํผ ๋ฐ˜๋ณต*/
    while (!q.empty()) {
        /*extract min*/
        int select_key = q.begin()->second;
        int min_of_key = q.begin()->first;
        q.erase(q.begin());

        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;
}

int dijkstra_array(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(V, false);      //์„ ํƒ๋œ vertex์ธ๊ฐ€

    vertex_key[start] = 0;
    std::cout << "\nstart : 0\n";

    /*Vertex๋งŒํผ ๋ฐ˜๋ณต*/
    for (int i = 0; i < V; i++) {
        /*extract min*/
        int select_idx = -1, min_of_key = INFINITY;
        for (int j = 0; j < V; j++) {
            if (!selected[j] && min_of_key > vertex_key[j]) {
                select_idx = j;
                min_of_key = vertex_key[j];
            }
        }

        sum += min_of_key;
        selected[select_idx] = true;

        std::cout << "dest : " << select_idx << " (dis : " << vertex_key[select_idx] << ")" << std::endl;

        /*decrease key*/
        for (int j = 0; j < V; j++) {
            if (!selected[j] && vertex_key[j] > adjMatrix[select_idx][j] + vertex_key[select_idx]) {
                vertex_key[j] = adjMatrix[select_idx][j] + vertex_key[select_idx];
            }
        }
    }
    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});
        }
    }
}

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;
        }
    }
}

/*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