A graph G . Excerpt from The Algorithm Design Manual While there are several possible variations, the two basic data structures for graphs are adjacency matrices and adjacency lists . If your graph has 100 vertices, your adjacency matrix contains 10,000 entries. If your graph has 1,000 vertices, your adjacency matrix contains 1,000,000 entries.
Adjacency matrices work only for small or very dense graphs.
However, most graph algorithms can be modified to eliminate such queries. However, more likely than modifying the topology of graph is modifying the attributes of a vertex or edge of the graph, such as size, weight, or color. Attributes are best handled as extra fields in the vertex or edge records of adjacency lists. Building a good general-purpose graph type is surprisingly tricky and difficult. Note that it costs only time linear in the size of the larger data structure to convert between adjacency matrices and adjacency lists.