-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph_creation_by_adjacency_list.c
164 lines (143 loc) · 3.06 KB
/
Graph_creation_by_adjacency_list.c
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include<stdio.h>
#include<stdlib.h>
struct node{
int dest;
int traverse_bit;
struct node * next;
};
struct adj_list{
struct node *head;
};
struct Graph{
int V;
struct adj_list *array;
};
struct node* createNode(int dest)
{
struct node* temp= (struct node*) malloc(sizeof(struct node));
temp->dest= dest;
temp->next=NULL;
return temp;
}
int i;
struct Graph* create_Graph(int V)
{
struct Graph *graph = (struct Graph*) malloc (sizeof (struct Graph)); //memorization of graph
graph->V=V;
graph->array= (struct adj_list*) malloc (V* sizeof(struct adj_list)); //memorization of array
// initialize every list corresponding to vertex with null
for( i=0;i<graph->V;i++)
{
graph->array[i].head=NULL;
}
return graph;
}
//add an edge
addEdge(struct Graph* graph,int src,int dest )
{
//add an edge from source to destination
struct node * newNode= createNode(dest);
newNode->next=graph->array[src].head;
graph->array[src].head= newNode;
newNode->traverse_bit=0;
//since graph is undirected add an edge from destination to source
newNode=createNode(src);
newNode->next=graph->array[dest].head;
graph->array[dest].head= newNode;
newNode->traverse_bit=0;
}
//print graph
void printGraph(struct Graph* graph)
{
for(i=0;i<graph->V;i++)
{
printf("\n Adjacency list of vertex %d\n head ", i);
struct node* temp= (struct node*) malloc(sizeof(struct node));
temp= graph->array[i].head;
while(temp!=NULL)
{
printf("%d->",temp->dest);
temp=temp->next;
}
printf("\n");
}
}
//stack.contains
int present(int x,int V,int visited[])
{
for(i=0;i<V;i++)
{
if(visited[i]==x)
{
// printf("found %d ",x);
return 1;
break;
}
}
return 0;
}
//DFS traversal o(VE)
void DFS_traversal(struct Graph * graph,int V,int src)
{
static int visited[5];
static int i=0;
int t;
visited[i++]=src;
printf("%d -> ",src);
//printf("\n");
struct node *temp= graph->array[src].head;
while(temp->next!=NULL)
{
if(!present(temp->dest,V,visited))
{
DFS_traversal(graph,V,temp->dest);
}
else
temp=temp->next;
if(i==V)
break;
}
}
//DFS O(V+E)
void DFS_traversal_by_coloring(struct Graph * graph,int V,int src,int * visit)
{
static int visited[5];
static int i=0;
//visited[i++]=src;
printf("%d -> ",src);
//printf("\n");
visit[src]=1;
struct node *temp= graph->array[src].head;
while(temp)
{
//printf("hello");
if(visit[temp->dest]==0)
{
visit[temp->dest]=1;
DFS_traversal_by_coloring(graph,V,temp->dest,visit);
}
//else
temp=temp->next;
//if(i==V)
// break;
}
}
//driver program
int main()
{
int V=5;
int visit[5]={0};
struct Graph *graph = create_Graph(V); // create a graph containing 5 nodes
addEdge(graph,0,1);
addEdge(graph,0,4);
addEdge(graph,1,2);
addEdge(graph,1,3);
addEdge(graph,1,4);
addEdge(graph,2,3);
addEdge(graph,3,4);
printGraph(graph);
printf("inefficient dfs");
DFS_traversal(graph,V,0);
printf("\nefficient dfs");
DFS_traversal_by_coloring(graph,V,0,visit);
}