You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: binary-search/binary-search.md
+1-18
Original file line number
Diff line number
Diff line change
@@ -77,24 +77,7 @@ Now that we have the three possible scenarios handled, we just need to define wh
77
77
78
78
We want to keep searching while we have some range of numbers to check, so if `lo` ever becomes greater than `hi`, then our target value is not in the array due to the fact that we have run out of numbers to check.
Remember that as you add items to a priority queue, they are automatically sorted within the queue, so that you are given the "smallest" item when you remove it from the queue.
128
61
129
62
First, we need to define our `Vertex` class and what attributes it will contain.
130
63
131
-
```java
132
-
// In order for the Vertex class to be sortable, we need to implement the Comparable interface
133
-
classVerteximplementsComparable<Vertex>
134
-
{
135
-
// id - the integer representing the vertex
136
-
// dist - the tenetative distance to the vertex
137
-
int id, dist;
138
-
139
-
// neighbors - the integer value of the neighboring vertices
140
-
ArrayList<Integer> neighbors;
141
-
142
-
publicVertex( intid )
143
-
{
144
-
this.id = id;
145
-
this.dist =Integer.MAX_VALUE;
146
-
this.neighbors =newArrayList<Integer>();
147
-
}
148
-
149
-
// This method needs to be overridden to determine how vertices are sorted
150
-
publicintcompareTo( Vertexv )
151
-
{
152
-
// The vertex that has a smaller tentative distance should be removed from the queue first
Remember that when we used `dist[]` to keep track of distances, we had to look through _all_ tentative distances to see which vertex to visit; with this method, we just remove the next vertex from the priority queue and it will be guaranteed to have the smallest distance.
166
67
@@ -194,45 +95,7 @@ Just like BFS, we are repeating the steps of the algorithm until the queue is em
194
95
195
96
Now that we have our stopping condition, we can implement the rest of the algorithm
196
97
197
-
```java
198
-
// While the queue still has vertices left to visit...
199
-
while ( !priorityQueue.isEmpty() )
200
-
{
201
-
// Get the current vertex
202
-
Vertex current = priorityQueue.remove();
203
-
204
-
// If the current vertex is our destination...
205
-
if ( current.id == destination )
206
-
{
207
-
// Break from the loop
208
-
break;
209
-
}
210
-
211
-
// Get the neighbors of the current vertex
212
-
ArrayList<Integer> neighbors = current.neighbors;
213
-
214
-
// For each of the neighbors...
215
-
for ( Integer j : neighbor )
216
-
{
217
-
// Get the Vertex representation of the neighbor
218
-
Vertex neighbor = graph[ j ];
219
-
220
-
// Calculate the new distance to the neighbor
221
-
int newDistance = current.dist + cost[ current.id ][ neighbor.id ];
222
-
223
-
// If the new distance we calculated is smaller than the tentative distance of the neighbor...
224
-
if ( newDistance < neighbor.dist )
225
-
{
226
-
// Remove the neighbor from the priority queue so we can update the value
227
-
priorityQueue.remove( neighbor );
228
-
// Update the tentative distance of the neighbor
229
-
neighbor.dist = newDistance;
230
-
// Add the neighbor into the priority queue so that it can be sorted among the other vertices
0 commit comments