Prepared by Volkan OBAN
optrees package in R
Finds optimal trees in weighted graphs. In particular, this package provid
es solving tools for minimum costspanning tree problems,minimum cost
arborescenceproblems,shortestpath tree problems and minimum cut tr
ee problem.
Examples:
Minimum Spanning Tree
>nodes <- 1:4
> arcs <- matrix(c(1,2,2, 1,3,15, 1,4,3, 2,3,1, 2,4,9, 3,4,1),
+ ncol = 3, byrow = TRUE)
> # Minimum cost spanning tree with several algorithms
> getMinimumSpanningTree(nodes, arcs, algorithm = "Prim")
Minimum Cost Spanning Tree
Algorithm: Prim
Stages: 3 | Time: 0
------------------------------
ept1 ept2 weight
1 2 2
2 3 1
3 4 1
------------------------------
Total = 4
> getMinimumSpanningTree(nodes, arcs, algorithm = "Kruskal")
Minimum Cost Spanning Tree
Algorithm: Kruskal
Stages: 3 | Time: 0
------------------------------
ept1 ept2 weight
2 3 1
3 4 1
1 2 2
------------------------------
Total = 4
> getMinimumSpanningTree(nodes, arcs, algorithm = "Boruvka")
Minimum Cost Spanning Tree
Algorithm: Boruvka
Stages: 1 | Time: 0
------------------------------
ept1 ept2 weight
1 2 2
2 3 1
3 4 1
------------------------------
Total = 4
Shortest Path Tree:
> nodes <- 1:5
> arcs <- matrix(c(1,2,2, 1,3,2, 1,4,3, 2,5,5, 3,2,4, 3,5,3, 4,3,1, 4,5,0),
+ ncol = 3, byrow = TRUE)
> # Shortest path tree
> getShortestPathTree(nodes, arcs, algorithm = "Dijkstra", directed=FALSE)
Shortest path tree
Algorithm: Dijkstra
Stages: 4 | Time: 0
------------------------------
ept1 ept2 weight
1 2 2
1 3 2
1 4 3
4 5 0
------------------------------
Total = 7
Distances from source:
------------------------------
source node distance
1 2 2
1 3 2
1 4 3
1 5 3
------------------------------
> getShortestPathTree(nodes, arcs, algorithm = "Bellman-Ford", directed=FAL
SE)
Shortest path tree
Algorithm: Bellman-Ford
Stages: 4 | Time: 0
------------------------------
ept1 ept2 weight
1 2 2
1 3 2
1 4 3
4 5 0
------------------------------
Total = 7
Distances from source:
------------------------------
source node distance
1 2 2
1 3 2
1 4 3
1 5 3
------------------------------
> # Graph with negative weights
> nodes <- 1:5
> arcs <- matrix(c(1,2,6, 1,3,7, 2,3,8, 2,4,5, 2,5,-4, 3,4,-3, 3,5,9, 4,2,-
2,
+ 5,1,2, 5,4,7), ncol = 3, byrow = TRUE)
> # Shortest path tree
> getShortestPathTree(nodes, arcs, algorithm = "Bellman-Ford", directed=TRU
E)
Shortest path tree
Algorithm: Bellman-Ford
Stages: 4 | Time: 0.09
------------------------------
head tail weight
1 3 7
2 5 -4
3 4 -3
4 2 -2
------------------------------
Total = -2
Distances from source:
------------------------------
source node distance
1 2 2
1 3 7
1 4 4
1 5 -2
------------------------------
Maximum flow:
> # Graph
> nodes <- 1:6
> arcs <- matrix(c(1,2,1, 1,3,7, 2,3,1, 2,4,3, 2,5,2, 3,5,4, 4,5,1, 4,6,6,
+ 5,6,2), byrow = TRUE, ncol = 3)
> # Maximum flow with Ford-Fulkerson algorithm
> maxFlowFordFulkerson(nodes, arcs, source.node = 2, sink.node = 6)
$s.cut
[1] 2 3 1 5
$t.cut
[1] 4 6
$max.flow
[1] 6

"optrees" package in R and examples.(optrees:finds optimal trees in weighted graphs)

  • 1.
    Prepared by VolkanOBAN optrees package in R Finds optimal trees in weighted graphs. In particular, this package provid es solving tools for minimum costspanning tree problems,minimum cost arborescenceproblems,shortestpath tree problems and minimum cut tr ee problem. Examples: Minimum Spanning Tree >nodes <- 1:4 > arcs <- matrix(c(1,2,2, 1,3,15, 1,4,3, 2,3,1, 2,4,9, 3,4,1), + ncol = 3, byrow = TRUE) > # Minimum cost spanning tree with several algorithms > getMinimumSpanningTree(nodes, arcs, algorithm = "Prim") Minimum Cost Spanning Tree Algorithm: Prim Stages: 3 | Time: 0 ------------------------------ ept1 ept2 weight 1 2 2 2 3 1 3 4 1 ------------------------------ Total = 4 > getMinimumSpanningTree(nodes, arcs, algorithm = "Kruskal") Minimum Cost Spanning Tree Algorithm: Kruskal Stages: 3 | Time: 0
  • 2.
    ------------------------------ ept1 ept2 weight 23 1 3 4 1 1 2 2 ------------------------------ Total = 4 > getMinimumSpanningTree(nodes, arcs, algorithm = "Boruvka") Minimum Cost Spanning Tree Algorithm: Boruvka Stages: 1 | Time: 0 ------------------------------ ept1 ept2 weight 1 2 2 2 3 1 3 4 1 ------------------------------ Total = 4 Shortest Path Tree: > nodes <- 1:5 > arcs <- matrix(c(1,2,2, 1,3,2, 1,4,3, 2,5,5, 3,2,4, 3,5,3, 4,3,1, 4,5,0), + ncol = 3, byrow = TRUE) > # Shortest path tree > getShortestPathTree(nodes, arcs, algorithm = "Dijkstra", directed=FALSE) Shortest path tree Algorithm: Dijkstra Stages: 4 | Time: 0 ------------------------------ ept1 ept2 weight 1 2 2 1 3 2 1 4 3 4 5 0 ------------------------------ Total = 7 Distances from source: ------------------------------ source node distance 1 2 2 1 3 2 1 4 3 1 5 3 ------------------------------ > getShortestPathTree(nodes, arcs, algorithm = "Bellman-Ford", directed=FAL SE) Shortest path tree
  • 3.
    Algorithm: Bellman-Ford Stages: 4| Time: 0 ------------------------------ ept1 ept2 weight 1 2 2 1 3 2 1 4 3 4 5 0 ------------------------------ Total = 7 Distances from source: ------------------------------ source node distance 1 2 2 1 3 2 1 4 3 1 5 3 ------------------------------ > # Graph with negative weights > nodes <- 1:5 > arcs <- matrix(c(1,2,6, 1,3,7, 2,3,8, 2,4,5, 2,5,-4, 3,4,-3, 3,5,9, 4,2,- 2, + 5,1,2, 5,4,7), ncol = 3, byrow = TRUE) > # Shortest path tree > getShortestPathTree(nodes, arcs, algorithm = "Bellman-Ford", directed=TRU E) Shortest path tree Algorithm: Bellman-Ford Stages: 4 | Time: 0.09 ------------------------------ head tail weight 1 3 7 2 5 -4 3 4 -3 4 2 -2 ------------------------------ Total = -2 Distances from source: ------------------------------ source node distance 1 2 2 1 3 7 1 4 4 1 5 -2 ------------------------------ Maximum flow: > # Graph > nodes <- 1:6 > arcs <- matrix(c(1,2,1, 1,3,7, 2,3,1, 2,4,3, 2,5,2, 3,5,4, 4,5,1, 4,6,6, + 5,6,2), byrow = TRUE, ncol = 3) > # Maximum flow with Ford-Fulkerson algorithm
  • 4.
    > maxFlowFordFulkerson(nodes, arcs,source.node = 2, sink.node = 6) $s.cut [1] 2 3 1 5 $t.cut [1] 4 6 $max.flow [1] 6