forked from yennanliu/CS_basics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphClient.java
More file actions
33 lines (26 loc) · 794 Bytes
/
GraphClient.java
File metadata and controls
33 lines (26 loc) · 794 Bytes
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
// Graph client basic API (OP)
// https://www.coursera.org/learn/algorithms-part2/lecture/4ZE6G/graph-api
public class GraphClient{
public static int degree(Graph G, int v){
int degree = 0;
for (int w : G.adj(v)) degree++;
return degree;
}
public static int maxDegree(Graph G){
int max = 0;
for (int v = 0; v < G.V(); v++)
if (degree(G, v) > max)
max = degree(G, v);
return max;
}
public static double averageDegree(Graph G){
return 2.0 * G.E() / G.V();
}
public static int numberOfSelfLoops(Graph G){
int count = 0;
for (int v = 0; v < G.V(); v++)
for (int w : G.adj(v))
if (v == w) count ++;
return count / 2 ;
}
}