-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Changes from 6 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
64ea23c
Create dfs.py
beladiyadarshan 44b16f5
Update dfs.py
beladiyadarshan 5bf76d5
Add files via upload
beladiyadarshan cf7917f
Update dfs.py
beladiyadarshan f5c4289
Update Dfs_exercise.py
beladiyadarshan 5f1c5f8
Update Dfs_Exerscise.md
beladiyadarshan e381bc8
Update Dfs_Exerscise.md
beladiyadarshan d6acab4
Update Dfs_exercise.py
beladiyadarshan 23a6cc9
flake 8 convention followed
c199807
Modified md file
4bf2c68
Rename Dfs_exercise.py to dfs_exercise.py
beladiyadarshan 7dbdb25
Update and rename Dfs_Exerscise.md to dfs_exerscise.md
beladiyadarshan 557eceb
corrected md file
f9284e0
Merge branch 'master' of https://github.com/beladiyadarshan/data-stru…
a1b46fb
Delete settings.json
groverkds 074c6ca
Update dfs_exercise.py
groverkds 57f9f13
Update dfs_exerscise.md
groverkds 1d7c308
Update dfs_exercise.py
groverkds File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# DFS Exercise | ||
|
||
 | ||
|
||
Given a Graph in Dictionary Form | ||
|
||
-print all employees who are reporting to given employee | ||
beladiyadarshan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
``` | ||
data = {"karan": {"darshan","nikhil"}, | ||
"darshan": {"khantil", "tanuj"}, | ||
'tanuj': {"nikhil"}, | ||
"krinish": {"hetul"}, | ||
"khantil" : set(), | ||
"nikhil" : set() | ||
} | ||
|
||
|
||
``` | ||
|
||
here darshan and nikhil are reporting to karan and so on.... | ||
|
||
``` | ||
Q.find all employees who are reporting to karan | ||
-do dfs on karan and print all the employees | ||
``` | ||
|
||
**Explanation :** | ||
|
||
-so here we wants to find all the childs of karan | ||
|
||
-we will do dfs on karan and then we will traverse all the childs of karan that are not visited | ||
|
||
**output will be :** | ||
|
||
karan : nikhil darshan tanuj khantil | ||
|
||
[Solution](https://github.com/beladiyadarshan/Dfs/blob/main/Dfs_exercise.py) | ||
|
||
|
||
|
||
|
||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#function for depth first search | ||
def dfs(data, start,emp,visited=set()): | ||
beladiyadarshan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
#if not visited print it | ||
if start not in visited: | ||
print(start,end=" ") | ||
|
||
if start==emp: | ||
print(":",end=" ") | ||
|
||
|
||
visited.add(start) | ||
|
||
|
||
for i in data[start] - visited: | ||
|
||
#if not visited go in depth of it | ||
dfs(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__': | ||
|
||
dfs(data, "karan","karan") |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#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') |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
Uh oh!
There was an error while loading. Please reload this page.