Skip to content

Commit 1bdf6a9

Browse files
authored
Update Graph-Theory.md
1 parent 93b7382 commit 1bdf6a9

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

Notes/Graph-Theory.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,20 @@ _Cons_
9494

9595
Rather than making space for all _N_ x _N_ possible edge connections, an _adjacency list_ keeps track of the vertices that a vertex has an edge to.
9696

97-
We are able to do this by creating an `ArrayList` that contains `ArrayLists` holding the values of the vertices that a vertex is connected to.
97+
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.
9898

9999
```java
100-
ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>();
100+
ArrayList<Integer>[] graph = new ArrayList<Integer>[ N + 1 ];
101101

102102
// For each vertex, we need to initialize the list of vertices the vertex has a connection to
103103
for ( int i = 0; i <= N; i++ )
104104
{
105-
graph.add( new ArrayList<Integer>() );
105+
graph[ i ] = new ArrayList<Integer>();
106106
}
107107

108-
graph.get( i ).add( j ); // get the list of vertices for vertex i and add a connection to vertex j
108+
graph[ i ].add( j ); // get the list of vertices for vertex i and add a connection to vertex j
109109

110-
ArrayList<Integer> neighbors = graph.get( k ); // get the list of vertices that vertex k is connected to
110+
ArrayList<Integer> neighbors = graph[ k ]; // get the list of vertices that vertex k is connected to
111111
```
112112

113113
#### Pros and Cons
@@ -168,7 +168,7 @@ In order to do this, we will be using a _Queue_ since it follows the "_first in,
168168
// Get the current vertex
169169
Integer current = queue.remove();
170170
// Get the current vertex's neighbors
171-
List<Integer> neighbors = graph.get( current );
171+
ArrayList<Integer> neighbors = graph[ current ];
172172

173173
// For each of the current vertex's neighbors...
174174
foreach ( Integer neighbor : neighbors )
@@ -225,7 +225,7 @@ Since a _Stack_ follows the "_last in, first out_" ordering, when we are adding
225225
// Get the current vertex
226226
Integer current = stack.pop();
227227
// Get the current vertex's neighbors
228-
List<Integer> neighbors = graph.get( current );
228+
ArrayList<Integer> neighbors = graph[ current ];
229229

230230
// For each of the current vertex's neighbors...
231231
foreach ( Integer neighbor : neighbors )

0 commit comments

Comments
 (0)