# Check if the given permutation is a valid DFS of graph

Given a graph with N nodes numbered from 1 to N and M edges and an array of numbers from 1 to N. Check if it is possible to obtain any permutation of array by applying DFS (Depth First Traversal) on given graph.

**Prerequisites : **DFS | Map in CPP

Examples :

Input : N = 3, M = 2 Edges are: 1) 1-2 2) 2-3 P = {1, 2, 3} Output : YESExplanation :Since there are edges between 1-2 and 2-3, therefore we can have DFS in the order 1-2-3 Input : N = 3, M = 2 Edges are: 1) 1-2 2) 2-3 P = {1, 3, 2} Output : NOExplanation :Since there is no edge between 1 and 3, the DFS traversal is not possible in the order of given permutation.

**Possible Permutations ** in which whole graph can be traversed are:

1) 1 2 3

2) 3 2 1

**Approach : ** We assume that the input graph is represented as adjacency list. The idea is to first sort all adjacency lists according to input order, then traverse the given graph starting from first node in given given permutation. If we visit all vertices in same order, then given permutation is a valid DFS.

- Store the indexes of each number in the given permutation in a Hash map.
- Sort every adjacency list according to the indexes of permutation since there is need to maintain the order.
- Perform the Depth First Traversal Search with source node as 1st number of given permutation.
- Keep a counter variable and at every recursive call, check if the counter has reached the number of nodes, i.e. N and set the flag as 1. If the flag is 0 after complete DFS, answer is ‘NO’ otherwise ‘YES’

**Below is the implementation of above approach :**

`// CPP program to check if given ` `// permutation can be obtained ` `// upon DFS traversal on given graph ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// To track of DFS is valid or not. ` `bool` `flag = ` `false` `; ` ` ` `// HashMap to store the indexes ` `// of given permutation ` `map<` `int` `, ` `int` `> mp; ` ` ` `// Comparator function for sort ` `bool` `cmp(` `int` `a, ` `int` `b) ` `{ ` ` ` `// Sort according ascending ` ` ` `// order of indexes ` ` ` `return` `mp[a] < mp[b]; ` `} ` ` ` `// Graph class represents a undirected ` `// using adjacency list representation ` `class` `Graph ` `{ ` ` ` `int` `V; ` `// No. of vertices ` ` ` `int` `counter; ` `// Counter variable ` ` ` `public` `: ` ` ` `// Pointer to an array containing ` ` ` `// adjacency lists ` ` ` `list<` `int` `>* adj; ` ` ` ` ` `Graph(` `int` `V); ` `// Constructor ` ` ` ` ` `// function to add an edge to graph ` ` ` `void` `addEdge(` `int` `u, ` `int` `v); ` ` ` ` ` `// DFS traversal of the vertices ` ` ` `// reachable from v ` ` ` `void` `DFS(` `int` `v, ` `int` `Perm[]); ` `}; ` ` ` `Graph::Graph(` `int` `V) ` `{ ` ` ` `this` `->V = V; ` ` ` `this` `->counter = 0; ` ` ` `adj = ` `new` `list<` `int` `>[V + 1]; ` `} ` ` ` `void` `Graph::addEdge(` `int` `u, ` `int` `v) ` `{ ` ` ` `adj[u].push_back(v); ` `// Add v to u’s list. ` ` ` `adj[v].push_back(u); ` `// Add u to v's list ` `} ` ` ` `// DFS traversal of the ` `// vertices reachable from v. ` `void` `Graph::DFS(` `int` `v, ` `int` `Perm[]) ` `{ ` ` ` `// Increment counter for ` ` ` `// every node being traversed ` ` ` `counter++; ` ` ` ` ` `// Check if counter has ` ` ` `// reached number of vertices ` ` ` `if` `(counter == V) { ` ` ` ` ` `// Set flag to 1 ` ` ` `flag = 1; ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// Recur for all vertices adjacent ` ` ` `// to this vertices only if it ` ` ` `// lies in the given permutation ` ` ` `list<` `int` `>::iterator i; ` ` ` `for` `(i = adj[v].begin(); ` ` ` `i != adj[v].end(); i++) ` ` ` `{ ` ` ` ` ` `// if the current node equals to ` ` ` `// current element of permutation ` ` ` `if` `(*i == Perm[counter]) ` ` ` `DFS(*i, Perm); ` ` ` `} ` `} ` ` ` `// Returns true if P[] is a valid DFS of given ` `// graph. In other words P[] can be obtained by ` `// doing a DFS of the graph. ` `bool` `checkPermutation(` `int` `N, ` `int` `M, ` ` ` `vector<pair<` `int` `, ` `int` `> > V, ` `int` `P[]) ` `{ ` ` ` `// Create the required graph with ` ` ` `// N vertices and M edges ` ` ` `Graph G(N); ` ` ` ` ` `// Add Edges to Graph G ` ` ` `for` `(` `int` `i = 0; i < M; i++) ` ` ` `G.addEdge(V[i].first, V[i].second); ` ` ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `mp[P[i]] = i; ` ` ` ` ` `// Sort every adjacency ` ` ` `// list according to HashMap ` ` ` `for` `(` `int` `i = 1; i <= N; i++) ` ` ` `G.adj[i].sort(cmp); ` ` ` ` ` `// Call DFS with source node as P[0] ` ` ` `G.DFS(P[0], P); ` ` ` ` ` `// If Flag has been set to 1, means ` ` ` `// given permutation is obtained ` ` ` `// by DFS on given graph ` ` ` `return` `flag; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// Number of vertices and number of edges ` ` ` `int` `N = 3, M = 2; ` ` ` ` ` `// Vector of pair to store edges ` ` ` `vector<pair<` `int` `, ` `int` `> > V; ` ` ` ` ` `V.push_back(make_pair(1, 2)); ` ` ` `V.push_back(make_pair(2, 3)); ` ` ` ` ` `int` `P[] = { 1, 2, 3 }; ` ` ` ` ` `// Return the answer ` ` ` `if` `(checkPermutation(N, M, V, P)) ` ` ` `cout << ` `"YES"` `<< endl; ` ` ` `else` ` ` `cout << ` `"NO"` `<< endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

YES

## Recommended Posts:

- Find the maximum value permutation of a graph
- Check if the game is valid or not
- Check if the given Binary Expressions are valid
- Check if any valid sequence is divisible by M
- Check if a given graph is Bipartite using DFS
- Check whether a given graph is Bipartite or not
- Check for star graph
- Check if a given graph is tree or not
- Check if a king can move a valid move or not when N nights are there in a modified chessboard
- Check if the given graph represents a Bus Topology
- Check if a given tree graph is linear or not
- Check if a directed graph is connected or not
- Minimum number of given operations required to convert a permutation into an identity permutation
- Check if removing a given edge disconnects a graph
- Check if the given graph represents a Star Topology

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.