CS Wiki | Cedric Schwyter

Graphs

Graphs

Directed Graphs

A directed graph consists of a finite set of vertices and a finite set of directed edges .

The degree of a vertex is defined as the number of edges that touch . For the degree of it holds that , where is defined as the number of incoming vertices and is analogously defined as the number of outgoing vertices of .

Source

A vertex is called a source if .

Sink

A vertex is called a sink if .

Undirected Graphs

An undirected graph consists of a finite set of vertices and a finite set of undirected edges .

The degree of a vertex is defined as the number of edges that touch .

Walk

A walk of length is tuple of vertices in a graph for which there exist edges for all , i.e., consecutive vertices in a walk are adjacent. A walk is closed if it starts and ends with the same vertex . A walk is closed if and only if is even. We say that vertex reaches vertex if there exists a walk starting in and ending in . This relation satisfies all properties of an equivalence relation: symmetry, reflexivity and transitivity.

The connected component of a vertex is the set of all vertices that it can reach, i.e., the connected component of is the equivalence class of the reachability relation that contains . A graph is connected if every vertex reaches every other vertex , i.e., if it has only one connected component.

A Eulerian tour is a closed walk that visits every edge exactly once.

📖 A connected graph has a Eulerian tour if and only if all vertex degrees are even.

Path

A path is a walk where all vertices are distinct.

Circuit

A circuit is a walk where .

Cycle

A cycle is a path where .

Handshake Lemma

For every graph ,

Eulerian Tours

Eulerian Walk/Trail

A Eulerian walk is a walk that goes through every edge in a graph exactly once. A Eulerian walk in an undirected graph exists if and only if the graph is connected and at most two vertices have an odd degree. A Eulerian walk in a directed graph exists if and only if the graph is connected and at most one vertex has , at most one vertex has , every other vertex has equal and .

Eulerian Circuit/Tour

A Eulerian circuit/tour is a Eulerian walk where there is an edge between the starting and the ending vertex. A Eulerian circuit in an undirected graph exists if and only if the graph is connected and all vertices have an even degree. A Eulerian circuit in a directed graph exists if and only if the graph is connected and every vertex has equal to the .

Hamiltonian Tours

Hamiltonian Path

A Hamiltonian path is a path that visits every vertex in a graph exactly once.

Hamiltonian Cycle

A Hamiltonian cycle is a Hamiltonian path where there is an edge between the starting and the ending vertex. Brute-force search appears to be unavoidable when computing whether there exists a Hamiltonian cycle for a given graph. The famous conjecture turns out to be equivalent to conjecturing that there is no polynomial-time algorithm for computing a Hamiltonian cycle.

Bipartite Graph

A graph is bipartite if it is possible to partition the vertices in two sets and (i.e. and ) such that every edge has one endpoint in and the other in .

📖 A graph is bipartite if and only if it does not contain any cycle of odd length.

Adjacency Matrix

The adjacency matrix , of is defined as

Adjacency List

The adjacency list representation of a graph is an -dimensional array such that is a list of all neighbors of vertex in (in arbitrary order).

Depth-First Search (DFS)

Runtime

Pseudocode

def dfs(s, graph):
    q = Stack(s)
    dist = [None] * len(graph.nodes)
    enter = [None] * len(graph.nodes)
    leave = [None] * len(graph.nodes)
    dist[s] = 0
    enter[s] = 0
    t = 1
    while !q.isEmpty():
        u = q.pop()
        leave[u] = t
        t = t + 1
        for v in u.children:
            if !enter[v]:
                q.push(v)
                dist[v] = dist[u] + 1
                enter[v] = t
                t = t + 1
    return dist

Further Resources

Depth-first search - Wikipedia

Topological Sort using Depth-First Search (DFS)

A directed graph has a topological sort if and only if it contains no directed cycles.

💡 There exists a directed cycle if and only if there exists a back-edge in the DFS tree of .

💡 For all edges in the DFS tree that are not a back-edge it holds that .

💡 From the above observations it follows that if there does not exist a directed cycle then there does not exist a back edge in the DFS tree. Since for all non-back-edges of the DFS tree it holds that it follows that the reverse post-ordering corresponds to a topological sort of .

Runtime

Adjacency-Matrix:

Adjacency-List:

Pseudocode

pre = []
post = []
t = 0

def visit(u):
    pre[u] = t
    t = t + 1
    mark(u)
    for v in u.children:
        if !marked(v):
            visit(v)
    post[u] = t
    t = t + 1

def dfs(graph):
    pre = [None] * len(graph.nodes)
    post = [None] * len(graph.nodes)
    t = 1
    unmarkAll(graph.nodes)
    for u in graph.nodes:
        if !marked(u):
            visit(u)

# reverse(post) gives a topological sort

Further Resources

Topological sorting - Wikipedia

Glossary of Graph Terms

Glossary of graph theory - Wikipedia

This project is maintained by D3PSI