News for hackers. Stuff that matters.
Re:Divisibility by 7 is a Walk on a Graph (2009) by sirsar on Sunday November 08, 2015 @10:35PM (#10531246)
This "walk on a graph" is known formally as a Deterministic Finite Automata. One of the interesting results of automata theory is that every FA has an equivalent regular expression (a subset of the "regexps" in most modern programming languages).

Where is this going? Behold, a 12,701 character regex for divisibility by 7: http://codegolf.stackexchange.com/questions/3503/hard-code-g...

This doesn't use any recursive features (it is a real Regular Expression in the theoretic sense) so it can be checked in linear time.

Re:Divisibility by 7 is a Walk on a Graph (2009) by fizbin on Monday November 09, 2015 @03:07PM (#10535447)
Note that the 12,701 character regex is WRONG. It doesn't even match "70". Even the binary regex is wrong.

The original 32K regex pointed to in the stackexchange problem statement however is perfectly correct.

Test code:

    open(F, '< /tmp/q.in');
    $myre = <F>;
    chomp($myre);
    $myrecomp = qr/^(?:$myre)$/;
    while (<>) {
      chomp();
      if (m/$myrecomp/) {
        print "$_\n";
      }
    }
Examples of multiples of 7 not matched by the given regex: 70, 707, 7007, 11095

(I spent all morning trying to find the bug in my regex compiler based on the assumption that the answer there was correct)

Re:Divisibility by 7 is a Walk on a Graph (2009) by newsignup on Sunday November 08, 2015 @11:15PM (#10531346)
> a 12,701 character regex for divisibility by 7:

I wonder if that's the optimal regex or can be reduced further using some rules[1] like the ones which we(humans) generally use to do it manually.

[1]. https://www.math.hmc.edu/funfacts/ffiles/10005.5.shtml

Re:Divisibility by 7 is a Walk on a Graph (2009) by anderskaseorg on Monday November 09, 2015 @05:55AM (#10532308)
I wrote a 11811-character regex for divisibility by 7 back in August 2008: http://web.mit.edu/andersk/Public/regex
Re:Divisibility by 7 is a Walk on a Graph (2009) by fizbin on Monday November 09, 2015 @03:09PM (#10535458)
And unlike the one on stackexchange, it's actually correct!

(I have a regex -> DFA compiler here that I just threw your regex through. Seven states, connected as expected)

Re:Divisibility by 7 is a Walk on a Graph (2009) by eru on Monday November 09, 2015 @01:14PM (#10534688)
Do you have the source to the program you wrote the regex with? (Or did you write it by hand?)
Re:Divisibility by 7 is a Walk on a Graph (2009) by eru on Monday November 09, 2015 @05:49AM (#10532286)
It's definitely not the optimal regex. Finding the smallest regex is a hard problem (at least in theory): https://cs.stackexchange.com/questions/19686/is-regex-golf-n...

(I think I've read that finding the smallest one is not only NP-hard, but P-Space complete.)

Re:Divisibility by 7 is a Walk on a Graph (2009) by ant6n on Sunday November 08, 2015 @11:23PM (#10531379)
Don't regular expressions have minimal, canonical forms?
[deleted]
Re:Divisibility by 7 is a Walk on a Graph (2009) by areyousure on Monday November 09, 2015 @12:25AM (#10531519)
The minimum DFA does not correspond in any particular sense to a minimal regular expression.
Re:Divisibility by 7 is a Walk on a Graph (2009) by dllthomas on Monday November 09, 2015 @12:36AM (#10531546)
Of course not - my bad!
Re:Divisibility by 7 is a Walk on a Graph (2009) by Ar-Curunir on Sunday November 08, 2015 @11:32PM (#10531407)
Yes they do. For reference, check Sipser.
Re:Divisibility by 7 is a Walk on a Graph (2009) by sirsar on Sunday November 08, 2015 @11:48PM (#10531448)
Could you give a page number? I skimmed the regular expression section and couldn't find it. Also, while it is self-evident to me that there is a minimal form for each regex, the construction for finding one isn't immediately obvious to me (I can surely enumerate several ways of eliminating redundancy). Beyond that, I'm not sure a minimal form would minimize character length.

Of (a \star b \star ) \star and ( a ∪ b ) \star, which is the canonical form? The transformation from the former to the latter is nontrivial as a \star b \star is not equivalent with a ∪ b.

Re:Divisibility by 7 is a Walk on a Graph (2009) by areyousure on Monday November 09, 2015 @12:24AM (#10531514)
Of course, there is a shortest equivalent regular expression, but in general it's PSPACE-complete to find it: https://en.wikipedia.org/wiki/PSPACE-complete#Regular_expres...

It remains PSPACE-complete if you're given a corresponding DFA: "Jiang and Ravikumar [7] show moreover that the minimization problem for ... regular expressions remains PSPACE-complete, even when specifying the regular language by a dfa" from http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.60....

In that sense, the grandparent is incorrect. It is unknown, in both practice and theory, how to find the shortest equivalent regex.

Re:Divisibility by 7 is a Walk on a Graph (2009) by adrianN on Monday November 09, 2015 @02:43AM (#10531795)
It's not unknown, it just takes a long time.
Re:Divisibility by 7 is a Walk on a Graph (2009) by acchow on Monday November 09, 2015 @04:04AM (#10532005)
I think you should think deeper on the idea of "unknown"
Re:Divisibility by 7 is a Walk on a Graph (2009) by adrianN on Monday November 09, 2015 @05:07AM (#10532159)
I don't understand what you mean. Regex minimization is in PSPACE, so take any PSPACE complete problem, eg Q-Sat, do the reduction and apply the algorithm. Q-Sat can be solved by recursively enumerating all possibilities. So there is a method for minimizing regular expressions. It just takes a long time to run. If you have time you can probably think of an algorithm that works directly on the regex problem without doing the Q-Sat thing. It might even be faster.

Heck, there might be an algorithm that works really well in practice. People who do model checking and verification laugh PSPACE completeness in the face and are not even really concerned with undecidability.

Re:Divisibility by 7 is a Walk on a Graph (2009) by eru on Monday November 09, 2015 @01:16PM (#10534706)
I would be very interested in finding an approach that works well in practice.
Re:Divisibility by 7 is a Walk on a Graph (2009) by kragen on Monday November 09, 2015 @09:12AM (#10533061)
Okay, fine, but in this case N=7. What are the constant factors?
Re:Divisibility by 7 is a Walk on a Graph (2009) by raverbashing on Monday November 09, 2015 @03:38AM (#10531924)
The weird part is that the regex is much bigger than the DFA (but the DFA is not accepting the symbols directly, it's transforming them first

It also seems that regexes can't express accepting expressions like AB where A is accepted by itself (considering AB is accepted), which in the DFA is implemented by sending it to the initial state

Re:Divisibility by 7 is a Walk on a Graph (2009) by robryk on Monday November 09, 2015 @03:42AM (#10531940)
> It also seems that regexes can't express accepting expressions like AB where A is accepted by itself (considering AB is accepted), which in the DFA is implemented by sending it to the initial state

B* does that.

Re:Divisibility by 7 is a Walk on a Graph (2009) by anewhnaccount on Monday November 09, 2015 @02:57AM (#10531826)
This is a DFA over the alphabet {0, 1} where the number is first transformed by writing out each digit in unary with 1 and delimiting digits with 0.
Re:Divisibility by 7 is a Walk on a Graph (2009) by eru on Monday November 09, 2015 @05:42AM (#10532262)
Wow, I'm famous.