Bellman-ford Algorithm

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

์Œ์˜ ๊ฐ€์ค‘์น˜๋„ ๊ณ„์‚ฐ ํ• ์ˆ˜ ์žˆ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

Vertex์˜ ๊ฐœ์ˆ˜๊ฐ€ N๊ฐœ์ผ ๋•Œ, ํ•œ vertex์—์„œ ๋‹ค๋ฅธ vertex๊นŒ์ง€ ๊ฐ€๋Š”๋ฐ ๊ฑฐ์น˜๋Š” edge์ˆ˜๋Š” ์ตœ์†Œ 1๊ฐœ๋ถ€ํ„ฐ, ์ตœ๋Œ€ N-1๋ฒˆ ๊ฑฐ์น˜๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค.

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

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

ํŠน์ง•

  • ์Œ์˜ ๊ฐ€์ค‘์น˜๋ฅผ ๊ฐ–๋Š” ๊ฒฝ๋กœ๋ฅผ ํฌํ•จํ•ด๋„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

  • ์Œ์˜ ์‚ฌ์ดํด ์กด์žฌ ์—ฌ๋ถ€๋ฅผ ์•Œ ์ˆ˜ ์žˆ๋‹ค. ( ๋ฌดํ•œ ๋ฃจํ”„ )

  • edge ์˜ ์ •๋ณด๋กœ ์ž…๋ ฅ์ด ์ฃผ์–ด์ง€๋ฉด ์ธ์ ‘ํ–‰๋ ฌ, ์ธ์ ‘๋ฆฌ์ŠคํŠธ๋ฅผ ์•ˆ ๋งŒ๋“ค๊ณ ๋„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

Pesudo Code

BELLMAN-FORD(G, w , s){
    INITIALIZE-SINGLE-SOURCE(G, s)
    for i = 1 to |G.V| - 1
        for each edge (u,v) โˆˆ G.E
            RELAX (u,v,w)

    for each edge (u,v) โˆˆ G.E
        if v.d > u.d + w(u,v)
            return false
    return true
}
RELAX(u,v,w)
    if v.d > u.d + w(u,v)
        v.d = u.d + w(u,v)
        v.ฯ€ = u

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

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

  2. start vertex์˜ key๊ฐ’์„ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.

  3. ๋ชจ๋“  edge์— ๋Œ€ํ•ด relax๋ฅผ ์ˆ˜ํ–‰ํ•œ๋‹ค.

  4. 3๋ฒˆ์„ V-1๋ฒˆ ๋ฐ˜๋ณต ํ•œ๋‹ค.

  5. 4๋ฒˆ์ด ๋๋‚˜๋ฉด ์ตœ๋‹จ ๊ฒฝ๋กœ๋Š” ๊ตฌํ•ด์กŒ์œผ๋ฉฐ, ์Œ์˜ ์‚ฌ์ดํด์ด ์žˆ๋Š”์ง€ ์•„๋ž˜์˜ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฒ€์‚ฌํ•œ๋‹ค.

  6. ๋ชจ๋“  edge์— ๋Œ€ํ•ด ๋ชฉ์ ์ง€ vertex์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ ๊ฐ’์ด ์ถœ๋ฐœ์ง€ vertex๊นŒ์ง€์˜ ์ตœ๋‹จ ๊ฒฝ๋กœ ๊ฐ’ + w(u,v) ๋ณด๋‹ค ํฐ์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค.

  7. ๋งŒ์•ฝ ํฌ๋‹ค๋ฉด ์Œ์˜ ์‚ฌ์ดํด์ด ์กด์žฌํ•˜๊ณ  ๋ชจ๋“  edge์— ๋Œ€ํ•ด ์•„๋‹ˆ๋ผ๋ฉด ์Œ์˜ ์‚ฌ์ดํด์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ทธ๋ž˜ํ”„์ด๋‹ค.

  • ์ธ์ ‘ ํ–‰๋ ฌ

    std::vector<int> vertex_key(V, INFINITY);  // vertex์˜ ์ตœ์†Œ weight๊ฐ’ ๊ณ„์‚ฐ
    vertex_key[start] = 0;

    std::cout << "\nsrc : " << start << std::endl;
    for (int i = 0; i < V - 1; i++) {
        for (int j = 0; j < V; j++) {
            for (int k = 0; k < V; k++) {
                if (vertex_key[k] > adjMatrix[j][k] + vertex_key[j]) {
                    vertex_key[k] = adjMatrix[j][k] + vertex_key[j];
                }
            }
        }
    }
    for (int j = 0; j < V; j++) {
        for (int k = 0; k < V; k++) {
            if (vertex_key[k] > adjMatrix[j][k] + vertex_key[j]) {
                std::cout << "์Œ์˜ ์‚ฌ์ดํด์„ ๊ฐ–๋Š” ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค." << std::endl;
                exit(1);
            }
        }
    }
    return vertex_key;

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

std::vector<int> vertex_key(V, INFINITY);  // vertex์˜ ์ตœ์†Œ weight๊ฐ’ ๊ณ„์‚ฐ
    int dest, weight;
    vertex_key[start] = 0;

    std::cout << "\nsrc : " << start << std::endl;
    for (int i = 0; i < V - 1; i++) {
        for (int j = 0; j < adjList.size(); j++) {
            for (int k = 0; k < adjList[j].size(); k++) {
                dest = adjList[j][k].second;
                weight = adjList[j][k].first;
                if (vertex_key[dest] > weight + vertex_key[j]) {
                    vertex_key[dest] = weight + vertex_key[j];
                }
            }
        }
    }

    for (int j = 0; j < adjList.size(); j++) {
        for (int k = 0; k < adjList[j].size(); k++) {
            dest = adjList[j][k].second;
            weight = adjList[j][k].first;
            if (vertex_key[dest] > weight + vertex_key[j]) {
                std::cout << "์Œ์˜ ์‚ฌ์ดํด์„ ๊ฐ–๋Š” ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค." << std::endl;
                exit(1);
            }
        }
    }

    return vertex_key;

  • edge ์ •๋ณด๋กœ ์ฃผ์–ด์กŒ์„ ๊ฒฝ์šฐ

    std::vector<int> vertex_key(V, INFINITY);  // vertex์˜ ์ตœ์†Œ weight๊ฐ’ ๊ณ„์‚ฐ
    int src, dest, weight;
    vertex_key[start] = 0;

    std::cout << "\nsrc : " << start << std::endl;
    for (int i = 0; i < V - 1; i++) {
        for (int j = 0; j < g.size(); j++) {
            src = g[j].getSrc();
            dest = g[j].getDest();
            weight = g[j].getWeight();
            if (vertex_key[dest] > weight + vertex_key[src]) {
                vertex_key[dest] = weight + vertex_key[src];
            }
        }
    }
    for (int j = 0; j < g.size(); j++) {
        src = g[j].getSrc();
        dest = g[j].getDest();
        weight = g[j].getWeight();
        if (vertex_key[dest] > weight + vertex_key[src]) {
            std::cout << "์Œ์˜ ์‚ฌ์ดํด์„ ๊ฐ–๋Š” ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค." << std::endl;
            exit(1);
        }
    }
    return vertex_key;

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

์ดˆ๊ธฐํ™”ํ•˜๋Š”๋ฐ O(|V|) ์ด ์†Œ์š”๋˜๊ณ , ์ตœ๋‹จ ๊ฒฝ๋กœ๋ฅผ ๊ตฌํ•˜๋Š”๋ฐ O(|E|)๋ฒˆ์˜ relax๊ฐ€ |V-1|๋ฒˆ ๋ฐ˜๋ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์—, O(|V||E|)๋งŒํผ์˜ ์‹œ๊ฐ„์ด ์†Œ์š”๋œ๋‹ค. (relax๋Š” ๋น„๊ตํ›„ ๋‹จ์ˆœ ๋Œ€์ž…์ด๋ฏ€๋กœ O(1)์˜ ์‹œ๊ฐ„ ์†Œ์š”)

์Œ์˜ ์‚ฌ์ดํด์ด ์กด์žฌํ•˜๋Š” ์ง€ ๊ฒ€์‚ฌํ•˜๋Š”๋ฐ |E\๋ฒˆ ๋ฐ˜๋ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์—, O(|E|)๋งŒํผ์˜ ์‹œ๊ฐ„์ด ์†Œ์š”๋œ๋‹ค.

๋”ฐ๋ผ์„œ ์ด O(|V||E|)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ–๋Š”๋‹ค.

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

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

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

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

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

#define INFINITY 2140000000
#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>> &);  //์ฃผ์–ด์ง„ ๊ทธ๋ž˜ํ”„๋ฅผ ์ธ์ ‘ํ–‰๋ ค๋กœ ํ‘œํ˜„

std::vector<int> bellman_ford_list(std::vector<std::vector<II>>, int);
std::vector<int> bellman_ford_array(std::vector<std::vector<int>>, int);
std::vector<int> bellman_ford_edge(std::vector<Graph>, int);

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

int main() {
    std::vector<Graph> g;  // graph g
    std::vector<int> shortestPath;
    std::vector<std::vector<int>> adjMatrix;
    std::vector<std::vector<II>> adjList;

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

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

    start = clock();
    // shortestPath = bellman_ford_list(adjList, 0);  // list์„ ์ด์šฉํ•œ ๊ตฌํ˜„
    // shortestPath = bellman_ford_array(adjMatrix, 0);  // array ์ด์šฉํ•œ ๊ตฌํ˜„
    shortestPath = bellman_ford_edge(g, 0);  // array ์ด์šฉํ•œ ๊ตฌํ˜„
    finish = clock();

    for (int i = 0; i < V; i++) {
        std::cout << "dest : " << i << " (cost : " << ((shortestPath[i] == INFINITY) ? "no path" : std::to_string(shortestPath[i])) << " )"
                  << std::endl;
    }
    CalcTime();
    return 0;
}

std::vector<int> bellman_ford_list(std::vector<std::vector<II>> adjList, int start) {
    std::vector<int> vertex_key(V, INFINITY);  // vertex์˜ ์ตœ์†Œ weight๊ฐ’ ๊ณ„์‚ฐ
    int dest, weight;
    vertex_key[start] = 0;

    std::cout << "\nsrc : " << start << std::endl;
    for (int i = 0; i < V - 1; i++) {
        for (int j = 0; j < adjList.size(); j++) {
            for (int k = 0; k < adjList[j].size(); k++) {
                dest = adjList[j][k].second;
                weight = adjList[j][k].first;
                if (vertex_key[dest] > weight + vertex_key[j]) {
                    vertex_key[dest] = weight + vertex_key[j];
                }
            }
        }
    }

    for (int j = 0; j < adjList.size(); j++) {
        for (int k = 0; k < adjList[j].size(); k++) {
            dest = adjList[j][k].second;
            weight = adjList[j][k].first;
            if (vertex_key[dest] > weight + vertex_key[j]) {
                std::cout << "์Œ์˜ ์‚ฌ์ดํด์„ ๊ฐ–๋Š” ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค." << std::endl;
                exit(1);
            }
        }
    }

    return vertex_key;
}

std::vector<int> bellman_ford_array(std::vector<std::vector<int>> adjMatrix, int start) {
    std::vector<int> vertex_key(V, INFINITY);  // vertex์˜ ์ตœ์†Œ weight๊ฐ’ ๊ณ„์‚ฐ
    vertex_key[start] = 0;

    std::cout << "\nsrc : " << start << std::endl;
    for (int i = 0; i < V - 1; i++) {
        for (int j = 0; j < V; j++) {
            for (int k = 0; k < V; k++) {
                if (vertex_key[k] > adjMatrix[j][k] + vertex_key[j]) {
                    vertex_key[k] = adjMatrix[j][k] + vertex_key[j];
                }
            }
        }
    }
    for (int j = 0; j < V; j++) {
        for (int k = 0; k < V; k++) {
            if (vertex_key[k] > adjMatrix[j][k] + vertex_key[j]) {
                std::cout << "์Œ์˜ ์‚ฌ์ดํด์„ ๊ฐ–๋Š” ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค." << std::endl;
                exit(1);
            }
        }
    }
    return vertex_key;
}

std::vector<int> bellman_ford_edge(std::vector<Graph> g, int start) {
    std::vector<int> vertex_key(V, INFINITY);  // vertex์˜ ์ตœ์†Œ weight๊ฐ’ ๊ณ„์‚ฐ
    int src, dest, weight;
    vertex_key[start] = 0;

    std::cout << "\nsrc : " << start << std::endl;
    for (int i = 0; i < V - 1; i++) {
        for (int j = 0; j < g.size(); j++) {
            src = g[j].getSrc();
            dest = g[j].getDest();
            weight = g[j].getWeight();
            if (vertex_key[dest] > weight + vertex_key[src]) {
                vertex_key[dest] = weight + vertex_key[src];
            }
        }
    }
    for (int j = 0; j < g.size(); j++) {
        src = g[j].getSrc();
        dest = g[j].getDest();
        weight = g[j].getWeight();
        if (vertex_key[dest] > weight + vertex_key[src]) {
            std::cout << "์Œ์˜ ์‚ฌ์ดํด์„ ๊ฐ–๋Š” ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค." << std::endl;
            exit(1);
        }
    }
    return vertex_key;
}

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