From 57fa05dc68db356219e103c6f43ecb7adde3fb5d Mon Sep 17 00:00:00 2001
From: Tomas Pecka <peckato1@fit.cvut.cz>
Date: Sat, 30 Jan 2016 23:02:26 +0100
Subject: [PATCH] Minimize: Label groups by lexicographically smallest state
 label

---
 alib2algo/src/automaton/simplify/Minimize.cpp       | 10 +++++++---
 alib2algo/test-src/regexp/toAutomaton/re2faTest.cpp |  4 ++--
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/alib2algo/src/automaton/simplify/Minimize.cpp b/alib2algo/src/automaton/simplify/Minimize.cpp
index 8a76dd7d2a..3ae99c5fdd 100644
--- a/alib2algo/src/automaton/simplify/Minimize.cpp
+++ b/alib2algo/src/automaton/simplify/Minimize.cpp
@@ -51,11 +51,14 @@ automaton::DFA Minimize::minimize(const automaton::DFA& dfa) {
 	std::map<automaton::State, automaton::State> toEquvivalentStates2;
 	std::map<std::pair<automaton::State, std::set<std::pair<alphabet::Symbol, automaton::State> > >, std::set<automaton::State> > minimizedTransitionFunction2;
 
+	const automaton::State *firstFinal = NULL, *firstNonfinal = NULL;
 	for(auto iter = dfa.getStates().begin(); iter != dfa.getStates().end(); iter++) {
 		if(dfa.getFinalStates().count(*iter) == 0) { // not a final state
-			toEquvivalentStates2.insert(std::pair<automaton::State, automaton::State>(*iter, automaton::State(0)));
+			if(!firstNonfinal) firstNonfinal = &(*iter);
+			toEquvivalentStates2.insert(std::pair<automaton::State, automaton::State>(*iter, *firstNonfinal));
 		} else {
-			toEquvivalentStates2.insert(std::pair<automaton::State, automaton::State>(*iter, automaton::State(1)));
+			if(!firstFinal) firstFinal = &(*iter);
+			toEquvivalentStates2.insert(std::pair<automaton::State, automaton::State>(*iter, *firstFinal));
 		}
 	}
 
@@ -92,7 +95,8 @@ automaton::DFA Minimize::minimize(const automaton::DFA& dfa) {
 		int number = 0;
 		for(auto iter = minimizedTransitionFunction1.begin(); iter != minimizedTransitionFunction1.end(); iter++) {
 			for(auto iter2 = iter->second.begin(); iter2 != iter->second.end(); iter2++) {
-				toEquvivalentStates2.insert(std::pair<automaton::State, automaton::State>(*iter2, automaton::State(number)));
+				//toEquvivalentStates2.insert(std::pair<automaton::State, automaton::State>(*iter2, automaton::State(number)));
+				toEquvivalentStates2.insert(std::pair<automaton::State, automaton::State>(*iter2, *(iter->second.begin())));
 			}
 			number++;
 		}
diff --git a/alib2algo/test-src/regexp/toAutomaton/re2faTest.cpp b/alib2algo/test-src/regexp/toAutomaton/re2faTest.cpp
index 7c643022de..6adf98cc44 100644
--- a/alib2algo/test-src/regexp/toAutomaton/re2faTest.cpp
+++ b/alib2algo/test-src/regexp/toAutomaton/re2faTest.cpp
@@ -40,8 +40,8 @@ void re2faTest::testThompson() {
 	automaton::Automaton dfa1 = automaton::determinize::Determinize::determinize(nfa1);
 	automaton::Automaton dfa2 = automaton::determinize::Determinize::determinize(nfa2);
 
-	automaton::Automaton mdfa1 = automaton::simplify::Minimize::minimize(dfa1);
-	automaton::Automaton mdfa2 = automaton::simplify::Minimize::minimize(dfa2);
+	automaton::Automaton mdfa1 = automaton::simplify::Normalize::normalize(automaton::simplify::Minimize::minimize(dfa1));
+	automaton::Automaton mdfa2 = automaton::simplify::Normalize::normalize(automaton::simplify::Minimize::minimize(dfa2));
 
 	CPPUNIT_ASSERT( mdfa1 == mdfa2);
 }
-- 
GitLab