-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathGraph.java
More file actions
76 lines (54 loc) · 1.67 KB
/
Copy pathGraph.java
File metadata and controls
76 lines (54 loc) · 1.67 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
package org.example.graph;
import java.util.*;
public class Graph {
private Map<Node, List<Node>> adjacencyList = new HashMap<>();
public int maxDepth = -1;
public final List<Node> visited = new ArrayList<>();
private Map<String, Integer> depths = new HashMap<>();
public Map<Node, List<Node>> getAdjacencyList() {
return adjacencyList;
}
public void setAdjacencyList(Map<Node, List<Node>> adjacencyList) {
this.adjacencyList = adjacencyList;
}
public int getMaxDepth() {
return maxDepth;
}
public void setMaxDepth(int maxDepth) {
this.maxDepth = maxDepth;
}
public List<Node> getVisited() {
return visited;
}
public Map<String, Integer> getDepths() {
return depths;
}
public void setDepths(Map<String, Integer> depths) {
this.depths = depths;
}
public void addNode(Node node) {
adjacencyList.putIfAbsent(node, new ArrayList<>());
}
public void addEdge(Node node1, Node node2) {
addNode(node1);
addNode(node2);
adjacencyList.get(node2).add(node1);
}
public double averageDepth() {
return depths.values().stream().mapToInt(i -> i).average().orElse(0);
}
public int dfs(Node root, int count) {
visited.add(root);
for (Node node : adjacencyList.get(root)) {
if (!visited.contains(node)) {
int dfs = dfs(node, ++count);
if (maxDepth < dfs) {
maxDepth = dfs;
}
depths.put(node.getClassName(), count);
count--;
}
}
return count;
}
}