GATE CSE
First time here? Checkout the FAQ!
x
+2 votes
248 views

 

How many times 8 is pushed into stack ? a detail answer will be welcomed.

asked in Programming by Active (2.2k points)  
edited by | 248 views
What is the starting vertex of DFS?? SInce answer will differ based on this..
should be 1

Habibkhan . for the first part what will be the ans . According to me all vertices will pushed in to stack .

Why will a vertex be added more than once in the stack? https://www.tutorialspoint.com/data_structures_algorithms/depth_first_traversal.htm I checked and I still don't get how any node will be added more than once. Also, all nodes will be added in the stack. How can there be some nodes which are not added in the stack?

1 Answer

+5 votes
Best answer

Given graph according to adjacency list . Answer will be 3.

 Solution :

X means already visted  and Down arrow means need to check.

answered by Veteran (47.3k points)  
selected by

Every element is pushed onto stack only once, because in typical DFS implementation we check whether a vertex is visted or not befor calling dfs.

if(visited[i]==0) dfs(i)

If dfs(8) is excuted once means vertex-8 is pushed once, then next time visited[8]=1. And condition evaluates to be false and dfs is not called on vertex 8.

And What will be answer for part-1 of question?

@Anirudh sir What will be answer for part-1 of question?
Ideally all will be pushed, but since that is not the option, so i guess it is option 1.


Top Users May 2017
  1. akash.dinkar12

    3338 Points

  2. pawan kumarln

    2108 Points

  3. Bikram

    1922 Points

  4. sh!va

    1682 Points

  5. Arjun

    1614 Points

  6. Devshree Dubey

    1272 Points

  7. Debashish Deka

    1208 Points

  8. Angkit

    1056 Points

  9. LeenSharma

    1018 Points

  10. Arnab Bhadra

    812 Points

Monthly Topper: Rs. 500 gift card
Top Users 2017 May 22 - 28
  1. Bikram

    1008 Points

  2. pawan kumarln

    734 Points

  3. Arnab Bhadra

    726 Points

  4. Arjun

    342 Points

  5. bharti

    328 Points


22,893 questions
29,196 answers
65,302 comments
27,695 users