
# 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)
}