Professional Documents
Culture Documents
Project On Graph Theory
Project On Graph Theory
REQUIREMENT FOR THE DEGREE OF MATHEMATICS OF SCIENCE (PART II) IN MATHEMATICS CAM
PRACTICAL, PAPER (XIV)
Sheet Nihal Topno Binesh Kumar Yadav Ratan Kumar Mishra Tahsin Ahmad Anup Kumar
Keshri Surya Narayan Pradhan Md. Aslam Ansari Shamsad Ahmad (Group ‘A’)
SIGNATURE (GUIDE)
INTERNAL EXAMINER
EXTERNAL EXAMINER
Sheet Nihal Topno Binesh Kumar Yadav Ratan Kumar Mishra Tahsin Ahmed Anup Keshri
Md. Aslam Ansari SuryaNarayan Pradhan Shamshad Ahmad
GRAPH THEORY
INTRODUCTION There is an interesting story behind the development of Graph Theory.
A river Pregel (Preger) flows through the city of Königsberg, thereby forming two
islands in the city. These islands were connected by seven bridges as shown in
figure
Fig. 2 This is nothing but a mapping a real world problem into a paper. Various
networks like road map, electric network air routes are represented in this way.
Euler worked on the problem and developed new branch of mathematics called graph
theory. With the help of the algorithms and theorems of graph theory many problems
of real world are solved. Many mathematicians have also worked on this topic and
given mathematical theorems and algorithms. Euler was one who translated a real
world problem into a mathematical problem. In this project we present the way this
mathematical problem can be transferred to a computer. We also provide a few C
program code to solve some problems of graph theory with the help of computer
easily. Mathematically such a representation is called a graph. Mathematical
definitions and properties of graph are discussed further.
Definition:-A graph G is a set of two tuples (V,E), where V represents the non
empty set of vertices of G and E is a subset of V×V called edge set of G.
Fig. 3 Directed Graph: A graph G=(V,E) is said to be a directed graph if its every
edge ek=(vi,vj) is represented as a line segment from vertex vi to vj with an arrow
directed from vi to vj.
Fig. 4
Weighted Graph: Let G be a graph. For any edge e, w(e) called the weight of e, is a
real number. If each edge of the graph is assigned with some real number then G is
called a weighted graph.
Fig. 5
Fig. 7
REPRESENTATION OF GRAPH IN COMPUTER Usually the first step in representing a graph
is to map the vertices to a set of contiguous integers. (0,|V|-1) is the most
convenient in C- programs. There are two popular ways that are used to maintain a
graph in a computer memory. These are: 1. Sequential 2. Linked list Sequential
Representation: The graphs can be represented as matrices in sequential
representation. There are two most common matrices. These are: 1. Adjacency 2.
Incidence The adjacency matrix is a sequence matrix with one row and one column for
each vertex. The entries of the matrix are either 0 or 1. A value of 1 for row i
and column j implies that edge eij exists between vi and vj vertices. A value of 0
implies that there is no edge between vi and vj. In other words we can say that if
graph G consists of v1, v2, v3,…..,vn vertices then the adjacency matrix A=[aij] of
graph G is the n×n matrix and can be defined as: 1 aij = If vi is adjacent to vj
(that is if there is an edge between vi and vj) 0 If there is no edge between vi
and vj.
Fig.7 Such a matrix A. which contains entries of only 0 or 1, is called a bit
matrix or a Boolean. For example consider the graph G illustrated in above figure,
which consists of 5 vertices and 10 edges. The set of vertices is as follows: V=
{ v1, v2, v3, v4, v5} and the set of edges is: E= {(v1, v2), (v1, v3), (v2, v1),
(v2, v3), (v2, v5), (v3, v1), (v3, v4), (v4, v1), (v4, v5), (v5, v3)} The adjacency
matrix A for G is as follows: v1 v1 v2 A= v3 v4 v5 0 1 1 1 0 v2 1 0 0 0 0 v3 v4 v5
1 1 0 0 1 0 0 1 0 0 0 1 0 1 0
From the matrix A, it is clear that number of 1’s in the adjacency matrix is equal
to the number of edges in the graph. When one is constructing an adjacency matrix
for a graph, one must follow the following points: 1. Adjacency matrix does not
depend on the ordering of vertex of a graph G, i.e. different ordering of vertices
may result in a different adjacency matrix. One can obtain the same matrix by
interchanging rows and columns. 2. If the graph G is undirected then the adjacency
matrix of G will be symmetric. i.e. [aij]=[aji] for every i and j. The incidence
matrix consists of a row for every vertex and a column for every edge. The entries
of the matrix are -1, 0 and 1. If kth edge is (vi ,vj), the kth column has a value
1 in ith row, 1 in the jth row and 0 elsewhere. For example, the incidence matrix I
for the above graph is as follows:
1 -1 I=
1 0
-1 1 0 0 0
0 1 -1 0
0 1 0
-1 0 1
0 0 1 -1
-1 0 0 1 0
0 0
0 0
0 -1 0 0 0 0
0 -1 1 -1 0 1
0 0
0 -1 0 0
/* Program for creation of adjacency matrix of a simple graph */
#include<stdio.h> #include<conio.h> #define max 20 int adj[max][max]; /*Adjacency
matrix */ int n; /* Denotes number of nodes in the graph */ main() { int
max_edges,i,j,origin,destin; char graph_type; printf("Enter number of vertices :
"); scanf("%d",&n); printf("Enter type of graph,directed/undirected (d/u):");
fflush(stdin); scanf("%c",&graph_type); if(graph_type=='u') max_edges=n*(n-1); else
max_edges=n*(n-1)/2;
for(i=1;i<=max_edges;i++) { printf("Enter edge %d(end vertices) : ",i); scanf("%d
%d",&origin,&destin); if( (origin==0) && (destin==0) ) break; if(origin > n ||
destin> n || origin<=0 || destin<=0) { printf("Invalid edge!\n"); i--; } else
{ adj[origin][destin]=1; if( graph_type=='u') adj[destin][origin]=1; else
adj[destin][origin]=-1; } }/*End of for*/
printf("The adjacency matrix is :\n"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++)
printf("%4d",adj[i][j]); printf("\n"); } getch(); }
OUTPUT Adjacency matrix of the following undirected graph through c programming :
1 0 0 1
OUTPUT Adjacency matrix of the following directed graph through c programming :
1 -1 -1 0
0 -1 1 0
DEFINITIONS AND THEOREMS OF GRAPH THEORY Loop: An edge of the form (a, a), a
belonging to V, vertex set of G is called a loop.
Fig.8
Multiple Edges: Two edges e1 and e2 of a graph G are said to be multiple edges if
they have same end vertices.
. Fig.9
Null Graph: A null graph is a graph which has no edges.
Fig. 10 A vertex in a graph G which is not connected to the other vertices of the
graph by an edge is called an isolated vertex. All the vertices of null graph are
isolated.
…………………………………… 1 1 1 1 ………... 0
Fig. 12 Bipartite Graph: A graph G(V,E) is said to be bipartite if the vertex set
can be split into two non-empty subsets X and Y such that X Y=φ and X Y=V and
for any edge (a, b)ЄE either a Є X & b Є Y or a Є Y & b Є X.
Fig. 13
In the graph of Fig.12 the vertices can be partitioned into X{v1, v6, v8, v3 } and
Y{v2, v4, v5,v7}. Every edge in the graph has one end in X and another end in Y
which can be easily inspected. A graph G is bipartite if and only if each cycle
of G if exists is of even length. Complete bipartite graph: A bipartite graph G
with partitions X and Y is said to be complete if each vertex of X is adjacent to
each vertex of Y.
Fig. 14 In the graph of Fig. 14 the set of vertices can be partitioned into X{v1,
v2, v3 } and Y{v3, v4, v5} so that each vertex if the graph has an end in both the
sets. Also each vertex of X is connected with each vertex of Y, making the graph
bipartite. Degree of vertex: Let V be any vertex of a graph G. The degree of v is
denoted by deg(v) or d(v) and defined as deg(v)=number of edge end associated with
v in G.
Even or Odd degree vertex: A vertex v of a graph G is said to be even or odd
according as the degree of the vertex is even or odd. The number of odd vertices
in a graph G is even. This fact is used in handshaking lemma. Subgraph: A graph
H(V(H),E(H)) is said to be a subgraph of G(V(G),E(G)) if V(H) V(G) and E(H)
E(G).
Fig. 15(a)
Fig. 15(b)
Fig.16 The graph given in Fig.16 edges (v3, v4) and (v6, v7) are cut edges
(bridges). An edge e of a graph G is a cut edge of G if and only of e is
contained in no cycle of G. Cut Set: A cut set of a graph G is a subset of edge set
of G such that after removal of all the edges of the set the graph G becomes
disconnected.
PATHS AND CYCLES Walk:Any sequence of edges of a graph such that the terminal
vertex of an edge is the initial vertex of the edge appearing next to it is called
a walk. Path: A walk is said to be a path if there is no vertices with repetation
except possibly end vertex. Trail:A walk is said to be trail if there are no edges
with repetation. Connected graph: A graph G is said to be connected if there exists
a path from a vertex to another vertex of the graph. Cycle: A closed path in a
graph G is said to be a cycle. If G is a graph with minimum degree of vertices of
G, δ(G) ≥2, then G contains a cycle of length at least δ+1. If G is a graph with
p edges and q vertices such that q≥p, then G contains a cycle. Eulerian Trail:A
closed trail containing all the edges of a graph G is called an Eulerian trail of
the graph. Eulerian Graph: A graph G is said to be Eulerian if it contains an
Eulerian trail. Euler finally proposed a theorem to show the impossibility of
occurance of the rule discussed previously. Furthermore his theorem goes on to say
that such rule is impossible for every graph having a certain property. If such a
rule is possible for a graph then we call that graph Eulerian.
Euler’s theorem: A connected graph G is said to be Eulerian if and only if degree
of each vertex of G is even. Euler considered a problem which assumes no repetation
of edges however vertices can be repeated. We now consider a modified case in which
vertices are non repetative. Hamiltonian cycle: A cycle containing all the vertices
of a graph G is called a Hamiltonian cycle of G. Hamiltonian Graph: A graph is said
to be Hamiltonian if it contains a Hamiltonian cycle. Following theorem of Dirac is
very important in order to check whether a given graph is hamiltonian or not. Let
G be a simple connected graph with n (≥3) vertices and δ(u)≥n/2 for each vertex u
in G, then G is Hamiltoian.
TREE Cyclic Graph: A graph is said to be cyclic if it contains a cycle. Tree: A
connected acyclic graph is called a tree.
Fig. 17 In any tree two vertices are connected by a unique path. In a tree
number of edges is one less than number of vertices. Every non-trivial tree has
at least two vertices of degree one. A connected graph is a tree if and only if
every edge is a cut set. If G is a graph with ν vertices and v-1 edges then
following statements are equivalent: (a) G is connected, (b) G is acyclic, (c) G is
a tree. Forest: A graph having no cycle is called forest.
Spanning tree: A spanning tree of a connected graph G is a subgraph of G which is a
tree containing all the vertices of G. Every connected graph contains a spanning
tree. Minimum Spanning Tree: A graph G may have many spanning tries. If weigth is
associated with each edge of G then the spanning tree of minimum weigth is called
the minimun spanning tree of graph G. There are a few algorithms to find out
minimum spanning tree of a graph. We disscuss Kruskal’s Algorithm and present C
programming code for this algorithm.
int father[MAX]; /*Holds father of each node */ struct edge tree[MAX]; /* Will
contain the edges of spanning tree */ int n; /*Denotes total number of nodes in the
graph */ int wt_tree=0; /*Weight of the spanning tree */ int count=0; /* Denotes
number of edges included in the tree */
/* Functions */ void make_tree(); void insert_tree(int i,int j,int wt); void
insert_pque(int i,int j,int wt); struct edge *del_pque(); create_graph(); main()
{ int i; create_graph(); make_tree(); printf("Edges to be included in spanning tree
are :\n"); for(i=1;i<=count;i++) { printf("%d->",tree[i].u); printf("%d\
n",tree[i].v); } printf("Weight of this minimum spanning tree is : %d\n", wt_tree);
getch(); }/*End of main()*/
create_graph() { int i,wt,max_edges,origin,destin; printf("Enter number of vertices
: "); scanf("%d",&n); max_edges=n*(n-1)/2; for(i=1;i<=max_edges;i++)
{ printf("Enter edge %d(end vertices): ",i); scanf("%d %d",&origin,&destin);
if( (origin==0) && (destin==0) ) break; printf("Enter weight for this edge : ");
scanf("%d",&wt); if( origin > n || destin > n || origin<=0 || destin<=0)
{ printf("Invalid edge!\n"); i--; } else insert_pque(origin,destin,wt);
}/*End of for*/ if(i==0) { printf("Spanning tree is not possible\n"); } }/*End of
create_graph()*/
/*Inserting edges in the priority queue */ void insert_pque(int i,int j,int wt)
{ struct edge *tmp,*q; tmp = (struct edge *)malloc(sizeof(struct edge)); tmp->u=i;
tmp->v=j; tmp->weight = wt;
/*Queue is empty or edge to be added has weight less than first edge*/ if( front ==
NULL || tmp->weight < front->weight )
{ tmp->link = front; front = tmp; } else { q = front; while( q->link != NULL && q-
>link->weight <= tmp>weight ) q=q->link; tmp->link = q->link; q->link = tmp; if(q-
>link == NULL) /*Edge to be added at the end*/ tmp->link = NULL; }/*End of
else*/ }/*End of insert_pque()*/
/*Deleting an edge from the priority queue*/ struct edge *del_pque() { struct edge
*tmp;
tmp = front; printf("Edge processed is %d->%d %d\n",tmp->u,tmp>v,tmp->weight);
front = front->link; return tmp; }/*End of del_pque()*/
OUTPUT The minimum spanning tree of the following graph through c programming :
Enter number of vertices : 5 Enter edge 1 (end vertices) : 1 2 Enter weight for
this edge : 2 Enter edge 2 (end vertices) : 2 3 Enter weight for this edge : 3
Enter edge 3 (end vertices) : 3 4 Enter weight for this edge : 2 Enter edge 4 (end
vertices) : 4 5 Enter weight for this edge : 1 Enter edge 5 (end vertices) : 5 1
Enter weight for this edge : 2 Enter weight for this edge : 5 2 Enter edge 6 (end
vertices) : 3 Enter weight for this edge : 2 5 Enter edge 7 (end vertices) : 1 5
Enter weight for this edge : 2 Enter edge 8 (end vertices) : 5 4 Enter weight for
this edge : 1 Enter edge 9 (end vertices) : 4 3 Enter weight for this edge : 2
Enter edge 10 (end vertices) : 3 2 Enter weight for this edge : 3 Edge processed is
4->5 1 n1=4 n2=5 rootn1=4 rootn2=5 This edge is inserted in the spanning tree Edge
processed is 5->4 1 n1=5 n2=4 rootn1=4 rootn2=4
Edge processed is 1->2 2 n1=1 n2=2 rootn1=1 rootn2=2 This edge is inserted in the
spanning tree Edge processed is 3->4 2 n1=3 n2=4 rootn1=3 rootn2=4 This edge is
inserted in the spanning tree Edge processed is 5->1 2 n1=5 n2=1 rootn1=3 rootn2=1
This edge is inserted in the spanning tree Edges to be inserted in the spanning
tree are : 4->5 1->2 3->4 5->1 Weight of this minimum spanning tree is : 7 Hence
the minimum spanning tree of above graph is :
PLANARITY Planar Graph:A graph G is said to be plannar if it can be drawn in a
plane without intersecting the edges except its end vertices. Every simple planar
graph contains a vertex of degree atmost five. Graph K5 (Complete graph in 5
vertices) and K3,3(a complete bipartite graph in 6 vertices) are non-plannar. A
graph is plannar if and only if it contains no subgraph homeomorphic to K5 or K3,3.
A graph is plannar if and only if it contains no subgraph contracible to K5 or
K3,3. Region or Face: Let G be a plannar graph. A region of G is the area in which
any arbitrary pair of points may be joined by a curve without intersecting any
edges of G. Let G be a connected planar graph with n vertices, m edges and r
regions then n-m+r=2. This interrelation was proposed by Euler. Let G be a
plannar graph with n vertices, m edges, r regions and k components. Then n-m+r=k+1.
If G is a connected simple plannar graph with n≥3 vertices and m edges then m≤3n-
6. If in addition G has no triangle then m≤2n-4.
/* Program for checking planarity of simple graph */
#include<stdio.h> #include<conio.h> #define max 20 int adj[max][max],c=0;
/*Adjacency matrix */ int n; /* Denotes number of nodes in the graph */ main()
{ int max_edges,i,origin,destin, no_edges; printf("Enter number of vertices : ");
scanf("%d",&n); if(n<=2) { printf("\nGraph is planar "); } else { max_edges=n*(n-
1); for(i=1;i<=max_edges;i++) { printf("Enter edge %d(end vertices) : ",i);
scanf("%d %d",&origin,&destin);
if( (origin==0) && (destin==0) ) break; if( origin > n || destin > n || origin<=0
|| destin<=0) { printf("Invalid edge!\n"); i--; } else { adj[origin][destin]=1;
c=c+1; } }/*End of for*/ no_edges=c/2; if(no_edges<=3*n-6) printf("\nGraph is
planar."); else printf("\nGraph is not planar."); } getch(); }
OUTPUT Test for the graph K3.
Enter number of vertices : 3 Enter edge 1 (end vertices) : 1 2 Enter edge 2 (end
vertices) : 2 3 Enter edge 3 (end vertices) : 3 1 Enter edge 4 (end vertices) : 1 3
Enter edge 5 (end vertices) : 3 2 Enter edge 6 (end vertices) : 2 1
Graph is planar.
OUTPUT Test for the graph K5.
Enter number of vertices : 5 Enter edges 1 (end vertices) : 1 2 Enter edges 2 (end
vertices) : 1 3 Enter edges 3 (end vertices) : 1 4 Enter edges 4 (end vertices) : 1
5 Enter edges 5 (end vertices) : 2 1 Enter edges 6 (end vertices) : 2 3 Enter edges
7 (end vertices) : 2 4 Enter edges 8 (end vertices) : 2 5 Enter edges 9 (end
vertices) : 3 1 Enter edges 10 (end vertices) : 3 2
Enter edges 11 (end vertices) : 3 4 Enter edges 12 (end vertices) : 3 5 Enter edges
13 (end vertices) : 4 1 Enter edges 14 (end vertices) : 4 2 Enter edges 15 (end
vertices) : 4 3 Enter edges 16 (end vertices) : 4 5 Enter edges 17 (end vertices) :
5 1 Enter edges 18 (end vertices) : 5 2 Enter edges 19 (end vertices) : 5 3 Enter
edges 20 (end vertices) : 5 4
Dijkstra’s Technique This technique is used to determine the shortest path between
two arbitrary vertices in a graph. Let w(vi,vj) is associated with every edge
(vi,vj) in a given graph G. Furthermore, the weights are such that the total weight
from vertex vi to the vertex vk via vertex vj is w(vi,vj)+w(vj+vk). Using this
technique the weight from a vertex vs (starting of the path) to vertex vt (the end
of the path) in the graph G for a given path (vs,v1), (v1,v2), (v2,v3), …, (vi, vt)
is given by w(vs,v1) + w(v1,v2) +
w(v2,v3) +...…+w(vi, vt). In a graph there are many possible paths between vs and
vt. Dijkstra’s method is very popular and efficient one to find every path from
starting to terminal vertices. If there is an edge between two vertices, then the
weight of this edge is its length. If several edges exist however, use the shortest
edge. If no edge actually exists set the length to infinity. Edge (vi,vj) does not
necessarily have the same length as edge (vj,vi). This allows different paths
depending on the direction of travel. Dijkstra’s technique is based on assigning
labels to each vertex. The label is equal to the distance (weight) from the
starting vertex to that vertex. Obviously, the starting vertex vs has a label 0. A
label can be in one of two state-temporary or permanent. A permanent label that
lies along the shortest path while a temporary label is one that has uncertainty as
to label is along the shortest path.
Dijkstra’s Algorithm Step 1: Assign a temporary label 1 (vs) = ∞ to all vertices
except vs. Step 2: [Mark vs as permanent by assigning 0 label to it] 1 (vs)=0 Step
3: [Assignment value] of vs to vr where vr is last vertex to be made permanent]
vr=vs Step 4: If 1(vi)>1(vk)+w(vk,vi) 1(vi)=1(vk)+w(vk,vi) Step 5: vr=vi Step 6: If
vt temporary label, repeat Step 4 to Step 5 otherwise the value of vt is permanent
label and is equal to the shortest path vs to vt. Step 7: Exit.
/*Program of shortest path between two node in graph using Djikstra’s algorithm */
#include<stdio.h> #define MAX 10 #define TEMP 0 #define PERM 1 #define infinity
9999 struct node { int predecessor; int dist; /*minimum distance of node from
source*/ int status; }; create_graph(); int adj[MAX][MAX]; int n; int findpath(int
s,int d,int path[MAX],int *sdist) { struct node state[MAX]; int
i,min,count=0,current,newdist,u,v; *sdist=0;
/* Make all nodes temporary */ for(i=1;i<=n;i++) { state[i].predecessor=0;
state[i].dist = infinity; state[i].status = TEMP; }
}/*End of display()*/
OUTPUT Shortest path between vertices 5 and 3 of following graph through c
programming :
Enter number of vertices : 5 Enter edge 1 (end vertices) : 1 2 Enter weight for
this edge : 20 Enter edge 2 (end vertices) : 2 3 Enter weight for this edge : 14
Enter edge 3 (end vertices) : 3 4 Enter weight for this edge : 50 Enter edge 4 (end
vertices) : 4 5 Enter weight for this edge : 5 Enter edge 5 (end vertices) : 5 1
Enter weight for this edge : 17 Enter edge 6 (end vertices) : 1 4 Enter weight for
this edge : 10 Enter edge 7 (end vertices) : 4 1 Enter weight for this edge : 10
Enter edge 8 (end vertices) : 1 5 Enter weight for this edge : 17 Enter edge 9 (end
verties) : 5 4 Enter weight for this edge : 5 Enter edge 10 (end vertices) : 4 3
Enter weight for this edge : 50 Enter edge 11 (end vertices) : 3 2 Enter weight for
this edge : 14 Enter edge 12 (end vertices) : 2 1 Enter weight for this edge : 20
Enter edge 13 (end vertices) : 1
2 Enter weight for this edge : 20 Enter edge 14 (end vertices) : 1 2 Enter weight
for this edge : 20 Enter edge 15 (end vertices) : 1 2 Enter weight for this edge :
20 Enter edge 16 (end vertices) : 1 2 Enter weight for this edge : 20 Enter edge 17
(end vertices) : 1 2 Enter weight for this edge : 20 Enter edge 18 (end vertices) :
1 2 Enter weight for this edge : 20 Enter edge 19 (end vertices) : 1 2 Enter weight
for this edge : 20 Enter edge 20 (end vertices) : 1 2 Enter weight for this edge :
20
Enter source vertex : 5 Enter destination vertex : 3 Shortest distance is : 49
Shortest path is : 5->4->1->2->3 Enter source vertex :
BIBLIOGRAPHY
Gupta M. K., “Discrete Mathematics”, Edition 2008, chapter 7-11, pages 327-502.
Patel, R. B., “Data structure through C”, Edition 2008, chapter 8, pages 591-664.
Wilson, R. J., “Graph Theory” ,Edition 2007, chapter 1-6 , page 196. Website:
http://www.indiastudychannel.com/
THE END