-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShortestPathNoWeight.java
More file actions
68 lines (58 loc) · 1.86 KB
/
Copy pathShortestPathNoWeight.java
File metadata and controls
68 lines (58 loc) · 1.86 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.util.*;
public class ShortestPathNoWeight{
public static void main(String[] args){
int[][] matrix = {
{0, 1, 1, 0, 0, 0},
{1, 0, 1, 1, 0, 0},
{1, 1, 0, 1, 1, 0},
{0, 1, 1, 0, 1, 1},
{0, 0, 1, 1, 0, 0},
{0, 0, 0, 1, 0, 0}
};
int start = Integer.valueOf(args[0]);
int end = Integer.valueOf(args[1]);
List<Integer> ans = shortest(matrix, start, end);
for(int i=ans.size()-1; i>=0; i--)
System.out.println(ans.get(i));
}
public static List<Integer> shortest(int[][] matrix, int start, int end){
Map<Integer, List<Integer>> graph = new HashMap<>();
buildGraph(graph, matrix);
List<Integer> ans = new ArrayList<>();
Set<Integer> hs = new HashSet<>();
hs.add(start);
int[] parent = new int[matrix.length];
for(int i=0; i<matrix.length; i++)
parent[i] = i;
Queue<Integer> queue = new LinkedList<>();
queue.offer(start);
while(!queue.isEmpty()){
int cur = queue.poll();
for(int n : graph.get(cur)){
if(!hs.contains(n)){
queue.offer(n);
hs.add(n);
parent[n] = cur;
}
}
}
int c = end;
while(parent[c] != c){
ans.add(c);
c = parent[c];
}
ans.add(c);
return ans;
}
public static void buildGraph(Map<Integer, List<Integer>> graph, int[][] matrix){
for(int i=0; i<matrix.length; i++){
graph.put(i, new ArrayList<>());
}
for(int i=0; i<matrix.length; i++){
for(int j=0; j<matrix.length; j++){
if(matrix[i][j] != 0)
graph.get(i).add(j);
}
}
}
}