2. Friday, March 27, 20092
Basics
A graph G is a pair (V, E), where
V is a finite set called vertex set of G, and its elements are called vertices
V={v1, v2, v3}
E is a binary relation on V, i.e. It is called the edge set
of G, and its elements are called edges
E= {(v1, v1), (v1, v2), (v1, v3),
(v2, v1), (v2, v2), (v2, v3),
(v3, v1), (v3, v2), (v3, v3)}
3. Friday, March 27, 2009
3
Lecture #11
Adapted from slides by Dr A. Sattar
Directed Graphs
A graph G=(V,E) with vertex set
V={v1, v2 , v3,……vn } is called
directed or digraph if
(vi, vj) ≠ (vj, vi) for i ≠ j
In other words edges (vi, vj) and
(vj, vi), associated with any pair of
vertices vi, vj are considered
distinct
Self loops (edges from a vertex to
itself) are possible
For an edge (u, v), we say it is
incident from or leaves vertex u,
and is incident to or enters vertex v
4. Friday, March 27, 2009
4
Lecture #11
Adapted from slides by Dr A. Sattar
Undirected Graphs
In an undirected graph G=(V,E),
the edge set E consists of
unordered pairs of vertices rather
than ordered pairs i.e. (vi, vj) = (vj,
vi) for i ≠ j
In an undirected graph, self loops
are forbidden and so every edge
consists of two distinct vertices
For an edge (u, v), we say it is
incident on vertices u and v
5. Friday, March 27, 20095
Lecture #11
Adapted from slides by Dr A. Sattar
Adjacency
If (u, v) is an edge in a graph G=(V, E), we say vertex v is adjacent to
vertex u
When graph is undirected, the adjacency relationship is symmetric
When graph is directed, the adjacency relationship is not necessarily
symmetric
If v is adjacent to u in a directed graph, we say u → v
6. Friday, March 27, 20096
Lecture #11
Adapted from slides by Dr A. Sattar
Complete Graphs
A graph in which every pair of vertices is adjacent is called a complete
graph. In other words, a graph which has all the edges is referred to as
complete
An undirected complete graph , with n vertices, has n(n-1)/2 edges. Its
space complexity is O(n2
) . Such a graph is called dense.
A complete directed graph with n vertices has n2
edges, including edges
associated with all individual vertices. If individual edges are excluded,
the total number of edges is n(n-1)
7. Friday, March 27, 20097
Lecture #11
Adapted from slides by Dr A. Sattar
Degree
In an undirected graph, the number of edges incident on a vertex is
called the degree of a vertex
A vertex with degree 0 is said to be isolated
In a directed graph, the out-degree of a vertex is the number of edges
leaving it
In a directed graph, the in-degree of a vertex is the number of edges
entering it
In a directed graph, the degree of a vertex is the in-degree + the out-
degree
8. Friday, March 27, 2009
8
Lecture #11
Adapted from slides by Dr A. Sattar
Path
A path is a sequence of vertices which are connected by consecutive edges
The number of edges in a path is called the length of the path
In the figure, path <v1, v2, v4, v6> has length 3
The path contains the vertices v1, v2, v3, and v4, and edges (v1, v2), (v2, v3),
(v3, v4)
If there is a path p from u to v, we say v is reachable from u via p
A path is simple if all vertices in the path are distinct
9. Friday, March 27, 2009
9
Cycles
In a directed graph, a path < v0,
v1, …. vk> forms a cycle if v0=vk
and the path contains at least one
edge
In an undirected graph, a path <
v0, v1, …. vk> forms a cycle if
v0=vk and k>=3
A cycle is simple if <v1, …. vk>
are distinct
A self loop is a cycle of length 1
A graph with no cycles is acyclic
10. Friday, March 27, 2009
10
Lecture #11
Adapted from slides by Dr A. Sattar
Connected Graphs
A undirected graph is said to be connected if there exists a path between all
pairs of vertices. In other words, each vertex is reachable from any other vertex
in the graph.
Connectedness in an undirected graph is simple because the edges can be
traversed in either direction to reach a vertex.
A directed graph is strongly connected if every two vertices are reachable from
each other
A graph can have more than one connected components
An undirected graph is connected if it has exactly one connected component
11. Friday, March 27, 2009
11
Lecture #11
Adapted from slides by Dr A. Sattar
Weighted Graphs
A graph in which values w1, w2, w3,……are associated with edges is
called weighted graph. Weights are typically costs or distances in
different applications of graphs.
12. Friday, March 27, 200912
Lecture #11
Adapted from slides by Dr A. Sattar
Graph Representation
Two standard ways to represent directed and undirected graphs
Adjacency list
Adjacency matrix
The list representation consists of an array of linked lists, each corresponding to a
vertex.
The list stores all of the vertices that are linked to a given vertex.
Let G=(V,E) be a graph. For each u V, the adjacency list Adj(u) contains all the
vertices v such that there is an edge (u ,v ) E
Vertices in an adjacency list are typically stored in an arbitrary order
13. Friday, March 27, 200913
Lecture #11
Adapted from slides by Dr A. Sattar
Adjacency List Representation
If G is a directed graph, the sum of the lengths of all adjacency lists is |E|
If G is an undirected graph, the sum of the lengths of all adjacency lists is
2|E|
14. Friday, March 27, 2009
14
Lecture #11
Adapted from slides by Dr A. Sattar
15. Friday, March 27, 2009
15
Lecture #11
Adapted from slides by Dr A. Sattar
16. Friday, March 27, 200916
Lecture #11
Adapted from slides by Dr A. Sattar
Advantages and Disadvantages
Usually preferred because it provides a compact way to represent
sparse graphs – those for which |E| is much less that |V|2
For both directed and undirected graphs, the amount of
representation required is θ(V+E)
Can be readily adapted to represent weighted graphs. The weight
is simply stored with vertex v in u’s adjacency list
There is no quicker way to determine if a given edge (u, v) is
present in the graph than to search for v in the adjacency list
Adj[u]
17. Friday, March 27, 2009
17
Lecture #11
Adapted from slides by Dr A. Sattar
Adjacency Matrix Representation
One of the standard ways was of representing graph is to use a matrix to denote
links between pairs of vertices in a graph. The matrix is known as adjacency
matrix
Let G=(V,E) be a graph, with V={v1,v2,……vn}, and E={(v1,v2),(v1,v3)….}
where n=|V|.
The adjacency matrix A=(aij) can be represented as
The size | V|x|V| of adjacency matrix is independent of the number of vertices.
The memory requirement is θ(|V|2
)
18. Friday, March 27, 200918
Lecture #11
Adapted from slides by Dr A. Sattar
Adjacency Matrix Representation
19. Friday, March 27, 200919
Lecture #11
Adapted from slides by Dr A. Sattar
Advantages and Disadvantages
Preferred when the graph is dense – |E| is close to |V|2
We can tell quickly if there is an edge connecting two given vertices
Can be readily adapted to represent weighted graphs. The weight is
simply stored as the entry in row u and column v
The simplicity of an adjacency matrix makes it preferable when graphs
are reasonably small. Also only one bit may be used per entry
20. Friday, March 27, 200920
Lecture #11
Adapted from slides by Dr A. Sattar
Advantages and Disadvantages
The in-degree and out-degree of a vertex can be computed by simply
summing the corresponding column and row of the adjacency matrix.
In-degree is number of edges terminating at, and out-degree the number
of edges originating from, a vertex.
A major disadvantage of matrix representation is inefficient utilization
of storage space if the graph is not dense, that is, there are fewer edges.
For example, the adjacency matrix of a graph with 10 vertices and four
edges would contain 100 elements, only four 1’s. The 96% storage
space would be wasted
21. Friday, March 27, 200921
Lecture #11
Adapted from slides by Dr A. Sattar
Trees
Free Trees
A free tree is a connected, acyclic, undirected graph
We often omit ‘free’ when we say that a graph is a tree
If an undirected graph is acyclic but possibly disconnected, it is a
forest
22. Friday, March 27, 200922
Lecture #11
Adapted from slides by Dr A. Sattar
Properties of Free Trees
Let G=(V,E) be an undirected graph. The following statements
are equivalent:
G is a free tree
Any two vertices in G are connected by a unique simple path
G is connected, but if any edge is removed from E, the resulting
graph is disconnected
G is connected and |E|=|V|-1
G is acyclic and |E|=|V|-1
G is acyclic, but if any edge is added to E, the resulting graph
contains a cycle
23. Friday, March 27, 200923
Lecture #11
Adapted from slides by Dr A. Sattar
Rooted Trees
A rooted tree is a free tree in which one of the vertices is
distinguished from the others
This distinguished vertex is called the root of the tree
Consider a node x in rooted tree T with root r. Any node y on the
unique path from r to x is called an ancestor of x
x is a descendant of y