# Warshall's algorithm -- survey and applications

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

## Authors

• 1 publication
• ### Word sense disambiguation: a survey

In this paper, we made a survey on Word Sense Disambiguation (WSD). Near...
08/06/2015 ∙ by Alok Ranjan Pal, et al. ∙ 0

• ### A Survey of Selected Algorithms Used in Military Applications from the Viewpoints of Dataflow and GaAs

This is a short survey of ten algorithms that are often used for militar...
09/30/2021 ∙ by Ilir Capuni, et al. ∙ 0

• ### Wi-Fi Sensing: Applications and Challenges

Wi-Fi technology has strong potentials in indoor and outdoor sensing app...
01/02/2019 ∙ by A. M. Khalili, et al. ∙ 0

• ### A Comprehensive Survey on the Multiple Travelling Salesman Problem: Applications, Approaches and Taxonomy

The Multiple Travelling Salesman Problem (MTSP) is among the most intere...
02/25/2021 ∙ by Omar Cheikhrouhou, et al. ∙ 0

• ### Relative Worst-Order Analysis: A Survey

Relative worst-order analysis is a technique for assessing the relative ...
02/20/2018 ∙ by Joan Boyar, et al. ∙ 0

• ### Small Survey Event Detection

A small survey on event detection using Twitter. This work first defines...
11/09/2020 ∙ by Debanjan Datta, et al. ∙ 2

• ### Dragonfly Algorithm and its Applications in Applied Science – Survey

One of the most recently developed heuristic optimization algorithms is ...
11/25/2019 ∙ by Chnoor M. Rahman, et al. ∙ 0

##### 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

 A=(aij)i=¯¯¯¯¯¯1,nj=¯¯¯¯¯¯1,n, where  aij={1if siRsj0otherwise.

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

 A∗=A+A2+⋯+An.

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.

## 2 Generalization of Warshall’s algorithm

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

 wij←wij+wikwkj

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.

 d0ij=⎧⎪⎨⎪⎩0if i=j∞if there is no edge from vertex vi to vertex vjwijthe weight of the edge from vi to vj

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

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).

### 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:

 Aij={{vivj},if there exist an arc% from vi to vj,∅,otherwise, for i,j=1,2,…,n,

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:

 AB={ab∣∣a∈A,b∈B, if the % strings a and b 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 .

### 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.

The adjacency matrix of the graph is defined by:

 aij={1,if j−i∈M,0,otherwise, for i=1,2,…,n,j=1,2,…,n.

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

:

 R=I+A+A2+⋯+Ak, where Ak+1=O(the null matrix).

The -complexity of a rainbow word is then

 K(n,M)=n∑i=1n∑j=1rij.

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:

 A=⎛⎜ ⎜ ⎜ ⎜ ⎜ ⎜ ⎜ ⎜⎝001111000111000011000001000000000000⎞⎟ ⎟ ⎟ ⎟ ⎟ ⎟ ⎟ ⎟⎠

After applying the Warshall-Path algorithm:

 W=⎛⎜ ⎜ ⎜ ⎜ ⎜ ⎜ ⎜ ⎜⎝001123000112000011000001000000000000⎞⎟ ⎟ ⎟ ⎟ ⎟ ⎟ ⎟ ⎟⎠,R=⎛⎜ ⎜ ⎜ ⎜ ⎜ ⎜ ⎜ ⎜⎝101123010112001011000101000010000001⎞⎟ ⎟ ⎟ ⎟ ⎟ ⎟ ⎟ ⎟⎠

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:

 Aij={{aiaj},if j−i∈M,∅,otherwise, for i=1,2,…,n,j=1,2,…,n.

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.:

 wpq={a∈Σ∣∃k≥1,δ(p,ak)=q}.

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

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

Matrices for graph in Fig. 10 are the following:

For example , , , for .