Implement Ford-Fulkerson

This commit is contained in:
2025-03-19 15:06:51 +01:00
parent 5680b67d95
commit 5d81549ac0
3 changed files with 164 additions and 15 deletions

View File

@@ -2,15 +2,14 @@ use eframe::{run_native, App, CreationContext, NativeOptions, Frame};
use egui::{CentralPanel, SidePanel, Context};
use egui_graphs::{GraphView, Graph, SettingsStyle, LayoutRandom, LayoutStateRandom};
use algorithms::ford_fulkerson;
use random_generator::MaxflowProblem;
use algorithms::{ford_fulk
//use petgraph::stable_graph::StableGraph;
mod random_generator;
mod algorithms;
mod graph;
pub struct MaxflowApp {
g: Graph<(), u64>,
g: Graph<(f32, f32), (u64, u64)>,
p: MaxflowProblem,
node_count: u64,
max_capacity: u64,
@@ -19,12 +18,13 @@ pub struct MaxflowApp {
impl MaxflowApp {
fn new(_: &CreationContext<'_>) -> Self {
let problem = MaxflowProblem::new(10, 10);
Self { g: problem.g.clone(), p: problem, node_count: 10, max_capacity: 5 }
Self { g: problem.to_gui_graph(), p: problem, node_count: 10, max_capacity: 5 }
}
}
impl App for MaxflowApp {
fn update(&mut self, ctx: &Context, _: &mut Frame) {
ctx.set_theme(egui::Theme::Light);
CentralPanel::default().show(ctx, |ui| {
ui.add(&mut GraphView::<_, _, _, _, _, _, LayoutStateRandom, LayoutRandom>::new(&mut self.g).with_styles(&SettingsStyle::default().with_labels_always(true)));
});
@@ -32,14 +32,17 @@ impl App for MaxflowApp {
.min_width(200.)
.show(ctx, |ui| {
ui.label("node count");
ui.add(egui::DragValue::new(&mut self.node_count).range(1..=1000));
ui.add(egui::DragValue::new(&mut self.node_count).range(2..=1000));
ui.label("maximum capacity");
ui.add(egui::DragValue::new(&mut self.max_capacity).range(1..=10));
ui.add(egui::DragValue::new(&mut self.max_capacity).range(1..=100));
if ui.button("generate graph").clicked() {
let problem = random_generator::MaxflowProblem::new(self.node_count, self.max_capacity);
self.g = problem.g;
self.p = random_generator::MaxflowProblem::new(self.node_count, self.max_capacity);
self.g = self.p.to_gui_graph();
}
if ui.button("run algorithm").clicked() {
let max_flow_graph = ford_fulkerson(self.p.g.clone(), self.p.s, self.p.t);
self.p = MaxflowProblem::from(max_flow_graph, self.p.s, self.p.t);
self.g = self.p.to_gui_graph();
}
});
}