Verify that given list of nodes of a graph is a correct topological order
<p>I've been given a task to write some code that takes in a list of nodes from a graph, and determines whether they are in correct topological order.</p>

<p>The graph is represented in memory as follows:</p>

<pre><code>typedef struct graph_t* Graph;
typedef struct node_t* Node;

struct node_t {
    int id;
    /*incoming edges*/
    Linked_list incoming;
    /*outgoing edges*/
    Linked_list outgoing;
};

struct graph_t {
    int order;
    int size;
    Node nodes;
};
</code></pre>

<p>I omitted the implementation of the linked list for brevity but it is just a standard implementation of a linked list.</p>

<p>I have also been given the following algorithm (pseudocode):</p>

<pre><code>L &lt;- topologically sorted list of nodes
V &lt;- empty list of visited nodes
for each node n in L do
    add n to V
    for each node m reachable from n do
        if m in V then L is not sorted
</code></pre>

<p>I do understand the definition of a topological order, but I don't understand how or why this would verify the topological sort.</p>

<p>How is this algorithm correct? Also, given the above representation of the graph, how would the line <code>for each node m reachable from n do</code> be implemented?</p>

<p>Also, is there a better algorithm than the one above to perform this task?</p>