Vertices: node (v) Edge: lines (e=[0, v(v-1)/2]) Endpoints: at the end of edges Un-directed: (A, B) = (B, A) No Self-Edge: no (V, V) Neighbors: vertices' connected to Subgraph: a portion of the graph Complete graph: every vertex connects
Board Configuration: 2^{n^2} Possible Moves: n^2 Vertices: 2^{n^2} Edges: n^2 * 2^{n^2} / 2
Implicit graph: We don't need a graph data structure
it will be too big to graph all possibilities
we explore graph, we never build graph
Explicit graph: data structure in memory
Vertices: node (v) Edge: lines (e=[0, v(v-1)/2]) Neighbors: [0, min(v, e)] Cost: e \in O(v^2), but might be lower if we use e too.
Linked list of edges | Hash set of edges | Matrix | List | ||
---|---|---|---|---|---|
graph_hasedge | O(e) | O(1) avg+amt | O(1) | O(min(v,e)) | |
graph_addedge | O(1) | O(1) | O(1) | O(1) | |
graph_get_neighbors | O(e) | O(e) | O(v) | O(1) cuz grab | |
space | O(v^2) | O(v+e)\O(max(v,e)) |
iterate neighbors: O(1)*O(min(v,e)) dense graph: e \in O(v^2) sparse graph: e \in O(v) \lor e \in O(vlog(v)) (we typically work with sparse graphs)
adjacency matrix mirrors
nothing in the middle
Next Neighbors - Adjacency list costs O(v+e) - Adjacency matrix costs O(v^2) - for dense graph they are the same, for sparse graph Adjacency list is better
Table of Content