-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFS:ShortestReachinaGraph.java
More file actions
152 lines (119 loc) · 4.16 KB
/
Copy pathBFS:ShortestReachinaGraph.java
File metadata and controls
152 lines (119 loc) · 4.16 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static class Node {
private int id;
private List<Node> edges;
public Node(int id) {
this.id = id;
this.edges = new ArrayList<Node>();
}
public int getId() {
return id;
}
public void addEdge(Node node) {
this.edges.add(node);
}
public List<Node> getEdges() {
return edges;
}
public String toString() {
return id + "";
}
}
public static class NodeCost {
private int cost;
private Node node;
public NodeCost(Node node, int cost) {
this.node = node;
this.cost = cost;
}
public Node getNode() {
return node;
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
}
public static class Graph {
private HashMap<Integer, Node> nodes;
public Graph(int size) {
nodes = new HashMap<Integer, Node>();
for (int i = 0; i < size; i++) {
nodes.put(i, new Node(i));
}
}
public void addEdge(int first, int second) {
Node u = nodes.get(first);
Node v = nodes.get(second);
u.addEdge(v);
v.addEdge(u);
}
public int shortestPathBFS(Node start, Node end) {
if (start.getEdges().size() == 0 || end.getEdges().size() == 0) {
return -1;
}
Queue<Node> queue = new LinkedList<Node>();
HashMap<Integer, Integer> cost = new HashMap<Integer, Integer>();
queue.add(start);
cost.put(start.getId(), 0);
while (!queue.isEmpty()) {
Node current = queue.poll();
if (current.getId() == end.getId()) {
return cost.get(current.getId());
}
for (Node n : current.getEdges()) {
if (cost.containsKey(n.getId())) {
continue;
}
cost.put(n.getId(), cost.get(current.getId()) + 6);
queue.add(n);
}
}
return -1;
}
public int[] shortestReach(int startId) { // 0 indexed
int results[] = new int[nodes.size()];
for (int i = 0; i < nodes.size(); i++){
if (i == startId) {
continue;
}
results[i] = shortestPathBFS(nodes.get(startId), nodes.get(i));
}
return results;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int queries = scanner.nextInt();
for (int t = 0; t < queries; t++) {
// Create a graph of size n where each edge weight is 6:
Graph graph = new Graph(scanner.nextInt());
int m = scanner.nextInt();
// read and set edges
for (int i = 0; i < m; i++) {
int u = scanner.nextInt() - 1;
int v = scanner.nextInt() - 1;
// add each edge to the graph
graph.addEdge(u, v);
}
// Find shortest reach from node s
int startId = scanner.nextInt() - 1;
int[] distances = graph.shortestReach(startId);
for (int i = 0; i < distances.length; i++) {
if (i != startId) {
System.out.print(distances[i]);
System.out.print(" ");
}
}
System.out.println();
}
scanner.close();
}
}