Warshall's algorithm -- survey and applications

05/01/2019 ∙ by Zoltán Kása, et al. ∙ Sapientia University 0

The survey presents the well-known Warshall's algorithm, a generalization and some interesting applications of this.



There are no comments yet.


page 1

page 2

page 3

page 4

This week in AI

Get the week's most popular data science and artificial intelligence research sent straight to your inbox every Saturday.

1 Introduction

Let be a binary relation on the set , we write if is in relation to . The adjacency matrix of the relation is

The transitive closure of the relation is the binary relation defined as:

if and only if there exists , , , such that , , . The adjacency matrix of is .

Let us define the following operations. If then for , and otherwise. for , and otherwise. In this case

The transitive closure of a relation can be computed easily by the Warshall’s algorithm [6], [1]:

Warshall() Input:  the adjacency matrix ; the no. of elements Output: 1 2 for to 3 do for to 4 do for to 5 do if and 6 then 7 return

A binary relation can be represented by a directed graph (i.e. digraph). See Fig. 1 for an example. Fig. 2 represents the graph of the corresponding transitive closure.

Figure 1: A binary relation represented by a graph with the corresponding adjacency matrix
Figure 2: The transitive closure of the relation in Fig. 1

2 Generalization of Warshall’s algorithm

Lines 5 and 6 in the Warshall algorithm described above can be changed in

using the operations defined above. If instead of the operations + and we use two operations and from a semiring, a generalized Warshall’s algorithm results [4]:

Generalized-Warshall() Input:  the adjacency matrix ; the no. of elements Output: 1 2 for to 3 do for to 4 do for to 5 do 6 return

This generalization leads us to a number of interesting applications.

3 Applications

3.1 Distances between vertices. The Floyd-Warshall’s algorithm

Given a weighted (di)graph with the modified adjacency matrix , we can obtain the distance matrix in which represents the distance between vertices and . The distance is the length of the shortest path between the vertices.

Choosing for the operation (minimum between two reals), and for the real , we obtain the well-known Floyd-Warshall’s algorithm as a special case of the generalized Warshall’a algorithm [4, 5] :

Floyd-Warshall() Input:  the adjacency matrix ; the no. of elements Output: the distance matrix 1 2 for to 3 do for to 4 do for to 5 do 6 return

Figure 3: A weighted digraph
Figure 4: The corresponding matrices of the Fig. 3

Figures 3 and 4 contain az example. The shortest paths can be easily obtained if in the description of the algorithm in line 5 we store also the previous vertex on the path. In the case of acyclic digraph, the algorithm can be easily modified to obtain the longest distances between vertices, and consequently the longest paths.

3.2 Number of paths in acyclic digraphs

Here by path we understand directed path. In an acyclic digraph the following algorithm count the number of paths between vertices [3, 2]. The operation are the classical add and multiply operations for real numbers.

Warshall-Path() Input:  the adjacency matrix ; the no. of elements Output: with no. of paths between vertices 1 2 for to 3 do for to 4 do for to 5 do 6 return

An example can be seen in Figures 5 and 6. For example between vertices 1 and 3 there are 3 paths: (1,2,3); (1,2,5,3) and (1,6,5,3).

Figure 5: An acyclic digraph


Figure 6: Matrices of the example in Fig. 5

3.3 All paths in digraphs

The Warshall algorithm combined with the Latin square method can be used to obtain all paths in a (not necessarily acyclic) digraph [3]. A path will be denoted by a string formed by its vertices in there natural order.

Let us consider a matrix with the elements which are set of strings. Initially elements of this matrix are defined as:

If and are sets of strings, will be formed by the set of concatenation of each string from with each string from , if they have no common elements:

If is a string, let us denote by the string obtained from by eliminating the first character: . Let us denote by the set in which we eliminate from each element the first character. In this case is a matrix with elements

Operations are: the set union and set product defined as before.

Starting with the matrix defined as before, the algorithm to obtain all paths is the following:

Warshall-Latin() Input:  the adjacency matrix ; the no. of elements Output: matrix of paths between vertices 1 2 for to 3 do for to 4 do for to 5 do if and 6 then 7 return

In Figures 7 and 8 an example is given. For example between vertices and there are two paths: and .

Figure 7: An example of digraph for all paths problem

Figure 8: Matrices for graph in Fig. 7

3.4 Scattered complexity for rainbow words

The application mentioned here can be found in [3]. Let be an alphabet, the set of all -length words over , the set of all finite word over .

Definition 1

Let and be positive integers, and . An -subword of length of is defined as where


for ,

Definition 2

The number of -subwords of a word for a given set is the scattered subword complexity, simply -complexity.

Examples. The word has 11 -subwords: , , , , , , , , , , . The -subwords of the word are the following: , , , , , , , , , , , , , , , , , , , .

Words with different letters are called rainbow words. The -complexity of a length- rainbow word does not depend on what letters it contains, and is denoted by .

To compute the -complexity of a rainbow word of length we will use graph theoretical results. Let us consider the rainbow word and the corresponding digraph , with



For see Fig. 9.

Figure 9: Graph for -subwords of the rainbow word of length-6

The adjacency matrix of the graph is defined by:

Because the graph has no directed cycles, the element in row and column in (where , with ) will represent the number of length- directed paths from to . If

is the identity matrix (with elements equal to 1 only on the first diagonal, and 0 otherwise), let us define the matrix


The -complexity of a rainbow word is then

Matrix can be better computed using the Warshall-Path algorithm.

From we obtain easily .

For example let us consider the graph in Fig. 9. The corresponding adjacency matrix is:

After applying the Warshall-Path algorithm:

and then the sum of elements in .

Using the Warshall-Latin algorithm we can obtain all nontrivial (with length at least 2) -subwords of a given length- rainbow word . Let us consider a matrix with the elements which are set of strings. Initially this matrix is defined as:

The set of nontrivial -subwords is .

For , the initial matrix is:

The result of the algorithm in this case is:

3.5 Special paths in finite automata

Let us consider a finite automaton , where is a finite set of states, the input alphabet, the transition function, the initial state, the set of finale states. In following we do not need to mark the initial and the finite states.

The transition function can be generalized for words too: , where .

We are interesting in finding for each pair of states the letters for which there exists a natural such that we have the transition [4], i.e.:

Instead of we use here set union () and instead of set intersection ().

Warshall-Automata() Input:  the adjacency matrix ; the no. of elements Output: with sets of states 1 2 for to 3 do for to 4 do for to 5 do 6 return

Figure 10: An example of a finite automaton without indicating the initial and finite states

The transition table of the finite automaton in Fig. 10 is:

Matrices for graph in Fig. 10 are the following:

For example , , , for .