File tree 1 file changed +35
-0
lines changed
1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1
1
/*
2
2
Program to implement a simple graph
3
3
4
+ METHOD1: linked list
5
+ METHOD2: arrays
4
6
5
7
*/
8
+ //METHOD1
6
9
#include <stdio.h>
7
10
#include <stdlib.h>
8
11
@@ -66,4 +69,36 @@ int main(){
66
69
67
70
printGraph (myGraph );
68
71
return 0 ;
72
+ }
73
+ //====================================================================================================
74
+ //METHOD2
75
+ #include <stdio.h>
76
+ #include <stdlib.h>
77
+
78
+ void addEdge (int arr [][4 ],int src , int dest ){
79
+ arr [src ][dest ] = arr [dest ][src ] = 1 ;
80
+ }
81
+
82
+ void printGraph (int arr [][4 ]){
83
+ for (int i = 0 ;i < 4 ;i ++ ){
84
+ printf ("%d --> " , i );
85
+ for (int j = 0 ;j < 4 ;j ++ ){
86
+ if (arr [i ][j ]){
87
+ printf ("%d --> " , j );
88
+ }
89
+ }
90
+ printf ("\n" );
91
+ }
92
+ }
93
+
94
+ int main (){
95
+ int size = 4 ;
96
+ int graph [4 ][4 ] = {0 };
97
+ addEdge (graph , 0 , 1 );
98
+ addEdge (graph , 1 , 2 );
99
+ addEdge (graph , 1 , 3 );
100
+ addEdge (graph , 2 , 3 );
101
+
102
+ printGraph (graph );
103
+ return 0 ;
69
104
}
You can’t perform that action at this time.
0 commit comments