Lets take a variable visited, which will store the nodes which we have already visited
In DFS, we start with 8, we push it on to stack, and mark it visited, so stack will contain
Visited : 8
From 8 we start to look for adjacent nodes, and push next adjacent onto the stack, suppose we choose 4, we mark 4 as visited, so stack will contains
Visited : 8 4
From 4 we look at its adjacent, 2 and 8 are its adjacent, since 8 is already visited, we ignore it. 2 is not visited we pushed it onto stack, and marked 2 as visited
Visited : 8 4 2
From 2 we look at adjacent nodes, we have 4 5 and 1, since 4 is already visited we ignore it, Lets say we choose 1 as next node, we are going to pushed it onto stack and mark it visited.
Visited : 8 4 2 1
From 1 adjacent nodes are 2 and 3, 2 is already visited, so we push 3 onto the stack and marked it as visited.
Visited : 8 4 2 1 3
From 3 adjacent nodes are 1, 6 and 7, 1 is already visited and suppose we choose 7 as next node, we mark it as visited, so stack will contain
Visited : 8 4 2 1 3 7
Now, adjacent nodes of 7 are 3 and 8, both of them are already visited, its time to backtrack, we pop 7 from stack
Now top of the stack is 3, 6 is the only adjacent node that is not visited, so we push it on to the stack.
Visited : 8 4 2 1 3 7 6
All the adjacent node of 6 is already visited, so we pop 6 from the stack, Then Top of the stack is 3, and all the adjacent nodes of 3 is already visited, we pop it from the stack, Next top of the stack is 1 all of its adjacent nodes are already visited, so we pop 1 from the stack, next is 2 one of its adjacent node is not visited i.e 5 so we push it onto the stack
Visited : 8 4 2 1 3 7 6
So now every node is visited, we pop 5, we pop 2, we pop 4, we pop 8
Now the sequence in which we push nodes onto the stack is : 8 -> 4 -> 2 -> 1 -> 3 -> 7 -> 6 -> 5
This is just one sequence, if we consider this sequence our answer would be B i.e 6 7 5
But problem with this question is, there are multiple way in which we can choose nodes like, from 8 we could have choose anyone of 4 or 5 or 6 or 7, its now a rule we have to choose only 4 .
Another possible sequence in which we push nodes onto the stack is : 8 -> 4 -> 2 -> 1 -> 3 -> 6 -> 7 -> 5
If we consider this sequence our answer would be D i.e none of the above
Another possible sequence is 8 -> 7 - > 3 -> 1 -> 2 -> 4 -> 5 -> 6
If we consider this sequence our answer would be A i.e 4,7
I find this question ambiguous, answer would depend upon how our algorithm would choose the sequence.
Also one more possibility is question might be asking about the sequence of vertex which might never be pushed onto the stack, Here since graph is connected and undirected, so every node is going to push onto the stack. In that case answer would be D none of the above.