Skip to content

[BUG]Bug in C-Plus-Plus-Project/C-Plus-Plus3.5/graph/dijkstra.cpp #2952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
18781875724 opened this issue Jun 10, 2025 · 2 comments
Open

[BUG]Bug in C-Plus-Plus-Project/C-Plus-Plus3.5/graph/dijkstra.cpp #2952

18781875724 opened this issue Jun 10, 2025 · 2 comments
Labels

Comments

@18781875724
Copy link

Description

The current implementation contains a defect when handling graphs with 0 edges. While not guaranteed to crash, the algorithm may exhibit undefined behavior when processing empty adjacency lists. The issue stems from line 84 in dijkstra.cpp where the code unconditionally processes edges without first verifying if the adjacency list for the current node is empty. This could lead to either incorrect results or runtime errors depending on the implementation's memory behavior.

Expected behavior

The program proceeds with Dijkstra's algorithm normally despite having no valid edges

At line 84, it attempts to iterate through the adjacency list of each node ((*adj)[currentNode])

While this doesn't immediately crash (iterating empty vectors is technically valid in C++), it:

Wastes computation cycles checking empty edge lists

Reveals flawed assumptions in the algorithm's design

May lead to undefined behavior if the adjacency list implementation changes

The algorithm fails to explicitly handle this degenerate case, which is poor practice for graph algorithms

Actual behavior

When the input specifies 0 edges:

The program should safely handle graph traversal without attempting to access elements of empty adjacency lists

No undefined behavior should occur at line 84 where the algorithm currently iterates through edges of a node

For the source node (s), the distance should correctly be reported as 0

For all other nodes, the distance should be reported as unreachable (INF or -1)

The program should never attempt to dereference or iterate through non-existent edges

Steps to reproduce

No response

Context

I encountered this issue while testing the Dijkstra's algorithm implementation for edge cases. Specifically, I wanted to verify how the program handles:

Disconnected graphs (graphs with no edges between nodes)

Single-node graphs (edge case where vertices = 1 and edges = 0)

Instead of properly recognizing unreachable nodes or returning early for trivial cases, the algorithm proceeds with unnecessary computations on empty adjacency lists.

Additional information

No response

@MQ-06
Copy link

MQ-06 commented Jun 11, 2025

Thanks for catching this behavior! I agree that returning 0 in this edge-case seems unintended. Could you share a minimal test input that reproduces it? I’d be happy to help submit a PR to fix it.

@18781875724
Copy link
Author

Thanks for catching this behavior! I agree that returning 0 in this edge-case seems unintended. Could you share a minimal test input that reproduces it? I’d be happy to help submit a PR to fix it.

When prompted, enter:

Number of vertices: 1

Number of edges: 0

s: 0

t: 0
The error occurs

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants