Skip to content

Commit

Permalink
Merge pull request #10 from guissalustiano/feat/adds_util_topology_me…
Browse files Browse the repository at this point in the history
…thods

add utils methods to the topology
  • Loading branch information
digizeph authored Feb 3, 2024
2 parents ddda736 + 7eb22eb commit d1887d3
Show file tree
Hide file tree
Showing 5 changed files with 360 additions and 192 deletions.
39 changes: 2 additions & 37 deletions examples/all_paths.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use petgraph::graph::NodeIndex;
use valley_free::{RelType, Topology};

type GraphPath = Vec<NodeIndex>;

fn main() {
let topo = Topology::from_edges(vec![
(1, 2, RelType::ProviderToCustomer),
Expand All @@ -12,41 +9,9 @@ fn main() {
(3, 4, RelType::ProviderToCustomer),
]);

let start = 4;
let topo = topo.paths_graph(start);

let start_idx = topo.index_of(start).unwrap(); // Node index
let mut stack: Vec<(NodeIndex, GraphPath)> = vec![(start_idx, vec![start_idx])];
let mut visited: Vec<NodeIndex> = vec![];
let mut all_paths: Vec<GraphPath> = vec![];

while !stack.is_empty() {
let (node_idx, path) = stack.pop().unwrap();

if visited.contains(&node_idx) {
continue;
}

visited.push(node_idx);
all_paths.push(path.clone());

let childrens = topo
.graph
.neighbors_directed(node_idx, petgraph::Direction::Outgoing)
.map(|child_idx| {
let mut path = path.clone();
path.push(child_idx);
(child_idx, path)
});
stack.extend(childrens);
}

for path in all_paths {
let path = path
.iter()
.map(|node_idx| topo.asn_of(*node_idx))
.collect::<Vec<_>>();
let topo = topo.valley_free_of(4);

for path in topo.path_to_all_ases().unwrap() {
println!("{:?}", path);
}
}
17 changes: 2 additions & 15 deletions examples/all_paths_between_two_ases.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::fs::File;

use petgraph::algo::all_simple_paths;
use valley_free::Topology;

fn main() {
Expand All @@ -9,22 +8,10 @@ fn main() {

let university_of_twente_asn = 1133;
let universidade_de_sao_paulo_asn = 28571;
let ut_path = topo.paths_graph(university_of_twente_asn);

let paths = all_simple_paths::<Vec<_>, _>(
&ut_path.graph,
ut_path.index_of(university_of_twente_asn).unwrap(),
ut_path.index_of(universidade_de_sao_paulo_asn).unwrap(),
0,
None,
);
let ut_path = topo.valley_free_of(university_of_twente_asn);

println!("Paths from UT to USP:");
for path in paths {
let path = path
.iter()
.map(|node| ut_path.asn_of(*node))
.collect::<Vec<_>>();
for path in ut_path.all_paths_to(universidade_de_sao_paulo_asn).unwrap() {
println!(" {:?}", path);
}
}
2 changes: 1 addition & 1 deletion examples/draw_path_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
println!("Basic topology");
println!("{:?}", Dot::new(&topo.graph));

let topo_path = topo.paths_graph(4);
let topo_path = topo.valley_free_of(4);
println!("Path topology");
println!("{:?}", Dot::new(&topo_path.graph));

Expand Down
25 changes: 5 additions & 20 deletions examples/shortest_path_between_two_ases.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
use std::fs::File;

use petgraph::algo::astar;
use valley_free::{RelType, Topology};
use valley_free::Topology;

fn main() {
let file = File::open("20231201.as-rel.txt").unwrap();
let topo = Topology::from_caida(file).unwrap();

let university_of_twente_asn = 1133;
let universidade_de_sao_paulo_asn = 28571;
let ut_path = topo.paths_graph(university_of_twente_asn);
let ut_path = topo.valley_free_of(university_of_twente_asn);

// Use A* to find the shortest path between two nodes
let (_len, path) = astar(
&ut_path.graph,
ut_path.index_of(university_of_twente_asn).unwrap(),
|finish| finish == ut_path.index_of(universidade_de_sao_paulo_asn).unwrap(),
|edge| match edge.weight() {
// priorize pearing
RelType::PearToPear => 0,
RelType::ProviderToCustomer => 1,
RelType::CustomerToProvider => 2,
},
|_| 0,
)
.unwrap();
let path = path
.iter()
.map(|node| ut_path.asn_of(*node))
.collect::<Vec<_>>();
let path = ut_path
.shortest_path_to(universidade_de_sao_paulo_asn)
.unwrap();

println!("Path from UT to USP: {:?}", path);
}
Loading

0 comments on commit d1887d3

Please sign in to comment.