Longest way for all vertex
Few habits to learn early in life.


passing big arguments by references. ( bool&amp; visited[] )
deleting unused memory. (delete visited)
proper indentation always makes it better.


Here is the complete code.

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

class Graph
{
    int V;
    list&lt;int&gt; *adj;
    void DFSUtil(int v, bool&amp; visited[]);
    public:
    Graph(int V);
    void addEdge(int v, int w);
    void DFS(int v);
};

Graph::Graph(int V)
{
    this-&gt;V = V;
    adj = new list&lt;int&gt;[V];
}

void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w);
}

void Graph::DFSUtil(int v, bool&amp; visited[])
{
    visited[v] = true;
    cout &lt;&lt; v &lt;&lt; " ";
    list&lt;int&gt;::iterator i;
    for (i = adj[v].begin(); i != adj[v].end(); ++i)
        if (!visited[*i])
            DFSUtil(*i, visited);
}

void Graph::DFS(int v)
{
    bool *visited = new bool[V];
    for (int i = 0; i &lt; V; i++)
        visited[i] = false;
    DFSUtil(v, visited);
    delete visited; // avoids memory leaks
}

int main()
{
    Graph g(4);
    g.addEdge(0, 1);
    g.addEdge(0, 2);
    g.addEdge(1, 2);
    g.addEdge(2, 0);
    g.addEdge(2, 3);
    g.addEdge(3, 3);

    for(int i = 0; i &lt; 4; i++) {
        g.DFS(i);
        puts("");
    }
    return 0;
}


Apart from these your code is very optimized possible.