-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountPaths.java
More file actions
43 lines (40 loc) · 1.62 KB
/
CountPaths.java
File metadata and controls
43 lines (40 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.util.*;
public class CountPaths {
public static void addEdge(Map<Character, List<Character>> graph, char from, char to) {
graph.putIfAbsent(from, new ArrayList<>());
graph.get(from).add(to);
}
public static int countPaths(Map<Character, List<Character>> graph, char source, char destination) {
int[] count = {0};
Set<Character> visited = new HashSet<>();
dfs(graph, source, destination, visited, count);
return count[0];
}
private static void dfs(Map<Character, List<Character>> graph, char current, char destination, Set<Character> visited, int[] count) {
if (current == destination) {
count[0]++;
return;
}
visited.add(current);
for (char neighbor : graph.getOrDefault(current, new ArrayList<>())) {
if (!visited.contains(neighbor)) {
dfs(graph, neighbor, destination, visited, count);
}
}
visited.remove(current);
}
public static void main(String[] args) {
Map<Character, List<Character>> graph = new HashMap<>();
addEdge(graph, 'A', 'B');
addEdge(graph, 'A', 'C');
addEdge(graph, 'B', 'C');
addEdge(graph, 'B', 'E');
addEdge(graph, 'C', 'D');
addEdge(graph, 'D', 'B');
addEdge(graph, 'D', 'E');
char source = 'A';
char destination = 'E';
int totalPaths = countPaths(graph, source, destination);
System.out.println("Total number of paths from " + source + " to " + destination + " is: " + totalPaths);
}
}