-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocialnetworkproject.c
More file actions
161 lines (140 loc) · 3.02 KB
/
Copy pathsocialnetworkproject.c
File metadata and controls
161 lines (140 loc) · 3.02 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
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
#include<stdio.h>
#include<stdlib.h>
#define Max 100
typedef struct Node
{
int data;
struct Node*next;
}
Node;
typedef struct Queue
{
int items [Max];
int front, rear;
}
Queue;
Node*graph[Max];
int visited[Max];
Queue*createQueue()
{
Queue*q = (Queue*)malloc(sizeof(Queue));
q->front=-1;
q->rear=-1;
return q;
}
int isEmpty(Queue*q)
{
return(q->rear==-1);
}
void enqueue (Queue*q, int value)
{
if(q->rear==Max-1)
return;
if(q->front==-1)
q->front=0;
q->rear++;
q->items[q->rear] = value;
}
int dequeue(Queue*q)
{
int item;
if(isEmpty(q))return-1;
item=q->items[q->front];
q->front++;
if(q->front > q->rear) q->front = q->rear = -1;
return item;
}
void addEdge (int u, int v)
{
Node*newNode = (Node*)malloc(sizeof(Node));
newNode->data=v;
newNode->next=graph[u];
graph[u]=newNode;
newNode = (Node*)malloc(sizeof(Node));
newNode->data=u;
newNode->next=graph[v];
graph[v]=newNode;
}
void bfsRecommendations(int start, int n)
{
Queue*q = createQueue();
int level[Max] = {0};
int friends[Max] = {0};
int reco[Max] = {0};
for(int i=0; i<n; i++)
visited[i]=0;
enqueue(q, start);
visited[start]=1;
level[start]=0;
while(!isEmpty(q))
{
int cur = dequeue(q);
Node*temp = graph[cur];
while(temp!=NULL)
{
if(!visited[temp->data])
{
enqueue(q, temp->data);
visited[temp->data]=1;
level[temp->data]=level[cur]+1;
}
temp=temp->next;
}
}
Node*temp = graph[start];
while(temp!=NULL)
{
friends[temp->data]=1;
temp=temp->next;
}
printf("Friend Recommendations for user %d:\n", start);
int found = 0;
for (int i=0; i<n; i++)
{
if(i==start) continue;
if(level[i]==2 && !friends[i])
{
printf("User %d\n", i);
found = 1;
}
}
if(!found)
printf("No recommendations found.\n");
free(q);
}
int main()
{
int n,m;
printf("Enter the number of users (max%d): ", Max);
scanf("%d", &n);
if(n>Max)
n = Max;
for(int i=0; i<n; i++)
{
graph[i] = NULL;
}
printf("Enter the number of conections: ");
scanf("%d", &m);
printf("Enter each connections as two user IDs(0to%d):\n", n-1);
for(int i=0; i<m; i++)
{
int u,v;
scanf("%d%d", &u, &v);
addEdge(u, v);
}
int user;
printf("Enter the user ID to get recommendations for: ");
scanf("%d", &user);
bfsRecommendations (user, n);
for(int i=0; i<n; i++)
{
Node*temp = graph[i];
while (temp)
{
Node*to_free = temp;
temp = temp-> next;
free(to_free);
}
}
return 0;
}