Home > Uncategorized > Determine cycle in undirected graph in O(v) time.

Determine cycle in undirected graph in O(v) time.

Observation says, an undirected graph G = ( V,E)  will have at most |v-1| edges for it to be acyclic. So, in any undirected graph, if we find more than |v-1| edges we can say that it has a cycle.

So, the solution is :

1. Run DFS on the graph G .

2. It it finds more than |v-1| edges stop the algorithm and assert a cycle.

 

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: