Graph

Graph

A graph is a collection of nodes that are connected to each other by edges. Edges can be either directed (one-way) or undirected and they can also have weights (values). A tree can be considered an undirected graph with no cycles.

OperationBig-O
Depth-first searchO(N + E)
Breadth-first searchO(N + E)
Topological sortO(N + E)

N being number of nodes and E being number of edges

Graph representation

  • Adjacency matrix
  • Adjacency list
  • Hash table of hash tables

Corner cases

  • Empty graph
  • Graph with 1/2 nodes
  • Disconnected graphs
  • Graph with cycles, keep track of visited nodes

Graph traversal and sorting

References