This video explains two primary methods for representing graphs in C++: adjacency matrix and adjacency list. It covers how to take graph input, demonstrates the implementation of each representation, and discusses their respective space complexities, highlighting why adjacency lists are generally preferred. The video also briefly touches upon how to adapt these representations for weighted graphs and directed graphs.
matrix[i][j] indicates an edge between node i and node j. For undirected graphs, the matrix is symmetric. The space complexity is O(n^2), which can be inefficient for sparse graphs.i stores a list (or vector) of nodes adjacent to node i. This is more space-efficient for sparse graphs, with a space complexity of O(V + E), where V is the number of vertices and E is the number of edges. For undirected graphs, each edge is stored twice (once for each node it connects).In 1-based indexing, if you have n nodes, they are typically numbered from 1 to n. To accommodate these n nodes directly using their numbers as indices in an array or matrix, you need an array or matrix that can hold indices from 1 up to n. This requires a size of n + 1 to include the index n. The index 0 is often left unused or is used for a specific purpose depending on the implementation, but the key is that all n nodes (1 through n) have a corresponding index available.