Graph Data Structure
A graph is an abstract data type that represents a set of vertices and the edges that connect those vertices.
When an algorithm traverses a graph, it typically moves across the edges. For example, the traveling salesman problem non-deterministic-polynomial-time is best represented using a graph data structure.
Properties
- Graphs can have between 0 and n vertices, there’s no maximum
- Graphs can have between 0 and n(n - 1)/2 edges, where n is the number of vertices
- Vertices don’t need to be connected at all (they can have no edges). That said, they may be unreachable if this is the case
- An edge connects exactly 2 vertices, and there can’t be 2 edges between the same two vertices.
- Some graphs (called weighted graphs) assign a “cost” to each edge. For example, if a graph represented a geographical map of cities in the real world, the cities would be vertices and the edges would be roads. The weight on the roads could be their lengths.
References
- boot.dev
Next -> adjacency-matrix-for-graphs
Next -> types-of-graph
#computer_science #search #function #structure #boot_dev #hash #graph #programming #data #memory