Started implementing Hopcroft algorithm
This commit is contained in:
@@ -66,7 +66,7 @@ public class DFA<T extends Comparable> extends Automata<T> {
|
||||
return new Taal(addSymbols(n - 1, taal));
|
||||
}
|
||||
|
||||
public NDFA<String> Reverse()
|
||||
public NDFA<String> reverse()
|
||||
{
|
||||
NDFA<String> reversed = new NDFA<String>(this.symbols);
|
||||
|
||||
@@ -87,6 +87,6 @@ public class DFA<T extends Comparable> extends Automata<T> {
|
||||
}
|
||||
|
||||
public DFA<String> minimaliseer() {
|
||||
return this.Reverse().toDFA().Reverse().toDFA();
|
||||
return this.reverse().toDFA().reverse().toDFA();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
package com.imegumii;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.SortedSet;
|
||||
import java.util.TreeSet;
|
||||
|
||||
/**
|
||||
* Created by kenny on 30-5-2017.
|
||||
*/
|
||||
public class HopcroftConverter {
|
||||
|
||||
private DFA<String> automata;
|
||||
private SortedSet<HopcroftSet> sets;
|
||||
|
||||
public HopcroftConverter(DFA<String> automata)
|
||||
{
|
||||
this.automata = automata;
|
||||
this.sets = new TreeSet<HopcroftSet>();
|
||||
|
||||
DFAtoHopcroft();
|
||||
updateGroupReferences();
|
||||
}
|
||||
|
||||
public DFA<String> minimize()
|
||||
{
|
||||
while(splitGroups())
|
||||
updateGroupReferences();
|
||||
|
||||
return HopcrofttoDFA();
|
||||
}
|
||||
|
||||
private void DFAtoHopcroft()
|
||||
{
|
||||
String beginGroup = getGroupName();
|
||||
String endGroup = getGroupName();
|
||||
|
||||
for(Transition t : automata.transistions)
|
||||
{
|
||||
boolean found = false;
|
||||
HopcroftSet currentSet = null;
|
||||
|
||||
for(HopcroftSet s : sets) {
|
||||
if (t.vanState == s.state) {
|
||||
found = true;
|
||||
currentSet = s;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(!found) {
|
||||
String groupName = beginGroup;
|
||||
boolean endState = false;
|
||||
if(automata.eindStates.contains(t.vanState)) {
|
||||
groupName = endGroup;
|
||||
endState = true;
|
||||
}
|
||||
currentSet = new HopcroftSet(groupName, (String)t.vanState, endState);
|
||||
}
|
||||
|
||||
if(t.symbol == 'a')
|
||||
currentSet.transitionAState = (String)t.naarState;
|
||||
else if(t.symbol == 'b')
|
||||
currentSet.transitionBState = (String)t.naarState;
|
||||
}
|
||||
}
|
||||
|
||||
private DFA<String> HopcrofttoDFA()
|
||||
{
|
||||
Character [] characters = {'a', 'b'};
|
||||
DFA<String> result = new DFA<String>(characters);
|
||||
|
||||
ArrayList<String> groups = new ArrayList<String>();
|
||||
|
||||
for(HopcroftSet s : sets)
|
||||
{
|
||||
if(!groups.contains(s.group))
|
||||
{
|
||||
result.addTransition(new Transition<String>(s.group, 'a', s.transitionAGroup));
|
||||
result.addTransition(new Transition<String>(s.group, 'b', s.transitionBGroup));
|
||||
|
||||
if(s.isEndState)
|
||||
result.defineAsEndState(s.group);
|
||||
|
||||
groups.add(s.group);
|
||||
}
|
||||
|
||||
if(automata.beginStates.contains(s.state))
|
||||
result.defineAsStartState(s.group);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private void updateGroupReferences()
|
||||
{
|
||||
for(HopcroftSet s : sets)
|
||||
{
|
||||
for(HopcroftSet h : sets)
|
||||
{
|
||||
if(s.transitionAState == h.state)
|
||||
s.transitionAGroup = h.group;
|
||||
|
||||
if(s.transitionBState == h.state)
|
||||
s.transitionBGroup = h.group;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean splitGroups()
|
||||
{
|
||||
int splitCount = 0;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
if(splitCount == 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
private int count = 0;
|
||||
|
||||
private String getGroupName()
|
||||
{
|
||||
count++;
|
||||
return "G" + count;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class HopcroftSet {
|
||||
|
||||
public String group;
|
||||
public String state;
|
||||
|
||||
public String transitionAState;
|
||||
public String transitionAGroup;
|
||||
|
||||
public String transitionBState;
|
||||
public String transitionBGroup;
|
||||
|
||||
public boolean isEndState = false;
|
||||
|
||||
public HopcroftSet(String group, String state, boolean isEndState)
|
||||
{
|
||||
this.group = group;
|
||||
this.state = state;
|
||||
this.isEndState = isEndState;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
package com.imegumii;
|
||||
|
||||
import com.imegumii.codevandocent.*;
|
||||
|
||||
import java.util.TreeSet;
|
||||
|
||||
public class Main {
|
||||
@@ -111,7 +109,7 @@ public class Main {
|
||||
|
||||
RegExp expr7 = expr5.punt(expr6);
|
||||
|
||||
NDFA<String> test = ThompsonConverter.Convert(expr7);
|
||||
NDFA<String> test = ThompsonConverter.convert(expr7);
|
||||
|
||||
System.out.println(Graph.generateGraphString(test));
|
||||
}
|
||||
@@ -135,7 +133,7 @@ public class Main {
|
||||
|
||||
System.out.println("\nREVERSE:");
|
||||
|
||||
NDFA<String> reverseAutomata = myAutomata.Reverse();
|
||||
NDFA<String> reverseAutomata = myAutomata.reverse();
|
||||
|
||||
System.out.println(Graph.generateGraphString(reverseAutomata));
|
||||
}
|
||||
|
||||
@@ -7,24 +7,24 @@ public class ThompsonConverter {
|
||||
|
||||
private static int count = 0;
|
||||
|
||||
public static NDFA<String> Convert(RegExp regex)
|
||||
public static NDFA<String> convert(RegExp regex)
|
||||
{
|
||||
Character [] characters = {'a', 'b'};
|
||||
NDFA generated = new NDFA<String>(characters);
|
||||
|
||||
ConvertStatement(regex, generated);
|
||||
convertStatement(regex, generated);
|
||||
|
||||
return generated;
|
||||
}
|
||||
|
||||
private static void ConvertStatement(RegExp regex, NDFA automata)
|
||||
private static void convertStatement(RegExp regex, NDFA automata)
|
||||
{
|
||||
switch(regex.operator)
|
||||
{
|
||||
case PLUS:
|
||||
|
||||
//Links
|
||||
ConvertStatement(regex.links, automata);
|
||||
convertStatement(regex.links, automata);
|
||||
String endLink1 = (String) automata.eindStates.first();
|
||||
String beginLink1 = (String) automata.beginStates.first();
|
||||
|
||||
@@ -41,7 +41,7 @@ public class ThompsonConverter {
|
||||
case STER:
|
||||
|
||||
//Links
|
||||
ConvertStatement(regex.links, automata);
|
||||
convertStatement(regex.links, automata);
|
||||
String endLink2 = (String) automata.eindStates.first();
|
||||
String beginLink2 = (String) automata.beginStates.first();
|
||||
|
||||
@@ -65,12 +65,12 @@ public class ThompsonConverter {
|
||||
case OF:
|
||||
|
||||
//Links
|
||||
ConvertStatement(regex.links, automata);
|
||||
convertStatement(regex.links, automata);
|
||||
String topEnd = (String) automata.eindStates.first();
|
||||
String topBegin = (String) automata.beginStates.first();
|
||||
|
||||
//Rechts
|
||||
ConvertStatement(regex.rechts, automata);
|
||||
convertStatement(regex.rechts, automata);
|
||||
String bottomBegin = (String) automata.beginStates.first();
|
||||
String bottomEnd = (String) automata.eindStates.first();
|
||||
|
||||
@@ -95,12 +95,12 @@ public class ThompsonConverter {
|
||||
case PUNT:
|
||||
|
||||
//Links
|
||||
ConvertStatement(regex.links, automata);
|
||||
convertStatement(regex.links, automata);
|
||||
String endLink = (String) automata.eindStates.first();
|
||||
String newBeginLink = (String) automata.beginStates.first();
|
||||
|
||||
//Rechts
|
||||
ConvertStatement(regex.rechts, automata);
|
||||
convertStatement(regex.rechts, automata);
|
||||
String beginLink = (String) automata.beginStates.first();
|
||||
String newEndLink = (String) automata.eindStates.first();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user