r - Using igraph/network packages, how do I determine if network is fully connected (each vertex can reach each other vertex somehow)
问题描述
I have a table of Inbound/Outbound flights and travel times. I need to determine if the flight network is "fully connected", meaning that you can get from every city to every other city somehow. It doesn't matter how many flights it takes (whether a direct flight, or multiple flights).
There are ~58 flight locations in total, and ~438 different combinations (not sure if it matters or not). This is a sample of the data:
╔═════════╦══════════╦═════════╗
║ Inbound ║ Outbound ║ Minutes ║
╠═════════╬══════════╬═════════╣
║ ACY ║ ATL ║ 102 ║
╠═════════╬══════════╬═════════╣
║ ACY ║ FLL ║ 136 ║
╠═════════╬══════════╬═════════╣
║ ACY ║ MCO ║ 122 ║
╠═════════╬══════════╬═════════╣
║ ACY ║ MYR ║ 90 ║
╠═════════╬══════════╬═════════╣
║ ACY ║ RSW ║ 137 ║
╠═════════╬══════════╬═════════╣
║ ACY ║ TPA ║ 129 ║
╠═════════╬══════════╬═════════╣
║ ATL ║ ACY ║ 102 ║
╠═════════╬══════════╬═════════╣
║ ATL ║ BOS ║ 132 ║
╚═════════╩══════════╩═════════╝
This is what I have set up so far:
data <- read.table(file="filename.txt", header=TRUE, sep = "\t")
colnames(data) <- c("Inbound", "Outbound", "Minutes")
# Make a directed igraph object from inbound/outbound columns
graph <- graph_from_data_frame(data[, c("Inbound", "Outbound")], directed=TRUE,vertices=NULL)
# Make an edgelist from the igraph object
edgelist <- as_edgelist(graph, names=T)
# Make network object from edgelist
network <- as.network(edgelist)
# Use Minutes as weight for edges
E(graph)$weight <- data$Minutes
Then (I THINK) I have code that properly gets the shortest path from each city to every other city:
shortestPaths <- do.call(c, lapply(V(graph), function(v) get.shortest.paths(graph,v,V(graph), weights=weights, output='epath')$epath))
But how do I find the pairs of airports that can't reach each other using only flights? (As in, no direct/layover flights to connect them)
EDIT So I have made some progress, but it isn't 100% what I need.
Using this code, I get the total time for each path (from one city to another):
shortestPathsTimes <- do.call(c, lapply(V(graph),
function(v) shortest.paths(graph,v,V(graph),
weights=weights)))
I added a couple of fake lines that don't have any shared inbound/outbound. I discovered that the code above returns "inf" any time a path isn't connected. So using this:
noPath <- shortestPathsTimes[which(is.infinite(shortestPathsTimes))]
Returns output like this (basically each location has 2 Inf, one for each of the 2 fake locations I created).
+===============================================================================+
| ACY59 ACY60 ATL59 ATL60 AUA59 AUA60 AXM59 AXM60 BDL59 BDL60 BOG59 BOG60 |
+===============================================================================+
| Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf Inf |
+-------------------------------------------------------------------------------+
HOWEVER, what I need is the actual text path shown, like how get.shortest.paths
outputs. So for example, I need it to just return something like ACY=>TESTIN, ACY=>TESTOUT, ATL=>TESTIN, ATL=>TESTOUT, and so on.
解决方案
推荐阅读
- azure - Cordapps 对等方列表在 azure corda 部署中返回自己的节点信息
- angular - Angular 5 - 将数据从按钮动态传递到组件
- python - 如何从用户那里获取多个输入?
- google-maps - 如何根据谷歌自动完成中的类型更改图标(pac-icon)图像
- maven - 我们可以为 Maven 插件使用依赖版本范围吗?
- java - 使用科尔多瓦时出现 JAVA_HOME 错误
- android - 即使设备被锁定,如何通过服务捕获图像并录制声音?
- php - 无法在php中运行flyway命令
- angular - 如何在 nativescript angular 中创建休息服务
- oracle - SQL Server 2012 Management Studio 提供程序未在 Windows Server 2012 上显示 OraOLEDB.Oracle