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: Notes/Graph-Theory.md
+70-1Lines changed: 70 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -144,7 +144,7 @@ Above is another visual representation of the BFS process starting from the top
144
144
145
145
We need to use some data structure that will allow us to visit vertices "_level-by-level_", that is, visit every vertex at level _j_ before we visit any vertex at level _j_+1.
146
146
147
-
In order to do this, we will be using a `Queue` since it follows the "_first in, first out_" ordering; this means if we put all the vertices at level _j_ into the queue before the vertices at level _j_+1, we are guaranteed to visit the lower level vertices first.
147
+
In order to do this, we will be using a _Queue_ since it follows the "_first in, first out_" ordering; this means if we put all the vertices at level _j_ into the queue before the vertices at level _j_+1, we are guaranteed to visit the lower level vertices first.
148
148
149
149
Below are the steps to follow for BFS:
150
150
1. Push the root vertex onto the queue
@@ -184,3 +184,72 @@ In order to do this, we will be using a `Queue` since it follows the "_first in,
184
184
}
185
185
}
186
186
```
187
+
188
+
## Depth-first Search
189
+
190
+
_Depth-first search_ (or DFS) is another form of graph traversal, but rather than visiting vertices "_level-by-level_", DFS aims to go as deep as possible in the graph before backtracking.
Imagine a "family tree", like one shown in the picture above. DFS will go as deep as it can in the graph before backtracking and repeating this process.
195
+
196
+
Starting at vertex 1, the graph will travel down the left side of the graph until it hits vertex 4, where it can no longer visit any unvisited vertices.
197
+
198
+
From vertex 4, it backtrack to vertex 3 and visits vertex 5, and since it can no longer visit any unvisited vertices, it backtracks to vertex 2, where it visits vertex 6.
199
+
200
+
This process repeats until the entire graph has been traversed.
Above is another visual representation of the DFS process starting from the top vertex.
205
+
206
+
### Coding DFS
207
+
208
+
The algorithm for DFS is very similar to that of BFS, except instead of using a _Queue_, we will be using a _Stack_.
209
+
210
+
Since a _Stack_ follows the "_last in, first out_" ordering, when we are adding neighbors of a vertex to the _Stack_, the last one we push will be the next one we visit, allowing us to constantly go deeper into the graph rather than traversing level-by-level.
211
+
212
+
```java
213
+
// Initialize the stack
214
+
Stack<Integer> stack =newStack<Integer>();
215
+
216
+
// This array will tell us if we have visited a vertex
217
+
boolean[] visited =newboolean[ N ];
218
+
219
+
// Push the root vertex onto the stack
220
+
stack.push( rootVertex );
221
+
222
+
// While there is a vertex still in the stack...
223
+
while ( !stack.isEmpty() )
224
+
{
225
+
// Get the current vertex
226
+
Integer current = stack.pop();
227
+
// Get the current vertex's neighbors
228
+
List<Integer> neighbors = graph.get( current );
229
+
230
+
// For each of the current vertex's neighbors...
231
+
foreach ( Integer neighbor : neighbors )
232
+
{
233
+
// If we haven't visited the neighbor...
234
+
if ( !visited[ neighbor ] )
235
+
{
236
+
// Add the neighbor to the stack
237
+
stack.push( neighbor );
238
+
// Mark the neighbor as visited
239
+
visited[ neighbor ] =true;
240
+
}
241
+
}
242
+
}
243
+
```
244
+
245
+
## Trees
246
+
247
+
A _tree_ is a special kind of graph that exhibits the following properties:
248
+
1. Acyclic graph
249
+
2._N_ vertices with _N_-1 edges
250
+
251
+
The graphs above that we used for BFS and DFS were both trees.
0 commit comments