Detecting a cycle in an undirected graph and printing the vertices if there is a cycle, printing no cycle otherwise Problem: Find an O(E + V) time algorithm that outputs the vertices of a cycle of G, if it exists. If G has no cycles, the algorithm outputs no cycle. So if there is a cycle 1, ... 3, 1, it would print 1, ..., 3, 1.

So I was thinking of doing a BFS, and if the currently examined vertex's neighbor v has been visited prior, then there is a cycle. However, I am not sure how I might go about tracking the vertices in order to print. I believe I would have to view the BFS-Tree, but unsure.
You've basically got it!

The usual way to record the cycle is to mark the predecessor or parent of each node: that is, the node from which it was first visited. Then you can follow the trail of parents to print out the cycle.