Vous êtes sur la page 1sur 42

Some Basic Terms

Definition of Graph:

A graph, G=(V,E), consists of two sets V and E.

V is a finite non-empty set of vertices also called nodes or


points.

E is a set of pairs of vertices called edges also called links or


arcs.

V(G) and E(G) represent the sets of vertices and edges of graph
G.

Some Basic Terms (Contd.)

Undirected Graph:

In an undirected graph the pair of vertices representing any


edge is unordered i.e. the pairs (v1,v2) and (v2,v1) represent the
same edge.

The edges have no direction in undirected graph.

Directed Graph:

In a directed graph each edge is represented by a


directed pair <v1, v2>

v1 is the tail and v2 the head of the edge

The pairs <v1,v2> and <v2, v1> are different edges.

The edges have direction in directed graph.

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

(2,3),(2,4),(3,4)}

4
G1
1

1
2
4

3
5

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

3
G3
V(G3)={1,2,3}
E(G3)={<1,2>,<2,
1>,<2,3>}

The graphs G1 and G2 are undirected and G3 is directed graph.

Multigraph
If the graph contains self loop or have multiple
occurrences of the same edge then it is called as
multigraph.

Note

The maximum number of distinct unordered pairs (v i,vj)


with vivj, in a undirected graph with n vertices is n(n-1)/2.

The maximum number of distinct ordered pairs <v i,vj>


with vivj, in a directed graph with n vertices is n(n-1).

Complete Graph:
An n vertex undirected graph with exactly n(n-1)/2 edges is
said to be complete.

G1 is complete graph while G2 and G3 is not complete.

Incident edges and adjacent vertices:


If (v ,v ) is an edge in E(G), then we shall say the vertices v
1
2
1
and v2 are adjacent and that the edge (v1,v2) is incident on
vertices v1 and v2.

e.g. The vertices adjacent to vertex 2 in G2 are 4,5 and 1.


The edges incident on vertex 3 in G2 are (1,3), (3,6) and
(3,7).
If <v1,v2> is a directed edge, then vertex v1, will be said to
be adjacent to v2 while v2 is adjacent from v1. The edge
<v1,v2> is incident to v1 and v2.

Subgraph:
A subgraph of G is a graph G such that V(G)V(G) and
E(G)E(G).
e.g. Some of the subgraphs of G are shown below:
1

2
3

3
4

3
4

Path

A path from vertex vp to vertex vq in graph G is a sequence


of vertices vp, v1, v2, vn, vq such that (vp,v1), (v1, v2), ,
(vn, vq) are edges in E(G).

If G is directed then the path consists of <vp, v1>, <v1, v2>,


<vn,vq>, edges in E(G).

Length:

The length of a path is the number of edges on it.

Simple Path

A simple path is a path in which all vertices except


possibly the first and last are distinct.

e.g. The path 1,2,4,3 is simple path whereas 1,2,4,2 is not


simple path. The length of both the paths is 3.

Cycle

A cycle is a simple path in which the first and last vertices


are the same.

e.g. 1,2,3,1 is a cycle in G1 whereas 1,2,1 is a cycle in G3.

Connected vertices
In an undirected graph, G, two vertices v and v are said to
1
2
be connected if there is a path in G from v1 to v2.

e.g. In graph G2, vertices 2 and 7 are said to be connected


because there is a path from 2 to 7.

Connected Graph:
An undirected graph is said to be connected if for every
pair of distinct vertices vi, vj in V(G) there is a path from vi
to vj in G.
H1

1
2

3
4

H2

5
7

6
8
G4

e.g. Graphs G1 and G2 are connected while G4 shown below


is not. connected.

Connected Component

A connected component or simply a component of an


undirected graph is a maximal connected sub-graph.

e.g. G4 has two components H1 and H2.

Tree

A tree is a connected acyclic graph.

e.g. Graph G2 is a connected acyclic graph therefore it is a


tree.

Strongly Connected Graph

A directed graph G is said to be strongly connected if for


every pair of distinct vertices vi, vj in V(G) there is directed
path from vi to vj and also from vj to vi.

G5

e.g. The graph G3 is not strongly connected graph because


there is no path from v3 to v2 , whereas the graph G5 is
strongly connected graph.

Strongly Connected Component:


It is a maximal sub-graph of a directed graph that is
strongly connected.
e.g. G has two strongly connected components as shown
3
below.
1

2
Two strongly connected components of G 3

Degree of Vertex
The degree of a vertex is the number of edges incident to
that vertex.

e.g. The degree of vertex 1 in G1 is 3.

In-degree of Vertex:

If G is a directed graph we define in-degree of a vertex v


to be the number of edges for which v is the head.

That is in-degree of a vertex v is number of incoming


edges to v.

Out-degree of Vertex

The out-degree is defined to be the number of edges for


which v is the tail.

That is out-degree of a vertex v is number of out-going


edges from v.

In-degree and outdegree of directed


graph

Degree of undirected graph


3

1 2

1
2 3

3 3

3 2

1 in:1, out: 1
3 3
2 in: 1, out: 2

3
G1

G2

3 in: 1, out: 0
G3

in-degree and out-degree of each vertex in the given graph:


Verte
InOutx
degree
degree
v1

v2

v3

v4

v5

v6

Note:

If di is the degree of vertex i in a graph G with n vertices


and e edges, then

e (1 / 2) d i
i 1

Isolated vertex
An isolated vertex is a vertex with degree zero; that is, a
vertex that is not an endpoint of any edge.

G6

e.g. In graph G6 vertex d is an isolated vertex because its


a vertex with degree 0.

Leaf vertex
A leaf vertex (also called pendant vertex) is a vertex
with degree one.

G7

e.g. In graph G7, vertex 6 is an pendent vertex because it is


with degree one.

Source and Sink vertex


In a directed graph a source vertex is a vertex with indegree zero, while a sink vertex is a vertex with outdegree zero.
e.g. In the following directed graph G , the source and sink
8
is indicated.

Sink

Source
G8

Weighted graph
A graph is aweighted graphif a number (weight) is
assigned to each edge i.e. each edge is labeled with some
weight.
Such weights might represent, for example, costs, lengths
or capacities, etc. depending on the problem at hand.
Such a graph is also called a network.

G9

G10

e.g. Graph G9 is weighted directed and G10 is weighted


undirected graph.

Graph Representations

Adjacency Matrix
Adjacency Lists
Adjacency Multilists

Adjacency Matrix

Let G = (V, E) with n vertices, n1.


The adjacency matrix of G is a 2-dimensional n n
matrix, A such that:
A[i, j]=1 iff (v , v )E(G) (v , v for a diagraph)
i
j
i
j
A[i, j]=0 otherwise.
The adjacency matrix for an undirected graph is
symmetric; the adjacency matrix for a digraph need
not be symmetric

Adjacency matrix of graph G1, G3 & G4 respectively.

0
1

1
0
1
1

1
1
0
1

1
1
1

0 1 0

1
0
1

0 0 0

0 1 1 0 0 0 0 0
1 0 0 1 0 0 0 0

1 0 0 1 0 0 0 0

0 1 1 0 0 0 0 0
0 0 0 0 0 1 0 0

Symmetric

0 0 0 0 1 0 1 0
0 0 0 0 0 1 0 1
0 0 0 0 0 0 1

The

Adjacency Matrix Space


Requirement
space needed
to represent a graph using adjacency

matrix is n2 words for directed graph.

The space needed to represent a graph using adjacency


matrix for undirected graph can half the space needed for
directed graph by storing only the upper or lower triangle of
the matrix.

Degree calculation using


adjacency
matrix

For undirected graph the degree of a vertex is:


n 1

A[i ][ j ]
j 0

For a digraph, the row sum is the out degree, while


the column sum is the in degree.

Adjacency Lists

Adjacency list is a linked representation of a graph.

There is one list for each vertex in the graph.

Thus for n vertices there will be n linked lists.

Each row in adjacency matrix is represented as an


adjacency list.

Each node in the linked list of vertex i consists of:

Vertex number to which vertex i is adjacent.

Pointer to next node in the list.

The address of each linked lists first node (head node) is


kept in a separate array. This array of head node addresses
represents the graph.

Structure definition
#define MAX_VERTICES 50
typedef struct node {
int vertex;
struct node *link;
}NODE;
NODE * graph[MAX_VERTICES];

Example

Example
1

directed graph with n vertices and e edges n head nodes and 2e list n

Degree calculation using


adjacency list
The degree of any vertex in an undirected graph is the
number of nodes in its adjacency list.

In case of diagraph the out-degree of any vertex is the


number of nodes in its adjacency list.

To find in-degree of any vertex we are suppose to maintain


another list called inverse adjacency list.

This set of lists, will also contain one list for each vertex.

The vertex i list will contain a node for each vertex to which
i is adjacent.

Inverse adjacency list for G3

NULL

NULL

NULL

Orthogonal List

In certain applications, we require the in-degree as well as


out-degree of a vertex. Hence, instead of maintaining two
separate lists, we can use only one graph representation
which can give us both. This is called an orthogonal list.

The node structure of an orthogonal list for any directed


edge <vi , vj>:
vi

vj

Link
of vi

Link
of vj

oints to the node where vi is the tailPoints to the node where vj is the hea

Orthogonal representation for graph G3

1
Tai
l

2
X

Head

1
3

X
X

X
2

Adjacency Multilists

In the adjacency list representation of an undirected graph


each edge (vi,vj) is represented by two entries, one on the
list for vi and the other on the list for vj.
Adjacency Multilists
For each edge there will be exactly one node, but this
node will be in two lists, i.e., the adjacency lists for each
of the two nodes it is incident to.
The node structure is as below:
marked

vertex1

one bit mark


field that may be
used to indicate
whether or not
the edge has
been examined.

vertex2

path1

Pointer to the edge


where
edge
is
incident on vertex 1

path2
Pointer to the edge
where edge is incident
on vertex 2

Adjacency Multlists for graph G1

The lists are: vertex 1: N1 N2 N3


vertex 2: N1 N4 N5
vertex 3: N2 N4 N6
vertex 4: N3 N5 N6

Data Structures for Adjacency


Multilists

typedef struct edge {


short int marked;

int vertex1, vertex2;


struct edge *path1, *path2;
}EDGE_POINTER;
EDGE_POINTER *graph[MAX_VERTICES];