Skip to content

Commit 52aa012

Browse files
Add files via upload
1 parent ea8218a commit 52aa012

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//Implementation of graph by adjacency list
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
5+
typedef struct node{
6+
int info;
7+
struct node* link;
8+
}Node1;
9+
Node1* createNode(int);
10+
11+
typedef struct Graph {
12+
int numVertices;
13+
struct node** adjLists;
14+
}Node2;
15+
16+
// Create a node
17+
Node1* createNode(int value) {
18+
Node1 *newptr=NULL;
19+
newptr=(Node1*)malloc(sizeof(Node1));
20+
newptr->info=value;
21+
newptr->link=NULL;
22+
return newptr;
23+
}
24+
25+
// Create a graph
26+
Node2* createAGraph(int vertices) {
27+
Node2* graph;
28+
graph=(Node2*)malloc(sizeof(Node2));
29+
graph->numVertices = vertices;
30+
graph->adjLists = (Node1**)malloc(vertices * sizeof(Node1*));
31+
int i;
32+
for(i=0;i<vertices;i++){
33+
graph->adjLists[i]=NULL;
34+
}
35+
return graph;
36+
}
37+
38+
// Add edge
39+
void addEdge(Node2* graph, int s, int d) {
40+
// Add edge from s to d
41+
Node1* newNode = createNode(d);
42+
newNode->link = graph->adjLists[s];
43+
graph->adjLists[s] = newNode;
44+
45+
// Add edge from d to s
46+
newNode = createNode(s);
47+
newNode->link = graph->adjLists[d];
48+
graph->adjLists[d] = newNode;
49+
}
50+
51+
// Print the graph
52+
void printGraph(struct Graph* graph) {
53+
int v;
54+
for (v = 0; v < graph->numVertices; v++) {
55+
struct node* temp = graph->adjLists[v];
56+
printf("\n Vertex %d:: ", v+1);
57+
while(temp) {
58+
printf("%d->", temp->info+1);
59+
temp = temp->link;
60+
}
61+
printf("NULL\n");
62+
}
63+
}
64+
65+
int main() {
66+
int v,i,j;
67+
char choice;
68+
printf("Enter the no. of vertices: ");
69+
scanf("%d", &v);
70+
Node2* graph=createAGraph(v);
71+
for(i=0;i<v;i++){
72+
for(j=i;j<v;j++){
73+
printf("Is there any edge between %d and %d ?",i+1,j+1);
74+
scanf(" %c", &choice);
75+
if(choice=='Y' || choice=='y'){
76+
addEdge(graph, i, j);
77+
}
78+
else{
79+
continue;
80+
}
81+
}
82+
}
83+
printGraph(graph);
84+
85+
return 0;
86+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//Implementation of graph by adjacency matrix
2+
#include <stdio.h>
3+
#define MAX 5
4+
int v,e;
5+
6+
// Add edges
7+
void addEdge(int arr[][MAX], int i, int j) {
8+
arr[i][j] = 1;
9+
arr[j][i] = 1;
10+
}
11+
12+
int main(){
13+
int i,j,v,e;
14+
int arr[MAX][MAX];
15+
char choice;
16+
printf("Enter the no of vertices:\n");
17+
scanf("%d", &v);
18+
for(i=0;i<v;i++){
19+
for(j=0;j<v;j++){ //Initialize the matrix to zero
20+
arr[i][j]=0;
21+
}
22+
}
23+
for(i=0;i<v;i++){
24+
for(j=i;j<v;j++){
25+
printf("Is there any edge between %d and %d ?",i+1,j+1);
26+
scanf(" %c", &choice);
27+
if(choice=='Y' || choice=='y'){
28+
addEdge(arr, i, j);
29+
}
30+
else{
31+
continue;
32+
}
33+
}
34+
}
35+
printf("\nAdjacency matrix of the Graph::\n\n");
36+
for(i=0;i<v;i++) {
37+
printf("%d: ",i+1);
38+
for(j=0;j<v;j++){
39+
printf("%d ", arr[i][j]);
40+
}
41+
printf("\n");
42+
}
43+
return 0;
44+
}

0 commit comments

Comments
 (0)