forked from rafi007akhtar/c-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph_rep.c
More file actions
124 lines (103 loc) · 2.48 KB
/
graph_rep.c
File metadata and controls
124 lines (103 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
Representation of the following graph.
G(V) = {0, 1, 2, 3, 4}
ADJACENCY LIST
---------------
0 -> 1 -> 4
1 -> 0 -> 2 -> 3 -> 4
2 -> 1 -> 3
3 -> 1 -> 2 -> 4
4 -> 0 -> 1 -> 3
The problem info and its program can also be found at:
http://www.geeksforgeeks.org/graph-and-its-representations/
*/
#include <stdio.h>
#include <stdlib.h>
// inline functions
#define allo(x, n) (x*)malloc(n*sizeof(x))
// create a structure for adjacency node
typedef struct node
{
int dest;
struct node* next;
} node;
// create a structure for adjacency list
typedef struct list
{
node* head; // pointer to the head node of the list
} list;
// create a structure for graph vertex; a graph is an array of adjacency lists
typedef struct graph
{
int v; // number of vertices in the graph
struct list* arr;
} graph;
// user-defined function prototypes
node* createNode(int dest); // create a new node
graph* createGraph(int v); // create a graph of v vertices
void addEdge(graph* g, int src, int dest); // add an edge from src to dest to the undirected graph g
void printGraph(graph* g); // print adjacency list rep of the graph
int main()
{
// At first, create a graph of 5 vertices
graph *g = createGraph(5);
// Now, add the required vertices
addEdge(g, 0, 1);
addEdge(g, 0, 4);
addEdge(g, 1, 4);
addEdge(g, 1, 2);
addEdge(g, 1, 3);
addEdge(g, 2, 3);
addEdge(g, 3, 4);
// Finally, print the graph
printGraph(g);
return 0;
}
// define all the user-defined functions
node* createNode(int dest)
{
node* newNode = allo(node, 1);
newNode->dest = dest;
newNode->next = NULL;
return newNode;
}
graph* createGraph(int v)
{
int i;
graph *g = allo(graph, 1);
g->v = v;
// create an array of adjacency lists of size v
g->arr = allo(list, v);
// initialize each of these lists as empty making its head null
for (i = 0; i < v; i++)
g->arr[i].head = NULL;
return g;
}
void addEdge(graph *g, int src, int dest)
{
node* newNode;
// create a link from dest to src
newNode= createNode(dest);
newNode->next = g->arr[src].head;
g->arr[src].head = newNode;
// create a link from src to dest (since this is undirected graph)
newNode = createNode(src);
newNode->next = g->arr[dest].head;
g->arr[dest].head = newNode;
}
void printGraph(graph* g)
{
int v;
printf("ADJACENCY LIST OF THE GRAPH\n--------------------\n");
for (v = 0; v < g->v; v++)
{
node *travel = g->arr[v].head;
printf("%d ", v);
while (travel)
{
printf("-> %d ", travel->dest);
travel = travel->next;
}
printf("\n");
}
}