What is better, adjacency lists or adjacency matrices for graph problems in C++?

后端 未结 11 1099
小鲜肉
小鲜肉 2020-11-28 00:41

What is better, adjacency lists or adjacency matrix, for graph problems in C++? What are the advantages and disadvantages of each?

11条回答
  •  孤独总比滥情好
    2020-11-28 01:28

    Okay, I've compiled the Time and Space complexities of basic operations on graphs.
    The image below should be self-explanatory.
    Notice how Adjacency Matrix is preferable when we expect the graph to be dense, and how Adjacency List is preferable when we expect the graph to be sparse.
    I've made some assumptions. Ask me if a complexity (Time or Space) needs clarification. (For example, For a sparse graph, I've taken En to be a small constant, as I've assumed that addition of a new vertex will add only a few edges, because we expect the graph to remain sparse even after adding that vertex.)

    Please tell me if there are any mistakes.

    enter image description here

提交回复
热议问题