用于减少出度为1的序列中边缘的算法?

时间:2016-03-16 19:10:56

标签: r igraph

我正在使用GTFS标准中的公共交通数据,并且已经建立了原始停靠点边缘列表,以便在整个路线上按顺序停止目标。我在下面放了一些示例R代码来显示数据和图形的样本。

library(igraph)

# edgelist with two nodes with outdegree > 1. 
edgelist <- data.frame(source = c("Z","A", "B", "C", "D", "E", "F", "F", "A"),
                       target = c("A","B", "C", "D", "E", "F", "G", "H", "I"),
                       edge_sequence = c(0,1, 2, 3, 4, 5, 6, NA , NA),
                       source_node_out_degree = c(1,1, 1, 1, 1, 1, 2, 2, 2),
                       group = factor(c(1,1,1,1,1,1,1,2,2)))

# i would like to remove edges within my sequence that have an outdegree of 
# one and merge the original source with the 
plot(graph.data.frame(edgelist), edge.arrow.size = 0.3)

以下是我想要生成的边缘列表。在这个例子中,我减少了与A-> F的连接,因为它是a。顺序和b。只有out为1的节点在A和F之间。

# the expected edgelist after simplifying the network. Connecting nodes that 
# have outdegree > 1 on the sequence of edges. 
new_expected_edgelist <- data.frame(source = c("Z","A", "F", "F", "A"),
                                    target = c("A","F", "G", "H", "I"))

# edges with outdegree == 1 have been reduced. 
plot(graph.data.frame(new_expected_edgelist), edge.arrow.size = 0.3)

这样的应用将允许我简化我的网络,以仅可视化多个公共交通路线之间共享的边缘。一些路由延伸到与任何其他站点的连接之外的许多站点,并且使得可视化网络的复杂性变得更加困难。

1 个答案:

答案 0 :(得分:1)

您可以使用contract.vertices命令:

g<-graph.data.frame(edgelist)
h<-contract.vertices(g,c(1,2,3,3,3,3,3,8,9,10))