Graphs


A graph consists of:

  • A set of fixed objects, nodes.
  • A set of edges, which may have arrows and have values attached.

graph

A graph is composed of:

  • : set of vertices or nodes.
  • : set of edges or lines connecting the vertices in .
  • An edge is a connection between the vertices and .

For example:

170

  • = {1, 2, 3, 4, 5}
  • = {(1, 2), (1, 3), (3, 5), (2, 5), (5, 4)}

Terminology

Screenshot 2022-11-07 at 09.45.46

  • An undirected graph has no arrows on edges.
  • A directed graph or digraph has arrows on edges.
  • Two nodes connected by an edge are adjacent.
  • A weighted graph has values attached to edges.
  • A path from a node back to itself is a cycle.
  • An undirected graph with no cycles is a tree.
  • A directed graph with no cycles is a directed acyclic graph or DAG.
  • A DAG where no node is pointed to by more than one node is a directed tree.
  • A complete graph is normally an undirected graph with an edge between each pair of vertices.

We start at the source node and search until we find the target node. We normally want to visit each node once and only once.

Paths

A sequence of k vertices, [v1, v2, ..., vk], such that any pair of consecutive vertices, vi, vi+1 are adjacent (connected by an edge) is called a path.

Screenshot 2022-10-24 at 12.49.45

How do we represent a graph when it comes to implementation?

We often represent a graph as a matrix (2D array), although other data structures can be used depending on the application.

If we have nodes to represent: for an by matrix , a non-zero value of (th row, th column of ) means there is an edge between node and .

Undirected

  • We assume that is the same as .

Directed

  • is not always the same as .

Non-weighted

  • is either one for an edge or zero for no edge.

Weighted

  • is the edge weight or zero for no edge.

Complete

  • is never zero.

Adjacency Matrix - Recap

If we have nodes to represent:

  • For an by matrix a non-zero value of (th row )

See also