Skip to content
M397's Blog
Go back

Daily DSA 02: Graphs

Edit page

We continue with the Daily DSA series. The obvious choice after the Queue yesterday would be the stack. But because this would again be really basic we’re continuing with a bit more of an advanced topic (but no worries, still easy to learn).

Graphs

Simply days a graph is a bunch of points connected with lines. We call the points vertices (or a single one vertex) and the connecting lines edges.

We write this as G=(V,E)G = (V, E) where VV is the set of vertices and EE the set of edges. For analysis we often use n=∣V∣n = |V| and m=∣E∣m = |E|.

Graphs are very useful because they can represent a lot of real-world and also other theoretical problems. Examples would be: A street network, dependencies, a communication network, or a production chain.

Variations

Normally we have graphs in which there is at most a single edge between two vertices (one per direction for directed graphs) and no self loops.

In a Multigraph multiple edges between two vertices are allowed. I don’t even know if there is a special name for graphs with self loops.

Paths and Walks

A path of length kk is a series of vertices (v0,v1,...,vk)(v_0, v_1, ..., v_k) where ∀i,j:vi≠vk\forall i,j: v_i \neq v_k. Notice that the length is the amount of edges and not the amount of vertices.

A walk is similar, with the difference that it allows duplicate vertices.

Vertices

Vertices are the nodes of the graph. They can theoretically be any object and have metadata associated with them. In practical applications most often they’re just a single number. If they aren’t it’s really useful to have a unique key associated with each one.

Properties

Degree

Every vertex has a degree. We denote it with deg(v)deg(v). For an undirected graph this is simple the amount of connected edges. For directed graphs a node can have both an in- and an out-degree, denoting the incoming and the outgoing edges. We can denote this with e.g. deg+(v)deg^+ (v) and deg−(v)deg^- (v).

In every directed graph the Degree Sum Formula holds. It describes a simple relation between the degrees and the amount of edges. It’s explanation is really easy: Every edge connects to two nodes and therefore adds two to the sum of degrees.

For undirected graphs the sum of in-degrees must match the sum out the out-degrees. The sum of both of them must again be twice the number of edges.

PROPERTIES

Undirected graph:

  • ∑v∈Vdeg(v)=2∣E∣\sum_{v \in V} deg(v) = 2 |E|

Directed graph:

  • ∑v∈Vdeg+(v)=∑v∈Vdeg−(v)\sum_{v \in V} deg^+ (v) = \sum_{v \in V} deg^- (v)
  • ∑v∈V(deg+(v)+deg−(v))=2∣E∣\sum_{v \in V} (deg^+ (v) + deg^- (v)) = 2 |E|

Edges

Properties

(Un)Directed

The edges can be directed (you can just go through them in one direction) or undirected. Graphically we often illustrate this using arrows vs just simple lines.

Weight

A graph can either be weighted or unweighted. In a weighted graph we associate a weight (commonly either in R\mathbb{R} or N\mathbb{N}) with every edge. Sometimes this is also called cost. Mathematically we do this via a weight function w:E→Rw: E \rightarrow \mathbb{R}.

We can use this to represent e.g. a distance between nodes (for shortest path problems) or a cost to build a connection (for MST (you’ll learn about that later) problems).

Graph Representations

There are many different ways. They all have some advantages and disadvantages.

Adjacency Matrix

An adjacency matrix is a matrix A∈Rn×nA \in \mathbb{R}^{n \times n}. AijA_{i j} represents the edge from uu to vv. In an unweighted graph we can just use the values 0 and 1, in a weighted graph the value can be the weight of an edge. If 0 is a possible weight we can use INF (e.g. u32::MAX) to represent the absence of an edge.

Analysis

Cool Tricks

These all work for an unweighted graph, A∈0,1n×nA \in {0,1}^{n \times n}.

Counting Paths

The entry (Ak)ij(A^k)_{i j} is the exact number of walks (paths allowing repeated edges and vertices) of length kk from ii to jj.

Counting Triangles

We can count the amount of triangles TT in the graph using the trace of the adjacency matrix.

T=1/6∑i=1n(A3)iiT = 1/6 \sum_{i=1}^n (A^3)_{i i}

The reason this works is because each triangle contributes to the paths 6 times: 3 different vertices and 2 directions for the walk for each.

Adjacency List

Here we just store for each vertex it’s adjacent vertices (aka neighbours). For undirected edges we store uu as neighbour of vv and vv as neighbour of uu.

I’d say from my experience this is for most algorithms the most common and useful representation. In Rust this can be as simple as a Vec<Vec<usize>>, if you need weights make it a tuple with the weight.

Analysis

I’ll assume we do this in Rust, and use a Vec for this, so we can get the length of the neighbours in constant time.

Edge List

We can represent a graph with just a list of the edges, the nodes we can then figure out from that. This has an issue that you can’t directly represent isolated nodes (vv with deg(v)=0deg(v) = 0). Therefore you’d need some additional data.

This is only used rarely from my experience.

Special Graphs

There are some special graphs which have special properties and therefore are used or allow for special algorithms.

Tree

A tree is a graph which is connected and has no cycles. For some algorithms we also use rooted trees. There is a special vertex called root.

Vertices vv with deg⁡(v)=1\deg(v) = 1 we call leaves.

Trees have because some special properties of their form.

PROPERTIES

The following statements are all equivalent for a finite graph T=(V,E)T = (V,E) with ∣V∣≤1|V| \leq 1.

  • TT is connected and acyclic (aka a tree)
  • For every pair u,vinVu,v in V there is a unique path
  • TT is minimally connected. Deleting any edge makes it disconnected.
  • TT is maximally acyclic1. That means that adding any new edge to it, makes it cyclic.
  • TT is connected with ∣E∣=n−1|E| = n - 1.
  • TT is acyclic with ∣E∣=n−1|E| = n - 1.

Others

There would be many more things which we could list here. For many I haven’t yet really found out about many specific algorithms etc. I’ll perhaps make posts for them or update this one. Examples would include

Footnotes

  1. I admit, looked that term up, but knew about the property. ↩


Edit page
Share this post:

Previous Post
Daily DSA 01: Queue