Repository navigation
Expand file tree
/
Copy pathGraph.java
More file actions
165 lines (136 loc) · 5.85 KB
/
Copy pathGraph.java
File metadata and controls
165 lines (136 loc) · 5.85 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
157
158
159
160
161
162
163
164
165
// Реализация графа с использованием собственных коллекций
public class Graph {
private final MyList<Integer> vertices; // Список всех вершин графа
private final MyList<MyList<Integer>> adjacencyList; // Списки смежности для каждой вершины
private final boolean directed; // Флаг ориентированности графа
// Конструкторы и базовые методы управления графом
public Graph() {
this(false);
}
public Graph(boolean directed) {
this.vertices = new MyList<>();
this.adjacencyList = new MyList<>();
this.directed = directed;
}
// Добавление вершины (игнорирует дубликаты)
public void addVertex(int vertex) {
if (!vertices.contains(vertex)) {
vertices.add(vertex);
adjacencyList.add(new MyList<>());
}
}
// Добавление ребра между вершинами
public void addEdge(int from, int to) {
addVertex(from); // Гарантирует существование вершин
addVertex(to);
int fromIndex = vertices.indexOf(from);
int toIndex = vertices.indexOf(to);
MyList<Integer> neighbors = adjacencyList.get(fromIndex);
if (!neighbors.contains(to)) {
neighbors.add(to);
}
// Для неориентированного графа добавляем обратное ребро
if (!directed) {
MyList<Integer> reverseNeighbors = adjacencyList.get(toIndex);
if (!reverseNeighbors.contains(from)) {
reverseNeighbors.add(from);
}
}
}
// Проверка существования вершины
public boolean hasVertex(int vertex) {
return vertices.contains(vertex);
}
// Проверка существования ребра
public boolean hasEdge(int from, int to) {
if (!hasVertex(from) || !hasVertex(to)) {
return false;
}
int fromIndex = vertices.indexOf(from);
MyList<Integer> neighbors = adjacencyList.get(fromIndex);
return neighbors.contains(to);
}
// Получение соседей вершины
public MyList<Integer> getNeighbors(int vertex) {
if (!hasVertex(vertex)) {
return new MyList<>();
}
int index = vertices.indexOf(vertex);
return adjacencyList.get(index);
}
// Обход в глубину (DFS)
public MyList<Integer> dfs(int startVertex) {
MyList<Integer> result = new MyList<>();
if (!hasVertex(startVertex)) return result;
boolean[] visited = new boolean[vertices.size()];
dfsHelper(startVertex, visited, result);
return result;
}
// Рекурсивный помощник для DFS
private void dfsHelper(int vertex, boolean[] visited, MyList<Integer> result) {
int index = vertices.indexOf(vertex);
visited[index] = true;
result.add(vertex);
// Рекурсивно посещаем всех непосещенных соседей
MyList<Integer> neighbors = getNeighbors(vertex);
for (int i = 0; i < neighbors.size(); i++) {
int neighbor = neighbors.get(i);
int neighborIndex = vertices.indexOf(neighbor);
if (!visited[neighborIndex]) {
dfsHelper(neighbor, visited, result);
}
}
}
// Обход в ширину (BFS)
public MyList<Integer> bfs(int startVertex) {
MyList<Integer> result = new MyList<>();
if (!hasVertex(startVertex)) return result;
boolean[] visited = new boolean[vertices.size()];
MyList<Integer> queue = new MyList<>(); // Используется как очередь
int startIndex = vertices.indexOf(startVertex);
visited[startIndex] = true;
queue.add(startVertex);
while (!queue.isEmpty()) {
int current = queue.removeAt(0); // Извлекаем из начала
result.add(current);
// Добавляем всех непосещенных соседей в очередь
MyList<Integer> neighbors = getNeighbors(current);
for (int i = 0; i < neighbors.size(); i++) {
int neighbor = neighbors.get(i);
int neighborIndex = vertices.indexOf(neighbor);
if (!visited[neighborIndex]) {
visited[neighborIndex] = true;
queue.add(neighbor);
}
}
}
return result;
}
// Статистика графа
public int getVertexCount() {
return vertices.size();
}
public int getEdgeCount() {
int count = 0;
for (int i = 0; i < adjacencyList.size(); i++) {
count += adjacencyList.get(i).size();
}
return directed ? count : count / 2; // Для неориентированного делим на 2
}
public boolean isDirected() {
return directed;
}
// Строковое представление графа
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Graph (").append(directed ? "directed" : "undirected").append(")\n");
sb.append("Vertices: ").append(vertices.size()).append(", Edges: ").append(getEdgeCount()).append("\n");
// Вывод списков смежности
for (int i = 0; i < vertices.size(); i++) {
int vertex = vertices.get(i);
sb.append(vertex).append(" -> ").append(adjacencyList.get(i)).append("\n");
}
return sb.toString();
}
}