You are on page 1of 5

ASSIGNMENT 2 AIM OF THE EXPERIMENT:To write a program to construct weighted directed graph.

METHOD: Two structures namely header_node and edge are used to create node and edges of graph respectively. User is asked to enter no. of vertices, edges and weight of each edge and then the output is send to a file namely-grphop.c. The structure header_node contains two pointers:-one pointer is of header_node type which points to next header node and other pointer is of edge type which points to next egde. Thus completing structure of graph. header_node header node(source node) pointer to next header node pointer to adjacent node

Edge store destination node weight of edge pointer to next edge

SOURCE CODE ANALYSIS:Functions used in the program are:1. insert_node (int):First allocate memory for the structure header_node and then assigns the value to respective fields. 2. insert_edge (int, int, int):three parameters origin, destination and weight of edge are passed. The source and destination nodes are find using function find (int) so as to know whether they are present or not. If they are present their value is returned else 0 is returned. first allocate memory for the structure edge and then assign the value to the respective fields. Then all the adjacent edges are joined. 3. display ():This function is used to display the weighted directed graph. The output is send to file named grphop.c. In that file it displays what are the edges and their weight.

SOURCE CODE:-

#include<stdio.h> #include<stdlib.h> /*for malloc()*/ struct edge { int f; /*to store destination node*/ int wt; /*to store weight*/ struct edge *s; /*points to next edge*/ }; struct header_node { int v; /*to store header or source node*/ struct header_node *next; /*point to next header node*/ struct edge *adj; /*point to adjacent node*/ }*start='\0'; /*starting node is assigned name start and is set to value zero*/ struct header_node *find(int item); int main() { int i,n,edg,weight; int no,org,dest; printf("Enter no of vertices in the graph:"); scanf("%d",&n); printf("vertices are:"); for(i=1;i<=n;i++) { scanf("%d",&no); insert_node(no); /*all the nodes are created*/ } printf("Enter no of edges:"); scanf("%d",&edg); for(i=1;i<=edg;i++) { /*enter edge and weight*/ printf("Enter edge %d\t",i); scanf("%d %d",&org,&dest); printf("Enter weight:"); scanf("%d",&weight); insert_edge(org,dest,weight); /*insertion of edge*/ } display(); } insert_node(int node) { struct header_node *a,*b;

a=malloc(sizeof(struct header_node)); a->v=node; a->next='\0'; a->adj='\0'; if(start=='\0') { start=a; return; } b=start; while(b->next!='\0') { b=b->next; } b->next=a; } insert_edge(int l,int m,int k) { struct header_node *locu,*locv; struct edge *a,*b; locu=find(l); /*source node is searched*/ locv=find(m); /*destination node is searched*/ if(locu=='\0') { printf("Source node not present\n"); return; } if(locv=='\0') { printf("Destination node not present\n"); return; } a=malloc(sizeof(struct edge)); /*an edge is created*/ a->f=m; /*destination node is assigned*/ a->wt=k; /*weight is assigned*/ a->s='\0'; if(locu->adj=='\0') { locu->adj=a; return; } b=locu->adj;

while(b->s!='\0') b=b->s; b->s=a; } /*this function finds if source and destination nodes are present or not */ struct header_node *find(int item) { struct header_node *b,*loc; b=start; while(b!='\0') { if(item==b->v) { loc=b; return loc; } else { b=b->next; } } loc='\0'; return loc; } display() /*sends the output to a file*/ { FILE *p; struct header_node *b; struct edge *z; b=start; /*open file to send output*/ fopen("grphop.c","w",stdout); p=fopen("grphop.c","w"); fprintf(p,"\nedges are:\n"); fscanf(p,"%d%d",&b->v,z->f); while(b!='\0') { z=b->adj; while(z!='\0') { fprintf(p,"%d%d and weight is %d\n",b->v,z->f,z->wt); z=z->s; } fprintf(p,"\n"); b=b->next; } fclose(p); /*close file*/

CONCLUSION:Asks user to enter: Enter no of vertices in the graph: 4 Vertices are: 1 2 3 4 Enter no of edges: 5 Enter edge 1 1 2 Enter weight: 3 Enter edge 2 1 4 Enter weight: 7 Enter edge 3 3 2 Enter weight: 6 Enter edge 4 3 1 Enter weight: 8 Enter edge 5 2 3 Enter weight: 4 Output in file grphop.c Edges are: 12 and weight is 3 14 and weight is 7 23 and weight is 4 32 and weight is 6 31 and weight is 8

You might also like