Anda di halaman 1dari 40

GRAPHS

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
G2
The graph G1 is undirected and G2 is directed
graph.
Graphs
GRAPH G1 GRAPH G2

1 2 1 2

3
4 3

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

V(G2)={1,2,3,4}
E(G2)={(1,2),(2,2),(2,3),(3,4),(4,1),(4,2),(4,3)}

A graph consists of a finite set of nodes and


finite set of edges connecting pairs of these
nodes
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.
Graphs
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.
2

1 3

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


L23
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
Graphs
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.
Example
Graphs
Degree of a graph: The maximum degree of
any vertex in a graph is called degree of a
graph.
Example: the degree of graph is 2
1 2

3
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

3
1 4 nil

4
2 3 5 nil
5
2 4 6 nil
6
5 nil
Representations of Graph
(a) A Weighted graph
1
25 2 16
5
11 6
5 3
18 4
14
32
6
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

3
1 5 4 6 Nil
4
3 18 Nil
5
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
indegree.
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

A C

B D

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,
NOW
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

A D B C
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.
DEPTH FIRST SEARCH

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

Algorithm DFS(v)
{
Visited(v)=true
For each vertex w adjacent to v do
If(visited(w)true)then // if unvisited node
DFS(w)
}
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
BREADTH
FIRST SEARCH
BREADTH FIRST SEARCH

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
Algorithm BFS(v)
{
Visited(v)=true
Initialize queue to zero
addqueue(q,v)
While not empty queue(q)
{
Deletequeue(q,v)
For all vertices w adjacent to v
If(visited(w)true) then //if unvisited node
Addqueue(q,w)
Visited(w)=true
}
}
BREADTH FIRST SEARCH
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