Skip to content

Graph Note

Xin Wan edited this page Feb 12, 2018 · 27 revisions

How to represent a graph?

  1. adjacent matric
class Graph {
    boolean[][] adjacentMatrix;
}

class Graph {
    Weight[][] adjacentMatric;
}

Space: O(|V|^2) Pros: Lookup edge for a certain pair - O(1) Cons: Possible sparse matrix if |E| << |V|

  1. Adjacent List
class Vertex {
    int id;
    List<Vertex> neighbors;
}

class Graph {
    List<Vertex> vertices;
}

Pros: space: O(|V| + |E|)

Clone this wiki locally