Uploaded by rafieameem

Graph

advertisement
Lab report:
Name of the experiment: Write a c program to implement Graph representation.
Objective: To implement graph using two dimensional matrix in c language.
Algorithm:
Graph Representation:::
Initialize(graph[][V])
1.Initialization i,j.
2.Repeat Steps 3 and 4 for i=1 to V
3.Repeat Steps 4 for j=1 to V
4.graph[i][j]=0.
5.return
AddEdge(graph[][V],source,destination)
1.Set graph[source][destination]=1.
2.return.
PrintAdjacent_Matrix(graph[][V])
1.Initialization i,j.
2.Repeat Steps 3 and 4 for i=1 to V
3.Repeat Steps 4 for j=1 to V
4.print:graph[i][j].
5.return.
Program code:
#include<stdio.h>
#define V 5
void Initialize(int graph[][V]);
void AddEdge(int graph[][V], int source, int destination);
void Printgraph(int graph[][V]);
int main()
{
int graph[V][V];
Initialize(graph);
AddEdge(graph, 0, 1);
AddEdge(graph, 0, 3);
AddEdge(graph, 0, 4);
AddEdge(graph, 1, 2);
AddEdge(graph, 1, 4);
AddEdge(graph, 2, 3);
AddEdge(graph, 3, 4);
PrintAdjMatrix(graph);
}
void Initialize(int graph[][V])
{
int i, j;
for(i=0; i<V; i++)
{
for(j=0; j<V; j++)
{
graph[i][j] = 0;
}
}
}
void AddEdge(int graph[][V], int source, int destination)
{
graph[source][destination] = 1;
}
void PrintAdjMatrix(int graph[][V])
{
int i, j;
for(i=0; i<V; i++)
{
for(j=0; j<V; j++)
{
printf("%d ", graph[i][j]);
}
printf("\n");
}
}
Output:
01011
00101
00010
00001
00000
Discussuion : This program is successfully compiled by code blocks.
Download