Skip to content

Refactor: problem constructors should take graph as input, not raw vertices/edges #73

@GiggleLiu

Description

@GiggleLiu

Motivation

Problem types like KColoring, MaximumIndependentSet, MaxCut, etc. currently have new(num_vertices, edges) constructors that internally construct a SimpleGraph. This makes the problem "pretend to be a graph" and hides the graph topology type from the constructor signature.

Current pattern

// Graph construction is hidden inside the problem
let problem = MaximumIndependentSet::<SimpleGraph, i32>::new(4, vec![(0, 1), (1, 2)]);

Proposed pattern

// Graph is an explicit input — topology type is clear
let graph = SimpleGraph::new(4, vec![(0, 1), (1, 2)]);
let problem = MaximumIndependentSet::new(graph, weights);

Benefits:

  • Graph topology type is reflected in the constructor signature
  • Problems don't duplicate graph construction logic
  • from_graph becomes new, which is more idiomatic
  • Works naturally with non-SimpleGraph topologies (UnitDiskGraph, GridGraph, etc.)

Affected types (9 total)

All in src/models/graph/:

Type Current constructor
MaximumIndependentSet new(num_vertices, edges)
MinimumVertexCover new(num_vertices, edges)
MinimumDominatingSet new(num_vertices, edges)
MaximumClique new(num_vertices, edges)
MaximalIS new(num_vertices, edges)
KColoring new(num_vertices, edges)
MaxCut new(num_vertices, edges_with_weights)
MaximumMatching new(num_vertices, edges_with_weights)
TravelingSalesman new(num_vertices, edges_with_weights)

Each already has a from_graph(graph, ...) constructor that takes the graph directly. The refactoring would:

  1. Remove new(num_vertices, edges)
  2. Rename from_graphnew
  3. Update all call sites (tests, examples, reductions)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions