0% found this document useful (0 votes)
31 views

2 - Representation of Grpahs

The document discusses representing graphs in C language using adjacency matrices. It provides code samples to represent undirected unweighted, directed weighted, and directed unweighted graphs. Graphs are represented using adjacency matrices where rows and columns correspond to vertices and values indicate edges between vertices or edge weights. The conclusion summarizes that adjacency matrices are used to input and output graphs with user-provided vertices and edges.

Uploaded by

Aman Goyal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

2 - Representation of Grpahs

The document discusses representing graphs in C language using adjacency matrices. It provides code samples to represent undirected unweighted, directed weighted, and directed unweighted graphs. Graphs are represented using adjacency matrices where rows and columns correspond to vertices and values indicate edges between vertices or edge weights. The conclusion summarizes that adjacency matrices are used to input and output graphs with user-provided vertices and edges.

Uploaded by

Aman Goyal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

EXPERIMENT No.

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)}

Graph representation: Representation of Graphs


1. Adjacency Matrix
COMPUTER NETWORK LAB MANUAL

SKIT/ECE/VI SEM/CN LAB

 Adjacency matrix is a way to represent a graph.


 It shows which nodes are adjacent to one another.
 Graph is represented using a square matrix.

Adjacency matrix of an undirected graph is always a symmetric matrix which means an edge (i, j)
implies the edge (j, i).

Fig. 2.2: Adjacency matrix representation of undirected graph

Adjacency matrix of a directed graph is never symmetric adj[i][j] = 1, indicated a directed edge
from vertex i to vertex j.

Fig. 2.3: Adjacency matrix representation of directed graph

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

SKIT/ECE/VI SEM/CN LAB

Fig. 2.4: Adjacency matrix representation of directed graph

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.

Fig. 2.5: Undirected graph

Directed: A directed graph is a graph in which all the edges are uni-directional i.e. the edges point
in a single direction.

Fig. 2.6: Directed graph

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

SKIT/ECE/VI SEM/CN LAB

Fig. 2.7: Weighted Directed graph

C Program for compilation of Undirected graph: for vertices 4 and edges 3


#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
*/

int vertices, edges;


int u, v;
int i, j;

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

SKIT/ECE/VI SEM/CN LAB


}
}

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:

Undirected Unweighted Graph:


Enter vertices and Edges:
43
Enter (u v):
12
31
23
Graph:
0000
0011
0101
0110

Page 5
COMPUTER NETWORK LAB MANUAL

SKIT/ECE/VI SEM/CN LAB


C Program for compilation of Directional Weighted Graph: vertices 3 and edges 3
#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
* w is the edge weight or path cost
*/
int vertices, edges;
int u, v, w;
int i, j;
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 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

SKIT/ECE/VI SEM/CN LAB


printf("\n");
}
int main(){
printf("Directed Weighted Graph:\n");
printf("============================\n\n");
InputGraph();
PrintGraph();
return 0;
}

OUTPUT:

Directed Weighted Graph:


Enter vertices and Edges:
33
Enter (u v w):
012
211

123

Graph:
020
003
010

C Program for compilation of Directional Unweighted Graph: vertices 4 and edges 3

#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
*/

int vertices, edges;


int u, v;
int i, j;

Page 7
COMPUTER NETWORK LAB MANUAL

SKIT/ECE/VI SEM/CN LAB

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

SKIT/ECE/VI SEM/CN LAB


OUTPUT:

Directed Unweighted Graph:

Enter vertices and Edges:


43
Enter (u v):
Enter (u v):
01
02
31
Graph:
0110
0000
0000
0100

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:

Q.1 What is graph traversing technique?


Q.2 Data structure used in traversing of graph.
Q.3 What is minimum spanning tree algorithm in graph?
Q.4 Write down the different category of graph.

Page 9
COMPUTER NETWORK LAB MANUAL

SKIT/ECE/VI SEM/CN LAB

Page 10

You might also like