Question

(10) Sketch an algorithm, in pseudocode, to find and return a shortest cycle in a graph, if one exists. You may return null o

0 0
Add a comment Improve this question Transcribed image text
Answer #1

We have the assumption that the graph is directed, unweighted and connected.

Depth frst traversal can be used to detect a cycle in the graph. DFS for a connected graph produces a tree. There is a cycle in a graph only if there is a back edge present in the graph.( back edge means loop in the graph from a particular node to itself).

Algorithm/ pseudocode:

1. Create the graph along with allowed directions.

2. Create a recursive function that initializes current index/ vertex and one recursion stack.

3. Mark the current node as visited and also mark the index in the stack.

4. Find all the vertices which are not visited( in the direction allowed by the graph). Recursively, call the function for those vertices, if the recursive function returns true, return true.

5. If the adjacent vertices are already marked in the recursion stack then return true.

6. Create a wrapper class, That calls the recursive function for all the vertices and if any function returns true, return true. Else if for all vertices the function returns false return false.

Add a comment
Know the answer?
Add Answer to:
(10) Sketch an algorithm, in pseudocode, to find and return a shortest cycle in a graph,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You are managing a large scale construction project with hundreds of subprojects, some which have to...

    You are managing a large scale construction project with hundreds of subprojects, some which have to be completed before others can begin whereas some subprojects can be carried out simultaneously. The project and its subprojects, and the presence of dependencies between subprojects (which subprojects have to be done before which), can be modeled as a connected unweighted directed graph with nodes representing subprojects and directed edges representing dependencies. 42. Which of the following algorithms will allow you to determine if...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT