Sei sulla pagina 1di 17

GRAPHS

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


E, Vis a finite non empty set of vertices, these
pair are called edges. V(G)and E(G) will represent
the set of vertices and edges of graph G. 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.
GRAPH G1 GRAPH G2
1 2 1 2

3
4 3
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)}
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
Example: the graph G1 has a degree 3 and the
graph G2 has the degree 5
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.
Example
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 for the
following figure

1 2

3
1) Representation 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*n matrix.
A=(aij) called the adjacency matrix for G.
A is defined by
aij={1 vi vj E
0, otherwise for 1<=i,j<=h
if G is a weighted graph
aij={W(vi vj) if vi vj E
C otherwise
C is a constant
1.2: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
An undirected graph a)
1 2

5 6

3 4
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
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
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: 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 b4 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.
Algorithm for topological sort:
1. Find the indegree of each node N ofG
2. Put in a queue all the nodes with zero indegree.
3. Remove the front node N of the queue.
4.Repeat the following foe 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
thequeue.
5.goto step 4 if queue is not empty.
6. end.

Potrebbero piacerti anche