Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FloydWarshall.cpp « src « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b1b8c0bce33e12bc308dba191de2bb11ac05e7de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <cassert>
#include <climits>
#include <vector>

#define MAX_DIST (INT_MAX / 2)

//#include "FloydWarshall.h"

// All-pairs shortest path algorithm
void floyd_warshall(const std::vector<std::vector<bool> >& edges, std::vector<std::vector<int> >& dist)
{
  assert(edges.size() == edges.front().size());
  dist.clear();
  dist.resize(edges.size(), std::vector<int>(edges.size(), 0));

  size_t num_edges = edges.size();

  for (size_t i=0; i<num_edges; ++i) {
    for (size_t j=0; j<num_edges; ++j) {
      if (edges[i][j])
        dist[i][j] = 1;
      else
        dist[i][j] = MAX_DIST;
      if (i == j) dist[i][j] = MAX_DIST;
    }
  }

  for (size_t k=0; k<num_edges; ++k)
    for (size_t i=0; i<num_edges; ++i)
      for (size_t j=0; j<num_edges; ++j)
        if (dist[i][j] > (dist[i][k] + dist[k][j]))
          dist[i][j] = dist[i][k] + dist[k][j];
}