Computer ScienceData StructuresMedium

Graph (computer science)

Also known as:NetworkDirected graph (digraph)Undirected graph

A graph is a non-linear data structure consisting of a finite set of vertices (nodes) and a set of edges that connect pairs of vertices. Graphs can be directed (edges have a direction) or undirected, and weighted (edges carry a cost) or unweighted. They are used to model real-world problems such as social networks, transportation systems, web page link structures, and dependency resolution.

Graph Representation Methods Comparison

RepresentationSpaceAdd EdgeCheck EdgeBest For
Adjacency MatrixO(V²)O(1)O(1)Dense graphs
Adjacency ListO(V + E)O(1)O(degree)Sparse graphs
Edge ListO(E)O(1)O(E)Edge-centric algorithms
Incidence MatrixO(V × E)O(1)O(E)Hypergraphs

Interactive Tools

Visualgo — Graph Traversal

Open Tool

Graph Online

Open Tool

Khan Academy — Graph Representation

Open Tool
An undirected graph with six vertices and edges connecting them

Wikimedia Commons, CC BY-SA

Related Terms

The mathematical study of graphs was initiated by Leonhard Euler in 1736 with his solution to the Königsberg bridge problem. The word "graph" derives from the Greek "graphein," meaning "to write or draw," referring to the visual representation of vertices and edges.

graphnetworkverticesedgesdata-structurealgorithms