fn is_cyclic(vertices: &HashMap<u64, DeviceVertex>) -> bool {
    enum State {
        Unprocessed,
        Processing,
        Processed,
    }

    // Process a vertex and its children for cycles.
    // Returns true if there's a cycle and false if there is not.
    fn process_vertex(
        id: u64,
        vertices: &HashMap<u64, DeviceVertex>,
        states: &mut HashMap<u64, State>,
    ) -> bool {
        states.insert(id, State::Processing);
        if let Some(vertex) = vertices.get(&id) {
            for child_id in vertex.child_ids.iter() {
                match states.get(child_id) {
                    // We found another Node that's processing which means
                    // there's a cycle.
                    Some(State::Processing) => {
                        return true;
                    }
                    Some(State::Unprocessed) => {
                        // Check our unprocessed child for cycles and return if
                        // we find one.
                        if process_vertex(*child_id, vertices, states) {
                            return true;
                        }
                    }
                    _ => {}
                }
            }
        }

        // We have not found any cycles so mark Node as processed.
        states.insert(id, State::Processed);
        false
    }

    // Key: ID of device vertex
    let mut states: HashMap<u64, State> =
        vertices.keys().map(|id| (*id, State::Unprocessed)).collect();
    for id in vertices.keys() {
        if let Some(State::Unprocessed) = states.get(id) {
            if process_vertex(*id, &vertices, &mut states) {
                return true;
            }
        }
    }
    false
}
// DFS approach to detect cycles.
// `vertices` acts similar to an adjacency matrix.
DFS approach to detect cycles.
`vertices` acts similar to an adjacency matrix.
DFS approach to detect cycles . ` vertices ` acts similar to an adjacency matrix .
fn is_cyclic(vertices: &HashMap<u64, DeviceVertex>) -> bool {
    enum State {
        Unprocessed,
        Processing,
        Processed,
    }
    // Process a vertex and its children for cycles.
    // Returns true if there's a cycle and false if there is not.
    fn process_vertex(
        id: u64,
        vertices: &HashMap<u64, DeviceVertex>,
        states: &mut HashMap<u64, State>,
    ) -> bool {
        states.insert(id, State::Processing);
        if let Some(vertex) = vertices.get(&id) {
            for child_id in vertex.child_ids.iter() {
                match states.get(child_id) {
                    // We found another Node that's processing which means
                    // there's a cycle.
                    Some(State::Processing) => {
                        return true;
                    }
                    Some(State::Unprocessed) => {
                        // Check our unprocessed child for cycles and return if
                        // we find one.
                        if process_vertex(*child_id, vertices, states) {
                            return true;
                        }
                    }
                    _ => {}
                }
            }
        }
        // We have not found any cycles so mark Node as processed.
        states.insert(id, State::Processed);
        false
    }
    // Key: ID of device vertex
    let mut states: HashMap<u64, State> =
        vertices.keys().map(|id| (*id, State::Unprocessed)).collect();
    for id in vertices.keys() {
        if let Some(State::Unprocessed) = states.get(id) {
            if process_vertex(*id, &vertices, &mut states) {
                return true;
            }
        }
    }
    false
}
fn is_cyclic ( vertices : & HashMap < u64 , DeviceVertex > ) -> bool { enum State { Unprocessed , Processing , Processed , } // Process a vertex and its children for cycles. // Returns true if there's a cycle and false if there is not. fn process_vertex ( id : u64 , vertices : & HashMap < u64 , DeviceVertex > , states : & mut HashMap < u64 , State > , ) -> bool { states . insert ( id , State :: Processing ) ; if let Some ( vertex ) = vertices . get ( & id ) { for child_id in vertex . child_ids . iter ( ) { match states . get ( child_id ) { // We found another Node that's processing which means // there's a cycle. Some ( State :: Processing ) => { return true ; } Some ( State :: Unprocessed ) => { // Check our unprocessed child for cycles and return if // we find one. if process_vertex ( * child_id , vertices , states ) { return true ; } } _ => { } } } } // We have not found any cycles so mark Node as processed. states . insert ( id , State :: Processed ) ; false } // Key: ID of device vertex let mut states : HashMap < u64 , State > = vertices . keys ( ) . map ( | id | ( * id , State :: Unprocessed ) ) . collect ( ) ; for id in vertices . keys ( ) { if let Some ( State :: Unprocessed ) = states . get ( id ) { if process_vertex ( * id , & vertices , & mut states ) { return true ; } } } false }
DFS approach to detect cycles.
DFS approach to detect cycles .
vertices
&HashMap<u64, DeviceVertex>
vertices
&HashMap<u64, DeviceVertex>