Graph Traversal

As for Trees, we can traverse a Graph by using DFS or BFS.

The complexity of the two algorithms is for both time and space, where:

  • 1 represents the number of the Vertices/Nodes.
  • 1 represents the number of the Edges.

In fact, we need time to build the Adjacency List and time to push and pop elements to and from the stack, respectively.

Regarding the space, we need to store edges in the dictionary and Vertices/Nodes in the set.

1

means the cardinality of the set , which is the number of elements.