Graph Basics

some pseudo ( not python ) code for traversal in a graph:
visited  =  {}
Depth First -  going  depth wise  first
def dfs( graph, start ):
visit(start)
visited.add(start)
for  node  in  graph.adj[start]:
if  visited.contains(node):
continue
else
dfs(graph, node)

Breadth First  -  visiting nearer nodes first w.r.t. distance measured using number of edges
visited = {}
queue =  {}
distance  =  {}
def bfs( graph, start ):
distance[start] = 0
queue.push(start)
while(queue is not empty ) :
node  =  queue.top()
queue.pop()
visit(node)
visited.add(node)
for  x  in  graph.adj[node]:
if  visited.contains(x):
continue
else:
queue.push(x)
distance[x] =  distance[node]+1

Time Complexity for both DFS and BFS = O(E+V)
Space complexity for both = O(V)
where:
E = number  of  edges
V = number  of  vertices
DFS is bit slower and less space efficient since we are pushing and popping activation records and activation record stack may generally take up more space compared to queue