RAMP Consulting Group needs to identify circular dependencies in a directed graph of microservices. Each edge A -> B means service A calls or depends on service B.
Implement a function that finds every cyclic group of services and returns all services participating in at least one cycle. A cyclic group is a strongly connected component containing at least two services, or a single service with a self-loop. Treat each cyclic strongly connected component as one cycle group. Results must be deterministic: sort service names within each group, sort groups by their first service name, and sort the complete anomalous-service list.
Input graph is a dictionary mapping a service name to a list of directly dependent service names. A service may appear only as a dependency and need not be a dictionary key. Service names are non-empty strings. Return a dictionary with:
cycles: a list of sorted lists, one for each cyclic strongly connected component.anomalous_nodes: a sorted list containing the union of all services in cycles.Do not mutate the input graph.
def analyze_service_cycles(graph):