This repository has been archived by the owner on Jun 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
F_Jay_Patel.c
107 lines (84 loc) · 1.74 KB
/
F_Jay_Patel.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
#include <stdio.h>
#include <stdlib.h>
#define N 1000
int heap[N];
int size = 0;
void swap(int* a, int * b) {
int temp = *a;
*a = *b;
*b = temp;
}
void minHeapify(int heap[], int i) {
int parent = (i-1)/2;
if(i >= 0 && heap[parent] > heap[i]) {
swap(&heap[parent],&heap[i]);
minHeapify(heap, parent);
}
}
void insertNode(int x) {
heap[size] = x;
size++;
minHeapify(heap, size-1);
}
struct Node {
int vertex;
struct Node * next;
};
struct Graph {
int numVertices;
struct Node** adjList;
};
typedef struct Node Node;
typedef struct Graph Graph;
Node* createNode(int v) {
Node* node = (Node*) malloc(sizeof(Node));
node -> vertex = v;
node -> next = NULL;
return node;
}
Graph* createGraph(int numVertices) {
Graph* graph = (Graph*) malloc(sizeof(Graph));
graph -> numVertices = numVertices;
graph -> adjList = malloc(numVertices * sizeof(Node));
for(int i = 1; i <= numVertices; i++) {
graph -> adjList[i] = NULL;
}
return graph;
}
void addEdge(Graph* graph, int u, int v) {
Node* node = createNode(v);
node -> next = graph -> adjList[u];
graph -> adjList[u] = node;
node = createNode(u);
node -> next = graph -> adjList[v];
graph -> adjList[v] = node;
}
void BFS(Graph* graph, int start) {
int visited[N] = {0};
insertNode(start);
visited[start] = 1;
while(size) {
int v = heap[0];
printf("%d ",v);
swap(&heap[0],&heap[size-1]);
size--;
for(Node* u = graph -> adjList[v]; u != NULL; u = u -> next){
if(! visited[u -> vertex]) {
visited[u -> vertex] = 1;
insertNode(u -> vertex);
}
}
}
}
int main() {
int n, m;
scanf("%d %d",&n,&m);
Graph* graph = createGraph(n);
for(int i = 0; i < m; i++) {
int u, v;
scanf("%d %d",&u,&v);
addEdge(graph, u, v);
}
BFS(graph, 1);
return 0;
}