An adjacency list is just a vector / array of lists. Each element in the graph is an element in the array, and any addition of it is added to the adjacency list. So it looks something like this:
A β {B, C}
B β {A, C, D, E}
C β {A, B}
D β {B, E}
E β {B, D}
So, we start with something like std::vector<std::list<vertex>> . However, we can do it better because the verticals are unique, so we can use map . In addition, a vertex can only appear in the list of edges once, so we change it to std::map<vertex, std::set<vertex>> .
So, for starters, something like:
struct vertex { // }; class undirected_graph { private: std::map<vertex, std::set<vertex>> graph_container; public: void add_vertex(const vertex& v) { //add a vertex to the map } void add_edge(const vertex& v, const vertex& u) { //look up vertex in map and add to the vertex adjacency list } //Other methods //... };
Yuushi
source share