Professional Documents
Culture Documents
Definition of Graph:
V(G) and E(G) represent the sets of vertices and edges of graph
G.
Undirected Graph:
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>}
Multigraph
If the graph contains self loop or have multiple
occurrences of the same edge then it is called as
multigraph.
Note
Complete Graph:
An n vertex undirected graph with exactly n(n-1)/2 edges is
said to be complete.
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
Length:
Simple Path
Cycle
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.
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
Connected Component
Tree
G5
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.
In-degree of Vertex:
Out-degree of Vertex
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
v2
v3
v4
v5
v6
Note:
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
Leaf vertex
A leaf vertex (also called pendant vertex) is a vertex
with degree one.
G7
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
Graph Representations
Adjacency Matrix
Adjacency Lists
Adjacency Multilists
Adjacency Matrix
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
A[i ][ j ]
j 0
Adjacency Lists
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
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.
NULL
NULL
NULL
Orthogonal List
vj
Link
of vi
Link
of vj
oints to the node where vi is the tailPoints to the node where vj is the hea
1
Tai
l
2
X
Head
1
3
X
X
X
2
Adjacency Multilists
vertex1
vertex2
path1
path2
Pointer to the edge
where edge is incident
on vertex 2