How to find reverse relations between jobs?
<p>I've been trying to find reverse relations between jobs. To be more specific I will tell it with an example.</p>

<p>Suppose I have n jobs i.e <code>{0,1,2,3,4,...n}</code>. I also have relations between jobs. I know only successor jobs, i.e., <code>2</code> is followed by <code>4,5</code>. <code>5</code> is followed by <code>7,8</code> etc. I have it in a text file. I want to obtaing precedence relations between jobs (what is the predecessor job of <code>5</code>?).</p>

<p>Having text output would be great. I have some code but it does not work. </p>

<pre><code>#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;iomanip&gt;
#include &lt;cstdlib&gt;
#include &lt;ctime&gt;
using namespace std;

#define TOTAL_ACTIVITY 123

void readFile();
void change ();
void writeFile ();

struct Activity {
    int precedessor [3];
    int successor [3];
    int id;
};

Activity activityList[TOTAL_ACTIVITY];

void main() {
    readFile();
    change();
    writeFile();
}

void  readFile() {
    ifstream myReadFile;
    myReadFile.open("pre.txt");
    if (!myReadFile) { //check whether the file can be opened
        cerr &lt;&lt; "Unable to open file"; // terminate with error
    }

    while (!myReadFile.eof()) {
        int Id,suc1, suc2, suc3;
        int t = 0;
        while (myReadFile &gt;&gt; Id &gt;&gt;  suc1 &gt;&gt; suc2 &gt;&gt; suc3) //data should be in this order
        {
            activityList[t].id = Id;
            activityList[t].successor [0] = suc1;
            activityList[t].successor [1] = suc2;
            activityList[t].successor [2] = suc3;
            t++;
        }
    }
    return;
}

void change() {
    int act;
    for (int i=1;i&lt;TOTAL_ACTIVITY;i++){
        for (int j=0;j&lt;TOTAL_ACTIVITY;j++){
            for (int k=0;k&lt;3;k++) {
                if (activityList[j].successor[k]==i;)
            }
        }
    }
}

void writeFile() {
    ofstream out("out.txt");
    out &lt;&lt; "id\t" &lt;&lt; "Pre1\t" &lt;&lt; "Pre2\t" &lt;&lt; "Pre3\t"&lt;&lt;"\n";
    for (int j = 0; j &lt; TOTAL_ACTIVITY; j++) {
        out &lt;&lt; activityList[j].id &lt;&lt; "\t";
        out &lt;&lt; activityList[j].precedessor[0]&lt;&lt; "\t";
        out &lt;&lt; activityList[j].precedessor[1] &lt;&lt; "\t";
        out &lt;&lt; activityList[j].precedessor[2] &lt;&lt; "\t";
        out &lt;&lt; "\n"; 
    }
    out.close();
}
</code></pre>

<p>Here is a sample input:</p>

<pre><code>ID  Successor1  Successor2  Successor3
1   2   3   4
2   6   11  15
3   7   8   13
4   5   9   10
5   20      
6   30      
7   27      
8   12  19  27
9   14      
10  16  25  
11  20  26  
12  14      
13  17  18  
14  17      
15  25      
16  21  22  
17  22      
18  20  22  
19  24  29  
20  23  25  
21  28      
22  23      
23  24      
24  30      
25  30      
26  31      
27  28      
28  31      
29  32      
30  32      
31  32      
</code></pre>

<p>Output should be something like this:</p>

<pre><code>Id Predecesor1  Predecesor2  Predecesor3
........................................
...........................................
...........................................
</code></pre>
