Topological sort using STL I am trying to implement topological sort using STL. I have used vector of lists to store adjacency list. Indegree is an array which stores indegree of vertices.

Indegree0 is a queue implemented using STL list to store vertices with indegree 0. Can someone please help me improve my code?

# include &lt;iostream&gt;  
# include &lt;bits/stdc++.h&gt;
using namespace std;

vector&lt;int&gt; topologicalsort(vector&lt;list&lt;int&gt; &gt; adjacencylist,int  indegree[],int v,int e) 
{

    vector&lt;int&gt;ans(v+1);
    int k=1;
    //list to store vertices with indegree 0
    list&lt;int&gt;indegree0;
    for(int i=1;i&lt;=v;i++)
    {
        if(indegree[i]==0)
            indegree0.push_back(i);
    }
    while(!indegree0.empty())
    {
        int x=indegree0.front();
        indegree0.pop_front();
        ans[k]=x;
        k++;
        list&lt;int&gt;::iterator itr=adjacencylist[x].begin();
        while(itr!=adjacencylist[x].end())
        {
            indegree[*itr]--;
            if(indegree[*itr]==0)
                indegree0.push_back(*itr);
            itr++;
        }
    }
    return ans;
}

int main()
{
    int v,e;
    cin&gt;&gt;v&gt;&gt;e;
    vector&lt;list&lt;int&gt; &gt; adjacencylist(v+1);
    int indegree[v+1];
    for(int i=1;i&lt;=v;i++)
        indegree[i]=0;
    for(int i=1;i&lt;=e;i++)
    {
        int v1,v2;
        cin&gt;&gt;v1&gt;&gt;v2;
        adjacencylist[v1].push_back(v2);
        indegree[v2]++;
    }
    //cout&lt;&lt;adjacencylist.size()&lt;&lt;endl;
    for(int i=1;i&lt;=v;i++)
    {
        printf("adjacencylist[%d] :",i);
        list&lt;int&gt;::iterator itr=adjacencylist[i].begin();
        while(itr!=adjacencylist[i].end())
        {
            cout&lt;&lt;*itr&lt;&lt;"  ";
            itr++;
        }
        cout&lt;&lt;endl;
    }
    vector&lt;int&gt; ans=topologicalsort(adjacencylist,indegree,v,e);
    for(int i=1;i&lt;=v;i++)
        cout&lt;&lt;ans[i]&lt;&lt;"  ";

}
Headers and namespaces

#include "bits/stdc++.h"


That's not a standard C++ header; prefer to use standard headers so that others can use your code.

using namespace std;


Bringing all names in from a namespace is problematic; namespace std particularly so. See Why is “using namespace std” considered bad practice?.

Used but not included: &lt;cstdio&gt;, &lt;vector&gt;,  &lt;list&gt;.  I'd recommend you stick to one of &lt;cstdio&gt; and &lt;iostream&gt;.

Function signature

std::vector&lt;int&gt; topologicalsort(std::vector&lt;std::list&lt;int&gt; &gt; adjacencylist,
                                 int indegree[], int v,
                                 int e)


Why do you require e?  It's never used.

Passing indegree as a pointer and length is very C-style; prefer a C++ range (e.g. a pair of iterators, or a standard container).

There's no need to pass adjacencylist by value: a const-ref is more appropriate.

Result vector

Instead of creating v+1 elements in the result vector and having to keep track of our insert position with k, we can create it empty and push_back each result as we find it.  We can avoid reallocation by using reserve().

Looping over elements

Instead of the error prone iterator-based loop, prefer to use range-based for when applicable:

    for (int i: adjacencylist[x]) {
        if (--indegree[i] == 0)
            indegree0.push_back(i);
    }


Variable-length arrays

This is a non-standard extension:

int indegree[v+1];


It's not clear why you ignore the 0th element of these arrays; that probably warrants a comment.



Cleaned code

#include &lt;iostream&gt;
#include &lt;list&gt;
#include &lt;vector&gt;

std::vector&lt;int&gt; topologicalsort(const std::vector&lt;std::list&lt;int&gt;&gt;&amp; adjacencylist,
                                 std::vector&lt;int&gt; indegree)
{
    std::vector&lt;int&gt; ans;
    ans.reserve(indegree.size()+1);
    //list to store vertices with indegree 0
    std::list&lt;int&gt; indegree0;
    for (size_t i = 1;  i &lt; indegree.size();  i++) {
        if (indegree[i] == 0)
            indegree0.push_back(i);
    }
    while (!indegree0.empty()) {
        const int x = indegree0.front();
        indegree0.pop_front();
        ans.push_back(x);
        for (const int i: adjacencylist[x]) {
            if (--indegree[i] == 0)
                indegree0.push_back(i);
        }
    }
    return ans;
}

int main()
{
    int v, e;
    std::cin &gt;&gt; v &gt;&gt; e;
    std::vector&lt;std::list&lt;int&gt;&gt; adjacencylist(v+1);
    std::vector&lt;int&gt; indegree(v+1);
    for (int i = 1;  i &lt;= e;  i++) {
        int v1, v2;
        std::cin &gt;&gt; v1 &gt;&gt; v2;
        adjacencylist[v1].push_back(v2);
        ++indegree[v2];
    }
    for (int i = 1;  i &lt;= v;  i++) {
        std::cout &lt;&lt; "adjacencylist[" &lt;&lt; i &lt;&lt; "%d] :";
        for (int j: adjacencylist[i]) {
            std::cout &lt;&lt; j &lt;&lt; "  ";
        }
        std::cout &lt;&lt; std::endl;
    }
    auto const&amp; ans = topologicalsort(adjacencylist, std::move(indegree));
    for (int i: ans)
        std::cout &lt;&lt; i &lt;&lt; "  ";
}