You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于igraph包有向图的技术咨询:如何定义经节点中转的有向边?

Modeling a Directed Edge with a Non-Stop Stopover in igraph

Great question! In igraph, there’s no native construct for a directed edge that explicitly "passes through" an intermediate node without stopping—but we have two practical workarounds to model your train scenario (Spain → Switzerland (no stop) → Russia) perfectly:

Option 1: Add a Custom Edge Attribute

If you just need to record that the edge has a stopover node without modifying the graph’s structure, the simplest approach is to add a custom attribute to the direct Spain → Russia edge. This keeps the graph clean while retaining the stopover information.

Example (Python igraph):

from igraph import Graph

# Initialize directed graph
g = Graph(directed=True)

# Add your three nodes
g.add_vertices(["Spain", "Switzerland", "Russia"])

# Add edge with a custom "via" attribute
g.add_edge("Spain", "Russia", via="Switzerland")

# Access the stopover info
print(g.es[0]["via"])  # Output: Switzerland

Example (R igraph):

library(igraph)

# Initialize directed graph
g <- make_empty_graph(directed = TRUE)
g <- add_vertices(g, 3, name = c("Spain", "Switzerland", "Russia"))

# Add edge with "via" attribute
g <- add_edges(g, c("Spain", "Russia"), attr = list(via = "Switzerland"))

# Access the attribute
E(g)$via  # Output: "Switzerland"

This works well if you don’t need to run path analysis that accounts for the stopover node—you’re just storing metadata about the edge.

Option 2: Split the Path with Non-Stop Node Attributes

If you need to model the stopover in the graph structure (e.g., for path traversal, shortest path calculations), split the journey into two edges (Spain → Switzerland and Switzerland → Russia), then mark the intermediate node as a non-stop stopover using a custom node attribute.

Example (Python igraph):

from igraph import Graph

g = Graph(directed=True)
g.add_vertices(["Spain", "Switzerland", "Russia"])

# Mark Switzerland as a non-stop node
g.vs.find("Switzerland")["is_stop"] = False

# Add the two segments of the journey
g.add_edge("Spain", "Switzerland")
g.add_edge("Switzerland", "Russia")

# When analyzing paths, filter out non-stop nodes
shortest_path = g.get_shortest_paths("Spain", "Russia")[0]
node_names = g.vs[shortest_path]["name"]
stop_nodes = [node for node in node_names if g.vs.find(node).get("is_stop", True)]

print("Stops on the train route:", stop_nodes)  # Output: ['Spain', 'Russia']

This approach is better if you need to perform graph operations that consider the route’s structure—like finding all possible routes, or calculating travel time that accounts for non-stop segments.

Which to Choose?

  • Use Option 1 if you only need to annotate edges with stopover info and don’t need to interact with the intermediate node in graph operations.
  • Use Option 2 if you need the graph structure to reflect the full route, including non-stop stops, for analysis or visualization.

内容的提问来源于stack exchange,提问作者meilirog

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:10:03