Skip to content
Sahithyan's S2
1
Sahithyan's S2 — Data Structures and Algorithms

Graph

Consists of a set of nodes and a set of edges that connect pairs of nodes.

Used in many different domains to model complex relationships and solve various computational problems. Such as social networks, transportation networks, and web page links.

A fundamental point or unit. Aka. vertex. Holds a value.

Connects a pair of nodes. Aka. link. Optionally has an associated weight. Can either be directed (one-way) or undirected (two-way).

Aka. multi edge. A pair of edges that connect 2 nodes with the same source and destination.

An edge that connects a node to itself.

Two nodes are adjacent iff they are connected by an edge.

Number of edges connected to a vertex. In case of directed graphs, in-degree and out-degree are defined.

Number of edges that point towards a vertex.

Number of edges that point away from a vertex.

A sequence of vertices where each adjacent pair is connected by an edge. Number of edges in a path is the length of the graph.

A path that does not visit any vertex more than once.

A path that starts and ends at the same vertex.

A closed path without repeating any edges or vertices.

The sum of the weights of the edges in a path.

A vertex UU is reachable from vertex VV iff there exists a path from UU to VV.

A graph is complete iff every pair of distinct vertices is connected by a unique edge.

E=(V2)=V(V1)2E = \binom{V}{2} = \frac{V(V-1)}{2}

A subset of the vertices and edges of a graph.

A maximal set of vertices such that every pair is connected.

A 2-dimensional array of size V×VV \times V. The rows and columns represent the vertices of the graph. The value at each cell indicates whether there is an edge between the corresponding vertices. Better for dense graph.

Slow to add or remove vertices, because matrix must be resized/copied.

A collection of linked lists or arrays where each element represents a vertex and contains a list of its adjacent vertices. Better for sparse graph.

Slow to remove vertices and edges, because it needs to find all vertices or edges.

A 2-dimensional array of size V×EV \times E. The rows represent the vertices and the columns represent the edges. The value at each cell indicates whether the corresponding vertex is incident to the corresponding edge.

Slow to add or remove vertices and edges, because matrix must be resized/copied.

Adjacency listAdjacency matrixIncidence matrix
Store graphO(V+E)O(V+E)O(V2)O(V^2)O(VE)O(V \cdot E)
Add vertexO(1)O(1)O(V2)O(V^2)O(VE)O(V \cdot E)
Add edgeO(1)O(1)O(1)O(1)O(VE)O(V \cdot E)
Remove vertexO(E)O(E)O(V2)O(V^2)O(VE)O(V \cdot E)
Remove edgeO(V)O(V)O(1)O(1)O(VE)O(V \cdot E)
Check adjacencyO(V)O(V)O(1)O(1)O(E)O(E)
Best forSparse graphsDense graphsDense graphs, edge-centric algorithms

A graph with no self-loops or multiple edges.

  • For directed graphs: 0EV(V1)0 \le E \le V (V - 1)
  • For undirected graphs: 0EV(V1)/20 \le E \le V (V - 1) / 2

When all vertices are reachable from all other vertices.

A graph is dense iff the number of edges is close to the maximum possible number of edges. Or EV2E \approx V^2.

A graph is sparse iff the number of edges is close to the minimum possible number of edges. Or EV2E \ll V^2.

Edges have no direction. The edges are not ordered.

Edges have a direction. The edges are ordered pairs of nodes.

A connected directed graph.

EV1E \ge V - 1

When a directed graph is not connected and its undirected version is connected.

All edges have no weights associated with them.

All edges have weights associated with them.

A graph with atleast 1 cycle.

A graph with no cycles.

A connected, acyclic graph where E=V1E = V - 1.

A collection of Trees.

When a graph’s vertices can be divided into two disjoint sets such that every edge connects a vertex in one set to a vertex in the other set. No edge exists between vertices within the same set.

Does not contain odd-length cycles.