Skip to content

Commit a76024a

Browse files
committed
dfs
1 parent 6bb56c4 commit a76024a

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

graph-depth-first-search.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<div class="alert alert-note">
2+
<p>
3+
<b>✍ Note</b>
4+
</p>
5+
<p>
6+
You must know this algorithm before entering to interview room.
7+
</p>
8+
</div>
9+
10+
<div class="alert alert-tip">
11+
<p>
12+
<b>🕑 Time Complexity = O(V+E)</b>
13+
</p>
14+
<p>
15+
<b> Vertex </b>is the node like (A, B, C). The line between nodes who is connecting 2 nodes are called as <b>Edges</b>.
16+
When we traverse to each vertex and add in to array we take O(V) time.
17+
<p>
18+
When we iterate all the children nodes of the current nodes. For Node A we have 3 children nodes and there are 3 edges. For B we have 2 children nodes and 2 edges. Therefore, the Time complexity is O(E).
19+
</p>
20+
Therefore, total time complexity is O(V+E)
21+
</p>
22+
</p>
23+
<p>
24+
<img src="https://i.imgur.com/sC5Ofsm.png" />
25+
</p>
26+
</div>
27+
28+
<div class="alert alert-tip">
29+
<b> ☁ Space Complexity = O (V) </b>
30+
31+
<p>
32+
Since we are calling DFS recursively. And for every recursive call it will create one entry in the Implicit Memory Stack in computer. Imagine if you had one branch only then you could have called all the nodes (Vertices) and you will have V frames on the implicit memory stack. Therefore, the space complexity is <b> O(V) </b>. Where V=vertices.
33+
</p>
34+
</p>
35+
<p>
36+
<img src="https://i.imgur.com/s9wAlWx.png" />
37+
</p>
38+
</div>
39+
40+
41+
<p class="codepen" data-height="265" data-theme-id="dark" data-default-tab="js,result" data-user="roopkt" data-slug-hash="NWpJrjg" style="height: 265px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;" data-pen-title="Graph-Easy-DFS Answer">
42+
<span>See the Pen <a href="https://codepen.io/roopkt/pen/NWpJrjg">
43+
Graph-Easy-DFS Answer</a> by Rupesh Tiwari (<a href="https://codepen.io/roopkt">@roopkt</a>)
44+
on <a href="https://codepen.io">CodePen</a>.</span>
45+
</p>
46+
<script async src="https://cpwebassets.codepen.io/assets/embed/ei.js"></script>
47+
48+
49+

0 commit comments

Comments
 (0)