Professional Documents
Culture Documents
Algo Mod6 Graphs
Algo Mod6 Graphs
Graphs
Dr. ISSAM ALHADID
Modification date 4/3/2019
Outlines: Graphs
Applications: Communication
Network
vertex = city, edge = communication link
Street Map
Some streets are one way
A bidirectional link represented by 2
if V= 4 then |E| ≤ 16
Graph Terminology
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.
Graph Terminology
Undirected graph; an edge is an unordered
pair of vertices {v1, v2}
◦ edge(v1, v2) = edge(v2, v1).
graph.
Graph Terminology
Graph Terminology
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)
Graph Terminology
Two representations of a directed graph. (a) A
directed graph G having six vertices and eight
edges. (b) The adjacency-matrix representation
of G.
Graph Terminology
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:
Graph Terminology
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.
Graph Terminology
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 w i and wi+1
The length of the path is k, which is the number of
edges on the path.
Graph Terminology
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.
Graph Terminology
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.
Graph Terminology
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.
Graph Terminology
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.
Graph Terminology
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.
Graph Terminology
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),
not) complete.
A complete graph is a simple graph that
≤ n(n-1).
Vertex Degree
Degree (of a vertex): # of adjacent vertices.
degree(2) = 2
degree(5) = 3
degree(3) = 1
Sum of Vertex Degrees
(Undirected Graph)
Sum of degrees of all vertex = 2|E|
Since adjacent vertices each count the
a n × n matrix A:
{
A[i, j] = 1 if edge (i, j) ∈E (or weight of edge).
= 0 if edge (i, j) ∉ E.
Adjacency Matrix
Adjacency Matrix
Adjacency Matrix
Adjacency Matrix
Adjacency Matrix
Adjacency Matrix
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
Adjacency Matrix
In the example 10
Adjacency List
Adjacency List
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.
Adjacency Matrix vs. List
Next Slide
Adjacency Matrix vs. List