GeeksforGeeks » Interview Questions

Yahoo Interview Question for Software Engineer/Developer (Fresher)

(3 posts)

Tags:

  1. Algoseekar
    guest
    Posted 1 year ago #

    Write a Complete program check whether Graph is Connected or Not & also for whether Graph is Tree or not.

  2. kartik
    Moderator
    Posted 1 year ago #

    Algorithm to check whether a graph is connected or not

    1) Mark all the vertices as not visited. (Make a bool array)
    2) Pick any vertex as source, call BFS traversal for the graph taking the picked vertex as source and construct the BFS tree. While constructing the BFS tree, keep marking the vertices as visited.
    3) After BFS Treversal, If you see any vertex which is not marked visited, then graph is not connected.

    We can also use DFS instead of BFS.

  3. kartik
    Moderator
    Posted 1 year ago #

    Algorithm for Cycle Detection in Graph

    The standard way to recognize cycles in a graph is to do a depth-first search, marking vertices along the way. If you hit a vertex that you have already marked, then you have a cycle.

    See following link for implementation.
    http://web.eecs.utk.edu/~parker/Courses/CS302-fall05/Notes/GraphIntro/


Reply

You must log in to post.

RSS feed for this topic