Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

depth first search added #8

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

Merged
merged 18 commits into from
Dec 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions algorithms/8_depth_first_search/dfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# function for depth first search
def dfs(data, start, visited=set()):

# if not visited print it
if start not in visited:
print(start, end=" ")

visited.add(start)

for i in data[start] - visited:

# if not visited gi in depth of it
dfs(data, i, visited)
return


# sample data in dictionary form
data = {
'A': {'B'},
'B': {'A', 'C', 'D'},
'C': {'B', 'E'},
'D': {'B', 'E'},
'E': {'C', 'D', 'F'},
'F': {'E'}
}


if __name__ == '__main__':

dfs(data, 'A')
29 changes: 29 additions & 0 deletions algorithms/8_depth_first_search/dfs_exercise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# function for depth first search
def find_employees(data, start, employee, visited=set()):
# if not visited print it
if start not in visited:
print(start, end=" ")
if start == employee:
print(":", end=" ")
visited.add(start)

for i in data[start] - visited:
# if not visited go in depth of it
find_employees(data, i, visited)
return


# sample data in dictionary form
data = {
"karan": {"darshan", "nikhil"},
"darshan": {"khantil", "tanuj"},
"tanuj": {"nikhil"},
"krinish": {"hetul"},
"khantil": set(),
"nikhil": set()
}


if __name__ == '__main__':

find_employees(data, "karan", "karan")
36 changes: 36 additions & 0 deletions algorithms/8_depth_first_search/dfs_exerscise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# DFS Exercise

![Overview : Employee hierarchy](https://github.com/beladiyadarshan/DFS/blob/main/emp.png?raw=true)

Given a graph containing managers and their employees as a dictionary of sets, print all employees reporting to a given manager.

```
data = {
"karan": {"darshan","nikhil"},
"darshan": {"khantil", "tanuj"},
"tanuj": {"nikhil"},
"krinish": {"hetul"},
"khantil" : set(),
"nikhil" : set()
}


```

Example: Darshan and Nikhil are reporting to Karan. Khantil and Tanuj are reporting to Darshan.

```
Q. Find all employees who are reporting to Karan.
```

**Explanation:**

-So here, we want to find all the children nodes of Karan.

-We will perform DFS starting on Karan and then traverse all the children of Karan which are unvisited.

**Output:**

karan : nikhil darshan tanuj khantil

[Solution](https://github.com/beladiyadarshan/DFS/blob/main/dfs_exercise.py)
Binary file added algorithms/8_depth_first_search/emp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added algorithms/8_depth_first_search/updated_graph.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.