Graphs
Graphs
Graphs
Graphs
Introduction
Definition
A graph G is defined as an ordered set (V, E), where
V(G) represent the set of vertices and E(G) represents
the edges that connect the vertices.
The figure given shows a graph with V(G) = { A, B, C,
D and E} and E(G) = { (A, B), (B, C), (A, D), (B, D), (D,
E), (C, E) }. Note that there are 5 vertices or nodes
and 6 edges in the graph.
A
Definition
A graph can be directed or undirected. In an
undirected graph, the edges do not have any
direction associated with them. That is, if an edge is
drawn between nodes A and B, then the nodes can be
traversed from A to B as well as from B to A. The
above figure shows an undirected graph because it
does not gives any information about the direction of
the edges.
The given figure shows a directed graph. In a
directed graph, edges form an ordered pair. If there is
an edge from A to B, then
there
is a path
from A to B
A
B
C
but not from B to A. The edge (A, B) is said to initiate
from node A (also known as initial node) and
D
E
terminate at node B (terminal
node).
Graph Terminology
Adjacent Nodes or Neighbors: For every edge, e = (u,
v) that connects nodes u and v; the nodes u and v are
the end-points and are said to be the adjacent nodes
or neighbors.
Degree of a node: Degree of a node u, deg(u), is the
total number of edges containing the node u. If deg(u)
= 0, it means that u does not belong to any edge and
such a node is known as an isolated node.
Regular graph: Regular graph is a graph where each
vertex has the same number of neighbors. That is
every node has the same
degree. A 2regular
graph with
1 regular graph
regular graph
regular graph
vertices Oof
degree k is called a kregular graph or
regular graph of degree k.
Graph Terminology
Graph Terminology
Labeled graph or weighted graph: A graph is said to be
labeled if every edge in the graph is assigned some data.
In a weighted graph, the edges of the graph are assigned
some weight or length. Weight of the edge, denoted by
w(e) is a positive value which indicates the cost of
traversing the edge.
Multiple edges: Distinct edges which connect the same
end points are called multiple edges. That is, e = {u, v)
and e = (u, v) are known as multiple edges of G.
Loop: An edge that has identical end-points is called a
loop. That is, e = (u, u).
Multi- graph: A graph with multiple edges and/or a loop is
called a multi-graph.
Size of the graph: The size of a graph is the total number
of edges in it.
Graph Terminology
(b) Tree
(a) Multi-graph
e1
e4
B
e3
D
e6
C
e7
B
2
e2
e5
F
D
B
3
Directed Graph
A directed graph G, also known as a digraph, is a
graph in which every edge has a direction assigned
to it. An edge of a directed graph is given as an
ordered pair (u, v) of nodes in G. For an edge (u, v) The edge begins at u and terminates at v
U is known as the origin or initial point of e.
Correspondingly, v is known as the destination or
terminal point of e
U is the predecessor of v. Correspondingly, v is the
successor of u
nodes u and v are adjacent to each other.
Terminology of a Directed
Graph
Terminology of a Directed
Graph
Directed Graph
Definition:
For a directed graph G = (V,E), where V is the set of vertices and E
is the set of edges, the transitive closure of G is a graph G* =
(V,E*). In G*, for every vertex v, w in V there is an edge (v, w) in E*
if and only if there is a valid path from v to w in G.
Why and where is it needed?
Finding the transitive closure of a directed graph is an important
problem in many computational tasks that listed below.
Transitive closure is used to find the reachability analysis of
transition networks representing distributed and parallel systems
It is used in the construction of parsing automata in compiler
construction
Recently, transitive closure computation is being used to evaluate
recursive database queries (because almost all practical recursive
queries are transitive in nature).
Directed Graph
Algorithm
The algorithm to find transitive enclosure of a graph G
is given in figure below. In order to determine the
transitive closure of a graph, we define a matrix t
where tkij = 1, (for i, j, k = 1, 2, 3, n) if there exists
a path in G from the vertex i to vertex j with
intermediate vertices in the set (1, 2, 3, .., k) and 0
otherwise. That is, G* is constructed by adding an
edge (i, j) into E* if and only if tkij = 1.
Directed Graph
When k = 0
T0ij =
0 if (i, j) is not in E
1 if (I, j) is in E
When k 1
Transitive_Closure( A, t, n)
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Step
Bi Connected Graphs
A vertex v of G is called an articulation point if
removing v along with the edges incident to v, results
in a graph that has at least two connected
components.
A bi-connected graph is defined as a connected graph
that has no articulation vertices. That is, a biconnected graph is connected and non-separable, in
the sense that even if we remove any vertex from the
graph, the resultant graph is still connected. By
definition,
A bi-connected undirected graph is a connected
graph that is not broken into disconnected pieces by
deleting any single vertex
Bi Connected Graphs
A
H
A
I
B
J
D
F
E
Note that the graph shown on the left is not a biconnected graph as deleting vertex C from the
graphs results in two connected components of the
original graph (figure on right)
Bi Connected Graphs
A
REPRESENTATION OF GRAPHS
There are two common ways of storing graphs in computers
memory. They are:
Sequential representation by using an adjacency matrix
Linked representation by using an adjacency list that stores the
neighbors of a node using a linked list
Adjacency Matrix
Representation
An adjacency matrix is used to represent which nodes are
adjacent to one another. By definition, we have learnt that,
two nodes are said to be adjacent if there is an edge
connecting them.
In a directed graph G, if node v is adjacent to node u, then
surely there is an edge from u to v. That is, if v is adjacent to
u, we can get from u to v by traversing one edge. For any
graph G having n nodes, the adjacency matrix will have
dimensions of n X n.
In an adjacency matrix, the rows and columns are labeled by
graph vertices. An entry aij in the adjacency matrix will
1
if v is adjacent to v , that is there is an edge (v , v )
contain
1,
if
vertices
vi
and vj are adjacent to each other.
aij
0
otherwise
However, if the nodes
are not adjacent, aij will be set to zero.
i
Adjacency Matrix
Representation
Adjacency Matrix
Representation
Adjacency Matrix
Representation
From adjacency matrix A1, we have learnt that an entry 1 in
the ith row and jth column means that there exists a path of
length 1 from vi to vj. Now consider, A2, A3 and A4
aij 2 = aik akj
Any entry aij = 1 if aik = akj = 1. That is, if there is an edge (vi,
vk) and (vk, vj). This implies that there is a path from vi to vj of
length 2.
Similarly, every entry in the ith row and jth column of A3 gives
the number of paths of length 3 from node vi to vj.
In general terms, we can conclude that every entry in the ith
row and jth column of An (where n is the number of nodes in
the graph) gives the number of paths of length n from node vi
Adjacency List
The adjacency list is another way in which graphs can be
represented in computers memory. This structure consists of a
list of all nodes in G. Furthermore, every node is in turn linked to
its own list that contains the names of all other nodes that are
adjacent to itself.
The key advantage of using an adjacency list includes:
Adjacency List
A
X
A
Adjacency List
X
D
X
E X
X
STATE OF THE
NODE
DESCRIPTION
Ready
Waiting
Processed
Adjacency Lists
B
ORIG = \0
A: B, C, D
B: E
C: B, G
D: C, G
E: C, F
F: C, H
G: F, H, I
H: E, I
I: F
QUEUE = A B C D
ORIG = \0 A A A
QUEUE = A B C D E
ORIG = \0 A A A B
QUEUE = A B C D E G
ORIG = \0 A A A B C
QUEUE = A B C D E G
ORIG = \0 A A A B C
QUEUE = A B C D E G F
ORIG = \0 A A A B C G
QUEUE = A B C D E G F H I
ORIG = \0 A A A B C G G G
A: B, C, D
B: E
C: B, G
D: C, G
E: C, F
F: C, H
G: F, H, I
H: E, I
I: F
STACK: H
Pop and Print the top element of the STACK, that is, H. Push all the
neighbors of H on to the stack that are in the ready state. The
STACK now becomes:
STACK: E, I
Pop and Print the top element of the STACK, that is, I. Push all the
neighbors of I on to the stack that are in the ready state. The
STACK now becomes:
PRINT: I
STACK: E, F
Pop and Print the top element of the STACK, that is, F. Push all the
neighbors of F on to the stack that are in the ready state. (Note F
has two neighbors C and H. but only C will be added as H is not in
the ready state). The STACK now becomes:
PRINT: F
STACK: E, C
Pop and Print the top element of the STACK, that is, C. Push all
the neighbors of C on to the stack that are in the ready state. The
STACK now becomes:
STACK: E, B, G
Pop and Print the top element of the STACK, that is, G. Push all the
neighbors of G on to the stack that are in the ready state. Since
there are no neighbors of G that are in the ready state no push
operation is performed. The STACK now becomes:
PRINT G:
STACK: E, B
Pop and Print the top element of the STACK, that is, B. Push all the
neighbors of B on to the stack that are in the ready state. Since
there are no neighbors of B that are in the ready state no Push
operation is performed. The STACK now becomes:
PRINT e:
STACK: E
Pop and Print the top element of the STACK, that is, E. Push all the
neighbors of E on to the stack that are in the ready state. Since there
are no neighbors of E that are in the ready state no Push operation is
performed. The STACK now becomes empty: PRINT: E. Since the
STACK is now empty, the depth-first search of G starting at node H is
complete and the nodes which were printed are-H, I, F, C, G, B E.
These are the nodes which are reachable from the node H.
Topological Sorting
Topological Sorting
Algorithm to find a Topological Sort T of a Directed Acyclic Graph, G
Step 1:
Step 2:
Step 3:
Step 4:
FRONT +
Step 5: