Dr.
ISSAM ALHADID
Modification date 4/3/2019
vertex = city, edge = communication link
Some streets are one way
A bidirectional link represented by 2 directed
edge : (5, 9) (9, 5)
Electronic Circuits cslab1a cslab1b
◦ Printed Circuit Board
◦ Integrated Circuits (ICs)
Computer networks
◦ Local Area Network (LAN)
◦ Internet
◦ Web
◦ A graph G = (V, E) consists of two sets V and E. The
elements of V are called the vertices and the elements
of E the edges of G.
◦ Each edge is a pair of vertices. For instance, the sets
V = {1, 2, 3, 4, 5} and E = {{1, 2}, {2, 3}, {3, 4}, {4, 5}}
define a graph with 5 vertices and 4 edges.
◦ Graphs have natural visual representations in which
each vertex is represented by a point and each edge by
a line connecting two points.
◦ Figure 1: Graph G = (V, E) with V = {1, 2, 3, 4, 5} and
E = {{1, 2}, {2, 3}, {3, 4}, {4, 5}}
A graph G = (V, E):
◦ V = set of vertices (nodes or points).
◦ E = set of edges (arcs or lines) = subset of V × V.
In describing the running time of a graph
algorithm on a given graph G = (V, E), we
usually measure the size of the input in terms
of the number of vertices |V| and the number
of edges |E| of the graph.
Thus, |E| ≤ |V|2 = O(|V|2).
if V= 4 then |E| ≤ 16
A graph is an ordered pair (V, E), where V is
called the vertex set and E is called the edge
set. With each e ∈ E, we associate two
vertices which we call the ends of e.
Undirected graph; an edge is an unordered
pair of vertices {v1, v2}
◦ edge(v1, v2) = edge(v2, v1).
Directed graph (called digraph): an edge is an
ordered pair of vertices (v1, v2)
◦ edge(v1,v2) goes from vertex v1 to vertex v2,
notated v1 → v2.
◦ edge(v1, v2) is NOT the same as edge(v1, v2)
A directed graph, called digraph for short, is a
ordered pair (V, E) – as usual V are vertices and E
edges.
If f(e) = (u, v), we call u the tail of e and v the
head of e. So, if f(e) = (u, v) we can represent
this as being able to travel from u to v along
edge e, but we cannot use edge e to travel from
v to u. The edges now have a direction
associated with them.
If I use the term “graph”, I mean an undirected
graph.
Two representations of an undirected graph. (a) An
undirected graph G having five vertices and seven
edges. (b)The adjacency-matrix representation of G.
(a)
(b)
Two representations of a directed graph. (a) A
directed graph G having six vertices and eight
edges. (b) The adjacency-matrix representation
of G.
adjacency-matrix representation of a graph
G = (V, E), we assume that the vertices are
numbered 1, 2, . . . , |V| in some arbitrary manner.
Then the adjacency-matrix representation of a
graph G consists of a |V| × |V| matrix A = (aij )
such that:
Adjacent vertices connected by an edge:
◦ Vertex v is adjacent to u if and only if (u, v) ∈E.
◦ In an undirected graph with edge (u, v), and hence
(v, u), v is adjacent to u and u is adjacent to v.
Two vertices are called adjacent if they are
connected by an edge.
Two edges are called incident, if they share
a vertex. Also, a vertex and an edge are
called incident, if the vertex is one of the
two vertices the edge connects.
Incident edges on a vertex:
◦ For example, edges:1, 2, and 3 are incident on
vertex a.
For example Adjacent vertices of 11 are 7,5 But 2,9,10
are NOT.
Incidence is placed between and edge and vertex, thus
Incident edge a incidents on vertex 11 from vertex 7,
same way incident edge d incidents from vertex 11 and
incidents on vertex 9.
Incident From
e is incident from u
v is incident from e.
Incident To
e is incident to v
u is incident to e.
A Path in a graph from u to v is a sequence of edges
between vertices w0, w1, …, wk, such that (wi, wi+1) ∈E,
where u = w0 and v = wk, for 0 ≤ i < k.
◦ (wi, wi+1) ∈E we have edges between vertices wi and wi+1
The length of the path is k, which is the number of
edges on the path.
Loops: If the graph contains an edge (v, v)
from a vertex to itself, then the path (v, v ) is
sometimes referred to as a loop.
The graphs we will be consider generally
loopless.
Path:
◦ sequence of alternating vertices and edges (follow
one another)
◦ begins with a vertex.
◦ ends with a vertex.
◦ each edge is preceded and followed by its
endpoints (vertices).
Simple path:
◦ path such that all its vertices and edges are distinct.
A simple path is a path such that all vertices
are distinct, except that the first and last
could be the same.
Examples:
P1 = (V, X, Z) is a simple path.
P2 = (U, W, X, Y, W, V) is a path that is not
simple.
Cycles:
◦ A cycle in a directed graph is a path of length at
least 2 such that the first vertex on the path is the
same as the last one; if the path is simple, then the
cycle is a simple cycle.
Cycles:
◦ A cycle in a undirected graph is a path of length at
least 3 such that the first vertex on the path is the
same as the last one and the edges on the path are
distinct.
Cycle:
◦ circular sequence of alternating vertices and edges.
◦ each edge is preceded and followed by its
endpoints (vertices).
Simple cycle:
◦ cycle such that all its vertices and edges are
distinct.
Examples:
◦ C1 = (V, X, Y, W, U, V) is a simple cycle.
◦ C2 = (U, W, X, Y, W, V, U) is a cycle that is not simple.
If each edge in the graph carries a value, then
the graph is called weighted graph.
A weighted graph is a graph G = (V, E, W),
where each edge, e ∈E is assigned a real
valued weight, W(e).
A complete graph is a graph with an edge
between every pair of vertices.
That is a graph is called complete graph if
every vertex is adjacent to every other vertex.
Complete undirected graph has all possible
edges.
◦ Examples:
Subgraph: subset of vertices and edges
forming a graph.
A graph Gs = (Vs, Es) is a subgraph of a graph
G = (V, E) if Vs ⊆V, Es ⊆E, and Es ⊆Vs×Vs.
Connected graph: any two vertices are
connected by some path.
◦ An undirected graph is connected if, for every pair
of vertices u and v there is a path from u to v.
A complete graph is connected.
A connected graph may not be (and often is
not) complete.
A complete graph is a simple graph that
contains exactly one edge between each pair
of distinct vertices.
In a connected graph, there is a path between
any given pair of vertices
Connected component: maximal connected
subgraph. E.g., the graph below has 3
connected components
A directed graph is strongly connected if
every two vertices are reachable from each
other.
A directed graph is strongly connected if
there is a path between all pairs of vertices
A strongly connected digraph is a directed
graph in which it is possible to reach any
node starting from any other node by
traversing (navigating) edges in the
direction(s) in which they point
A directed graph G is strongly connected if:
For any two vertices u and v:
◦ There is a directed path u → v, and
◦ There is a directed path v → u
Connected is usually associated with
undirected graphs (two way edges): there is
a path between every two nodes.
Strongly connected is usually associated with
directed graphs (one way edges): there is
a route between every two nodes.
Complete graphs are undirected graphs
where there is an edge between every pair of
nodes.
A digraph is weakly connected if when
considering it as an undirected graph it is
connected. I.e., for every pair of distinct
vertices u and v there exists an undirected path
(potentially running opposite the direction on an
edge) from u to v. (Next Slide)
Symmetric directed graphs are directed
graphs where all edges are bidirected (that is,
for every arrow that belongs to the digraph,
the corresponding inversed arrow also
belongs to it).
A symmetric digraph is a directed graph, G =
(V, E), such that if (u, v) ∈E, then (v, u) ∈E.
(Next Slide)
Tree - connected undirected graph without
cycles.
Forest - collection of trees.
Degree of a vertex:
◦ X has degree 5.
Parallel edges:
◦ h and i are parallel edges
Undirected Graph:
◦ Since edge(u, v) is the same as edge(v, u),
the number of edges in a complete
undirected graph is n(n-1)/2.
◦ Number of edges in an undirected graph is
≤ n(n-1)/2.
Directed Graph:
Since edge(u, v) is not the same as edge(v, u),
the number of edges in a complete directed
graph is n(n-1).
Number of edges in a directed graph is
≤ n(n-1).
Degree (of a vertex): # of adjacent vertices.
degree(2) = 2
degree(5) = 3
degree(3) = 1
Sum of degrees of all vertex = 2|E|
Since adjacent vertices each count the
adjoining edge, it will be counted twice.
In-degree is number of incoming edges.
In-degree(2) = 1 and in-degree(8) = 0.
Out-degree is number of outbound edges.
Out-degree(2) = 1 and out-degree(8) = 2.
Each edge contributes 1 to the in-degree of
some vertex and 1 to the out-degree of some
other vertex.
Sum of in-degrees = sum of out-degrees = |E|,
where |E| is the number of edges in the digraph.
The sum of all the out-degrees and all the in-degrees is,
of course, twice the number of edges (by the hand-
shaking lemma). Therefore, the sum of the out-degrees is
equal to the number of edges.
Hand-shaking
lemma Example.
deg(A) =3
deg(B) =3
deg(C) =1
deg(D) =3
deg(E) =3
deg(F) =4
deg(G) =2
deg(H) =3
Sum = 22
|E|= 22/2 = 11
We will typically express running times in
terms of
◦ |V| = number of vertices, and
◦ |E| = number of edges,
◦ If |E| ≈ |V|2 the graph is dense. (intensive)
◦ If |E| ≈ |V| the graph is sparse. (scattered)
If you know you are dealing with dense or
sparse graphs, different data structures may
make sense.
Adjacency Matrix
Adjacency Lists
Adjacency Matrix
Assume V = {1, 2, …, n}.
An adjacency matrix represents the graph as a n
× n matrix A:
{
A[i, j] = 1 if edge (i, j) ∈E (or weight of edge).
= 0 if edge (i, j) ∉ E.
How much storage does the adjacency matrix
require?
◦ Answer: O(|V|2). The time complexity to calculate
the number of edges in a graph whose information
is stored in form of an adjacency matrix n*n (in
bits)
What is the minimum amount of storage
needed by an adjacency matrix
representation of an undirected graph with 4
vertices?
Answer : (|V|-1) *|V|/2 6 bits
◦ Undirected graph → matrix is symmetric
◦ No self-loops → don’t need diagonal
minimum amount of storage
needed by an adjacency matrix
representation of an undirected
graph with 4 vertices 6 bits
But adjacency matrix require needs
n*n bits to represent the matrix in
the storage which means … it
needs 16 bits in real !
The adjacency matrix is a dense
representation.
◦ Usually too much storage for large graphs.
◦ But can be very efficient for small graphs.
◦ If graph is unweighted, there is an additional
advantage in storage for the adjacency matrix
presentation; rather than using one word of
computer memory for each matrix entry, the
adjacency matrix uses only one bit per entry.
◦ Prefer to be a directed graph.
Most large interesting graphs are sparse.
For this reason the adjacency list is often a
more appropriate representation.
Adjacency list: for each vertex v ∈V, store a
list of vertices adjacent to v.
Adjacency list for vertex i is a linear list of
vertices adjacent from vertex i.
Each adjacency list is a chain.
In the example 10
How much storage is required?
◦ The degree of a vertex v = # incident edges.
◦ Directed graphs have in-degree and 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 adjacency lists is Σ degree(v) = 2|E|.
◦ Also, Θ(V + E) storage.
So: Adjacency lists take O(V + E) storage.
Next Slide
Figure (b) : The graph Θ(V + E) storage = 6 + 8 = 14
Figure (a) : The graph O(V2) storage = 6 ^ 2= 36