Skip to content

Commit f7ec0de

Browse files
realDuYuanChaogithub-actions
andauthored
find all paths of graph (examplehub#132)
* find all path of graph * Formatted with Google Java Formatter Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent fb79e9b commit f7ec0de

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

images/adjacency_matrix_graph.jpeg

188 KB
Loading

src/main/java/com/examplehub/datastructures/graph/AdjacencyMatrix.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.examplehub.datastructures.graph;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Stack;
36
import java.util.StringJoiner;
47

58
public class AdjacencyMatrix<E> {
@@ -57,4 +60,49 @@ public String dfsPath() {
5760
}
5861
return dfsPath.toString();
5962
}
63+
64+
List<String> findAllPath(E startVertex, E endVertex) {
65+
int start = -1;
66+
int end = -1;
67+
for (int i = 0; i < numOfVertex; ++i) {
68+
if (startVertex == vertex[i]) {
69+
start = i;
70+
}
71+
if (endVertex == vertex[i]) {
72+
end = i;
73+
}
74+
}
75+
return (start == -1 || end == -1) ? null : findAllPath(start, end);
76+
}
77+
78+
private List<String> findAllPath(int start, int end) {
79+
List<String> allPath = new ArrayList<>();
80+
Stack<E> stack = new Stack<>();
81+
boolean[] visited = new boolean[vertex.length];
82+
doFindAllPath(allPath, stack, visited, start, end);
83+
return allPath;
84+
}
85+
86+
void doFindAllPath(List<String> allPath, Stack<E> stack, boolean[] visited, int start, int end) {
87+
stack.push(vertex[start]);
88+
visited[start] = true;
89+
if (start == end) {
90+
StringJoiner joiner = new StringJoiner("->");
91+
for (E item : stack) {
92+
joiner.add(item.toString());
93+
}
94+
allPath.add(joiner.toString());
95+
stack.pop();
96+
visited[start] = false;
97+
return;
98+
}
99+
100+
for (int i = 0; i < numOfVertex; ++i) {
101+
if (adj[start][i] == 1 && !visited[i]) {
102+
doFindAllPath(allPath, stack, visited, i, end);
103+
}
104+
}
105+
stack.pop();
106+
visited[start] = false;
107+
}
60108
}

src/test/java/com/examplehub/datastructures/graph/AdjacencyMatrixTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,22 @@ void testGetVertexDegree() {
4848
assertEquals(4, adjacencyMatrix.getVertexDegree("D"));
4949
assertEquals(2, adjacencyMatrix.getVertexDegree("E"));
5050
}
51+
52+
@Test
53+
void testFindAllPath() {
54+
String[] vertex = {"A", "B", "C", "D", "E"};
55+
AdjacencyMatrix<String> adjacencyMatrix = new AdjacencyMatrix<>(vertex);
56+
adjacencyMatrix.addEdge(0, 1); /* A - B */
57+
adjacencyMatrix.addEdge(0, 2); /* A - C */
58+
adjacencyMatrix.addEdge(0, 3); /* A - D */
59+
adjacencyMatrix.addEdge(1, 3); /* B - D */
60+
adjacencyMatrix.addEdge(2, 3); /* C - D */
61+
adjacencyMatrix.addEdge(1, 4); /* B - E */
62+
adjacencyMatrix.addEdge(3, 4); /* D - E */
63+
assertEquals(
64+
"A->B->D->E, A->B->E, A->C->D->B->E, A->C->D->E, A->D->B->E, A->D->E",
65+
String.join(", ", adjacencyMatrix.findAllPath("A", "E")));
66+
67+
assertNull(adjacencyMatrix.findAllPath("A", "F"));
68+
}
5169
}

0 commit comments

Comments
 (0)