Algorithm level

Breadth-first search (BFS)

From Algowiki
Jump to navigation Jump to search
Get Perf.Data


Алгоритм поиска в ширину (BFS)
Sequential algorithm
Serial complexity [math]O(|V| + |E|)[/math]
Input data [math]O(|V| + |E|)[/math]
Output data [math]O(|V|)[/math]
Parallel algorithm
Parallel form height [math]N/A, \max O(|V|) [/math]
Parallel form width [math]N/A, \max O(|E|) [/math]


Primary author of this description: I.V.Afanasyev.

1 Properties and structure of the algorithm

1.1 General description of the algorithm

The Breadth-First Search (BFS) makes it possible to calculate the shortest distances (measured in terms of the number of arcs) between a selected node of a directed graph and all the other its nodes and/or build up the directed rooted tree with the distances equal to those in the original graph. Moreover, the breadth-first search allows one to solve the reachability problem (that is, to find out where there exist paths leading from the source node to other nodes of the graph). The algorithm was first presented in the following publications: [1] and [2].

The algorithm traverses the nodes layer-wise. At each step, there is a set of "advanced" nodes, and their neighbor nodes are checked whether they were not yet. The not yet visited nodes are added to the new level of advanced nodes, which are processed at the next step. At first, the set of advanced nodes consists of the source node only, and the graph traversal begins from this node.

The serial complexity of the algorithm is [math]O(|V| + |E|)[/math], where [math]|V|[/math] is the number of nodes and [math]|E|[/math] is the number of arcs in the graph.

1.2 Mathematical description of the algorithm

Let [math]G = (V, E)[/math] be an unweighted graph with a singled-out source node [math]u[/math]. The path [math]P(u,v)[/math] between the nodes [math]u[/math] and [math]v[/math] is the set of arcs [math](u, v_1), (v_1, v_2), ... (v_{n-1}, v)[/math]. The length [math]d(u,v)[/math] of a path between the nodes [math]u[/math] and [math]v[/math] is the number of its arcs. The breadth-first search finds the shortest paths [math]d(u,v)[/math] from the node [math]u[/math] to all the other nodes in the manner described below.

At the start of its execution, the algorithm sets [math]d(u)=0[/math] as the distance to the source-node and [math]d(v) = \infty, \forall v \neq u [/math], as the distances to the other nodes. Also, at the start, the algorithm initializes the set [math]F = \{u\}[/math].

Then, at each step, the algorithm forms the set of nodes [math]P = {w} [/math] such that [math]\forall v \in F \exists (v, w) \in E | d(w) = \infty [/math]. In addition, the algorithm updates the distances [math]d(w)=d(v)+1[/math], [math]\forall w \in P [/math]. After this, the algorithm goes to the next step until [math]P \neq \emptyset[/math]. At the beginning of each step, the set F is replaced by P.

1.3 Computational kernel of the algorithm

The computational kernel of the algorithm is the traversal of nodes adjacent to a single-out node [math]v[/math] with the subsequent addition of not yet visited nodes to the set [math]P[/math]. This operation is performed at each step for each node [math]v \in F[/math].

1.4 Macro structure of the algorithm

The algorithm successively refines the values of the function [math]d(v)[/math].

The structure of the algorithm can be described as follows:

1. Inizialization: all the nodes are assigned the tentative distance [math]d(v)=\infty[/math]; the only exception is the source node for which [math]d(u)=0[/math] .

2. The source node [math]v[/math] is placed to the set of "advanced" nodes [math]F[/math].

3. A traversal of the nodes of the set [math]F[/math].

(a) Inizialization of the set [math]P=\emptyset[/math].

(b) For each node [math]v \in F[/math], a traversal of all the neighbor nodes [math]w | \exists (v, w)[/math]. The nodes [math]v[/math] such that [math]d(w)=\infty[/math] are placed to the set [math]P[/math].

(c) The set [math]F[/math] is replaced by [math]P[/math]. If [math]F \neq \emptyset[/math], the algorithm goes to Step 3.

1.5 Implementation scheme of the serial algorithm

The simplest version of the breadth-first search can be implemented in C++ using queues. The following code is composed under the assumption that the graph is stored in the format of an adjacency list; that is, for each node, the indices of the beginning and end of its adjacency list are stored in the array vertices_to_edges_ptrs.

// init distances
for(int i = 0; i < vertices_count; i++)
    _result[i] = MAX_INT;
    
// init queue and first vertex
std::queue<int> vertex_queue;
vertex_queue.push(_source_vertex);
_result[_source_vertex] = 1;
    
// do bfs
while(vertex_queue.size() > 0)
{
    int cur_vertex = vertex_queue.front();
    vertex_queue.pop();
        
    long long first = vertices_to_edges_ptrs[cur_vertex];
    long long last = vertices_to_edges_ptrs[cur_vertex + 1];
    for(long long i = first; i < last; i++)
    {
        int dst_id = dst_ids[i];
        if(_result[dst_id] == MAX_INT)
        {
            _result[dst_id] = _result[src_id] + 1;
            vertex_queue.push(dst_id);
        }
    }
}

1.6 Serial complexity of the algorithm

The serial complexity of the algorithm is [math]O(|V| + |E|)[/math], where [math]|V|[/math] and [math]|E|[/math] are the number of nodes and the number of arcs, respectively: the algorithm initializes the array of distances, which costs [math]O(|V|)[/math] operations, and then traverses all the nodes with a single visit to each node, which costs [math]O(|E|)[/math] operations. This estimate is valid if the scheme for storing graphs allows one to traverse the nodes adjacent to a chosen node. (This is true, for instance, of adjacent lists and condensed adjacent lists.) For other formats, the estimate for complexity may be greater.

1.7 Information graph

The information graph of the classical breadth-first search is shown in figure 1.

thumb|center|500px|Figure 1. Information graph of the BFS algorithm.

На рисунке 1 используются следующие обозначения:

[1] - добавление вершины-источника [math]u[/math] к множеству [math]F[/math].

[2] - извлечение добавленной вершины [math]v[/math] из множества [math]F[/math].

[3] - проверка расстояний до вершин, смежных с вершиной [math]v[/math].

[4] - добавление еще не посещенных вершин в множество [math]P[/math].

[5] - замена множества [math]F[/math] на [math]P[/math] и проверка его пустоты. В случае негустого множества - переход на следующую итерацию, иначе завершение работы алгоритма.

Данный алгоритм имеет один важный недостаток при реализации: операция [4] требует бесконфликтной возможности добавления элементов в множество P, что, на практике, всегда будет сводиться к сериализации обращений к структуре данных, моделирующих данное множество.

В результате часто используется модификация алгоритма (далее алгоритм-2), использующая набор независимых структур данных для каждого из параллельных процессов. Информационный граф данного подхода приведен на рисунке 2.

thumb|center|500px|Рисунок 2. Информационный граф алгоритма BFS (независимые структуры данных).

Обозначения для рисунка 2:

[1] - добавление вершины-источника в множество [math]F[/math].

[2] - разделение данных множества [math]F[/math] между процессами

[3] - помещение в множества [math]F_i[/math] соответствующих данных из [math]F[/math] каждым процессом с номером i.

[4] - извлечение очередной вершины из множеств [math]F_i[/math], обход её соседей и добавление их в множество [math]P_i[/math] в случае, если они еще не посещены

[5] - попарное слияние множеств [math]P_i[/math] для различных процессов, итоговое преобразование их в множество [math]F[/math].

[6] - проверка условия выхода из цикла

Кроме того, в случае, если реализация структур данных, моделирующих множества [math]F[/math] и [math]P[/math] невозможна, может использоваться квадратичный по сложности алгоритм, схожий с алгоритм Беллмана-Форда. Основная идея заключается в том, что на каждом шаге производится обход всех ребер графа с обновлением текущего массива дистанций. Информационный граф данной модификации алгоритма приведен на рисунке 3.

thumb|center|500px|Рисунок 3. Информационный граф алгоритма BFS (квадратичный подход к распараллеливанию).

Обозначения для рисунка 3:

[1] - инициализация расстояний до вершины-источника

[2] - инициалищация расстояний до остальных вершин графа

[3] - загрузка информации об очередном ребре и обновления дистанций до соответствующих вершин.

[4] - проверка условий выхода из цикла

1.8 Parallelization resource of the algorithm

В ходе работы классический вариант алгоритма обходит граф по слоям. В каждый слой добавляются еще не посещенные вершины, достижимые из вершин предыдущего слоя. Обход вершин каждого слоя, как и их соседей, может производиться параллельно. Точно оценить число вершин в каждом слое невозможно в силу того, что их количество зависит от структуры связанности входного графа. Аналогично невозможно оценить число шагов алгоритма, за которое будут найдены все кратчайшие пути.

Произведем оценку ширины ярусно-параллельной формы алгоритма через максимальное число вершин p в слое среди всех шагов алгоритма. Тогда число параллельных операций на данном слое будет равно сумме числа смежных вершин для каждой вершины слоя: [math]\sum_{n=1}^{p} degree(v_i)[/math], при том для каждого слоя данное значение будет различным. Высота ярусно-параллельной формы будет равна числу шагов в алгоритме, и может быть оценена только сверху (не более [math]|V|[/math]).

При квадратичном подходе к параллельной реализации алгоритма на каждом шаге алгоритма выполняется [math]O(|E|)[/math] операций, которые могут быть выполнены параллельно; таким образом, ширина ЯПФ данной модификации алгоритма равна [math]O(|E|)[/math]. Число шагов алгоритма как и в классическом случае зависит от структуры графа, и может быть оценено сверху как [math]O(|V|)[/math].

1.9 Input and output data of the algorithm

Входные данные: граф [math]G(V, E)[/math], [math]|V|[/math] вершин [math]v_i[/math] и [math]|E|[/math] рёбер [math]e_j = (v^{(1)}_{j}, v^{(2)}_{j})[/math], вершина-источник [math]u[/math].

Объём входных данных: [math]O(|V| + |E|)[/math].

Выходные данные (возможные варианты):

  1. для каждой вершины [math]v[/math] исходного графа расстояние [math]d(v)[/math], определенное как число ребер, лежащих на кратчайшем пути от вершины [math]u[/math] к [math]v[/math].
  2. для каждой вершины [math]v[/math] исходного графа значение достижимости (достижима или нет) от вершины-источника [math]u[/math].

Объём выходных данных: [math]O(|V|)[/math].

1.10 Properties of the algorithm

2 Software implementation of the algorithm

2.1 Implementation peculiarities of the serial algorithm

2.2 Locality of data and computations

2.2.1 Locality of implementation

2.2.1.1 Structure of memory access and a qualitative estimation of locality
2.2.1.2 Quantitative estimation of locality

2.3 Possible methods and considerations for parallel implementation of the algorithm

2.4 Scalability of the algorithm and its implementations

2.4.1 Scalability of the algorithm

2.4.2 Scalability of of the algorithm implementation

2.5 Dynamic characteristics and efficiency of the algorithm implementation

2.6 Conclusions for different classes of computer architecture

2.7 Existing implementations of the algorithm

3 References

  1. Moore, Edward F. “The Shortest Path Through a Maze,” International Symposium on the Theory of Switching, 285–92, 1959.
  2. Lee, C Y. “An Algorithm for Path Connections and Its Applications.” IEEE Transactions on Electronic Computers 10, no. 3 (September 1961): 346–65. doi:10.1109/TEC.1961.5219222.