2 - Representation of Grpahs
2 - Representation of Grpahs
AIM:
Write a program for representation of unidirectional, directional weighted and unweighted
graph in C language..
SOFTWARE REQUIRED:
TURBO C
THEORY:
A Graph is a non-linear data structure consisting of nodes and edges. The nodes are sometimes also
referred to as vertices and the edges are lines or arcs that connect any two nodes in the graph.
Graph consists of two following components:
1. Vertices
2. Edges
Graph is a set of vertices (V) and set of edges (E).
V is a finite number of vertices also called as nodes.
E is a set of ordered pair of vertices representing edges.
Fig. 2.1: Graph 1 and Graph 2 are Undirected Graph and Graph 3 is directed Graph
Graph 1:
V = {A, B, C, D, E, F}
E = {(A, B), (A, C), (B, C), (B, D), (D, E), (D, F), (E, F)}
Graph 2:
V = {A, B, C, D, E, F}
E = {(A, B), (A, C), (B, D), (C, E), (C, F)}
Graph 3:
V = {A, B, C}
E = {(A, B), (A, C), (C, B)}
Adjacency matrix of an undirected graph is always a symmetric matrix which means an edge (i, j)
implies the edge (j, i).
Adjacency matrix of a directed graph is never symmetric adj[i][j] = 1, indicated a directed edge
from vertex i to vertex j.
2. Adjacency List
Adjacency list is another representation of graphs.
It is a collection of unordered list, used to represent a finite graphs.
Each list describes the set of neighbors of a vertex in the graph.
Adjacency list requires less amount of memory.
For every vertex, adjacency list stores a list of vertices, which are adjacent to the current one.
In adjacency list, an array of linked list is used. Size of the array is equal to the number of
vertices.
Page 2
COMPUTER NETWORK LAB MANUAL
Types of graphs
Undirected: An undirected graph is a graph in which all the edges are bi-directional i.e. the
edges do not point in any specific direction.
Directed: A directed graph is a graph in which all the edges are uni-directional i.e. the edges point
in a single direction.
Weighted directed graph: In a weighted graph, each edge is assigned a weight or cost. Consider a
graph of 4 nodes as in the diagram below. As you can see each edge has a weight/cost assigned to
it.
Page 3
COMPUTER NETWORK LAB MANUAL
/*
* u is the current or source vertex
* v is the next or destination vertex
*/
void InputGraph(){
printf("Enter vertices and Edges:\n");
scanf("%d%d", &vertices, &edges);
// Reset graph
for(i = 0; i< vertices; ++i)
for(j = 0; j < vertices; ++j)
Graph[i][j] = 0;
// Input Graph
printf("Enter (u v):\n");
for(i = 0; i< edges; ++i){
scanf("%d%d", &u, &v);
// Here value of 1 represents there is an edge (u,v)
Graph[u][v] = Graph[v][u] = 1;
Page 4
COMPUTER NETWORK LAB MANUAL
void PrintGraph(){
// Print the current Graph
printf("\n");
printf("Graph:\n");
for(i = 0; i< vertices; ++i){
for(j = 0; j < vertices; ++j)
printf("%d ", Graph[i][j]);
printf("\n");
}
printf("\n");
}
int main(){
printf("Undirected Unweighted Graph:\n");
printf("============================\n\n");
InputGraph();
PrintGraph();
return 0;
}
OUTPUT:
Page 5
COMPUTER NETWORK LAB MANUAL
// Reset graph
for(i = 0; i< vertices; ++i)
for(j = 0; j < vertices; ++j)
Graph[i][j] = 0;
// Input Graph
printf("Enter (u v w):\n");
for(i = 0; i< edges; ++i){
scanf("%d%d%d", &u, &v, &w);
Graph[u][v] = w;
}
}
void PrintGraph(){
// Print the current Graph
printf("\n");
printf("Graph:\n");
for(i = 0; i< vertices; ++i){
for(j = 0; j < vertices; ++j)
printf("%d ", Graph[i][j]);
printf("\n");
}
Page 6
COMPUTER NETWORK LAB MANUAL
OUTPUT:
123
Graph:
020
003
010
#include<stdio.h>
#define N 100
* Graph is the graph representation in adjacency matrix
*/
int Graph[N][N];
/*
* u is the current or source vertex
* v is the next or destination vertex
*/
Page 7
COMPUTER NETWORK LAB MANUAL
void InputGraph(){
printf("Enter vertices and Edges:\n");
scanf("%d%d", &vertices, &edges);
// Reset graph
for(i = 0; i< vertices; ++i)
for(j = 0; j < vertices; ++j)
Graph[i][j] = 0;
// Input Graph
printf("Enter (u v):\n");
for(i = 0; i< edges; ++i){
scanf("%d%d", &u, &v);
// For directed graph edges (u,v) != (v,u)
Graph[u][v] = 1;
}
}
void PrintGraph(){
// Print the current Graph
printf("\n");
printf("Graph:\n");
for(i = 0; i< vertices; ++i){
for(j = 0; j < vertices; ++j)
printf("%d ", Graph[i][j]);
printf("\n");
}
printf("\n");
}
int main(){
printf("Directed Unweighted Graph:\n");
printf("============================\n\n");
InputGraph();
PrintGraph();
return 0;
}
Page 8
COMPUTER NETWORK LAB MANUAL
CONCLUSION:
Undirected Graph, directed graph is represented through Adjacency Matrix. Input values are taken
from user in the form of vertices and edges. Output is in the form of Adjacency Matrix where zero
shows that there is no connection between vertices and non-zero shows there is a connection
between any two vertices and that non-zero value define the weight between two vertices. In
direction graph, the direction of flow is from row address to column address. Vertices are connected
with others through Edges. Adjacency Matrix is a 2D matrix where rows and column are a set of
vertices.
DISCUSSION:
Page 9
COMPUTER NETWORK LAB MANUAL
Page 10