Skip to content

challenge 38 #37

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
|33|hashmap-left-join|[hashmap-left-join](./challenges/hash_table/hashmap-left-join/README.md)|
|35|Graphs|[Graphs](./data-structures/graph/README.md)|
|36|Graphs-breadth-first|[Graphs-breadth-first](./data-structures/graph/README_BF.md)|
|37|graph-business-trip|[graph-business-trip](./data-structures/graph/graph_business_trip/README.md)|
|37|graph-business-trip|[graph-business-trip](./challenges/graph/graph_business_trip/README.md)|
|38|graph-depth-first|[graph-depth-first](./data-structures/graph/README_DF.md)|



Expand Down
20 changes: 20 additions & 0 deletions data-structures/graph/README_DF.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Challenge Summary
create function that takes node as input and return all nodes exists in graph in depth first order

## Whiteboard Process
![image](./assets/challenge-38-graph-depth-first.png)

## Approach & Efficiency
time:O(n)
space:O(n)

## Solution
- declare empty set(visited)
- declare empty queue
- enqueue the starting node
- create while loop and the condition that if the queue is not empty
- dequeue the node from the queue to the variable (current)
- add current to the set(visited)
- get the neighbors of the current
- enqueue the neighbors if its not in the set(visited)
- after the while return the nodes array
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified data-structures/graph/graph/__pycache__/graph.cpython-39.pyc
Binary file not shown.
26 changes: 24 additions & 2 deletions data-structures/graph/graph/graph.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
sys.path.append("/home/bayan/code-401/data-structures-and-algorithms-401/data-structures/stack_and_queue")
from stack_and_queue.stack_and_queue import Queue
from stack_and_queue.stack_and_queue import Queue, Stack

class Node:
def __init__(self, value):
Expand Down Expand Up @@ -76,6 +76,28 @@ def breadth_first(self, node):

return nodes

def depth_first(self, node):
nodes=[]
stack= Stack()
visited= set()

if node not in self.adjacency_list or self.adjacency_list[node]==[]:
return None

stack.push(node)
visited.add(node.value)

while not stack.isEmpty():
vertix=stack.pop()
nodes.append(vertix.value)

for edge in self.adjacency_list[vertix]:
if edge.node.value not in visited:
visited.add(edge.node.value)
stack.push(edge.node)

return nodes



if __name__ == '__main__':
Expand Down Expand Up @@ -108,7 +130,7 @@ def breadth_first(self, node):
# graph.add_edge(a,b,5)
# graph.add_edge(a,c,7)
# print(graph.breadth_first(b))
print (graph)
print (graph.depth_first(e))



Binary file not shown.
11 changes: 10 additions & 1 deletion data-structures/graph/tests/test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ def test_breadth_first(graph_3):
actual=graph_3[0].breadth_first(graph_3[1])
assert actual == expected

def test_depth_first(graph_3):
expected=['b', 'f', 'e', 'd', 'a', 'c']
actual=graph_3[0].depth_first(graph_3[1])
assert actual == expected

def test_depth_first1(graph_3):
expected=['e', 'f', 'b', 'd', 'a', 'c']
actual=graph_3[0].depth_first(graph_3[2])
assert actual == expected


@pytest.fixture
Expand Down Expand Up @@ -124,4 +133,4 @@ def graph_3():
graph.add_edge(f, b)
graph.add_edge(f, e)

return graph, b
return graph, b, e