|
87 | 87 | // * Votes: 6
|
88 | 88 |
|
89 | 89 |
|
90 |
| -/* Dikstra Algorithm */ |
91 |
| -class Edge { |
92 |
| - int to; |
93 |
| - int weight; |
94 |
| - |
95 |
| - public Edge(int to, int weight){ |
96 |
| - this.to = to; |
97 |
| - this.weight = weight; |
98 |
| - } |
99 |
| -} |
| 90 | +// /* Dikstra Algorithm */ |
| 91 | +// class Edge { |
| 92 | +// int to; |
| 93 | +// int weight; |
| 94 | +// |
| 95 | +// public Edge(int to, int weight){ |
| 96 | +// this.to = to; |
| 97 | +// this.weight = weight; |
| 98 | +// } |
| 99 | +// } |
| 100 | +// |
| 101 | +// /* Dijkstra Algorithm */ |
| 102 | +// class Solution { |
| 103 | +// public int findTheCity(int n, int[][] edges, int distanceThreshold) { |
| 104 | +// // Create Linked list of edges as the vertex |
| 105 | +// LinkedList<Edge>[] graph = new LinkedList[n]; |
| 106 | +// |
| 107 | +// for(int i = 0; i < graph.length; i++){ |
| 108 | +// graph[i] = new LinkedList<>(); |
| 109 | +// } |
| 110 | +// |
| 111 | +// // Fill the matrix graph with bidirectional direct Edges |
| 112 | +// for(int[] edge : edges){ |
| 113 | +// graph[edge[0]].add(new Edge(edge[1], edge[2])); // from |
| 114 | +// graph[edge[1]].add(new Edge(edge[0], edge[2])); // to |
| 115 | +// } |
| 116 | +// |
| 117 | +// int maxNodeVisits = n + 1; |
| 118 | +// int cityWithLesserNeighbors = -1; |
| 119 | +// for(int i = 0; i < n; i++){ |
| 120 | +// // Visit all cities within distanceThreshold |
| 121 | +// int visits = bfs(graph, i, distanceThreshold); |
| 122 | +// if(visits <= maxNodeVisits){ |
| 123 | +// cityWithLesserNeighbors = i; |
| 124 | +// maxNodeVisits = Math.min(maxNodeVisits, visits); |
| 125 | +// } |
| 126 | +// } |
| 127 | +// |
| 128 | +// return cityWithLesserNeighbors; |
| 129 | +// } |
| 130 | +// |
| 131 | +// // Breadth-first Search (BFS) |
| 132 | +// // Returns the number of visited nodes within the given distance threshold |
| 133 | +// public int bfs(LinkedList<Edge>[] graph, int vertex, int thresh){ |
| 134 | +// // Storage for the explored vertices |
| 135 | +// Map<Integer,Integer> map = new HashMap<>(); |
| 136 | +// |
| 137 | +// // (Edge a, Edge b) -> (a.weight - b.weight) is a comparator lambda for |
| 138 | +// // sorting the smallest value (a.weight - b.weight) first. Therefore, this |
| 139 | +// // PQ prioritizes smaller numbers first (ascending). |
| 140 | +// PriorityQueue<Edge> pq = new PriorityQueue<>((Edge a, Edge b) -> (a.weight - b.weight)); |
| 141 | +// // Initialize with new Edge with 0 weight |
| 142 | +// pq.offer(new Edge(vertex, 0)); |
| 143 | +// |
| 144 | +// while(!pq.isEmpty()){ |
| 145 | +// Edge edge = pq.remove(); |
| 146 | +// |
| 147 | +// // Skip if edge already in the map and weight is greater |
| 148 | +// if(map.containsKey(edge.to) && edge.weight > map.get(edge.to)) |
| 149 | +// continue; |
| 150 | +// |
| 151 | +// // Add or update edge to map |
| 152 | +// map.put(edge.to, edge.weight); |
| 153 | +// |
| 154 | +// // Traverse next edge |
| 155 | +// for(Edge e : graph[edge.to]) { |
| 156 | +// int dist = e.weight + edge.weight; |
| 157 | +// |
| 158 | +// if(dist > thresh) |
| 159 | +// continue; |
| 160 | +// |
| 161 | +// // Skip if edge already in the map and distance is greater |
| 162 | +// if(map.containsKey(e.to) && dist > map.get(e.to)) |
| 163 | +// continue; |
| 164 | +// |
| 165 | +// // Add or update edge to map |
| 166 | +// map.put(e.to, dist); |
| 167 | +// pq.offer(new Edge(e.to,dist)); |
| 168 | +// } |
| 169 | +// } |
| 170 | +// |
| 171 | +// return map.size() - 1; |
| 172 | +// } |
| 173 | +// } |
100 | 174 |
|
101 |
| -/* Dijkstra Algorithm */ |
| 175 | +/* Floyd Warshall */ |
102 | 176 | class Solution {
|
103 | 177 | public int findTheCity(int n, int[][] edges, int distanceThreshold) {
|
104 |
| - // Create Linked list of edges as the vertex |
105 |
| - LinkedList<Edge>[] graph = new LinkedList[n]; |
106 |
| - |
107 |
| - for(int i = 0; i < graph.length; i++){ |
108 |
| - graph[i] = new LinkedList<>(); |
| 178 | + // This needs to be a float because it needs to store the Integer.MAX_VALUE. |
| 179 | + // Else if this is int, adding a positive number to the max value an integer |
| 180 | + // can handle, the bits will overflow and becomes a negative number. |
| 181 | + // Alternatively, instead of the MAX_VALUE as a placeholder, since the |
| 182 | + // constraint for distanceThreshold <= 10^4, we can initialize it with |
| 183 | + // anything greater than the threshold value (i.e., 10001). |
| 184 | + float[][] dp = new float[n][n]; |
| 185 | + |
| 186 | + // Initialize dp |
| 187 | + for (int i = 0; i < n; i++) { |
| 188 | + Arrays.fill(dp[i], Integer.MAX_VALUE); |
| 189 | + dp[i][i] = 0; |
109 | 190 | }
|
110 | 191 |
|
111 |
| - // Fill the matrix graph with bidirectional direct Edges |
112 |
| - for(int[] edge : edges){ |
113 |
| - graph[edge[0]].add(new Edge(edge[1], edge[2])); // from |
114 |
| - graph[edge[1]].add(new Edge(edge[0], edge[2])); // to |
| 192 | + for (int[] edge : edges) { |
| 193 | + // Fill dp with from to edge grid; dp[from][to] = weight |
| 194 | + dp[edge[0]][edge[1]] = edge[2]; |
| 195 | + dp[edge[1]][edge[0]] = edge[2]; |
115 | 196 | }
|
116 | 197 |
|
117 |
| - int maxNodeVisits = n + 1; |
118 |
| - int cityWithLesserNeighbors = -1; |
119 |
| - for(int i = 0; i < n; i++){ |
120 |
| - // Visit all cities within distanceThreshold |
121 |
| - int visits = bfs(graph, i, distanceThreshold); |
122 |
| - if(visits <= maxNodeVisits){ |
123 |
| - cityWithLesserNeighbors = i; |
124 |
| - maxNodeVisits = Math.min(maxNodeVisits, visits); |
| 198 | + // Find all shortest path |
| 199 | + for (int detour = 0; detour < n; detour++) { |
| 200 | + for (int from = 0; from < n; from++) { |
| 201 | + for (int to = 0; to < n; to++) { |
| 202 | + // Update edge path if detour city is shorter than direct |
| 203 | + dp[from][to] = Math.min(dp[from][to], dp[from][detour] + dp[detour][to]); |
| 204 | + } |
125 | 205 | }
|
126 | 206 | }
|
127 | 207 |
|
128 |
| - return cityWithLesserNeighbors; |
129 |
| - } |
130 |
| - |
131 |
| - // Breadth-first Search (BFS) |
132 |
| - // Returns the number of visited nodes within the given distance threshold |
133 |
| - public int bfs(LinkedList<Edge>[] graph, int vertex, int thresh){ |
134 |
| - // Storage for the explored vertices |
135 |
| - Map<Integer,Integer> map = new HashMap<>(); |
136 |
| - |
137 |
| - // (Edge a, Edge b) -> (a.weight - b.weight) is a comparator lambda for |
138 |
| - // sorting the smallest value (a.weight - b.weight) first. Therefore, this |
139 |
| - // PQ prioritizes smaller numbers first (ascending). |
140 |
| - PriorityQueue<Edge> pq = new PriorityQueue<>((Edge a, Edge b) -> (a.weight - b.weight)); |
141 |
| - // Initialize with new Edge with 0 weight |
142 |
| - pq.offer(new Edge(vertex, 0)); |
143 |
| - |
144 |
| - while(!pq.isEmpty()){ |
145 |
| - Edge edge = pq.remove(); |
146 |
| - |
147 |
| - // Skip if edge already in the map and weight is greater |
148 |
| - if(map.containsKey(edge.to) && edge.weight > map.get(edge.to)) |
149 |
| - continue; |
150 |
| - |
151 |
| - // Add or update edge to map |
152 |
| - map.put(edge.to, edge.weight); |
153 |
| - |
154 |
| - // Traverse next edge |
155 |
| - for(Edge e : graph[edge.to]) { |
156 |
| - int dist = e.weight + edge.weight; |
157 |
| - |
158 |
| - if(dist > thresh) |
159 |
| - continue; |
160 |
| - |
161 |
| - // Skip if edge already in the map and distance is greater |
162 |
| - if(map.containsKey(e.to) && dist > map.get(e.to)) |
163 |
| - continue; |
164 |
| - |
165 |
| - // Add or update edge to map |
166 |
| - map.put(e.to, dist); |
167 |
| - pq.offer(new Edge(e.to,dist)); |
| 208 | + int maxVisits = n + 1; |
| 209 | + int cityWithLesserNeighbors = -1; |
| 210 | + for(int from = 0; from < n; from++) { |
| 211 | + // Get all neighboring cities with less than distanceThreshold edge |
| 212 | + int neighborCitiesWithinLimit = 0; |
| 213 | + for(int to = 0; to < n; to++) { |
| 214 | + if(dp[from][to] <= distanceThreshold) |
| 215 | + neighborCitiesWithinLimit++; |
| 216 | + } |
| 217 | + if(neighborCitiesWithinLimit <= maxVisits){ |
| 218 | + cityWithLesserNeighbors = from; |
| 219 | + maxVisits = Math.min(maxVisits, neighborCitiesWithinLimit); |
168 | 220 | }
|
169 | 221 | }
|
170 | 222 |
|
171 |
| - return map.size() - 1; |
| 223 | + return cityWithLesserNeighbors; |
172 | 224 | }
|
173 | 225 | }
|
174 |
| - |
175 |
| -// /* Floyd Warshall */ |
176 |
| -// class Solution { |
177 |
| -// public int findTheCity(int n, int[][] edges, int distanceThreshold) { |
178 |
| -// // This needs to be a float because it needs to store the Integer.MAX_VALUE. |
179 |
| -// // Else if this is int, adding a positive number to the max value an integer |
180 |
| -// // can handle, the bits will overflow and becomes a negative number. |
181 |
| -// // Alternatively, instead of the MAX_VALUE as a placeholder, since the |
182 |
| -// // constraint for distanceThreshold <= 10^4, we can initialize it with |
183 |
| -// // anything greater than the threshold value (i.e., 10001). |
184 |
| -// float[][] dp = new float[n][n]; |
185 |
| -// |
186 |
| -// // Initialize dp |
187 |
| -// for (int i = 0; i < n; i++) { |
188 |
| -// Arrays.fill(dp[i], Integer.MAX_VALUE); |
189 |
| -// dp[i][i] = 0; |
190 |
| -// } |
191 |
| -// |
192 |
| -// for (int[] edge : edges) { |
193 |
| -// // Fill dp with from to edge grid; dp[from][to] = weight |
194 |
| -// dp[edge[0]][edge[1]] = edge[2]; |
195 |
| -// dp[edge[1]][edge[0]] = edge[2]; |
196 |
| -// } |
197 |
| -// |
198 |
| -// // Find all shortest path |
199 |
| -// for (int detour = 0; detour < n; detour++) { |
200 |
| -// for (int from = 0; from < n; from++) { |
201 |
| -// for (int to = 0; to < n; to++) { |
202 |
| -// // Update edge path if detour city is shorter than direct |
203 |
| -// dp[from][to] = Math.min(dp[from][to], dp[from][detour] + dp[detour][to]); |
204 |
| -// } |
205 |
| -// } |
206 |
| -// } |
207 |
| -// |
208 |
| -// int maxVisits = n + 1; |
209 |
| -// int cityWithLesserNeighbors = -1; |
210 |
| -// for(int from = 0; from < n; from++) { |
211 |
| -// // Get all neighboring cities with less than distanceThreshold edge |
212 |
| -// int neighborCitiesWithinLimit = 0; |
213 |
| -// for(int to = 0; to < n; to++) { |
214 |
| -// if(dp[from][to] <= distanceThreshold) |
215 |
| -// neighborCitiesWithinLimit++; |
216 |
| -// } |
217 |
| -// if(neighborCitiesWithinLimit <= maxVisits){ |
218 |
| -// cityWithLesserNeighbors = from; |
219 |
| -// maxVisits = Math.min(maxVisits, neighborCitiesWithinLimit); |
220 |
| -// } |
221 |
| -// } |
222 |
| -// |
223 |
| -// return cityWithLesserNeighbors; |
224 |
| -// } |
225 |
| -// } |
0 commit comments