<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://michaelnielsen.org/polymath/index.php?action=history&amp;feed=atom&amp;title=Depth-first_search</id>
	<title>Depth-first search - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://michaelnielsen.org/polymath/index.php?action=history&amp;feed=atom&amp;title=Depth-first_search"/>
	<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Depth-first_search&amp;action=history"/>
	<updated>2026-08-18T23:57:34Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.3</generator>
	<entry>
		<id>https://michaelnielsen.org/polymath/index.php?title=Depth-first_search&amp;diff=2668&amp;oldid=prev</id>
		<title>Alec: New page: The C program below executes a depth-first search. It was written quickly without much regard for user-friendliness, so requires some explanation.  Compile the program, say to an executabl...</title>
		<link rel="alternate" type="text/html" href="https://michaelnielsen.org/polymath/index.php?title=Depth-first_search&amp;diff=2668&amp;oldid=prev"/>
		<updated>2010-01-12T22:06:09Z</updated>

		<summary type="html">&lt;p&gt;New page: The C program below executes a depth-first search. It was written quickly without much regard for user-friendliness, so requires some explanation.  Compile the program, say to an executabl...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;The C program below executes a depth-first search. It was written quickly without much regard for user-friendliness, so requires some explanation.&lt;br /&gt;
&lt;br /&gt;
Compile the program, say to an executable called &amp;#039;erdos&amp;#039;. Then execute:&lt;br /&gt;
 erdos erdos1.log erdos2.log&lt;br /&gt;
Choose the names of the log files as you please. Important output (maximum-length sequences) goes to your console and to erdos2.log; some further information about how far it&amp;#039;s tracked back, and so on, goes in erdos1.log. I generally follow erdos1.log in a separate console window while the program is running.&lt;br /&gt;
&lt;br /&gt;
You will want to modify some bits and pieces in the code. Set C_min and C_max to the minimum and maximum values allowed for the partial sums (e.g. -2 and 2). Set &amp;#039;OPTIMIZE&amp;#039; to 1 if you want to use the &amp;lt;math&amp;gt;(p, q)&amp;lt;/math&amp;gt;-optimization trick: this only works if C_min=-2 and C_max=2 and you haven&amp;#039;t got any additional constraints. Set N to the limit of your search. Set N_TO_COUNT to the length you want to count instances of -- e.g. to count the number of 1124-length sequences you find, set it to 1124.&lt;br /&gt;
&lt;br /&gt;
Further down there is an EXCLUDE macro, where you set the constraints. You can see the format from the code that&amp;#039;s there. These are conditions for rejection. Note that these should be constraints on the x[i] for i&amp;lt;=n: don&amp;#039;t put something like x[n]==x[2*n] here or it won&amp;#039;t work.&lt;br /&gt;
&lt;br /&gt;
Further down still, the declaration of init_x is set to the initial value for the search.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdlib.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;lt;time.h&amp;gt;&lt;br /&gt;
#ifndef _WIN32&lt;br /&gt;
# include &amp;lt;stdbool.h&amp;gt;&lt;br /&gt;
#else&lt;br /&gt;
# define bool char&lt;br /&gt;
# define true 1&lt;br /&gt;
# define false 0&lt;br /&gt;
#endif&lt;br /&gt;
&lt;br /&gt;
#define OPTIMIZE 0&lt;br /&gt;
&lt;br /&gt;
#define N 1200 // limit of search&lt;br /&gt;
#define C_min -2 // limit on sums&lt;br /&gt;
#define C_max 2 // limit on sums&lt;br /&gt;
#define N_TO_COUNT 1124&lt;br /&gt;
&lt;br /&gt;
static unsigned int s[N+1][N+1];&lt;br /&gt;
static unsigned int l[N+1];&lt;br /&gt;
&lt;br /&gt;
static void print_seq(FILE *fp, char *seq, unsigned int n) {&lt;br /&gt;
    unsigned int j;&lt;br /&gt;
    fprintf(fp, &amp;quot;\n &amp;quot;);&lt;br /&gt;
    for (j = 1; j&amp;lt;=n; j++) {&lt;br /&gt;
        fprintf(fp, &amp;quot;%s&amp;quot;, (seq[j] == 1) ? &amp;quot;+&amp;quot; : &amp;quot;-&amp;quot;);&lt;br /&gt;
        if (j%60==0) fprintf(fp, &amp;quot;\n &amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    fprintf(fp, &amp;quot;\n&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static bool check(char *seq, unsigned int n) {&lt;br /&gt;
    bool ok = true;&lt;br /&gt;
    unsigned int d = 1;&lt;br /&gt;
    while ((d &amp;lt;= n) &amp;amp;&amp;amp; ok) {&lt;br /&gt;
        unsigned int i = d;&lt;br /&gt;
        char tot = 0;&lt;br /&gt;
        while ((i &amp;lt;= n) &amp;amp;&amp;amp; ok) {&lt;br /&gt;
            tot += seq[i];&lt;br /&gt;
            if ((tot&amp;lt;C_min) || (tot&amp;gt;C_max)) ok = false;&lt;br /&gt;
            i += d;&lt;br /&gt;
        }&lt;br /&gt;
        d++;&lt;br /&gt;
    }&lt;br /&gt;
    return ok;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
#define EXCLUDE \&lt;br /&gt;
    ((n%3==0) &amp;amp;&amp;amp; (x[n]==x[n/3])) || \&lt;br /&gt;
    ((n%5==0) &amp;amp;&amp;amp; (x[n]==-x[n/5]))// || \&lt;br /&gt;
//    ((n%7==0) &amp;amp;&amp;amp; (x[n]==x[n/7]))// || \&lt;br /&gt;
//    ((n%11==0) &amp;amp;&amp;amp; (x[n]==x[n/11])) || \&lt;br /&gt;
//    ((n%13==0) &amp;amp;&amp;amp; (x[n]==-x[n/13]))&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char *argv[]) {&lt;br /&gt;
    FILE *fp, *fp1;&lt;br /&gt;
    struct tm *local;&lt;br /&gt;
    time_t tt;&lt;br /&gt;
    bool is_prime[N+1];&lt;br /&gt;
    char t[N+1][N+1];&lt;br /&gt;
    char x[N+1];&lt;br /&gt;
    char x_ref[N+1];&lt;br /&gt;
    unsigned int n, max_n, min_back, max_fwd;&lt;br /&gt;
    unsigned long kk;&lt;br /&gt;
#if OPTIMIZE&lt;br /&gt;
    bool tracking = false; // whether I&amp;#039;m tracking from a prime to check for symmetry&lt;br /&gt;
    unsigned int tracking_p = 0; // base prime for tracking&lt;br /&gt;
    char tracking_sign = 0;&lt;br /&gt;
#endif&lt;br /&gt;
    bool hope;&lt;br /&gt;
&lt;br /&gt;
    const char init_x[689] = {&lt;br /&gt;
        0, +1, -1, -1, +1, -1, +1, +1, -1, -1, +1, -1, -1, +1, -1, +1, +1, -1, +1, +1, -1, -1, +1, -1, +1, +1, -1, +1, +1, -1, -1, +1, -1, +1, +1, -1, -1, +1, -1, -1, +1, -1, +1, +1, -1, +1, +1, -1, -1, +1, -1, +1, +1, -1, -1, +1, -1, -1, +1, -1, +1, +1, -1, +1, +1, -1, -1, +1, -1, +1, +1, -1, +1, -1, -1, -1, +1, -1, +1, +1, -1, -1, +1, -1, -1, +1, -1, +1, +1, +1, -1, +1, -1, -1, +1, -1, +1, +1, -1, -1, +1, -1, -1, +1, -1, +1, +1, -1, +1, +1, -1, -1, +1, -1, +1, +1, -1, +1, +1, -1, -1, +1, -1, +1, +1, -1, -1, -1, -1, +1, +1, -1, +1, +1, -1, -1, +1, -1, -1, +1, -1, +1, +1, -1, -1, +1, +1, -1, +1, +1, +1, -1, -1, +1, +1, -1, -1, +1, -1, -1, +1, -1, +1, +1, -1, -1, +1, -1, +1, +1, -1, -1, +1, +1, -1, +1, -1, +1, -1, +1, +1, -1, -1, -1, +1, -1, +1, +1, -1, -1, +1, +1, -1, +1, -1, +1, +1, -1, +1, -1, -1, -1, +1, -1, +1, +1, -1, +1, +1, -1, -1, +1, -1, +1, +1, -1, -1, +1, -1, +1, +1, -1, +1, -1, -1, -1, +1, +1, -1, +1, -1, +1, +1, -1, -1, +1, -1, -1, +1, -1, +1, -1, -1, +1, +1, -1, -1, +1, -1, +1, +1, -1, +1, +1, +1, -1, +1, -1, -1, +1, -1, -1, +1, -1, -1, +1, -1, +1, +1, +1, +1, -1, -1, -1, +1, -1, +1, +1, -1, -1, +1, -1, -1, +1, -1, +1, +1, -1, +1, +1, -1, -1, -1, +1, +1, +1, -1, +1, -1, -1, -1, +1, +1, -1, +1, -1, -1, +1, -1, +1, +1, +1, +1, -1, -1, -1, +1, -1, +1, +1, -1, -1, +1, -1, -1, +1, -1, +1, +1, -1, +1, +1, -1, +1, +1, -1, -1, -1, -1, +1, +1, -1, +1, +1, -1, -1, -1, +1, +1, +1, -1, -1, +1, -1, -1, +1, +1, +1, -1, -1, -1, +1, +1, -1, +1, +1, +1, -1, -1, +1, +1, -1, -1, +1, -1, +1, +1, -1, +1, -1, -1, -1, -1, +1, +1, +1, -1, -1, +1, +1, -1, +1, -1, +1, +1, -1, -1, -1, +1, -1, +1, -1, +1, +1, -1, +1, +1, -1, -1, +1, -1, +1, +1, -1, -1, +1, -1, -1, +1, +1, +1, -1, -1, +1, +1, -1, -1, +1, -1, +1, +1, -1, +1, -1, -1, -1, +1, -1, -1, +1, -1, +1, +1, +1, -1, -1, +1, -1, +1, -1, +1, +1, -1, +1, -1, -1, +1, +1, -1, -1, +1, +1, -1, -1, -1, +1, +1, -1, +1, +1, -1, -1, +1, -1, +1, +1, -1, -1, +1, -1, -1, +1, +1, +1, +1, -1, -1, +1, -1, -1, +1, -1, +1, +1, -1, +1, +1, -1, -1, +1, -1, +1, +1, -1, -1, +1, -1, -1, -1, +1, +1, +1, -1, -1, +1, -1, +1, +1, -1, -1, +1, +1, +1, -1, -1, -1, +1, -1, +1, +1, -1, +1, +1, -1, -1, +1, -1, +1, -1, -1, -1, +1, +1, -1, +1, -1, +1, +1, -1, -1, +1, -1, -1, +1, -1, +1, +1, -1, +1, +1, -1, -1, +1, -1, +1, +1, -1, +1, +1, -1, -1, +1, -1, +1, +1, -1, -1, +1, -1, -1, +1, -1, +1, +1, +1, -1, -1, -1, -1, +1, -1, +1, +1, -1, -1, +1, +1, +1, +1, -1, +1, -1, -1, +1, -1, -1, +1, +1, -1, -1, +1, -1, +1, +1, -1, -1, +1, +1, -1, +1, -1, +1, -1, -1, -1, +1, +1, +1, +1, -1, +1, -1, -1, -1, +1, +1, -1, +1, -1, -1, +1, +1, +1, -1, -1, -1, +1, -1, +1, +1, -1, +1, +1, -1, -1, +1, -1, -1, +1, +1, -1, -1, -1, +1, +1, -1, -1, +1, -1, +1, +1, -1, +1, -1, +1, +1, +1, -1, -1, -1, -1, +1, +1, +1, -1, +1, -1, +1, +1&lt;br /&gt;
    };&lt;br /&gt;
    const unsigned int init_x_max = 688;&lt;br /&gt;
&lt;br /&gt;
    { // initialize divisors table (s), lengths vector (l) and is_prime vector&lt;br /&gt;
        unsigned int n, d;&lt;br /&gt;
        for (n = 0; n &amp;lt;= N; n++) {&lt;br /&gt;
            unsigned int d_count = 0;&lt;br /&gt;
            for (d = 1; d &amp;lt;= N; d++) {&lt;br /&gt;
                if ((n%d) == 0) {&lt;br /&gt;
                    s[n][d_count] = d;&lt;br /&gt;
                    d_count++;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
            l[n] = d_count;&lt;br /&gt;
            is_prime[n] = (d_count == 2);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    { // initialize coordinates vector (x)&lt;br /&gt;
        unsigned int i;&lt;br /&gt;
        for (i = 0; i &amp;lt;= init_x_max; i++) x[i] = init_x[i];&lt;br /&gt;
        for (i = init_x_max+1; i &amp;lt;= N; i++) x[i] = 1;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    { // set x_ref = x&lt;br /&gt;
        unsigned int i;&lt;br /&gt;
        for (i = 0; i &amp;lt;= N; i++) x_ref[i] = x[i];&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    { // initialize totals table (t)&lt;br /&gt;
        unsigned int n, d;&lt;br /&gt;
        for (n = 0; n &amp;lt;= N; n++) {&lt;br /&gt;
            for (d = 0; d &amp;lt;= N; d++) t[n][d] = 0;&lt;br /&gt;
        }&lt;br /&gt;
        t[1][1] = x[1];&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    fp = fopen(argv[1], &amp;quot;w&amp;quot;);&lt;br /&gt;
    fp1 = fopen(argv[2], &amp;quot;w&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
    kk = 0;&lt;br /&gt;
    n = 2;&lt;br /&gt;
    max_n = 2;&lt;br /&gt;
    hope = true;&lt;br /&gt;
    min_back = 2;&lt;br /&gt;
    max_fwd = 2;&lt;br /&gt;
    while ((n &amp;lt;= N) &amp;amp;&amp;amp; hope) { // require that t[m][d] is correct for all m&amp;lt;n, d|m&lt;br /&gt;
        bool ok = true;&lt;br /&gt;
        unsigned int i = 0;&lt;br /&gt;
        if (n &amp;lt; min_back) { // record new track-back limit&lt;br /&gt;
            min_back = n;&lt;br /&gt;
            max_fwd = min_back;&lt;br /&gt;
            tt = time(NULL);&lt;br /&gt;
            local = localtime(&amp;amp;tt);&lt;br /&gt;
            fprintf(fp, &amp;quot;\n************\n%smaximum = %d\n&amp;quot;, asctime(local), max_n);&lt;br /&gt;
            fprintf(fp, &amp;quot;found %lu sequences of length %d\n&amp;quot;, kk, N_TO_COUNT);&lt;br /&gt;
            fprintf(fp, &amp;quot;backtracked to %d ... checking ...\n&amp;quot;, n);&lt;br /&gt;
        }&lt;br /&gt;
#if OPTIMIZE&lt;br /&gt;
        if ((!tracking) &amp;amp;&amp;amp; (n &amp;gt; N/3) &amp;amp;&amp;amp; is_prime[n] &amp;amp;&amp;amp; (x[n]==x_ref[n]) &amp;amp;&amp;amp; (t[n-1][1]==0)) {&lt;br /&gt;
            tracking = true;&lt;br /&gt;
            tracking_p = n;&lt;br /&gt;
            tracking_sign = x[n];&lt;br /&gt;
        }&lt;br /&gt;
        if (tracking &amp;amp;&amp;amp; is_prime[n] &amp;amp;&amp;amp; (x[n]==-tracking_sign)) { // track back to tracking_p and reverse value there&lt;br /&gt;
            while (n &amp;gt; tracking_p) {&lt;br /&gt;
                x[n] = x_ref[n];&lt;br /&gt;
                n--;&lt;br /&gt;
            }&lt;br /&gt;
            x[n] = -x_ref[n];&lt;br /&gt;
            tracking = false;&lt;br /&gt;
        }&lt;br /&gt;
#endif&lt;br /&gt;
        while ((i &amp;lt; l[n]) &amp;amp;&amp;amp; ok) {&lt;br /&gt;
            unsigned int d = s[n][i];&lt;br /&gt;
            char tot = t[n-d][d] + x[n];&lt;br /&gt;
#if OPTIMIZE&lt;br /&gt;
            if (tracking &amp;amp;&amp;amp; (d==1) &amp;amp;&amp;amp; (tot==-tracking_sign)) tracking = false;&lt;br /&gt;
#endif&lt;br /&gt;
            if ((tot&amp;lt;C_min) || (tot&amp;gt;C_max) || (EXCLUDE)) ok = false;&lt;br /&gt;
            else {&lt;br /&gt;
                t[n][d] = tot;&lt;br /&gt;
                i++;&lt;br /&gt;
            }&lt;br /&gt;
            if (!ok) {&lt;br /&gt;
                if (x[n] == x_ref[n]) {&lt;br /&gt;
                    x[n] = -x_ref[n];&lt;br /&gt;
                    i = 0;&lt;br /&gt;
                    ok = true;&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        if (ok) {&lt;br /&gt;
            if (n &amp;gt; max_fwd) { // set and record new max_fwd&lt;br /&gt;
                max_fwd = n;&lt;br /&gt;
                fprintf(fp, &amp;quot;%d ... &amp;quot;, n);&lt;br /&gt;
                fflush(fp);&lt;br /&gt;
            }&lt;br /&gt;
            if (n==N_TO_COUNT) kk++;&lt;br /&gt;
            if (n &amp;gt; max_n) { // update maximum and print sequence&lt;br /&gt;
                max_n = n;&lt;br /&gt;
                min_back = n;&lt;br /&gt;
                fprintf(fp1, &amp;quot;length: %d\n&amp;quot;, n);&lt;br /&gt;
                fprintf(fp1, &amp;quot;x: &amp;quot;);&lt;br /&gt;
                print_seq(fp1, x, n);&lt;br /&gt;
                fprintf(stdout, &amp;quot;length: %d\n&amp;quot;, n);&lt;br /&gt;
                fprintf(stdout, &amp;quot;x: &amp;quot;);&lt;br /&gt;
                print_seq(stdout, x, n);&lt;br /&gt;
                fprintf(stdout, &amp;quot;verify: %s\n&amp;quot;, check(x, n) ? &amp;quot;pass&amp;quot; : &amp;quot;fail&amp;quot;);&lt;br /&gt;
            }&lt;br /&gt;
            n++;&lt;br /&gt;
        }&lt;br /&gt;
        else { // track back&lt;br /&gt;
            while ((n &amp;gt; 0) &amp;amp;&amp;amp; (x[n] == -x_ref[n])) {&lt;br /&gt;
                x[n] = x_ref[n];&lt;br /&gt;
                n--;&lt;br /&gt;
            }&lt;br /&gt;
            x[n] = -x_ref[n];&lt;br /&gt;
#if OPTIMIZE&lt;br /&gt;
            if (n&amp;lt;=tracking_p) tracking = false;&lt;br /&gt;
#endif&lt;br /&gt;
            if (n == 1) hope = false;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    fprintf(fp, &amp;quot;END\n&amp;quot;);&lt;br /&gt;
    if (hope) { // reached N successfully: print final sequence&lt;br /&gt;
        fprintf(fp, &amp;quot;N = %d\n&amp;quot;, N);&lt;br /&gt;
        fprintf(fp, &amp;quot;x: &amp;quot;);&lt;br /&gt;
        print_seq(fp, x, N);&lt;br /&gt;
        fprintf(fp, &amp;quot;check: %s\n&amp;quot;, check(x, N) ? &amp;quot;pass&amp;quot; : &amp;quot;fail&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
    else fprintf(fp, &amp;quot;fail\n&amp;quot;);&lt;br /&gt;
    fprintf(fp, &amp;quot;found %lu sequences of length %d\n&amp;quot;, kk, N_TO_COUNT);&lt;br /&gt;
&lt;br /&gt;
    fclose(fp);&lt;br /&gt;
    fclose(fp1);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Alec</name></author>
	</entry>
</feed>