Open
Conversation
mxthu313
reviewed
Mar 9, 2020
| The function then enters the `for` loop. Just as in BFS, this `for` loop runs for each of the adjacent vertices of `v`. That is, `self.graph[v]` contains a list of all of the vertices from which there is an edge from `v` to it (see chart labeled *Graph In Terms of Adjacent Vertices* in the diagram below). The variable `i` stores this adjacent vertex throughout the `for` loop. The `for` loop contains an `if` statement that checks if said adjacent vertex (`i`) is unvisited (`visited[i] == False`). If this is the case, we recurse and call `DFSUtil`again, passing as arguments the adjacent vertex (`i`) and our updated `visited` array. | ||
|
|
||
| We will find the mid again and determine it to be 5 + (9-5) / 2 = 7. Since the value at location 7 is 41, we know that the target value should be in the lower portion of the remaining array (at an index less than 7, but greater than 4). | ||
| ```python |
|
|
||
| In binary search, the worst case is when the algorithm cannot find what it is looking for. In that case, the algorithm will keep on halving the list until it can no longer do so (meaning there is only one element left in the list). In that scenario, the worst case time complexity would be O(logn) time, becuase you will NEVER actually iterate through the whole list.This can be seen in the elif and else statements of the code: | ||
|
|
||
| ```python |
|
|
||
| Here is the code for `DFSUtil` in full. We will break it down step-by-step, but first, I wish to bring your attention to something important in these lines. What do you notice about the last line of the code? It seems like `DFSUtil` is calling the function `DFSUtil`. That is, `DFSUtil` is calling itself! When a function calls itself in its own body, it is known as a recursive function. Recognizing this is essential to understanding how the DFS algorithm is implemented. | ||
|
|
||
| ```python |
|
|
||
| Here is the completed code: | ||
|
|
||
| ```python |
|
|
||
| ``` | ||
| mid = low + (high - low) / 2 | ||
| ```Python |
| Our else case tells us that if the target element is not in the middle and it is not to the left, it must be on the right (and greater in value than the middle element's value). | ||
| Well, as it turns out, the while loop is actually hidden in the code above. Instead of looping through the vertices using a while loop, what the code does is that it recurses. What that means is that it calls itself n times for each and every vertex inside the graph we are traversing, thus making the while loop hidden. This can be seen in the last line of the `DFSUtil` function: | ||
|
|
||
| ```python |
Fixed code formatting
Fixed Code formatting
Fixed formatting
Fixed Code Formatting
new hint format
Jose 4.3.2 sorting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #493
...
Changes proposed in this pull request:
@reviewer/mxthu313