Professional Documents
Culture Documents
Algorithms
Lecture 5:
Graphs—an Introduction
Motivation
– Importance of graphs for algorithm design
– applications
Overview of algorithms to study
Review of basic graph theory
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Today’s Learning Objectives
Why graphs are useful throughout Computer
Science
Range of applications is large
We use some basic properties of graphs
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Motivation
For theoreticians:
Graph problems are neat, often difficult, hence interesting
For practitioners:
Massive graphs arise in networking, web modelling, ...
Problems in computational geometry can be expressed as
graph problems
Many abstract problems best viewed as graph problems
Extreme: Pointer-based data structures = graphs with extra
information at their nodes
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Examples of Networks
Network Nodes Arcs Flow
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Applications
Networking
– Internet, communication, transportation
VLSI & logic circuit design
Graphics
– surface meshes in CAD/CAM
AI and Robotics applications
– path planning for autonomous agents
– precedence constraints in scheduling
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Graphs
A collection of vertices or nodes, connected by a
collection of edges.
Useful in many applications where there is some
“connection” or “relationship” or “interaction”
between pairs of objects.
– network communication & transportation
– VLSI design & logic circuit design
– surface meshes in CAD/CAM
– path planning for autonomous agents
– precedence constraints in scheduling
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Basic Definitions
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Examples of Digraphs & Graphs
Figure B.2
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Definitions
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Combinatorial Facts
In a graph
• 0 ≤ |E | ≤ C(| V |, 2) = | V | (| V | – 1) / 2 ∈ O(| V | 2)
• ∑ v∈∈V degree(v) = 2 | E |
In a digraph
• 0 ≤ | E | ≤ | V| 2
• ∑ v∈∈V in-degree(v) = ∑ v∈∈V out-degree(v) = | E |
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Definitions (Path vs. Cycle)
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Historical Terms For Cycles and Paths
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Definitions (Connectivity)
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Examples of Isomorphic Graphs
Figure B.3
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Graphs, Trees, Forests
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
DAGs versus Trees
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Representing Graphs
Assume V = {1, 2, …, n}
An adjacency matrix represents the graph as
a n x n matrix A:
– A[i, j] = 1 if edge (i, j) ∈ E (or weight of edge)
= 0 if edge (i, j) ∉ E
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Graphs: Adjacency Matrix
Example:
A 1 2 3 4
1
a 1
2 d 4 2
3
b c ??
3 4
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Graphs: Adjacency Matrix
Example:
A 1 2 3 4
1
a 1 0 1 1 0
2 d 4 2 0 0 1 0
b c 3 0 0 0 0
3 4 0 0 1 0
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Graphs: Adjacency Matrix
How much storage does the adjacency matrix
require?
A: O(V2)
What is the minimum amount of storage
needed by an adjacency matrix
representation of an undirected graph with 4
vertices?
A: 6 bits
– Undirected graph → matrix is symmetric
– No self-loops → don’t need diagonal
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Graphs: Adjacency Matrix
The adjacency matrix is a dense
representation
– Usually too much storage for large graphs
– But can be very efficient for small graphs
Most large interesting graphs are sparse
– E.g., planar graphs, in which no edges cross,
have |E| = O(|V|) by Euler’s formula
– For this reason the adjacency list is often a more
appropriate respresentation
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Graphs: Adjacency List
Adjacency list: for each vertex v ∈ V, store a
list of vertices adjacent to v
Example:
– Adj[1] = {2,3} 1
– Adj[2] = {3}
– Adj[3] = {} 2 4
– Adj[4] = {3}
Variation: can also keep 3
a list of edges coming into vertex
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Graphs: Adjacency List
How much storage is required?
– The degree of a vertex v = # incident edges
• Directed graphs have in-degree, out-degree
– For directed graphs, # of items in adjacency lists is
Σ out-degree(v) = |E|
takes Θ(V + E) storage (Why?)
– For undirected graphs, # items in adj lists is
Σ degree(v) = 2 |E| (handshaking lemma)
also Θ(V + E) storage
So: Adjacency lists take O(V+E) storage
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Graph Representations
Let G = (V, E) be a digraph.
Adjacency Matrix: a |V | × |V | matrix for 1 ≤ v,w ≤ |V |
A[v, w] = 1, if (v, w) ∈ E and 0 otherwise
If digraph has weights, store them in the matrix.
Adjacency List: an array Adj[1…|V |] of pointers where for 1 ≤ v ≤
|V |, Adj[v] points to a linked list containing the vertices adjacent
to v. If the edges have weights then they may also be stored in the
linked list elements.
Incidence Matrix: a |V | × |E| matrix, B[i, j], of elements
bij = { -1, if edge j leaves vertex i }
bij = { 1, if edge j enters vertex i }
bij = { 0, otherwise }
– Note: must have no self-loops.
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Example for Graphs
Figure 22.1
Figure 22.2
CS 4407, Algorithms
University College Cork,
Gregory M. Provan
Lecture Summary
CS 4407, Algorithms
University College Cork,
Gregory M. Provan