initial commit

This commit is contained in:
2025-02-18 17:10:44 +01:00
commit 818bfd569e
6 changed files with 3987 additions and 0 deletions

43
src/main.rs Normal file
View File

@@ -0,0 +1,43 @@
use eframe::{run_native, App, CreationContext, NativeOptions, Frame};
use egui::{CentralPanel, SidePanel, Context};
use egui_graphs::{GraphView, Graph, SettingsStyle};
//use petgraph::stable_graph::StableGraph;
mod random_generator;
pub struct MaxflowApp {
g: Graph<(), u64>,
node_count: u64,
max_capacity: u64,
}
impl MaxflowApp {
fn new(_: &CreationContext<'_>) -> Self {
let problem = random_generator::MaxflowProblem::new(10, 10);
Self { g: problem.g, node_count: 10, max_capacity: 5 }
}
}
impl App for MaxflowApp {
fn update(&mut self, ctx: &Context, _: &mut Frame) {
CentralPanel::default().show(ctx, |ui| {
ui.add(&mut GraphView::new(&mut self.g).with_styles(&SettingsStyle::default().with_labels_always(true)));
});
SidePanel::right("right_panel")
.min_width(200.)
.show(ctx, |ui| {
ui.label("node count");
ui.add(egui::DragValue::new(&mut self.node_count).range(1..=1000));
ui.label("maximum capacity");
ui.add(egui::DragValue::new(&mut self.max_capacity).range(1..=10));
if ui.button("generate graph").clicked() {
let problem = random_generator::MaxflowProblem::new(self.node_count, self.max_capacity);
self.g = problem.g;
}
});
}
}
fn main() {
run_native("Maxflow.rs", NativeOptions::default(), Box::new(|cc| Ok(Box::new(MaxflowApp::new(cc)))),).unwrap();
}

56
src/random_generator.rs Normal file
View File

@@ -0,0 +1,56 @@
use petgraph::stable_graph::{StableGraph, NodeIndex};
use petgraph::{Directed};
use egui_graphs::{to_graph, Graph};
use egui::Pos2;
use rand::thread_rng;
use rand::Rng;
use rand::seq::SliceRandom;
pub struct MaxflowProblem {
pub g: Graph<(), u64, Directed, u32, egui_graphs::DefaultNodeShape, egui_graphs::DefaultEdgeShape>,
}
impl MaxflowProblem {
pub fn new(num_nodes: u64, max_capacity: u64) -> Self {
let mut graph: Graph<_, u64> = Graph::new(StableGraph::default());
let mut nodes = Vec::<NodeIndex>::new();
let mut rng = thread_rng();
// generate nodes
for _i in 0..num_nodes {
let x: f32 = rng.gen_range(0.0..=500.0);
let y: f32 = rng.gen_range(0.0..=500.0);
let pos = Pos2::new(x,y);
let n = graph.add_node_with_label_and_location((), String::new(), pos);
nodes.push(n);
// to achieve a strongly connected graph, every node
// should have at least one edge pointing from and to
// a node TODO
let node1 = nodes.choose(&mut rng).unwrap();
let capacity1: u64 = rng.gen_range(1..=max_capacity);
// if node1.position() overlaps do sth
graph.add_edge_with_label(n, *node1, capacity1, capacity1.to_string());
graph.add_edge_with_label(*node1, n, capacity1, capacity1.to_string());
}
// sort node pairs by distance
// randomly add edges
for _i in 0..num_nodes {
let node1 = nodes.choose(&mut rng).unwrap();
let node2 = nodes.choose(&mut rng).unwrap();
let capacity: u64 = rng.gen_range(1..=max_capacity);
//graph.add_edge_with_label(*node1, *node2, capacity, capacity.to_string());
}
//let widget_graph: Graph<_, _, _, _, egui_graphs::DefaultNodeShape, egui_graphs::DefaultEdgeShape> = to_graph(&graph);
Self { g: graph }
}
fn distance(a: Pos2, b: Pos2) -> f32 {
let delta_x = a.x - b.x;
let delta_y = a.y - b.y;
return f32::sqrt(delta_x.powf(2.) + delta_y.powf(2.));
}
}