def adj_matrix_to_edge_list(adj_matrix, directed=True, first_id=0, weighted=False):
    num_nodes = adj_matrix.shape[0]
    if directed:
        num_edges = np.sum(adj_matrix)
    else:
        num_edges = int((np.sum(adj_matrix) / 2))
    if weighted:
        edge_list = np.zeros([num_edges, 3], dtype=np.int32)
    else:
        edge_list = np.zeros([num_edges, 2], dtype=np.int32)
    i = 0
    for node_in in range(num_nodes):
        if directed:
            range_2 = range(num_nodes)
        else:
            range_2 = range((node_in + 1), num_nodes)
        for node_out in range_2:
            edge_val = adj_matrix[(node_in, node_out)]
            if (edge_val > 0):
                if weighted:
                    edge_list[i] = ((node_in + first_id), (node_out + first_id), edge_val)
                else:
                    edge_list[i] = ((node_in + first_id), (node_out + first_id))
                i += 1
    return edge_list
def adj_matrix_to_edge_list(adj_matrix, directed=True, first_id=0, weighted=False):
    num_nodes = adj_matrix.shape[0]
    if directed:
        num_edges = np.sum(adj_matrix)
    else:
        num_edges = int((np.sum(adj_matrix) / 2))
    if weighted:
        edge_list = np.zeros([num_edges, 3], dtype=np.int32)
    else:
        edge_list = np.zeros([num_edges, 2], dtype=np.int32)
    i = 0
    for node_in in range(num_nodes):
        if directed:
            range_2 = range(num_nodes)
        else:
            range_2 = range((node_in + 1), num_nodes)
        for node_out in range_2:
            edge_val = adj_matrix[(node_in, node_out)]
            if (edge_val > 0):
                if weighted:
                    edge_list[i] = ((node_in + first_id), (node_out + first_id), edge_val)
                else:
                    edge_list[i] = ((node_in + first_id), (node_out + first_id))
                i += 1
    return edge_list