Search in sources :

Example 1 with DirectedDFS

use of com.jimmysun.algorithms.chapter4_2.DirectedDFS in project AlgorithmsSolutions by Allenskoo856.

the class NFA method recognizes.

public boolean recognizes(String txt) {
    Bag<Integer> pc = new Bag<Integer>();
    DirectedDFS dfs = new DirectedDFS(G, 0);
    for (int v = 0; v < G.V(); v++) {
        if (dfs.marked(v)) {
            pc.add(v);
        }
    }
    for (int i = 0; i < txt.length(); i++) {
        Bag<Integer> match = new Bag<Integer>();
        for (int v : pc) {
            if (v < M) {
                if (re[v] == txt.charAt(i) || re[v] == '.') {
                    match.add(v + 1);
                }
            }
        }
        pc = new Bag<Integer>();
        dfs = new DirectedDFS(G, match);
        for (int v = 0; v < G.V(); v++) {
            if (dfs.marked(v)) {
                pc.add(v);
            }
        }
    }
    for (int v : pc) {
        if (v == M) {
            return true;
        }
    }
    return false;
}
Also used : Bag(com.jimmysun.algorithms.chapter1_3.Bag) DirectedDFS(com.jimmysun.algorithms.chapter4_2.DirectedDFS)

Example 2 with DirectedDFS

use of com.jimmysun.algorithms.chapter4_2.DirectedDFS in project algorithms-sedgewick-wayne by reneargento.

the class RegularExpressionMatcher method recognizes.

public boolean recognizes(String text) {
    Bag<Integer> allPossibleStates = new Bag<>();
    DirectedDFS directedDFS = new DirectedDFS(digraph, 0);
    for (int vertex = 0; vertex < digraph.vertices(); vertex++) {
        if (directedDFS.marked(vertex)) {
            allPossibleStates.add(vertex);
        }
    }
    for (int i = 0; i < text.length(); i++) {
        // Compute possible NFA states for text[i + 1]
        Bag<Integer> states = new Bag<>();
        for (int vertex : allPossibleStates) {
            if (vertex < numberOfStates) {
                if (regularExpression[vertex] == text.charAt(i) || regularExpression[vertex] == '.') {
                    states.add(vertex + 1);
                }
            }
        }
        allPossibleStates = new Bag<>();
        directedDFS = new DirectedDFS(digraph, states);
        for (int vertex = 0; vertex < digraph.vertices(); vertex++) {
            if (directedDFS.marked(vertex)) {
                allPossibleStates.add(vertex);
            }
        }
    }
    for (int vertex : allPossibleStates) {
        if (vertex == numberOfStates) {
            return true;
        }
    }
    return false;
}
Also used : Bag(chapter1.section3.Bag) DirectedDFS(chapter4.section2.DirectedDFS)

Aggregations

Bag (chapter1.section3.Bag)1 DirectedDFS (chapter4.section2.DirectedDFS)1 Bag (com.jimmysun.algorithms.chapter1_3.Bag)1 DirectedDFS (com.jimmysun.algorithms.chapter4_2.DirectedDFS)1