-
Notifications
You must be signed in to change notification settings - Fork 0
/
social_network.c
832 lines (666 loc) · 13.5 KB
/
social_network.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#define MAX_STR_LEN 1024
#ifndef MAT_SIZE
#define MAT_SIZE 3 // A small graph
#endif
typedef struct user_struct
{
char name[MAX_STR_LEN];
struct friend_node_struct *friends;
struct brand_node_struct *brands;
bool visited;
int distance;
} User;
typedef struct friend_node_struct
{
User *user;
struct friend_node_struct *next;
} FriendNode;
typedef struct brand_node_struct
{
char brand_name[MAX_STR_LEN];
struct brand_node_struct *next;
} BrandNode;
FriendNode *allUsers = NULL;
int brand_adjacency_matrix[MAT_SIZE][MAT_SIZE];
char brand_names[MAT_SIZE][MAX_STR_LEN];
bool in_friend_list(FriendNode *head, User *node)
{
for (FriendNode *cur = head; cur != NULL; cur = cur->next)
{
if (strcmp(cur->user->name, node->name) == 0)
{
return true;
}
}
return false;
}
bool in_brand_list(BrandNode *head, char *name)
{
for (BrandNode *cur = head; cur != NULL; cur = cur->next)
{
if (strcmp(cur->brand_name, name) == 0)
{
return true;
}
}
return false;
}
FriendNode *insert_into_friend_list(FriendNode *head, User *node)
{
if (node == NULL)
return head;
if (in_friend_list(head, node))
{
printf("User already in list\n");
return head;
}
FriendNode *fn = calloc(1, sizeof(FriendNode));
fn->user = node;
fn->next = NULL;
if (head == NULL)
return fn;
if (strcmp(head->user->name, node->name) > 0)
{
fn->next = head;
return fn;
}
FriendNode *cur;
for (cur = head; cur->next != NULL && strcmp(cur->next->user->name, node->name) < 0;
cur = cur->next)
;
fn->next = cur->next;
cur->next = fn;
return head;
}
BrandNode *insert_into_brand_list(BrandNode *head, char *node)
{
if (node == NULL)
return head;
if (in_brand_list(head, node))
{
printf("Brand already in list\n");
return head;
}
BrandNode *fn = calloc(1, sizeof(BrandNode));
strcpy(fn->brand_name, node);
fn->next = NULL;
if (head == NULL)
return fn;
if (strcmp(head->brand_name, node) > 0)
{
fn->next = head;
return fn;
}
BrandNode *cur;
for (cur = head; cur->next != NULL && strcmp(cur->next->brand_name, node) < 0;
cur = cur->next)
;
fn->next = cur->next;
cur->next = fn;
return head;
}
FriendNode *delete_from_friend_list(FriendNode *head, User *node)
{
if (node == NULL)
return head;
if (!in_friend_list(head, node))
{
printf("User not in list\n");
return head;
}
if (strcmp(head->user->name, node->name) == 0)
{
FriendNode *temp = head->next;
free(head);
return temp;
}
FriendNode *cur;
for (cur = head; cur->next->user != node; cur = cur->next)
;
FriendNode *temp = cur->next;
cur->next = temp->next;
free(temp);
return head;
}
BrandNode *delete_from_brand_list(BrandNode *head, char *node)
{
if (node == NULL)
return head;
if (!in_brand_list(head, node))
{
printf("Brand not in list\n");
return head;
}
if (strcmp(head->brand_name, node) == 0)
{
BrandNode *temp = head->next;
free(head);
return temp;
}
BrandNode *cur;
for (cur = head; strcmp(cur->next->brand_name, node) != 0; cur = cur->next)
;
BrandNode *temp = cur->next;
cur->next = temp->next;
free(temp);
return head;
}
void print_user_data(User *user)
{
printf("User name: %s\n", user->name);
printf("Friends:\n");
for (FriendNode *f = user->friends; f != NULL; f = f->next)
{
printf(" %s\n", f->user->name);
}
printf("Brands:\n");
for (BrandNode *b = user->brands; b != NULL; b = b->next)
{
printf(" %s\n", b->brand_name);
}
}
int get_brand_index(char *name)
{
for (int i = 0; i < MAT_SIZE; i++)
{
if (strcmp(brand_names[i], name) == 0)
{
return i;
}
}
printf("Brand '%s' not found\n", name);
return -1; // Not found
}
void print_brand_data(char *brand_name)
{
int idx = get_brand_index(brand_name);
if (idx < 0)
{
printf("Brand '%s' not in the list.\n", brand_name);
return;
}
printf("Brand name: %s\n", brand_name);
printf("Brand idx: %d\n", idx);
printf("Similar brands:\n");
for (int i = 0; i < MAT_SIZE; i++)
{
if (brand_adjacency_matrix[idx][i] == 1 && strcmp(brand_names[i], "") != 0)
{
printf(" %s\n", brand_names[i]);
}
}
}
void populate_brand_matrix(char *file_name)
{
// Read the file
char buff[MAX_STR_LEN];
FILE *f = fopen(file_name, "r");
fscanf(f, "%s", buff);
char *line = buff;
// Load up the brand_names matrix
for (int i = 0; i < MAT_SIZE; i++)
{
if (i == MAT_SIZE - 1)
{
strcpy(brand_names[i], line);
break;
}
int index = strchr(line, ',') - line;
strncpy(brand_names[i], line, index);
line = strchr(line, ',') + sizeof(char);
}
// Load up the brand_adjacency_matrix
for (int x = 0; x < MAT_SIZE; x++)
{
fscanf(f, "%s", buff);
for (int y = 0; y < MAT_SIZE; y++)
{
int value = (int)buff[y * 2];
if (value == 48)
{
value = 0;
}
else
{
value = 1;
}
brand_adjacency_matrix[x][y] = value;
}
}
}
User *create_user(char *name)
{
User *new_node;
new_node = (User *)calloc(1,sizeof(User));
strcpy(new_node->name, name);
new_node->friends = NULL;
new_node->brands = NULL;
new_node->distance= 0;
new_node->visited = false;
bool check = in_friend_list(allUsers, new_node);
if(check == true)
{
return NULL;
}
else
{
allUsers = insert_into_friend_list(allUsers, new_node);
return new_node;
}
}
int delete_user(User *user)
{
bool check = in_friend_list(allUsers, user); //check if u use allUsers here
if(check == false)
{
return -1;
}
else
{
allUsers = delete_from_friend_list(allUsers, user);
FriendNode *temp = allUsers;
while(temp != NULL)
{
bool check2 = in_friend_list(temp->user->friends, user);
if(check2 == true)
{
temp->user->friends = delete_from_friend_list(temp->user->friends, user);
}
temp = temp->next;
}
return 0;
}
}
int add_friend(User *user, User *friend)
{
bool check = in_friend_list(user->friends, friend);
if(check == true || strcmp(user->name, friend->name) == 0)
{
return -1;
}
else
{
user->friends = insert_into_friend_list(user->friends, friend);
friend->friends = insert_into_friend_list(friend->friends, user); //assume friend doesnt have user in their list?
return 0;
}
}
int remove_friend(User *user, User *friend)
{
bool check = in_friend_list(user->friends, friend);
if(check == false)
{
return -1;
}
else
{
user->friends = delete_from_friend_list(user->friends, friend);
friend->friends = delete_from_friend_list(friend->friends, user); //assume friend doesnt have user in their list?
return 0;
}
}
int follow_brand(User *user, char *brand_name)
{
bool check = in_brand_list(user->brands, brand_name);
int invalid = get_brand_index(brand_name);
if(check == true || invalid == -1)
{
return -1;
}
else
{
user->brands = insert_into_brand_list(user->brands, brand_name);
return 0;
}
}
int unfollow_brand(User *user, char *brand_name)
{
bool check = in_brand_list(user->brands, brand_name);
int invalid = get_brand_index(brand_name);
if(check == false || invalid == -1)
{
return -1;
}
else
{
user->brands = delete_from_brand_list(user->brands, brand_name);
return 0;
}
}
int get_len(FriendNode *head)
{
FriendNode *b = head;
int j = 0;
while(b != NULL)
{
j++;
b = b->next;
}
return j;
}
int get_mutual_friends(User *a, User *b)
{
FriendNode *j = a->friends;
FriendNode *f = b->friends;
int a_max = get_len(j);
char arr[a_max][MAX_STR_LEN];
int i = 0;
while(j != NULL && i < a_max)
{
strcpy(arr[i], j->user->name);
j = j->next;
i++;
}
int count = 0;
while(f != NULL)
{
for(int h = 0; h < a_max; h++)
{
if(strcmp(arr[h], f->user->name) == 0)
{
count++;
}
}
f = f->next;
}
return count;
}
FriendNode *insert_tail(FriendNode *head, User *a)
{
FriendNode *fn = NULL;
fn = calloc(1, sizeof(FriendNode));
fn->user = a;
fn->next = NULL;
if(head == NULL)
{
return fn;
}
FriendNode *p = NULL;
p = head;
while(p->next != NULL)
{
p = p->next;
}
fn->next = p->next;
p->next = fn;
return head;
}
int helper(User *a, User *b)
{
if(strcmp(a->name, b->name) == 0)
{
return 1;
}
return -1;
}
int get_degrees_of_connection(User *a, User *b)
{
FriendNode *f = allUsers;
while(f != NULL)
{
f->user->visited = false;
f->user->distance = 0;
f = f->next;
}
FriendNode *q = NULL;
int deg = 0;
if(strcmp(a->name, b->name) == 0)
{
return deg;
}
a->visited = true;
a->distance = 0;
q = insert_tail(q, a);
while(q != NULL)
{
User *temp = q->user;
q = delete_from_friend_list(q, temp);
FriendNode *t = temp->friends;
while(t != NULL)
{
if(t->user->visited == false)
{
q = insert_tail(q, t->user);
t->user->distance = temp->distance + 1;
t->user->visited = true;
int help = helper(t->user, b);
if(help == 1)
{
return t->user->distance;
}
}
t = t->next;
}
}
return -1;
}
void connect_similar_brands(char *brandNameA, char *brandNameB)
{
int ind_A = get_brand_index(brandNameA);
int ind_B = get_brand_index(brandNameB);
if(ind_A == -1 || ind_B == -1)
{
return;
}
else
{
brand_adjacency_matrix[ind_A][ind_B] = 1;
brand_adjacency_matrix[ind_B][ind_A] = 1;
return;
}
}
void remove_similar_brands(char *brandNameA, char *brandNameB)
{
int ind_A = get_brand_index(brandNameA);
int ind_B = get_brand_index(brandNameB);
if(ind_A == -1 || ind_B == -1)
{
return;
}
else
{
brand_adjacency_matrix[ind_A][ind_B] = 0;
brand_adjacency_matrix[ind_B][ind_A] = 0;
return;
}
}
int get_brand_len(BrandNode *head)
{
BrandNode *b = head;
int j = 0;
while(b != NULL)
{
j++;
b = b->next;
}
return j;
}
int get_mutual_brands(User *a, User *b)
{
BrandNode *j = a->brands;
BrandNode *f = b->brands;
int a_max = get_brand_len(j);
char arr[a_max][MAX_STR_LEN];
int i = 0;
while(j != NULL && i < a_max)
{
strcpy(arr[i], j->brand_name);
j = j->next;
i++;
}
int count = 0;
while(f != NULL)
{
for(int h = 0; h < a_max; h++)
{
if(strcmp(arr[h], f->brand_name) == 0)
{
count++;
}
}
f = f->next;
}
return count;
}
User *get_suggested_friend(User *user)
{
FriendNode *headptr = allUsers;
int max = 0;
int compare;
User *sugg_friend = NULL;
while(headptr != NULL)
{
bool check = in_friend_list(user->friends, headptr->user);
if(strcmp(headptr->user->name, user->name) != 0 && check == false)
{
compare = get_mutual_brands(user, headptr->user);
if(compare >= max)
{
max = compare;
sugg_friend = headptr->user;
}
}
headptr = headptr->next;
}
return sugg_friend;
}
int add_suggested_friends(User *user, int n)
{
int c = 0;
User *add = get_suggested_friend(user);
if(add != NULL)
{
int j = add_friend(user, add);
if(j != -1)
{
c++;
}
}
while(add != NULL && c < n)
{
User *add = get_suggested_friend(user);
if(add == NULL)
{
break;
}
else
{
int g = add_friend(user, add);
if(g != -1)
{
c++;
}
}
}
return c;
}
char follow[MAT_SIZE][MAX_STR_LEN];
char sugg_brand[MAT_SIZE][MAX_STR_LEN];
int get_follow_index(char array[MAT_SIZE][MAX_STR_LEN], char *name)
{
for (int i = 0; i < MAT_SIZE; i++)
{
if (strcmp(array[i], name) == 0)
{
return 3;
}
}
return -1;
}
char *get_suggested_brand(User *user)
{
int one;
int two;
BrandNode *ptr = user->brands;
int sim_brand[MAT_SIZE] = {0};
strcpy(sugg_brand[0], " ");
if(ptr == NULL)
{
for(int i = 0; i < MAT_SIZE; i++)
{
int check_emp = get_follow_index(follow, brand_names[i]);
if(check_emp == -1)
{
if(strcmp(brand_names[i], sugg_brand[0]) > 0)
{
strcpy(sugg_brand[0], brand_names[i]);
}
}
}
char *holder = sugg_brand[0];
return holder;
}
else
{
for(int i = 0; i < MAT_SIZE; i++)
{
bool check = in_brand_list(user->brands, brand_names[i]);
int check2 = get_follow_index(follow, brand_names[i]);
if(check == false && check2 == -1)
{
ptr = user->brands;
while(ptr != NULL)
{
one = get_brand_index(ptr->brand_name);
two = get_brand_index(brand_names[i]);
if(brand_adjacency_matrix[one][two] == 1 && brand_adjacency_matrix[two][one] == 1)
{
sim_brand[i] += 1;
}
ptr = ptr->next;
}
}
}
}
int h = 0;
int max = 0;
while(h != MAT_SIZE)
{
if(sim_brand[h] > max)
{
strcpy(sugg_brand[0], brand_names[h]);
max = sim_brand[h];
}
if(sim_brand[h] == max)
{
if(strcmp(brand_names[h], sugg_brand[0]) > 0)
{
strcpy(sugg_brand[0], brand_names[h]);
}
}
h++;
}
char *holder = sugg_brand[0];
return holder;
}
int follow_suggested_brands(User *user, int n)
{
if(user == NULL)
{
return 0;
}
int c = 0;
for (int i = 0; i < MAT_SIZE; i++)
{
strcpy(follow[i], " ");
}
for(int j = 0; j < n; j++)
{
strcpy(follow[j], get_suggested_brand(user));
c++;
if(strcmp(follow[j], " ") == 0)
{
c = c - 1;
break;
}
}
for(int f = 0; f < c; f++)
{
follow_brand(user, follow[f]);
}
return c;
}