CS 4407
Algorithms
Lecture 5:
Graphs—an Introduction
1
Prof. Gregory ProvanDepartment of Computer Science
University College Cork
Outline
� Motivation
– Importance of graphs for algorithm design
– applications
� Overview of algorithms to study
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
� Overview of algorithms to study
� Review of basic graph theory
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, AlgorithmsUniversity 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
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
� 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
Examples of Networks
communication
Network
telephone exchanges,computers, satellites
Nodes Arcs
cables, fiber optics,microwave relays
Flow
voice, video,packets
circuitsgates, registers,processors
wires current
mechanical joints rods, beams, springs heat, energy
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
mechanical joints rods, beams, springs heat, energy
hydraulicreservoirs, pumpingstations, lakes
pipelines fluid, oil
financial stocks, currency transactions money
transportationairports, rail yards,street intersections
highways, railbeds,airway routes
freight,vehicles,passengers
chemical sites bonds energy
Graph Algorithms Overview
� Standard graph algorithms
– Breadth-first search (BFS), Depth-first search
(DFS), heuristic algorithms
– Minimum Spanning Tree
– Shortest Path
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
– Shortest Path
– Max-Flow
– Tree-Decomposition Algorithms
• Convert arbitrary graphs to trees of cliques
Applications
�Networking– Internet, communication, transportation
�VLSI & logic circuit design�Graphics
– surface meshes in CAD/CAM
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
– surface meshes in CAD/CAM
�AI and Robotics applications– path planning for autonomous agents– precedence constraints in scheduling
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
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
– network communication & transportation– VLSI design & logic circuit design– surface meshes in CAD/CAM – path planning for autonomous agents– precedence constraints in scheduling
Basic Definitions
� A directed graph (or digraph) G = (V, E) consists of a finite set V, called vertices or nodes, and E, a finite set of ordered pairs, called edges of G. E is a binary relation on V. Cycles, including self-loops are allowed. Multiple edges are not allowed though; (v, w) and (w, v) are distinct edges.
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
� An undirected graph (or simply a graph) G = (V, E) consists of a finite set V of vertices, and a finite set E of unordered pairs of distinct vertices, called edges of G. Cycles are allowed, but not self-loops. Multiple edges are not allowed.
Examples of Digraphs & Graphs
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
Figure B.2
Definitions
� Vertex v is adjacent to vertex u if there is an edge (u, v).� Given an edge e = (u, v) in an undirected graph, u and v are the
endpoints of e, and e is incident on u and on v.� In a digraph with edge e = (u, v), u and v are the origin and
destination. We say that e leaves u and enters v. � A digraph or graph is weighted if its edges are labeled with numeric
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
� A digraph or graph is weighted if its edges are labeled with numeric values.
� In a digraph,
– the Out-degree of v is the number of edges coming from v.– the In-degree of v is the number of edges coming into v.
� In a graph, the degree of v is the number of edges incident to v. (The in-degree equals the out-degree).
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
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
� In a digraph
• 0 ≤≤≤≤ | E | ≤≤≤≤ | V | 2
• ∑∑∑∑ v∈∈∈∈V in-degree(v) = ∑∑∑∑ v∈∈∈∈V out-degree(v) = | E |
A graph is said to be sparse if | E | ∈∈∈∈ O(| V|), and denseotherwise.
Definitions (Path vs. Cycle)
� Path: a sequence of vertices <v0, …, vk> such that (vi-
1, vi) is an edge for i = 1 to k, in a digraph. The lengthof the path is the number of edges, k.
� w is reachable from u if there is a path from u to w. A path is simple if all vertices are distinct.
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
path is simple if all vertices are distinct.
� Cycle: a path in a digraph containing at least one edge and for which v0 = vk. A cycle is simple if, in addition, all vertices are distinct.
� For graphs, the definitions are the same, but a simple cycle must visit ≥≥≥≥ 3 distinct vertices.
Historical Terms For Cycles and Paths
� An Eulerian cycle is a cycle, not necessarily simple,
that visits every edge of a graph exactly once.
A Hamiltonian cycle (or path) is a cycle (path in a
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
� A Hamiltonian cycle (or path) is a cycle (path in a
directed graph) that visits every vertex exactly
once.
Definitions (Connectivity)
� A graph is acyclic, if it contains no simple cycles.
� A graph is connected, if every one of its vertices can reach every other vertex. I.e., every pair of vertices is connected by a path.
� The connected components of a graph are equivalence classes of vertices under the “is reachable from” relation.
� A digraph is strongly connected, if every two vertices are reachable
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
� A digraph is strongly connected, if every two vertices are reachable from each other.
� Graphs G = (V, E) and G’ = (V’, E’ ) are isomorphic, if ∃∃∃∃ a bijection f : V→→→→ V’ such that u, v∈∈∈∈E iff ( f(u), f(v)) ∈∈∈∈E’ .
Examples of Isomorphic Graphs
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
Figure B.3
Graphs, Trees, Forests
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
Free Tree Forest DAG Trees
DAGs versus Trees
� A tree is a digraph with a non-empty set of nodes such that:– There is exactly one node, the root, with in-degree of 0.
– Every node other than the root has in-degree 1.
– For every node a of the tree, there is a directed path from the root to a.
� Textbook (CLRS) suggests that a tree is an undirected graph, by association with free trees.
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
association with free trees.
� This is a valid approach, if you accept that the existence of the distinguished vertex (root) induces a direction on all the edges of the graph.
� However, we usually think of trees as being DAGs.
� Notice that a DAG may not be a tree, even if a root is designated.
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, AlgorithmsUniversity College Cork,
Gregory M. Provan
= 0 if edge (i, j) ∉∉∉∉ E
Graphs: Adjacency Matrix
� Example:
1a
A 1 2 3 4
1
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
2 4
3
a
d
b c
2
3 ??4
Graphs: Adjacency Matrix
� Example:
1a
A 1 2 3 4
1 0 1 1 0
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
2 4
3
a
d
b c
2 0 0 1 0
3 0 0 0 0
4 0 0 1 0
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
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
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
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
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
� 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
Graphs: Adjacency List
� Adjacency list: for each vertex v ∈∈∈∈ V, store a
list of vertices adjacent to v
� Example:
– Adj[1] = {2,3}
– Adj[2] = {3}1
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
– Adj[2] = {3}
– Adj[3] = {}
– Adj[4] = {3}
� Variation: can also keep
a list of edges coming into vertex
2 4
3
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?)
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
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
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 otherwiseIf 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
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
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.
Example for Graphs
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
NOTE: it is common to include cross links between corresponding edges, when needed to mark the edges previously visited. E.g. (v,w) = (w,v).
Figure 22.1
Example for Digraphs
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
Figure 22.2
Lecture Summary
� Motivation for studying graphs
– Importance of graphs for algorithm design
– applications
� Overview of algorithms
CS 4407, AlgorithmsUniversity College Cork,
Gregory M. Provan
� Overview of algorithms
– Basic algorithms: DFS, BFS
– More advanced algorithms: flows, tree-decompositions
� Review of basic graph theory