Skip to content

Conversation

@Jmendoza194
Copy link
Contributor

Closes #493

...

Changes proposed in this pull request:

  • Created the checkpoint cards and Big O analysis

@reviewer/mxthu313

@Jmendoza194 Jmendoza194 added the Stage 1: Pre-Review Stage 1 (Pre-Review): DevRel Stage 1 label Mar 8, 2020
@Jmendoza194 Jmendoza194 requested a review from mxthu313 March 8, 2020 05:10
@Jmendoza194 Jmendoza194 self-assigned this Mar 8, 2020
Copy link
Contributor

@mxthu313 mxthu313 left a 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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code formatting

mxthu313 and others added 13 commits March 8, 2020 21:51
typo fix
fix big O
Closes #617

Closes #621

Closes #624

Closes #625

...

Changes proposed in this pull request:

big o analysis
concept clarification
/spend 3 hr

@reviewer/kavuong
Fixed code formatting
Fixed Code formatting
Fixed formatting
Fixed Code Formatting
new hint format
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Stage 1: Pre-Review Stage 1 (Pre-Review): DevRel Stage 1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Activity 4.3.1] Searching and Sorting: Create, Context

4 participants