aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/itc/alg/misc/bfs/BFS.java
blob: 22ce89624d8409cd8e809f790a9e574769ed46b1 (plain) (blame)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*******************************************************************************
 * Copyright (c) 2010-2012, Tamas Szabo, Istvan Rath and Daniel Varro
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-v20.html.
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/

package tools.refinery.viatra.runtime.rete.itc.alg.misc.bfs;

import tools.refinery.viatra.runtime.rete.itc.igraph.IBiDirectionalGraphDataSource;
import tools.refinery.viatra.runtime.rete.itc.igraph.IGraphDataSource;

import java.util.*;

public class BFS<V> {

    private BFS() {/*Utility class constructor*/}

    /**
     * Performs a breadth first search on the given graph to determine whether source is reachable from target.
     *
     * @param <V>
     *            the type parameter of the nodes in the graph
     * @param source
     *            the source node
     * @param target
     *            the target node
     * @param graph
     *            the graph data source
     * @return true if source is reachable from target, false otherwise
     */
    public static <V> boolean isReachable(V source, V target, IGraphDataSource<V> graph) {
        Deque<V> nodeQueue = new ArrayDeque<V>();
        Set<V> visited = new HashSet<V>();

        nodeQueue.add(source);
        visited.add(source);

        boolean ret = _isReachable(target, graph, nodeQueue, visited);
        return ret;
    }

    private static <V> boolean _isReachable(V target, IGraphDataSource<V> graph, Deque<V> nodeQueue, Set<V> visited) {

        while (!nodeQueue.isEmpty()) {
            V node = nodeQueue.removeFirst();
            for (V t : graph.getTargetNodes(node).distinctValues()){
                if (t.equals(target)) {
                    return true;
                }
                if (!visited.contains(t)) {
                    visited.add(t);
                    nodeQueue.addLast(t);
                }
            }
        }
        return false;
    }

    public static <V> Set<V> reachableSources(IBiDirectionalGraphDataSource<V> graph, V target) {
        Set<V> retSet = new HashSet<V>();
        retSet.add(target);
        Deque<V> nodeQueue = new ArrayDeque<V>();
        nodeQueue.add(target);

        _reachableSources(graph, nodeQueue, retSet);

        return retSet;
    }

    private static <V> void _reachableSources(IBiDirectionalGraphDataSource<V> graph, Deque<V> nodeQueue,
            Set<V> retSet) {
        while (!nodeQueue.isEmpty()) {
            V node = nodeQueue.removeFirst();
            for (V _node : graph.getSourceNodes(node).distinctValues()) {
                if (!retSet.contains(_node)) {
                    retSet.add(_node);
                    nodeQueue.addLast(_node);
                }
            }
        }
    }

    public static <V> Set<V> reachableTargets(IGraphDataSource<V> graph, V source) {
        Set<V> retSet = new HashSet<V>();
        retSet.add(source);
        Deque<V> nodeQueue = new ArrayDeque<V>();
        nodeQueue.add(source);

        _reachableTargets(graph, nodeQueue, retSet);

        return retSet;
    }

    private static <V> void _reachableTargets(IGraphDataSource<V> graph, Deque<V> nodeQueue, Set<V> retSet) {
        while (!nodeQueue.isEmpty()) {
            V node = nodeQueue.removeFirst();

            for (V _node : graph.getTargetNodes(node).distinctValues()) {

                if (!retSet.contains(_node)) {
                    retSet.add(_node);
                    nodeQueue.addLast(_node);
                }
            }
        }
    }

    /**
     * Performs a breadth first search on the given graph and collects all the nodes along the path from source to
     * target if such path exists.
     *
     * @param <V>
     *            the type parameter of the nodes in the graph
     * @param source
     *            the source node
     * @param target
     *            the target node
     * @param graph
     *            the graph data source
     * @return the set of nodes along the path
     */
    public static <V> Set<V> collectNodesAlongPath(V source, V target, IGraphDataSource<V> graph) {
        Set<V> path = new HashSet<V>();
        _collectNodesAlongPath(source, target, graph, path);
        return path;
    }

    private static <V> boolean _collectNodesAlongPath(V node, V target, IGraphDataSource<V> graph, Set<V> path) {

        boolean res = false;

        // end recursion
        if (node.equals(target)) {
            path.add(node);
            return true;
        } else {
            for (V _nodeT : graph.getTargetNodes(node).distinctValues()) {
                res = (_collectNodesAlongPath(_nodeT, target, graph, path)) || res;
            }
            if (res)
                path.add(node);
            return res;
        }
    }
}