Undirected Graph

No arrows for the edges.

Definition, notation:

A graph is pair :

  • V is a finite set, whose elements are called vertices
  • E is a finite set, whose elements are unordered pairs of distinct vertices, and are called edges. Convention: n is the number of vertices, m is the number of edges.

Data structures

  • adjacency list: an array s.t. is the linked list of all edges connected to . list cells, total size , but testing if an edge exists is not .
    • (Each edge creates two cells. that is why it’s 2m).
  • adjacency matrix: a matrix of size , with iff is an edge. size , but testing if an edge exists is
    • 0 if v,w is not an edge, and 1 if v,w is an edge

Which one is better?

It depends. If you have a graph that is not full, that has a few edge, the number of edge can be linear, so choosing the list would give you instead of .

Choose the matrix if there is a dense amount of edges.

Can you compute BFS on an undirected graph to get the connected components?

Yes, you can use BFS (Breadth-First Search) to compute the connected components of an undirected graph. Here’s how you can do it: