Graphs
Graphs as a Data Structure
Section titled “Graphs as a Data Structure”Use linked-list concepts to implement graphs. A graph is a collection of nodes in which each node can have several neighboring nodes.
An example graph is provided here.
-
Define the structure of a graph node. It should have an
intfield for the node’s value and a dynamic buffer for its neighboring nodes. You can use code from static_buffer or dynamic_array.You can use
std::vector, but then make sure you understand RAII. -
Implement one of the following graph configurations:
-
In directed graphs, node
Acan be a neighbor of nodeB, while nodeBis not necessarily a neighbor of nodeA. In undirected graphs, nodesAandBare always neighbors of each other. Explain how this idea is reflected in the graph’s memory layout. -
Write a function that calculates the sum of the values of a given node’s neighbors.
-
Implement the DFS and BFS traversal algorithms. You can add additional information to the nodes themselves.