Professional Documents
Culture Documents
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.
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).
(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.
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.
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.
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