-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.java
More file actions
156 lines (134 loc) · 4.1 KB
/
Graph.java
File metadata and controls
156 lines (134 loc) · 4.1 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
import java.util.*;
public class Graph<T> {
private Map<Object, ArrayList<Object>> map;
private boolean bidirectional;
public Graph() {
map = new HashMap<>();
this.bidirectional = true;
}
public Graph(boolean bidirectional) {
map = new HashMap<>();
this.bidirectional = bidirectional;
}
public void addEdge(T start, T end) {
if (end == null && start != null) {
map.put(start, new ArrayList<>());
return;
}
if (start == null && end != null) {
map.put(end, new ArrayList<>());
return;
}
if (end == null && start == null) {
return;
}
if (!map.containsKey(start)) {
map.put(start, new ArrayList<>());
}
if (!map.containsKey(end)) {
map.put(end, new ArrayList<>());
}
if (bidirectional) {
map.get(start).add(end);
map.get(end).add(start);
} else {
map.get(start).add(end);
}
}
public List<List<T>> bfs() {
HashSet<T> vis;
vis = new HashSet<>();
List<List<T>> ans = new ArrayList<>();
map.forEach((k, v) -> {
if (!vis.contains(k)) {
ans.add(bfs((T) k, vis));
}
});
return ans;
}
public List<T> bfs(T source) {
if (!map.containsKey(source)) {
return new ArrayList<>();
}
HashSet<T> vis = new HashSet<>();
return this.bfs(source, vis);
}
private List<T> bfs(T source, HashSet<T> vis) {
ArrayList<T> ans = new ArrayList<>();
Queue<T> queue = new LinkedList<>();
queue.add(source);
vis.add(source);
while (!queue.isEmpty()) {
T node = queue.poll();
ans.add(node);
map.get(node).forEach(item -> {
if (!vis.contains((T) item)) {
vis.add((T) item);
queue.add((T) item);
}
});
}
return ans;
}
public List<List<T>> dfs() {
List<List<T>> ans = new ArrayList<>();
HashSet<T> visited = new HashSet<>();
map.forEach((k, v) -> {
if (!visited.contains(k)) {
ans.add(this.dfs((T) k, visited));
}
});
return ans;
}
// return only a component of graph containing that source in it
public List<T> dfs(T source) {
if (!map.containsKey(source)) {
return new ArrayList<>();
}
HashSet<T> vis = new HashSet<>();
return this.dfs(source, new HashSet<>());
}
private List<T> dfs(T s, HashSet<T> visited) {
ArrayList<T> ret = new ArrayList<>();
ret.add(s);
visited.add(s);
map.get(s).forEach((Object item) -> {
if (!visited.contains(item)) {
ret.addAll(dfs((T) item, visited));
}
});
return ret;
}
@Override // returns adjacency map
public String toString() {
return map.toString();
}
// Main function
public static void main(String[] args) {
// A Undirected graph of String(Countries)
Graph<String> graph = new Graph<>();
graph.addEdge("India", "Japan");
graph.addEdge("India", "USA");
graph.addEdge("Russia", "USA");
graph.addEdge("Korea", "Malaysia");
System.out.println(graph.dfs());
System.out.println(graph.bfs());
// dfs for a single component
System.out.println(graph.dfs("India"));
// bfs for a single component
System.out.println(graph.bfs("Japan"));
System.out.println(); // for line gap in output
// A directed graph of integers
Graph<Integer> directed=new Graph<>(false);
directed.addEdge(0,1);
directed.addEdge(0,2);
directed.addEdge(2,3);
directed.addEdge(3,1);
directed.addEdge(2,4);
directed.addEdge(3,5);
System.out.println(directed.dfs());
System.out.println(directed.bfs());
}
}
/**
* @author Pradumn Patel */