Skip to content

Commit bf543a2

Browse files
committed
Test adding code snapshots using Carbon
1 parent 8b52b92 commit bf543a2

10 files changed

+7
-194
lines changed

binary-search/binary-search.md

+1-18
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,7 @@ Now that we have the three possible scenarios handled, we just need to define wh
7777

7878
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.
7979

80-
```java
81-
int lo = 0;
82-
int hi = N-1;
83-
84-
while (lo <= hi)
85-
{
86-
mid = lo + (hi -lo) / 2;
87-
88-
if (array[mid] == targetValue)
89-
break;
90-
91-
if (array[mid] < targetValue)
92-
lo = mid + 1;
93-
94-
if (array[mid] > targetValue)
95-
hi = mid - 1;
96-
}
97-
```
80+
![binary-search](..\resources\binary-search.png)
9881

9982
## Problems
10083

graph-theory/introduction/introduction.md

+3-36
Original file line numberDiff line numberDiff line change
@@ -53,32 +53,11 @@ There are many ways to use an adjacency matrix to represent a matrix, but we wil
5353

5454
#### Connection matrix
5555

56-
```java
57-
boolean[][] conn = new boolean[ N ][ N ];
58-
```
59-
60-
The matrix `conn` tells us if two vertices are connected
61-
62-
```java
63-
if ( conn[ i ][ j ] )
64-
// there is an edge from vertex i to vertex j
65-
66-
else
67-
// there is no edge from vertex i to vertex j
68-
```
56+
![graph-theory-introduction-1](..\..\resources\graph-theory-introduction-1.png)
6957

7058
#### Cost matrix
7159

72-
```java
73-
int[][] cost = new int[ N ][ N ];
74-
```
75-
76-
The matrix `cost` tells us the cost (or edge weight) between two vertices
77-
78-
```java
79-
cost[ i ][ j ] = 7; // this means it costs 7 units to travel from vertex i to vertex j
80-
cost[ i ][ j ] = 0; // this usually means there is no edge from vertiex i to vertex j
81-
```
60+
![graph-theory-introduction-2](..\..\resources\graph-theory-introduction-2.png)
8261

8362
#### Pros and Cons
8463

@@ -96,19 +75,7 @@ Rather than making space for all _N_ x _N_ possible edge connections, an _adjace
9675

9776
We are able to do this by creating an array that contains `ArrayLists` holding the values of the vertices that a vertex is connected to.
9877

99-
```java
100-
ArrayList<Integer>[] graph = new ArrayList<Integer>[ N + 1 ];
101-
102-
// For each vertex, we need to initialize the list of vertices the vertex has a connection to
103-
for ( int i = 0; i <= N; i++ )
104-
{
105-
graph[ i ] = new ArrayList<Integer>();
106-
}
107-
108-
graph[ i ].add( j ); // get the list of vertices for vertex i and add a connection to vertex j
109-
110-
ArrayList<Integer> neighbors = graph[ k ]; // get the list of vertices that vertex k is connected to
111-
```
78+
![graph-theory-introduction-3](..\..\resources\graph-theory-introduction-3.png)
11279

11380
#### Pros and Cons
11481

graph-theory/shortest-path/dijkstras-algorithm/dijkstras-algorithm.md

+3-140
Original file line numberDiff line numberDiff line change
@@ -53,114 +53,15 @@ There are several ways to implement this algorithm
5353

5454
#### Using an array
5555

56-
```java
57-
// This array will keep track of the tentative distance up to each vertex
58-
int[] distance = new int[ N ];
59-
// This array will tell us if we have visited a vertex or not
60-
boolean[] visited = new boolean[ N ];
61-
62-
// Mark the tentative distance of the starting vertex as zero
63-
distance[ start ] = 0;
64-
// Set the current vertex as the starting vertex
65-
int current = start;
66-
67-
// While we have not reached our destination...
68-
while ( !visited[ destination ] )
69-
{
70-
// Mark the current vertex as visited
71-
visited[ current ] = true;
72-
// Get the neighboring vertices of the current vertex
73-
ArrayList<Integer> neighbors = graph[ current ];
74-
75-
// For each of the neighbors of the current vertex...
76-
for ( int neighbor : neighbors )
77-
{
78-
// Calculate the new distance to the neighbor
79-
// This can be calculated by taking the tentative distance up to the current vertex and adding the
80-
// cost to go from the current vertex to the neighbor
81-
int newDistance = distances[ current ] + costs[ current ][ neighbor ];
82-
83-
// If this new distance we calculated is smaller than the current tentative distance of the neighbor...
84-
if ( newDistance < distances[ neighbor ] )
85-
{
86-
// Update the tentative distance of the neighbor with this new distance
87-
distances[ neighbor ] = newDistance;
88-
}
89-
}
90-
91-
// We now need to calculate the new current vertex, which will be the vertex with the smallest tentative
92-
// distance that we have yet to visit in the graph
93-
94-
// This will keep track of the next vertex that we will set as the current vertex
95-
int index = -1;
96-
// This will keep track of the minimum tentative distance of the next current vertex
97-
int min = Integer.MAX_VALUE;
98-
99-
// For each of the vertices in the graph...
100-
for ( int j = 0; j < N; j++ )
101-
{
102-
// If we have not visited the vertex and the tentative distance of the vertex is smaller than our current min...
103-
if ( !visited[ j ] && distances[ j ] < min )
104-
{
105-
// Update the index with the vertex
106-
index = j;
107-
// Update the min distance value
108-
min = distances[ j ];
109-
}
110-
}
111-
112-
113-
// If we were unable to find another vertex to visit...
114-
if ( index == -1 )
115-
{
116-
// Break out of the loop
117-
break;
118-
}
119-
120-
// Update the current vertex
121-
current = index;
122-
}
123-
```
56+
![graph-theory-dijkstras-algorithm-1](..\..\..\resources\graph-theory-dijkstras-algorithm-1.png)
12457

12558
#### Using a priority queue
12659

12760
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.
12861

12962
First, we need to define our `Vertex` class and what attributes it will contain.
13063

131-
```java
132-
// In order for the Vertex class to be sortable, we need to implement the Comparable interface
133-
class Vertex implements Comparable<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-
public Vertex( int id )
143-
{
144-
this.id = id;
145-
this.dist = Integer.MAX_VALUE;
146-
this.neighbors = new ArrayList<Integer>();
147-
}
148-
149-
// This method needs to be overridden to determine how vertices are sorted
150-
public int compareTo( Vertex v )
151-
{
152-
// The vertex that has a smaller tentative distance should be removed from the queue first
153-
if ( this.dist < v.dist )
154-
{
155-
return -1;
156-
}
157-
else
158-
{
159-
return 1;
160-
}
161-
}
162-
}
163-
```
64+
![graph-theory-dijkstras-algorithm-2](..\..\..\resources\graph-theory-dijkstras-algorithm-2.png)
16465

16566
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.
16667

@@ -194,45 +95,7 @@ Just like BFS, we are repeating the steps of the algorithm until the queue is em
19495

19596
Now that we have our stopping condition, we can implement the rest of the algorithm
19697

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
231-
priorityQueue.add( neighbor );
232-
}
233-
}
234-
}
235-
```
98+
![graph-theory-dijkstras-algorithm-3](..\..\..\resources\graph-theory-dijkstras-algorithm-3.png)
23699

237100
## Problems
238101

resources/binary-search.png

57 KB
Loading
Loading
Loading
Loading
46.6 KB
Loading
52.4 KB
Loading
93.8 KB
Loading

0 commit comments

Comments
 (0)