Here is an SO post of an adjacency list. However I see no difference from a single-linked list? Also here is a wikipedia article which says that it is all the edges (of a graph, discrete math type) in a list which is pretty broad, if I have a graph, which is not a path graph. How do I code an adjacency list?
问题:
回答1:
A simple example: Suppose you have a vertex type Vertex
. They your graph consists of a set of vertices, which you can implement as:
std::unordered_set<Vertex> vertices;
Now for every pair of vertices between which there is an edge in your graph, you need to record that edge. In an adjacency list representation, you would make an edge type which could be a pair of vertices, and your adjacency list could simply be a list (or again a set) of such edges:
typedef std::pair<Vertex, Vertex> Edge; std::list<Edge> edges_as_list; std::unordered_set<Edge> edges_as_set;
(You will probably want to supply the last set with an undirected comparator for which (a,b) == (b,a)
if you have an undirected graph.)
On the other hand, if you want to make an adjacency matrix representation, you would instead create an array of bools and indicate which vertices have edges between them:
bool edges_as_matrix[vertices.size()][vertices.size()]; // `vector` is better // edges_as_matrix[i][j] = true if there's an edge
(For this you would need a way to enumerate the vertices. In an undirected graph, the adjacency matrix is symmetric; in a directed graph it need not be.)
The list representation is better if there are few edges, while the matrix representation is better if there are a lot of edges.
回答2:
struct Node { std::vector<std::shared_ptr<Node>> Neighbors; }; struct AdjacencyList{ std::vector<std::shared_ptr<Node>> Nodes; };
AdjacencyList
holds an array of all of the Node
s, and each Node
has links to all the other Node
s in the list. Technically the AdjacencyList
class isn't required, all that's required is a std::shared_ptr<Node> root;
, but having an the AdjacencyList
with a vector
makes iterating over them, and many other things, much easier.
A----B F | |\ | | \ C D--E
AdjacencyList
holds pointers toA
,B
,C
,D
,E
, andF
.A
has pointers toB
andC
.B
has pointers toA
andD
andE
.C
has pointers toA
.D
has pointers toB
andE
.E
has pointers toB
andD
.F
has pointers to no other nodes.
The AdjacencyList must contain pointers and not Node
values, or else when it resizes, all the Neighbors
pointers would be invalidated.
回答3:
So, Boost includes an adjacency_list container as part of boost::graph.
In general, and adjacency list is more than a singly linked list. It describes the direct connections (potentially, of a given direction) of any vertex to other vertices in the graph.
The boost docs provide a few basic examples, with pictures.