def generate_network(root_id, upstreams):
    """Generate the upstream network from a starting id.

    Intended to be used within an .apply() call
    
    Parameters
    ----------
    root_id : id type (int, str)
        starting segment id that forms the root node of the upstream network
    upstreams : dict
        Dictionary created from Pandas groupby().groups - keys are downstream_ids, values are upstream_ids
    
    Returns
    -------
    list of all upstream ids in network traversing upward from root_id
    """
    network = [root_id]
    ids = [root_id]
    while len(ids):
        upstream_ids = []
        for id in ids:
            upstream_ids.extend(upstreams.get(id, []))
        ids = upstream_ids
        network.extend(ids)
    return network
