Graph

  • Trees and Hierarchical Relationships
    • Trees are fantastic for representing strict hierarchical relationships.
      • But not every relationship is hierarchical.
      • Example: Paris Metro map.
        • That is not a Tree. that contains cycles
  • A graph consists of:
    • A set of nodes
    • A set of zero or more edges, each of which connects two nodes
  • all trees are graphs
  • Graph Problems

Graph Traversal

在设计算法的时候,选择适合的 Graph API 非常重要

Efficient

  • DFS is worse for spindly graphs. Imagine a graph with 10000 nodes all spindly. We'll end up making 10000 recursive calls, which is bad for space.
  • BFS is worse for "bushy" graphs, because our queue gets used a lot.