1

mrahmedcomputing

KS3, GCSE, A-Level Computing Resources

Lesson 4. Graphs


Lesson Objective

  • Be familiar with graphs and their typical.
  • Be able to explain the terms: graph, weighted graph, unweighted graph, vertex/node, edge/arc, undirected graph, directed graph.
  • Know how an adjacency matrix and an adjacency list may be used to represent a graph.
  • Be able to compare the use of adjacency matrices and adjacency lists.
  • Be able to traverse a graph using a depth first search and a breadth first search.

Lesson Notes

Graph Data Structures

A graph is the name given to an abstract data structure representing complex relationships. It is a non-linear data structure composed of nodes (or vertices) connected by edges.

Key Components of a Graph:

  • Vertices (Nodes): These represent the individual entities within the graph.
  • Edges: These connect pairs of vertices, indicating a relationship between them. Edges can be:
    • Directed: If the relationship has a direction (e.g., following someone on social media).
    • Undirected: If the relationship is bidirectional (e.g., friendship).
    • Weighted: If there's a value associated with the edge (e.g., distance between cities).

Common Graph Representations:

Applications of Graphs:

Graphs are used in a wide range of applications, including:

  • Social networks
  • Transportation systems
  • Routing algorithms
  • Recommendation systems
  • Image processing
  • Network analysis

An Undirected Weighted Graph

In this graph is weighted, the weights in the example to the right (or below on mobile devices) represent distances between towns.

It is undirected because there are no arrows on the edges indicating direction.


An Undirected (un?)Weighted Graph

In this graph is NOT weighted.

It is undirected because there are no arrows on the edges indicating direction.


An Directed (un?)Weighted Graph

This graph shows direction (e.g. Y has a link to X, but X does not have a link to Y).

It has no weights associated with the edges.


An Directed Weighted Graph

This graph shows direction. However it now has weights associated with the edges.


Graphs - Summary:


Representing a Graph

A human driver can find their way from one location to another using a map.

A computer needs to represent the information about distances and connections in a structured, numerical way.

Graphs can be implemented using one of two ways:

Adjacency Matrix

Each row and column represents a node. The item at [row, column] indicates a connection.

In a unweighted graph, this can be a represented as 1.

Note: Diagram to the right (or below on mobile devices) shows a undirected graph.

In a weighted graph, this can be a represented with the line weight.

Note: Diagram to the right (or below on mobile devices) shows a directed graph.

Matrix Considerations

Adjacency List

An adjacency list is an alternative way of representing a graph.

A list of nodes is created, and each node points to a list of adjacent nodes.

Note: Diagram to the right (or below on mobile devices) shows a directed graph.

A weighted graph can be represented as a dictionary of dictionaries, with each key in the dictionary being the node, and the value, a dictionary of adjacent edges and edge weights.

Note: Diagram to the right (or below on mobile devices) shows a undirected graph.

List Considerations


Traversing a Graph

You can not supply a single set of predefined steps to insert or remove a node. It depends on the graph and the situation.

Efficient algorithms are needed to traverse a graph to find specific nodes.

Depth-First Traversal

In this traversal, we go as far down one route as we can before backtracking and taking the next route. When given the chance we always start from the left.

Start from the left.

  1. [A]
  2. [A,E]
  3. [A,E,B]
  4. [A,E,B,F]
  5. [A,E,B,F,D]
  6. [A,E,B,F,D,C]

Note: Pay attention. Image to the right/below is animated.

Breadth-First Traversal

With a breadth-first traversal, we visit all the neighbours of a node, and then all the neighbours of the first node visited, then all the neighbours of the second node and so on, before moving further away from the start node.

Start from the left.

  1. [A]
  2. [A,E]
  3. [A,E,D]
  4. [A,E,D,C]
  5. [A,E,D,C,B]
  6. [A,E,D,C,B,F]

Note: Pay attention. Image to the right/below is animated.



3