Introduction to graph algorithms: definitions and examples (2024)

yourbasic.org

Introduction to graph algorithms: definitions and examples (1)

  • Definitions
    • Trees
    • Directed graphs
  • Data structures
    • Adjacency matrix
    • Adjacency list
  • Search algorithms
    • Depth-first search (DFS)
    • Breadth-first search (BFS)
    • Dijkstra’s algorithm

This text introduces basic graph terminology,standard graph data structures, and three fundamentalalgorithms for traversing a graph in a systematicway.

You may also want to take a look at the Githubyourbasic/graph repository.It’s a Go library with generic implementations of basic graph algorithms.

Definitions

A graph G consists of two types of elements:vertices and edges.Each edge has two endpoints, which belong to the vertex set.We say that the edge connects (orjoins) these twovertices.

The vertex set of G is denotedV(G),or just V if there is no ambiguity.

An edge between vertices u andv is written as {u,v}.The edge set of G is denotedE(G),or justE if there is no ambiguity.

The graph in this picture has the vertex set V = {1, 2, 3, 4, 5,6}.The edge set E = {{1,2}, {1,5}, {2,3}, {2,5}, {3,4}, {4,5}, {4,6}}.

Introduction to graph algorithms: definitions and examples (2)

A self-loop is an edge whose endpoints is a single vertex.Multiple edges are two or more edges that join the same two vertices.

A graph is called simple if it has no self-loops and no multiple edges,and a multigraph if it does have multiple edges.

The degree of a vertexv is the number of edgesthat connect tov.

A path in a graph G = (V, E) is a sequence of verticesv1, v2, …,vk, with the property that there are edges betweenvi and vi+1.We say that the path goes from v1to vk.The sequence 6, 4, 5, 1, 2 is a path from 6 to 2 in the graph above.A path is simple if its vertices are all different.

A cycle is a path v1, v2, …,vk for which k>2,the first k-1 vertices are all different,and v1=vk.The sequence 4, 5, 2, 3, 4 is a cycle in the graph above.

A graph is connected if for every pair of verticesu and v, there is a path from u tov.

If there is a path connecting u andv,the distance between these vertices is defined as the minimal numberof edges on a path from u tov.

A connected component is a subgraph of maximum size,in which every pair of vertices are connected by a path.Here is a graph with three connected components.

Introduction to graph algorithms: definitions and examples (3)

Trees

A tree is a connected simple acyclic graph.A vertex with degree1 in a tree is called aleaf.

Directed graphs

A directed graph or digraph G = (V, E)consists of a vertex set V and an edge set of ordered pairsE of elements in the vertexset.

Here is a simple acyclic digraph (often called a DAG,“directed acyclic graph”) with seven vertices and eight edges.

Introduction to graph algorithms: definitions and examples (4)

Data structures

Adjacency matrix

An adjacency matrix is a |V|x|V|-matrix of integers,representing a graphG = (V,E).

  • The vertices are number from 1 to|V|.
  • The number at position(i,j)indicates the number of edges fromi toj.

Here is an undirected graph and its symmetric adjacency matrix.

Introduction to graph algorithms: definitions and examples (5)

Introduction to graph algorithms: definitions and examples (6)

The adjacency matrix representation is best suited for dense graphs,graphs in which the number of edges is close to the maximal.

In a sparse graph, an adjacency matrix will have a large memory overhead,and finding all neighbors of a vertex will be costly.

Adjacency list

The adjacency list graph data structure is well suited for sparse graphs.It consists of an array of size|V|, wherepositionk in the array contains a listof all neighbors tok.

Note that the “neighbor list” doesn’t have to be an actual list. It could beany data structure representing a set ofvertices.Hash tables, arrays or linked lists are common choices.

Search algorithms

Depth-first search

Depth-first search (DFS) is an algorithmthat visits all edges in a graphG that belong to thesame connected component as a vertexv.

Algorithm DFS(G, v) if v is already visited return Mark v as visited. // Perform some operation on v. for all neighbors x of v DFS(G, x)

Before running the algorithm, all |V| vertices must be marked as not visited.

Time complexity

To compute the time complexity, we can use the number of calls to DFSas an elementary operation: the if statement and the mark operation bothrun in constant time, and the for loop makes a single call to DFS for each iteration.

Let E' be the set of all edges in the connected component visited by the algorithm.The algorithm makes two calls to DFS for each edge {u,v} in E':one time when the algorithm visits the neighbors ofu,and one time when it visits the neighbors ofv.

Hence, the time complexity of the algorithm is Θ(|V|+|E'|).

Breadth-first search

Breadth-first search (BFS) also visits all verticesthat belong to the same component asv.However, the vertices are visited in distance order: the algorithm first visitsv,then all neighbors ofv, then their neighbors, and soon.

Algorithm BFS(G, v) Q ← new empty FIFO queue Mark v as visited. Q.enqueue(v) while Q is not empty a ← Q.dequeue() // Perform some operation on a. for all unvisited neighbors x of a Mark x as visited. Q.enqueue(x)

Before running the algorithm, all |V| vertices must be marked as not visited.

Time complexity

The time complexity of BFS can be computed as the totalnumber of iterations performed by the for loop.

Let E' be the set of all edges in the connected component visited by the algorithm.For each edge {u,v} in E'the algorithm makes two for loop iteration steps:one time when the algorithm visits the neighbors ofu,and one time when it visits the neighbors ofv.

Hence, the time complexity is Θ(|V|+|E'|).

Dijkstra’s algorithm

Dijkstra’s algorithm computesthe shortest path from a vertex s, the source,to all other vertices. The graph must have non-negative edge costs.

The algorithm returns two arrays:

  • dist[k] holds the length of a shortest path froms tok,
  • prev[k] holds the previous vertex in a shortest pathfroms tok.
Algorithm Dijkstra(G, s) for each vertex v in G dist[v] ← ∞ prev[v] ← undefined dist[s] ← 0 Q ← the set of all nodes in G while Q is not empty u ← vertex in Q with smallest distance in dist[] Remove u from Q. if dist[u] = ∞ break for each neighbor v of u alt ← dist[u] + dist_between(u, v) if alt < dist[v] dist[v] ← alt prev[v] ← u return dist[], prev[]

Time complexity

To compute the time complexity we can use the same type of argumentas forBFS.

The main difference is that we need to accountfor the cost of adding, updating and finding the minimum distances in the queue.If we implement the queue with aheap,all of these operations can be performed in O(log|V|)time.

This gives the time complexity O((|E|+|V|)log|V|).

Share this page:

Introduction to graph algorithms: definitions and examples (2024)
Top Articles
Latest Posts
Article information

Author: Lilliana Bartoletti

Last Updated:

Views: 6310

Rating: 4.2 / 5 (53 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Lilliana Bartoletti

Birthday: 1999-11-18

Address: 58866 Tricia Spurs, North Melvinberg, HI 91346-3774

Phone: +50616620367928

Job: Real-Estate Liaison

Hobby: Graffiti, Astronomy, Handball, Magic, Origami, Fashion, Foreign language learning

Introduction: My name is Lilliana Bartoletti, I am a adventurous, pleasant, shiny, beautiful, handsome, zealous, tasty person who loves writing and wants to share my knowledge and understanding with you.