import itertools


def create_deduped_state_road_graph(graph_st, comps_dict, remove_comp_ids):
    """
    Creates a single graph with all state roads deduped of parallel one way roads with the same name
    Args:
        graph_st (networkx graph): with all state roads
        comps_dict (dict): mapping from graph id to component (graph)
        remove_comp_ids (list[int]): list of components (by id) to remove from `graph_st`.  These are the parallel one
         way edges and nodes left without any incident edges.
    Returns:
        NetworkX graph all state roads deduped for parallel one way roads
    """

    graph_st_deduped = graph_st.copy()

    # actually remove dupe one-way edges from g_st
    comps2remove = list(itertools.chain(*remove_comp_ids.values()))
    for cid in comps2remove:
        comp = comps_dict[cid]
        graph_st_deduped.remove_nodes_from(comp.nodes(), )

    # remove nodes w no edges
    remove_island_nodes = []
    for node in graph_st_deduped.nodes():
        if graph_st_deduped.degree(node) == 0:
            remove_island_nodes.append(node)
    graph_st_deduped.remove_nodes_from(remove_island_nodes)

    return graph_st_deduped