-
Notifications
You must be signed in to change notification settings - Fork 18
Did issue 493 #851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Did issue 493 #851
Conversation
mxthu313
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code formatting
| 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code formatting
|
|
||
| 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code formatting
|
|
||
| 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code formatting
|
|
||
| Here is the completed code: | ||
|
|
||
| ```python |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code formatting
|
|
||
| ``` | ||
| mid = low + (high - low) / 2 | ||
| ```Python |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code formatting
| 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
code formatting
Fixed code formatting
Fixed Code formatting
Fixed formatting
Fixed Code Formatting
new hint format
Jose 4.3.2 sorting
Closes #493
...
Changes proposed in this pull request:
@reviewer/mxthu313