#' @title Keep latest node
#' @description From a lists of nodenames of an igraph object, the function returns the last node created. 
#' @param known_names A character vector containing names of nodes to be searched in the graph.
#' @param graph The graph where the nodes are located.
#' @return The name of the latest node found in the graph from the list of known names.

# Function to keep only the name with the highest position
keep_latest_node <- function(known_names, graph) {
  max_index <- -1
  max_name <- NULL
  
  for (name in known_names) {
    if (name %in% V(graph)$name) {
      node_index <- which(V(graph)$name == name)
      if (node_index > max_index) {
        max_index <- node_index
        max_name <- name
      }
    }
  }
  
  return(max_name)
}