# Printing pre and post visited times in DFS of a graph

Depth First Search (DFS) marks all the vertices of a graph as visited. So for making DFS useful, some additional information can also be stored. For instance, the order in which the vertices are visited while running DFS.

Pre-visit and Post-visit numbers are the extra information that can be stored while running a DFS on a graph and which turns out to be really useful. Pre-visit number tells the time at which the node gets into the recursion stack and Post-visit number tells the time at which the node comes out from recursion stack of DFS.

**Example:**

The numbers written in brackets denote [Pre-visit number, Post-visit number].

PreandPostnumbers are widely used ingraph algorithms. For example, they can be used to find out whether a particularnode lies in the sub-tree of another node.

To find whetherulies in thesub-treeofvor not we just compare the pre and post number of u and v. Ifpre[u] > pre[v]andpost[u] < post[v]thenulies in the sub-tree ofvotherwise not. You can see above example for more clarification.

Pre-visit and Post-visit numbers can be found out by simple DFS. We will take two arrays one for storing pre numbers and one for post numbers and by taking a variable which will keep track of the time. The implementation of the same is given below:

`import` `java.util.*; ` `public` `class` `GFG { ` ` ` ` ` `// Variable to keep track of time ` ` ` `static` `int` `time = ` `1` `; ` ` ` ` ` `// Function to perform DFS starting from node u ` ` ` `static` `void` `dfs(` `int` `u, ArrayList<ArrayList<Integer> > aList, ` ` ` `int` `pre[], ` `int` `post[], ` `int` `vis[]) ` ` ` `{ ` ` ` ` ` `// Storing the pre number whenever ` ` ` `// the node comes into recursion stack ` ` ` `pre[u] = time; ` ` ` ` ` `// Increment time ` ` ` `time++; ` ` ` `vis[u] = ` `1` `; ` ` ` `for` `(` `int` `v : aList.get(u)) { ` ` ` `if` `(vis[v] == ` `0` `) ` ` ` `dfs(v, aList, pre, post, vis); ` ` ` `} ` ` ` ` ` `// Storing the post number whenever ` ` ` `// the node goes out of recursion stack ` ` ` `post[u] = time; ` ` ` `time++; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` ` ` `// Number of nodes in graph ` ` ` `int` `n = ` `6` `; ` ` ` ` ` `// Adjacency list ` ` ` `ArrayList<ArrayList<Integer> > aList ` ` ` `= ` `new` `ArrayList<ArrayList<Integer> >(n + ` `1` `); ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) { ` ` ` `ArrayList<Integer> list = ` `new` `ArrayList<>(); ` ` ` `aList.add(list); ` ` ` `} ` ` ` `aList.add(` `new` `ArrayList<Integer>()); ` ` ` `int` `pre[] = ` `new` `int` `[n + ` `1` `]; ` ` ` `int` `post[] = ` `new` `int` `[n + ` `1` `]; ` ` ` ` ` `// Visited array ` ` ` `int` `vis[] = ` `new` `int` `[n + ` `1` `]; ` ` ` ` ` `// Edges ` ` ` `aList.get(` `1` `).add(` `2` `); ` ` ` `aList.get(` `2` `).add(` `1` `); ` ` ` `aList.get(` `2` `).add(` `4` `); ` ` ` `aList.get(` `4` `).add(` `2` `); ` ` ` `aList.get(` `1` `).add(` `3` `); ` ` ` `aList.get(` `3` `).add(` `1` `); ` ` ` `aList.get(` `3` `).add(` `4` `); ` ` ` `aList.get(` `4` `).add(` `3` `); ` ` ` `aList.get(` `3` `).add(` `5` `); ` ` ` `aList.get(` `5` `).add(` `3` `); ` ` ` `aList.get(` `5` `).add(` `6` `); ` ` ` `aList.get(` `6` `).add(` `5` `); ` ` ` ` ` `// DFS starting at Node 1 ` ` ` `dfs(` `1` `, aList, pre, post, vis); ` ` ` ` ` `// Number of nodes in graph ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) ` ` ` `System.out.println(` `"Node "` `+ i + ` `" Pre number "` ` ` `+ pre[i] + ` `" Post number "` `+ post[i]); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Node 1 Pre number 1 Post number 12 Node 2 Pre number 2 Post number 11 Node 3 Pre number 4 Post number 9 Node 4 Pre number 3 Post number 10 Node 5 Pre number 5 Post number 8 Node 6 Pre number 6 Post number 7

## Recommended Posts:

- Check if a cell can be visited more than once in a String
- Count the total number of squares that can be visited by Bishop in one move
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
- Printing Triangle Pattern in Java
- Java Program for Dijkstra's Algorithm with Path Printing
- Printing Paths in Dijkstra's Shortest Path Algorithm
- Fleury's Algorithm for printing Eulerian Path or Circuit
- String which when repeated exactly K times gives a permutation of S
- Remove elements from the array which appear more than k times
- Find the value of XXXX.....(N times) % M where N is large
- Convert a number of length N such that it contains any one digit at least 'K' times
- Round Robin Scheduling with different arrival times
- First element that appears even number of times in an array
- Largest element in the array that is repeated exactly k times
- Count distinct substrings that contain some characters at most k times

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.