What is a graph?
Answer
A graph is a non-linear data structure consisting of vertices (nodes) and edges (connections between nodes). Formally G = (V, E). Types: Undirected — edges have no direction (Facebook friends); Directed (digraph) — edges have direction (Twitter follows, website links); Weighted — edges have a numerical weight/cost (road distances, flight costs); Unweighted — edges are equal; Cyclic — contains at least one cycle; Acyclic — no cycles; DAG (Directed Acyclic Graph) — directed, no cycles (dependency graphs, task scheduling). Representations: (1) Adjacency Matrix: V×V matrix, adj[i][j]=1 if edge exists. Space: O(V²). Edge lookup O(1), but wastes space for sparse graphs; (2) Adjacency List: array of lists, each containing neighbors. Space: O(V+E). Most common for sparse graphs; (3) Edge List: list of all edges as pairs. Useful for algorithms like Kruskal's. Real-world graphs: social networks, internet routing, maps, dependencies, recommendation systems.