An Efficient Algorithm for Enumerating all Minimal Paths of a Graph

The enumeration of all minimal paths between a terminal pair of a given graph is widely used in a lot of applications such as network reliability assessment. In this paper, we present a new and efficient algorithm to generate all minimal paths in a graph G(V, E). The algorithm proposed builds the set of minimal paths gradually, starting from the source node s. We present two versions of our algorithm; the first version determines all feasible paths between a pair of terminals in a directed graph without cycle, and this version runs in linear time O(|V | + |E|). The second version determines all minimal paths in a general graph (directed and undirected graph). In order to show the process and the effectiveness of our method, an illustrative example is presented for each case. Keywords—Minimal path; network reliability; linked path structure; recursive algorithm

The use of MPs and MCs for reliability assessment is well documented in [15], [16], and for details on the use of MPs and MCs in reliability evaluation, we refer to these papers.In this paper, we especially focused on determination of all minimal paths in graph.In the literature, there exists several algorithms related to minimal paths' problem [1], [5], [10], [17], [18], [19], [20], [21], [9].
In [17], Al-Ghanim presents, to generate all minimal paths, an algorithm based on heuristic programming.The algorithm proposed produces redundant paths, and to remove them, the author uses an extensive comparison.Al-Ghanim's algorithm has been improved by Yeh [18] through eliminating the possibility of generating duplicate MPs.The last two approaches, [17] and [18], and others like [22], [19], belong to the category of search algorithms based on augmentation.The general principle of these approaches consists of adding arc by arc, starting from the source until completing the target network.After each increase, the MPs thus constructed are collected.
Another family of algorithms for MPs enumeration is called, according to Chen [21], direct search-based algorithms [21], [23], [24], [25], [20], [10].These methods are based on depth-first search (DFS).In [10], Shen introduced an algorithm to enumerate all minimal paths between specified single terminal pair of arbitrary graphs.The proposed algorithm is based on elementary concept of graph theory and dual principle.To improve Shen's algorithm, Kobayashi [20] adds some additional processes based on the level set of nodes.In [21], Chen uses a backtracking process to generate all MPs.Backtracking is a family of algorithms that consists of going back on decisions made to get out of a deadlock.This process, which is a characteristic of the descriptive software languages, makes it possible to abandon each partial candidate which cannot lead to a valid solution.Bai further improved Chen's algorithm by adding conditions for backtracking to reduce the number of search branches [9].To the author's Knowledge, currently, Bai, Tian, and Zuo's algorithm [9] is the best known DFS algorithm.
In this work, we present a new method to enumerate all minimal paths in an oriented graph with no cycles.We also give a more general algorithm which determines all the minimal paths of a general graph.This paper is organized as follows: In the next section we present the basic definitions and terminology.In Section 3, we introduce a new algorithm to find all minimal paths in an oriented graph with no cycle.For this, we will, first, introduce a directed graph reduction algorithm to eliminate nodes that cannot appear in the set of minimal paths.In Section 4, we introduce the enumeration algorithm of all minimal paths in a general graph (oriented or not).For that, we will, first, introduce an algorithm for the reduction of undirected graphs allowing the elimination of the nodes which cannot appear in any path of the set of minimal paths.In Section 5, we provide an analysis of the complexity of our algorithms.We also present a comparative study of our method with the recent method developed in 2016 by Bai [9].In Section 6, we present all the tests we made and the results obtained.We also compare our work to recent works.An extension to the multi-terminal case is presented in Section 7. Finally, we will conclude with some suggestions for future research in the field of minimal paths' enumeration.

A. Graph Representation
There are two classical ways to represent a garph: an adjacency matrix, or a set of adjacency lists.The choice of the type of representation depends on the operations performed on this structure: • for the representation by the adjacency matrix, the verification of the existence of an arc between two given vertices is in O(1), whereas the search for the neighbors of a given vertex is in O(n).
• for the representation by the adjacency list, the verification of the existence of an arc between two given vertices is in O(n), whereas the search for the neighbors of a given vertex is in O(1).
In our case, we have opted for the adjacency list representation because the most common operation is the traversal of the list of neighbors.The graph (a) of Fig. 1 and its oriented representation (b) will be represented by the following list: L ={(1, 2, 3), (0, 2, 3, 4, 5), (0, 1, 5), (0, 1), (1,5,6), (1,2,4,6), (4,5) In this paper, we opted for using the oriented representation of the graphs.Thus, in the case of an undirected graph, it is necessary to convert the non-oriented edges into oriented edges.For this, we used the traditional Hagstrom technique that involves replacing each undirected edge by two directed edges with the opposite direction [26].

B. Notations
Let G= (V, E) be a graph with n = |V | vertices and m = |E| edges.One refers to source vertex by s and to sink vertex by t.For a vertex v∈V, we denote by Γ(v), the set of all vertices in V that are adjacent to v.In the case of directed graph, for any vertex v∈V, the incoming neighborhood (also called predecessors) of v is defined as = examined } all the outgoing neighbors of which all the incoming neighborhood are already processed.One refers to the set of all incoming neighbors who are already processed by Γ − * and by Γ +nt the set of outgoing neighbors who are not yet processed.G/u is the vertex-induced sub graph G(V \{v}, E ) for v∈V where E'={(p,q)∈E / p =v and q =v}.Likewise, for edge e∈E, G/e= (V, E\{e}) is the edge-induced sub graph.We refer to a path π=(a l →a 2 →a 3 ... →a k ) by its natural sequence of vertices (a l ,a 2 , a 3 , ... , a k ) such that any two consecutive vertices x i and x i + 1 are connected by an arc of G: ∀i , 0 ≤ i ≤ n − 1 , (x i , x i+1 ) ∈ E. A path π from s to t is denoted by st-path.We noted by P st the set of all st-minimal paths in G.A node or edge is called invalid if and only if it cannot appear in any path in the P st set.An active node is defined as any processed node having at least one of its outgoing neighbors not yet processed.
1) Delete Edges from a Graph: At the beginning, we remove all incoming arcs to the source s, {(x, s) ∈ E}, because the starting node is s and the searched paths are minimal paths, so no arc in the set {(x, s) ∈ E} cannot be appeared in the paths of P s,t (a path cannot contain the node s twice).The same applies to the arcs {(t, y) ∈ E} because the destination node is t.
2) Delete Vertices from a Graph: All nodes in the set V \ {s, t} cannot be starting nodes, because all paths in the set P st start from s.Similarly, these nodes cannot be destination nodes.From this remark, all the nodes of V \ {s, t} must have the following two characteristics: {x ∈ V \{s}/ card(Γ − (x)) = 0} and {x ∈ V \{t}/ card(Γ + (y)) = 0}.Consequently the nodes of V\{s, t} whose {x ∈ V \ {s} / card (Γ − (x)) = 0} or {x ∈ V \ {t} / card (Γ + (x )) = 0} cannot be intermediate nodes between s and t and should be deleted.
Note: When a node is deleted, the arcs linking this node to the graph will also be deleted.So we have G\v = G(V \{v}, E ) where E = {(p, q) ∈ E / p = v and q = v}.
After each deletion of a node, the nodes' validity testing process will be iterated on these predecessors in the case of the nodes' checking {x ∈ V \ {t} / card (Γ + (y)) = 0}, and on the successors for the nodes checking {x ∈ V \ {s} / card (Γ − (x)) = 0}.
Algorithm 1 Oriented graph reduction(G, s, t) x ← element f rom T o delete

Delete edges
LsOC 1,2 and 3: the arc (0,3) will be removed.LsOC 4,5 and 6: the arcs (16,14) and (16,18) will be removed.Delete vertices LsOC 7, 8, 9, 10 and 11: these LsOC initialize the stack of vertices to be deleted by all vertices that have card(Γ − ) = 0, in our case by 0, 18. LsOC from 12 to 21: this loop treats the nodes of the stack of vertices to delete as follows: • a node is retired from the stack; it is noted by x.
• delete vertices x from a graph.
Table I below gives the various iterations of the loop.A line represents one iteration of the loop.Column 1 shows the contents of the 'T o delete' stack at the beginning of the loop.Column 2 shows the element removed from the stack.Column 3 shows the nodes to be added to the stack of nodes to be removed.The content of the stack after the addition of these nodes is given in column 4. Column 5 shows the accumulation of the deleted nodes.Deleting a node implies deleting all the arcs connect to that node.Column 6 shows the accumulation of deleted arcs.
LsOC from 22 to 26: these LsOC initializes the stack of vertices to delete by all vertices that have card(Γ + (x)) = 0, in our case by {20}.
LsOC 27 to 36: these LsOC treats the nodes of the stack of vertices to delete.Table II below presents the various iterations of the loop.
The result of the reduction process is shown in Figure 1.e.

B. The Enumeration of all Minimal Paths in Directed Graph without Cycle
1) The principle of the algorithm: Starting from the fact that the minimal paths linking a source node s to another node x can be obtained from the lists of minimal paths linking s to the predecessors of x by applying a simple increase of the paths eq. 1, the main idea of our method is to build, little by little, all minimal paths P st .The algorithm starts with an initialization of P ss by the set {(s)}.Afterwards, the algorithm constructs all the possible paths of the outgoing neighbors of s who's all the predecessors are already processed.Let's note by Γ + * all the outgoing neighbors of which all the predecessors are already processed.The process is thus repeated passing each time to the outgoing neighbors whose predecessors are already processed until the processing of node t.
The fact that the size of the graph is reduced from one iteration to another, implies the termination of the algorithm.
To optimize the algorithm in terms of memory space, we proceed as follows: let x be the current node (LOC 21: the element extracted from T o T reat) and let y ∈ Γ − * (x).At the level of LsOC 22 to 26, if the current node x is the last node of the set Γ + (y) which is not yet processed, so we move all the minimal paths of P sy , after having increased them, to P sx .This decision is made because all these successors are already processed, so P sy will never be used in the process of building paths for nodes that have not yet been processed.

C. Illustration on an Example
Considering the reduced graph in Fig. 1(e) where the source node s=3 and sink node t=16.
Firstly, we initialize the set P ss by {(s)}, in our case P 33 = {(3)} and the list of vertices to treats by all elements of Γ + * (s), in our case {1, 7}.Table III presents the various iterations of the loop (iterations from 20 to 32); • Column 1 shows the contents of the list T o T reat at the beginning of the loop.
• Column 2 shows the the node that is retrieved from T o T reat (LOC 21).
Algorithm 2 M P s DirectedGraph(G, s, t) x ← pop(T o T reat);

IV. MINIMAL PATHS ALGORITHM FOR GENERAL GRAPHS
The algorithm presented in the previous section cannot be applied to graphs containing cycles, which also implies nonoriented graphs.This is due to the fact that the condition that a node must satisfy to be added to the list of nodes to be processed (the nodes whose predecessors are already processed) is not necessarily verified at each iteration.To overcome this problem, we propose to keep the same principle in its general philosophy; that is, the construction of the set of minimal paths between two given nodes s and t is done starting from nodes s, then these neighbors, then the neighbors of the neighbors, until the processing of the final node t.During the process of treatment, we pass from a node to its successors even that all their predecessors are not yet processed.
The problem: knowing that the construction of the set of paths P sx for a given node x, is made from the sets of paths of these predecessors, when creating P sx for a given node x ∈ V \{s, t}, it is possible that there are nodes in Γ − (x) that are not yet processed; therefore, P sx will not be complete (it does not contain all minimal paths from s to x).Suggestion: to complete the set of minimal paths of a node x, we propose to update it as soon as we process a node that belongs to Γ − (x).The details of update procedure are given in the next section.It is a backtracking to update the already processed nodes.
To optimize the memory space, we propose to adopt the principle used in the algorithm introduced in the previous section; if the current node x is the last node of the set Γ + (y) which is not yet processed, then we move all the minimal paths of P sy , after increasing them, to P sx , but for a given node x, if all its successors are already processed, the set P sx will never be displaced from this node to one of these successors (because according to the hypothesis, they are already processed).To illustrate this, consider the graph given in Fig. 1(c), if the order of treatment of the nodes is 1, 2, 3, 4, 5 then 6, then the set of minimal paths P S3 cannot be moved from P S3 to P S1 because node 1 is processed before 3.This implies a problem of optimization of the memory space.To solve this problem, at each iteration we will select from the set T o T reat (LOC 21) the node that has the largest distance to t.The algorithm we used for the determination of distances to the sink is given in subsection 4.B.

A. Update Process
At the moment of processing a given node x, from LOC 22 to LOC 26, it is possible that there exist among its all the minimal paths of a node x as soon as a node of its predecessors has just been processed or updated.In the worst case, the case where the graph is strongly connected, the overall number of execution of the update process is equal to 1+2+...+(|V |−2) = (|V |−3)(|V |−2)/2 (no update after the first iteration, only one update after the second iteration, ..., (|V | − 2) update after the iteration (V − 1)).
The nodes of the sub-graph already processed will be updated from the list of the current node paths using a list of suffixes representing the different possibilities of reaching these nodes from current node.These suffixes will be used in the phase of increasing paths.To determine these suffixes, we will explore the sub-graph already processed starting from the current node.In the beginning, the suffix is initialized by null.Before the update process is executed on a given node x, the suffix is first incremented by x.
In order to optimize the update algorithm, we consider the following assertions: 1) The updates of the successors of a given node x will be made only based on the new paths added to P SX .2) During the update process (the exploration of subgraph already processed), we do not pass from a node, denoted by x, to these successors only if P SX was powered by new paths.3) In the update process, if all the successors of a node to update are already processed, it is obvious that the update of this node is useless, since it will not contribute any more in the process of determination of the P ST because all their successors are already processed.It is also obvious that the exploration of the already processed sub-graph must continue in order to explore all the active nodes; The purpose of the update process is to update the active nodes only.

Proof
Assertion 1: let c be the processed node, x a successor of c, and y a successor of x, where x ∈ Γ − * (c) and y ∈ Γ − * (x).Node x will be updated from P SC paths which do not pass through x.The y node will be updated from P SC paths which do not pass through x and y, i.e. the paths that have already been added to P SX and do not pass through y.
Assertion 2: if the node x has not been fed by other paths, forcing these successors will not be powered too (result of assertion 1).It is therefore useless to continue the update process.
In the last assertion, the finalized nodes will not be updated, which will allow us to optimize the memory space, but in this case, the exploration of sub-graph already processed will be maximum, that on is the one hand.On the other hand, the updating of the active nodes, in most cases, will be done directly from the list of current node paths (the node from which we started the exploration of the already processed sub-graph), and this is very expensive.To avoid useless explorations, during the process of updating the list of y-node paths from the list of x-node paths, if the node y is active, then we update P SY from paths of P SX .Otherwise, we do a simple swap of the paths of P SX while moving the paths that can be added to P SY (P SX paths which do not pass through the node y) at the beginning of P SX .In the latter case, the updating process is continued if at least one permutation operation takes place.For more details, see update procedure given in Algorithm 4.
Algorithm 4 update(x, y, n, suf f ix, T M A)
• node 5: since Γ +nt (5) = {6}, P S5 will be moved after increase to P S9 .P S9 becomes {(0,  where λ indicates the average number of links for each minimal path, and π denotes the total number of minimal paths.This complexity is the result of the exhaustive traversal of all possible branches starting from the source node.This is the case in [21] where the author has used a exhaustive traversal and principle of backtracking which consists of going back as soon as a cycle is detected or when there are no more outgoing neighborhoods.In [9], the same principle is used with the addition of a condition on backtracking.This condition has made it possible to reduce the number of cycles visited, which implies the reduction of number of tests to O(λ * π+C).In our algorithm, no test is performed when building the P SX sets from the predecessors.The tests are performed at the update level.In this case, a large number of paths will be built without doing any test.Let π be the number of paths built at the update level.Consequently, the time complexity of our algorithm can be written The storage complexity of G. Bai et al's algorithm is less than ours.But in order to compare our algorithm to that of Bai, and not to count the execution time necessary for output immediately each path found (screen display or storage in a file or others), when an minimal path is found, it will be kept in memory until the end of the execution.This makes the memory space necessary for the execution of the Bai algorithm O(3(|V | − 1) + λ * π).

A. Benchmarks
To test our algorithm, we have used a set of networks taken in the literature.The network presented in Fig. 2 is taken from [1].A classical grid network, used in [9], [21], is shown in Fig. 3.We have implemented and tested our algorithm using C language.All tests were performed on a personal computer equipped with CPU being an Intel Core i3 1.7 GHz, and with 4Gb RAM.
In order to compare our method to that of Bai which allows to enumerate all the minimal paths in the general graphs, we used our second algorithm (Algorithm 3) in all the tests we performed.

B. Comparison with G. Bai et al's Algorithm
In order to show the effectiveness of our algorithm, we compared it with that of G. Bai [9].For that, we are interested in the required execution time with respect to different networks.The first test presented in this study is done using the benchmark network given by Luo and Trivedi [27], as shown in Fig. 2. Using the proposed algorithm, the number of minimal paths found is 780, which agrees the result in [9] and in [21].The execution time for the proposed algorithm and the Bai's algorithm are 0.4320ms and 2.668ms, respectively.The ratio, which is defined as the ratio of the CPU time of the Bai's algorithm to the proposed algorithm, is about 6.1759, indicating that the proposed algorithm is 6.1759 times faster than Bai's algorithm in finding all the minimal paths of the benchmark network.
In the second test, we used the classical grid networks.A typical example of 12 nodes and 17 edges is shown in Fig. 3. On all the networks tested in this experiment, the two algorithms generate the same sets of minimal paths.Fig. 4 shows a comparison of the average CPU times (in milliseconds) of 14 grid networks for the Bai's algorithm, and our second algorithm.In order to illustrate the difference between these two algorithms according to the size of the network, the ratio of the CPU time of the Bai's algorithm over the proposed algorithm is given in Fig. 5.As we can see, as the network size increases, the efficiency of our algorithm increases accordingly.

VII. ENUMERATION OF MPS FOR NETWORKS WITH MULTIPLE SOURCE/SINK NODES
As introduced in [28], a multi-terminals network is said to be operative if there exist operating paths between each pair of node (s, t) such that s belongs to the set of source nodes and t belongs to the set of sink nodes.
The simple way to extend the minimal paths enumeration algorithm introduced previously to the multi-terminals' case is to convert the multi-terminals network into a simple binary network.To do this, we can use the classical technique introduced in [28].The technique is as follows: first, we add two nodes that will play the role of the two terminals in the new network, naming these nodes artificial source and artificial sink.The second step is the addition of artificial direct links from artificial source node to the source nodes.The latest step consists of adding the artificial direct links from sink nodes to the artificial sink node.Fig. 6 shows the benchmark network from [21] and its transformation.This technique is also used by Bai [9].
To test this technique, we used the network shown in Fig. 6.Using the proposed algorithm, the number of minimal paths found is 145, which agrees with the results in [21] and the same number is obtained using Bai's algorithm.The execution time for the proposed algorithm and the Bai's algorithm are 0.052ms and 0.135ms, respectively, and the ratio is about 2.5961.

VIII. CONCLUSION
In this paper, we have proposed a new method that finds all the minimal paths in the graph.We started by presenting a version for graphs without cycles.This version was subsequently extended to the general case (oriented, undirected and mixed graphs).We also presented an algorithm for graph reduction.For the case of graphs with multi-terminals, we adopted, as in [21], [9] and others, the method introduced in [28].
The analysis of the complexity of our algorithm and the comparison with that of Bai's algorithm show that the proposed method herein is very efficient.
Another advantage of our algorithm is the possibility to implement a large part of the algorithm, such as update operations, using parallel programming.This will allow us to further improve the effectiveness of our approach.

Fig. 1 .
Fig. 1.Example of networks indexed by nodes: (a) undirected network and its oriented representation (b).(c) reduction result of the graph b.(d) Oriented network without cycle.(e) reduction result of the graph d.
} Since the displacement number is different from zero (equal to 1) and since the set {x ∈ Γ + (1)/ (State[x] = Examined or State[x] = f inalized) and x ∈ {5, 4, 1}} = {3, 2}, then we will make the following recursive calls: [Update (3, 4, 1, {1,3}, {5,4,1}) and Update (2, 4, 1, {1,2}, {5,4,1})].Call N2.1.1:Update(3, 4, 1, {1,3},{5,4,1}) of Minimal Paths in an Oriented Graph without Cycles (Algorithm 1 and 2) For the graph reduction algorithm (Algorithm 1), the execution time depends on the number of nodes to be deleted; in the worst case O(|V | − 2).The memory space used by the algorithm is in the order of O(2 * (|V | − 1)) with respect to one input list O(V −1), one list of nodes to delete O(|V |−2).For the path enumeration algorithm in an oriented graph without cycles (Algorithm 2), the execution time complexity is in the order O(|V | + |E|); each node is visited once O(|V |).At each node, the search for successors whose predecessors are already processed runs in O(deg(node)), which gives O(|E| in the worst case).Let λ denote the average number of nodes in a MPs, and π denotes the total number of MPs.The memory space required for the execution of the algorithm is O(3(|V | − 1) + λ * π), with respect to the buffer memory used for storing paths O(λ * π), one input list O(|V | − 1), one list used for storing the items to be processed O(|V | − 1), and one list that contains the state of each node O(|V | − 1).B. Enumeration of Minimal Paths in the General Graph (Algorithm 3 and 4) The maximum time complexity, in number of tests, for the minimal paths enumeration algorithms based on BFS/DFS (Breadth First Search / Depth First Search) is O(λ * π + C) as follows: O(λ * π + C) where λ indicates the average number of links for each π.The advantage of our algorithm can be seen as follows, O(λ * π) O(λ * π).The memory space required by the minimal paths enumeration algorithm (Algorithm 3 and 4) O(4|v| + (λ * π) − 6) with respect to one input list O(V − 1 in length), one list used for storing the items to be processed O(|V | − 1 in the worst case), the buffer memory used for storing paths O(λ * π in length), one list of distances to the terminal O(V − 1 in length), and one list used for storing the suffix O(V − 3 in the worst case), where the suffix is an ordered subset of nodes used in the path-augmentation phase at the update level.The storage complexity of G. Bai et al's algorithm [9] is O(4(|V | − 1)), with respect to one input list L(|V | − 1 in length), one path buffer P (|V | in the worst case), one distance list Q(|V | − 1 in the worst case), and one distance checking list S(|V | − 1 in the worst case).The time complexity of its algorithm is O(η * (π + c)), where c denotes the number of cycles in the network who are visited by its algorithm.

Fig. 4 .Fig. 5 .
Fig. 4. Comparison of the CPU time of the Bai's algorithm to the proposed algorithm.

TABLE I .
ILLUSTRATION OF THE GRAPH REDUCTION ALGORITHM (ALGORITHM 1).LINES OF CODE BETWEEN 12 AND 21.

TABLE II .
ILLUSTRATION OF THE GRAPH REDUCTION ALGORITHM (ALGORITHM 1).CODE LINES BETWEEN 27 AND 36.

TABLE III .
ILLUSTRATION OF THE MPS DIRECTEDGRAPH ALGORITHM (ALGORITHM 2).CODE LINES BETWEEN 20 AND 32.