Check if the given graph represents a Ring Topology
Given a graph G, the task is to check if it represents a Ring Topology.
A Ring Topology is the one shown in the image below:
Input : Graph =
Output : YES Input : Graph =
Output : NO
A graph of V vertices represents a Ring topology if it satisfies the following three conditions:
- Number of vertices >= 3.
- All vertices should have degree 2.
- No of edges = No of Vertices.
The idea is to traverse the graph and check if it satisfies the above three conditions. If yes, then it represents a Ring Topology otherwise not.
Below is the implementation of the above approach:
Time Complexity: O(V + E) where V and E are the numbers of vertices and edges in the graph respectively.
Auxiliary Space: O(V + E).