Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Add isLinear and isChordless methods to GraphTests#1286

Open
Wissal-Chbani wants to merge 4 commits into
jgrapht:masterjgrapht/jgrapht:masterfrom
Wissal-Chbani:feature/isLinear-isChordlessWissal-Chbani/jgrapht:feature/isLinear-isChordlessCopy head branch name to clipboard
Open

Add isLinear and isChordless methods to GraphTests#1286
Wissal-Chbani wants to merge 4 commits into
jgrapht:masterjgrapht/jgrapht:masterfrom
Wissal-Chbani:feature/isLinear-isChordlessWissal-Chbani/jgrapht:feature/isLinear-isChordlessCopy head branch name to clipboard

Conversation

@Wissal-Chbani

@Wissal-Chbani Wissal-Chbani commented Nov 25, 2025

Copy link
Copy Markdown

This PR addresses #1235.
This PR adds two new utility methods to the GraphTests class for testing graph properties:

Changes Made

  1. isLinear(Graph<V, E> graph)

    • Tests whether a graph is a path graph (linear graph)
    • A graph is linear if it's undirected, connected, acyclic, and all vertices have degree ≤ 2
    • Handles edge cases: empty graphs and single-vertex graphs are considered linear
  2. isChordless(Graph<V, E> graph, GraphPath<V, E> cycle)

    • Tests whether a cycle is chordless (induced cycle/hole)
    • A cycle is chordless if it has no edges between non-consecutive vertices
    • Validates input to ensure the path is a valid cycle

Why These Methods?

These methods provide useful graph property testing functionality that was previously missing from the library. Linear graphs and chordless cycles are fundamental concepts in graph theory with applications in network analysis, structural analysis, and algorithm design.

Testing

Comprehensive unit tests have been added in GraphTestsTest:

  • Edge cases (empty graphs, single vertices, small paths)
  • Valid linear graphs of various sizes (P2, P4, P6)
  • Non-linear graphs (cycles, stars, disconnected graphs, complete graphs, Y-shaped trees)
  • Chordless cycles of various lengths (C3, C4, C5)
  • Cycles with chords
  • Exception handling for null inputs and invalid arguments

Documentation

  • Added detailed Javadoc for both methods
  • Documented all parameters, return values

@jsichi

jsichi commented Dec 1, 2025

Copy link
Copy Markdown
Member

A couple of trivial checkstyle failures need to be fixed.

jsichi

This comment was marked as outdated.

@jsichi
jsichi dismissed their stale review January 9, 2026 20:01

Oops, ignore this.

@jsichi jsichi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more review comments.


/**
* Test whether a graph is linear (path graph).
* A graph is linear if:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use proper Javadoc format for numbered lists (otherwise this will come out unreadable in the generated Javadoc).

/**
* Test whether a graph is linear (path graph).
* A graph is linear if:
* 1. It is undirected.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does the graph have to be undirected? A linear directed graph is a well-defined concept.

* Test whether a cycle is chordless (induced).
*
* <p>
* A cycle is chordless if there are no edges between non-consecutive

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For long cycles, isn't there a more efficient algorithm?

* Test whether a graph is empty. An empty graph on n nodes consists of n isolated vertices with
* no edges.
*
*

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't modify unrelated code; it causes spurious merge conflicts. Set your editor to only reformat code that you are actually changing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.