DSA - Graph

Graph = Nodes + Edges

  • dfs - stack
  • bfs - queue

Code Snippets

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# dfs
class Solution():
  """
  {
    "a": ["b", "c"]
  }
  """
  def dfs(self, adj, src):
    st = [src]
    while st:
      cur = st.pop()
      print(cur)
      for nxt in adj[cur]:
        st.append(nxt)
Licensed under CC BY-NC-SA 4.0
Get Things Done
Built with Hugo
Theme Stack designed by Jimmy