Skip to content

Commit 93b7382

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

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

Notes/Graph-Theory.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ Above is another visual representation of the BFS process starting from the top
144144

145145
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.
146146

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.
148148

149149
Below are the steps to follow for BFS:
150150
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,
184184
}
185185
}
186186
```
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.
191+
192+
![img](https://upload.wikimedia.org/wikipedia/commons/thumb/1/1f/Depth-first-tree.svg/300px-Depth-first-tree.svg.png)
193+
194+
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.
201+
202+
![img](https://upload.wikimedia.org/wikipedia/commons/7/7f/Depth-First-Search.gif)
203+
204+
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 = new Stack<Integer>();
215+
216+
// This array will tell us if we have visited a vertex
217+
boolean[] visited = new boolean[ 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.
252+
253+
![img](https://upload.wikimedia.org/wikipedia/commons/thumb/2/24/Tree_graph.svg/180px-Tree_graph.svg.png)
254+
255+
Above is another example of a tree.

0 commit comments

Comments
 (0)