A graph consists of two main components: vertices also called nodes which represent entities or points, and edges which represent connections or relationships between vertices. This is the fundamental definition of a graph in data structures.
Vertices can represent any entity like cities, people, web pages, or computers. Edges represent relationships like roads between cities, friendships between people, or network connections. Edges can be directed showing one-way relationships or undirected showing two-way relationships. Some graphs also have weighted edges where each edge has an associated value representing distance, cost, or capacity.
Correct Answer: Vertices and Edges
Example Code
// Graph representation
class Graph {
int vertices; // Number of vertices
List<List<Integer>> adj; // Adjacency list
}
// Example Graph:
// Vertices: {A, B, C, D}
// Edges: {(A,B), (A,C), (B,D), (C,D)}
//
// A --- B
// | |
// C --- D
//
// Adjacency list representation:
// A: [B, C]
// B: [A, D]
// C: [A, D]
// D: [B, C]
// Directed graph example:
// A --> B
// ↓ ↓
// C --> D
// A: [B, C]
// B: [D]
// C: [D]
// D: []