Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Fix/ Dijkstra priority-queue#7169

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
bhavani1406-amc wants to merge3 commits intoTheAlgorithms:master
base:master
Choose a base branch
Loading
frombhavani1406-amc:fix/dijkstra-priorityqueue
Open
Show file tree
Hide file tree
Changes from1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
test: add DijkstraPriorityQueue tests; format imports
  • Loading branch information
SUZUB committedDec 17, 2025
commit28f0f574248af5250df5bca5b6db434085bff4e6
3 changes: 2 additions & 1 deletionsrc/main/java/com/thealgorithms/others/Dijkstra.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
package com.thealgorithms.others;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Comparator;

/**
* Dijkstra's algorithm,is a graph search algorithm that solves the
* single-source shortest path problem for a graph with nonnegative edge path
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
package com.thealgorithms.graphs;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

/**
* Unit tests for {@link DijkstraPriorityQueue}.
*/
public class DijkstraPriorityQueueTest {

private DijkstraPriorityQueue dijkstra;

@BeforeEach
void setUp() {
dijkstra = new DijkstraPriorityQueue();
}

@Test
void testSimpleGraph() {
Map<Integer, List<DijkstraPriorityQueue.Edge>> graph = new HashMap<>();
graph.put(0, List.of(new DijkstraPriorityQueue.Edge(1, 7), new DijkstraPriorityQueue.Edge(2, 9)));
graph.put(1, List.of(new DijkstraPriorityQueue.Edge(2, 10)));
graph.put(2, new ArrayList<>());

int[] result = dijkstra.runDijkstra(0, graph, 3);
int[] expected = {0, 7, 9};
assertArrayEquals(expected, result);
}

@Test
void testNegativeWeightThrows() {
Map<Integer, List<DijkstraPriorityQueue.Edge>> graph = new HashMap<>();
graph.put(0, List.of(new DijkstraPriorityQueue.Edge(1, -1)));
graph.put(1, new ArrayList<>());

assertThrows(IllegalArgumentException.class, () -> dijkstra.runDijkstra(0, graph, 2));
}

@Test
void testDisconnectedGraph() {
Map<Integer, List<DijkstraPriorityQueue.Edge>> graph = new HashMap<>();
graph.put(0, new ArrayList<>());
graph.put(1, new ArrayList<>());

int[] result = dijkstra.runDijkstra(0, graph, 2);
int[] expected = {0, Integer.MAX_VALUE};
assertArrayEquals(expected, result);
}

@Test
void testEmptyGraph() {
Map<Integer, List<DijkstraPriorityQueue.Edge>> graph = new HashMap<>();
int[] result = dijkstra.runDijkstra(0, graph, 0);
int[] expected = {};
assertArrayEquals(expected, result);
}
}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp