Anda di halaman 1dari 40


Definition: A graph, G consists of two sets V and

E, V is a finite non-empty set of vertices. E is a set
of pairs of vertices, these pair are called edges.
V(G)and E(G) will represent the set of vertices
and edges of graph G.
So, we can write G= (V,E) to represent a graph.
The following figure shows the two graph G1 and
The graph G1 is undirected and G2 is directed

1 2 1 2

4 3

Fig.1(a) Fig.1(b)
V(G1)= {1,2,3}
E(G1)= {(1,2),(1,3),(2,1), (2,3),(3,1),(3,2)}


A graph consists of a finite set of nodes and

finite set of edges connecting pairs of these
Directed graph

A graph is directed when its edges have an

orientation and this provide a one-way
connection an indicated by an arrow. The
graph G2 is directed graph.
Weighted graph: When each edge of a graph
is associated with a real no, called its weight,
the graph is said to be weighted. The weighted
graph may directed or undirected.
Degree of vertex: the degree of a vertex in an
undirected graph is the number of edges
incident on it
The degree of a vertex in a directed graph is its
in-degree plus its out-degree
Oriented Degree
when Edges Directed
The in-degree of a vertex (deg-) counts the
number of edges that stick in to the vertex.
The out-degree (deg+) counts the number
sticking out.

1 3

Q: What are in-degrees and out-degrees of

all the vertices? 6
Oriented Degree
when Edges Directed
A: deg-(1) = 0
deg-(2) = 3
deg-(3) = 4 2

deg+(1) = 2 1 3
deg+(2) = 3
deg+(3) = 2

L23 7
In-degree and out-degree of graph
For directed graph the in-degree and out-
degree of a vertex are the number of incoming
and outgoing edges respectively.
Example: the following figure has vertex 2 has
in-degree 3, out-degree 2.

1 2

4 3
Complete graph: A simple graph in which
there exists an edge between every pair of
vertices is called a complete graph. Complete
graph of four vertices is shown in the figure. A
complete graph is sometimes also referred to
as a universal graph.
Degree of a graph: The maximum degree of
any vertex in a graph is called degree of a
Example: the degree of graph is 2
1 2

Representations of Graph
1.1 Adjacency matrix:
Let G = (V,E) be a graph with
n= |V| m= |E| and V = {V1, V2,, Vn}
G can be represented by an n x n matrix.
A=(aij) called the adjacency matrix for G.
A is defined by
Representations of Graph
aij = 1 vi vj E
0, otherwise for 1i,j h
if G is a weighted graph
aij = W(vi vj ) if vi vj E
C otherwise
c is a constant
Representations of Graph
Adjacency list:
An alternative to the adjacency matrix
representation is an array indexed by vector
number containing linked list is called
adjacency lists.
For each vertex vi, the ith array contains a list
with information on all edges of G that leave vi
Representations of Graph
(a) An undirected graph
1 2

5 6

3 4
Representations of Graph

Adjacency matrix of (a)

0 1 1 0 0 0
1 0 0 1 1 0
1 0 0 1 0 0
0 1 10 1 0
0 1 0 1 0 1
0 0 0 0 1 0
Representations of Graph
Adjacency list structure of (a)
1 2 3 nil

2 1 4 5 nil

1 4 nil

2 3 5 nil
2 4 6 nil
5 nil
Representations of Graph
(a) A Weighted graph
25 2 16
11 6
5 3
18 4
Fig(2) Two representations for a weighted
directed graph
Representations of Graph

Adjacency matrix of (a)

0 25 0 0 0 0
0 0 0 14 16 0
5 0 0 6 0 0
0 0 18 0 0 0
0 0 0 32 0 11
0 0 0 0 0 0
Representations of Graph
Adjacency list structure of (a)
1 2 25 Nil

2 4 14 5 16 Nil

1 5 4 6 Nil
3 18 Nil
4 32 6 11 Nil
6 Nil
Representations of Graph
Directed acyclic graphs: A directed acyclic
graph, or DAG is a directed graph with no
cycles. The following figure is dag

1 2

4 3
Topological Sort
Topological sort: It is a process of assigning a
linear ordering to the vertices of a DAG so that if
there is an edge from vertex i to vertex j, then i
appears before j in the linear ordering.
Algorithm start with the node N, with zero
indegree ie., without any predecessors.
The algorithm will repeat the following steps until
the graph G is empty.
1. Finding a node N with zero in degree
2. Deleting N and its edge from the graph G.
Topological Sort
Algorithm for topological sort:
1. Find the indegree of each node N of G
2. Put in a queue all the nodes with zero
3. Remove the front node N of the queue.
4.Repeat the following for each w adjacent to N
a) Reduce the indegree of w
b) if indegree of w is zero then add w to the
rear of the queue.
5.goto step 4 if queue is not empty.
6. end.
Consider the directed acyclic graph G in Fig 3.7



The steps of the algorithm as follows

1.In degree of each node N of the graph G follows
In degree of A is 0
In degree of B is 2
In degree of C is 3
In degree of D is 0

2.Add all the nodes with zero in degree into the

queue. Now
A D .

3.Remove the front element A from the queue.

A D .

4.Decrease by 1 the indegree of each adjacent to

A ,Now
Indegree of B is 1
Indegree of C is 2
There is no node with zero indegree
5.Remove the front element D from the queue.
6.Decrease by 1,The indegree of each adjacent toD,
Indegree of B is 0
Indegree of C is 1
7.add the node B with zero indegree now

A D B .
8.Remove the front element B from the QUEUE.
9.Decrease by1,the indegree of each adjacent to B
Now indegree of C is 0
10.Add the node C with zero indegree now

Remove the front element C from the queue.
The node C has no adjacent node . The queue has no
front element so, we can stop the process. The
required topological sort is as follows ADBC.

1.Algorithm start the vertex v and making it

as visited.

2. Next an unvisited vertex w adjacent to v

is selected.

3. Call the same algorithm with vertex w.

4. Continue the process until all vertices have

been visited.

Algorithm DFS(v)
For each vertex w adjacent to v do
If(visited(w)true)then // if unvisited node
Depth first search
The steps followed by the algorithm are
1.begin by marking 1 visited and invoking DFS(1)
2.Both 2 and 3 are adjacent to1 , on a left to right ,so
goto 2. Invoke DFS(2) and mark2 as visited.
3.Both 4 and5 are adjacent to 2 ,4 is encountered
first on a left to right ,so goto 4. Invoke DFS (4) and
mark 4 as visited.
4.Both 2 and 8 are adjacent to 4 .But only 8 is
unvisited adjacent node. so goto 8. Invoke DFS(8) and
mark 8 as visited.
5.Here 5,6,7 are adjacent to 8 .Goto 5 invoke DFS(5)
and mark 5 as visited.
Stack Implementation in DFS

1. We start at vertex v and making it as visited.

2. All unvisited vertices adjacent to v are
visited next.
3. Then unvisited vertices adjacent to these
vertices are visited.
4. This process will continue until all vertices
have been visited.
Algorithm BFS(v)
Initialize queue to zero
While not empty queue(q)
For all vertices w adjacent to v
If(visited(w)true) then //if unvisited node
The procedure addqueue is used to add one item onto
the queue.
The procedure deletequeue is used to delete one item
from the queue.
Here q is queue name
Queue Implementation in BFS