-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkAdjList.java
More file actions
134 lines (114 loc) · 3.8 KB
/
NetworkAdjList.java
File metadata and controls
134 lines (114 loc) · 3.8 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
package a4;
import static org.junit.Assert.assertEquals;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Iterator;
import java.util.LinkedList;
public class NetworkAdjList {
// variables
private static int max_num_vertices = 7960;
private static LinkedList<Integer>[] adjacencyList;
@SuppressWarnings("unchecked")
// uses some assignment 2 file reader code modified a little
// reads each line and adds them to the adjacency list, undirected edges
public static void createAdjacencyList() {
adjacencyList = new LinkedList[max_num_vertices];
// creates adjacencyList, each position has its own linked list
for (int i = 0; i < max_num_vertices; i++) {
adjacencyList[i] = new LinkedList<Integer>();
}
try {
// reads file
@SuppressWarnings("resource")
BufferedReader br = new BufferedReader(new FileReader("3980.edges"));
while (br.ready()) {
// string variables are split into array and then parsed as integers
String temp = br.readLine();
String[] tempArr = temp.split("\\s+");
int i = Integer.parseInt(tempArr[0]);
int j = Integer.parseInt(tempArr[1]);
// if adj list position is empty vertex is added
if (adjacencyList[i].isEmpty()) {
adjacencyList[i].add(i);
}
// if adj list position is empty vertex is added
if (adjacencyList[j].isEmpty()) {
adjacencyList[j].add(j);
}
// links vertices
adjacencyList[i].add(j);
adjacencyList[j].add(i);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
// checks if two numbers "are friends", have a shared edge
public static boolean areFriends(int A, int B) {
// if adjacencyList position A contains B returns true
if (adjacencyList[A].contains(B)) {
return true;
}
return false;
}
// breadth first search of facebook users friends with 3980
public static void BFStraversal(int start) {
int distance = 0;
int vNum = max_num_vertices;
boolean userTest[] = new boolean[vNum];
LinkedList<Integer> usersChecked = new LinkedList<Integer>();
// sets value for current user as true for being the user focused on
userTest[start] = true;
usersChecked.add(start);
// while users is not 0 runs
while (usersChecked.size() != 0) {
// next user becomes the center of friend search
start = usersChecked.poll();
// distance increases
distance = distance + 1;
Iterator<Integer> i = adjacencyList[start].listIterator();
// while iterates till user's friend list depletes
while (i.hasNext()) {
int nextUser = i.next();
/*
* if checks if user's connection has been checked already prints message first
* connection found.
*/
if (!userTest[nextUser]) {
System.out.println(nextUser + " is at a distance of " + distance + " from 3980.");
userTest[nextUser] = true;
usersChecked.add(nextUser);
}
}
}
}
public static void main(String[] args) {
/**
* These test cases assume the file 3980.edges was used
*/
createAdjacencyList();
System.out.println("Testing...");
assertEquals(areFriends(4038, 4014), true);
System.out.println("1 of 7");
System.out.println("Testing...");
assertEquals(areFriends(3982, 4037), true);
System.out.println("2 of 7");
System.out.println("Testing...");
assertEquals(areFriends(4030, 4017), true);
System.out.println("3 of 7");
System.out.println("Testing...");
assertEquals(areFriends(4030, 1), false);
System.out.println("4 of 7");
System.out.println("Testing...");
assertEquals(areFriends(1, 4030), false);
System.out.println("5 of 7");
System.out.println("Testing...");
assertEquals(areFriends(4003, 3980), true);
System.out.println("6 of 7");
System.out.println("Testing...");
assertEquals(areFriends(3985, 4038), false);
System.out.println("7 of 7");
System.out.println("Success!");
BFStraversal(3980);
}
}