|
2 | 2 |
|
3 | 3 | import java.util.*; |
4 | 4 |
|
5 | | -class Edge { |
6 | | - /** |
7 | | - * way from -> to == way to -> to |
8 | | - */ |
9 | | - public char from; |
10 | | - public char to; |
11 | | - public int distance; |
12 | | - |
13 | | - public Edge(char from, char to, int distance) { |
14 | | - this.from = from; |
15 | | - this.to = to; |
16 | | - this.distance = distance; |
17 | | - |
18 | | - } |
19 | | -} |
20 | | - |
21 | 5 | public class Graph { |
22 | | - private static final int SIZE = 26; |
23 | | - |
24 | | - private int countVertexes; |
25 | | - |
26 | | - private final int[][] inputGraph; |
27 | | - private final int[][] outputGraph; |
28 | | - |
29 | | - |
30 | | - private final List<Edge> inputEdges; |
31 | | - private final List<Edge> outputEdges; |
32 | | - |
33 | | - private final Set<Character> inputVertices; |
34 | | - private final List<Set<Character>> outputVertices; |
35 | | - |
36 | | - |
37 | | - public Graph() { |
38 | | - inputGraph = new int[SIZE][SIZE]; |
39 | | - outputGraph = new int[SIZE][SIZE]; |
40 | | - inputEdges = new LinkedList<>(); |
41 | | - outputEdges = new LinkedList<>(); |
42 | | - inputVertices = new HashSet<>(); |
43 | | - outputVertices = new LinkedList<>(); |
44 | | - } |
45 | | - |
46 | | - public void readGraph() { |
47 | | - // TODO adapter |
48 | | - System.out.println("Ready to enter"); |
49 | | - try (Scanner scanner = new Scanner(System.in)) { |
50 | | -// scanner.useDelimiter("[\n ]"); |
51 | | - int n = Integer.parseInt(scanner.nextLine()); |
52 | | -// System.out.println(n); |
53 | | - for (int i = 0; i < n; i++) { |
54 | | - String[] parts = scanner.nextLine().split(" "); |
55 | | - char from = parts[0].toLowerCase().toCharArray()[0]; |
56 | | - char to = parts[1].toLowerCase().toCharArray()[0]; |
57 | | - int distance = Integer.parseInt(parts[2]); |
58 | | - |
59 | | - assert (from >= 'a' && from <= 'z'); |
60 | | - assert (to >= 'a' && to <= 'z'); |
61 | | - assert (distance > 0); |
62 | | - |
63 | | - while (inputGraph[from - 'a'][to - 'a'] != 0) { |
64 | | - parts = scanner.nextLine().split(" "); |
65 | | - from = parts[0].toLowerCase().toCharArray()[0]; |
66 | | - to = parts[1].toLowerCase().toCharArray()[0]; |
67 | | - distance = Integer.parseInt(parts[2]); |
68 | | - } |
| 6 | +// private static final int SIZE = 26; |
69 | 7 |
|
70 | | - Edge edge = new Edge(from, to, distance); |
71 | | - inputEdges.add(edge); |
| 8 | + private List<Edge> inputEdges; |
| 9 | + private List<Node> inputVertices; |
| 10 | + public boolean isModified = false; |
72 | 11 |
|
73 | | - Character vertex1 = from; |
74 | | - Character vertex2 = to; |
75 | | - inputVertices.add(vertex1); |
76 | | - inputVertices.add(vertex2); |
| 12 | +// public |
77 | 13 |
|
78 | | -// System.out.println(from + " " + to + " " + distance); |
79 | | - inputGraph[from - 'a'][to - 'a'] = distance; |
80 | | - inputGraph[to - 'a'][from - 'a'] = distance; |
81 | | - } |
82 | | - } |
83 | | - countVertexes = inputVertices.size(); |
| 14 | + public List<Edge> getInputEdges() { |
| 15 | + return inputEdges; |
84 | 16 | } |
85 | 17 |
|
86 | | - public void printGraph() { |
87 | | - for (int i = 0; i < SIZE; i++) { |
88 | | - for (int j = 0; j < SIZE; j++) { |
89 | | - System.out.print(inputGraph[i][j] + " "); |
90 | | - } |
91 | | - System.out.println(); |
92 | | - } |
| 18 | + public List<Node> getInputVertices() { |
| 19 | + return inputVertices; |
93 | 20 | } |
94 | 21 |
|
95 | | - private void printEdges(List<Edge> edges) { |
96 | | - System.out.println("From\tTo\t\tDistance"); |
97 | | -// System.out.println("F\tT\tD"); |
98 | | - for (Edge edge : edges) { |
99 | | - System.out.println( |
100 | | - edge.from + "\t\t" + |
101 | | - edge.to + "\t\t" + |
102 | | - edge.distance |
103 | | - ); |
104 | | - } |
105 | | - } |
106 | | - |
107 | | - public void sortEdges() { |
108 | | - inputEdges.sort(new Comparator<Edge>() { |
109 | | - @Override |
110 | | - public int compare(Edge edge1, Edge edge2) { |
111 | | - int distance1 = edge1.distance; |
112 | | - int distance2 = edge2.distance; |
113 | | - return Integer.compare(distance1, distance2); |
114 | | - } |
115 | | - }); |
116 | | - System.err.println(State.SORT); |
117 | | - } |
118 | | - |
119 | | - private State nextStep(Edge edge) { |
120 | | - State state = null; |
121 | | - Set<Character> tempVertexes = new HashSet<>(); |
122 | | - Character vertex1 = edge.from; |
123 | | - Character vertex2 = edge.to; |
124 | | - tempVertexes.add(vertex1); |
125 | | - tempVertexes.add(vertex2); |
126 | | - |
127 | | - boolean hasFound = false; |
128 | | - for (Set<Character> currently : outputVertices) { |
129 | | - if (isOld(currently, tempVertexes)) { |
130 | | - return State.LOOP; |
131 | | - } |
132 | | - if (isCommon(currently, tempVertexes)) { |
133 | | - hasFound = true; |
134 | | - currently.addAll(tempVertexes); |
135 | | - state = State.APPEND; |
136 | | - } |
137 | | - } |
138 | | - |
139 | | - for (int i = 0; i < outputVertices.size(); i++) { |
140 | | - for (int j = i + 1; j < outputVertices.size(); j++) { |
141 | | - Set<Character> first = outputVertices.get(i); |
142 | | - Set<Character> second = outputVertices.get(j); |
143 | | - if (isCommon(first, second)) { |
144 | | - state = State.CONNECT_COMPONENTS; |
145 | | - first.addAll(second); |
146 | | - second.clear(); |
147 | | - } |
148 | | - } |
149 | | - } |
150 | | - |
151 | | - outputEdges.add(edge); |
152 | | - if (!hasFound) { |
153 | | - state = State.NEW_COMPONENT; |
154 | | - outputVertices.add(tempVertexes); |
155 | | - } |
156 | | - if (outputVertices.get(0).size() == countVertexes) { |
157 | | - state = State.END; |
158 | | - } |
159 | | -// assert state != null; |
160 | | - return state; |
| 22 | + public Graph() { |
| 23 | + this.inputEdges = new LinkedList<>(); |
| 24 | + this.inputVertices = new LinkedList<>(); |
161 | 25 | } |
162 | 26 |
|
163 | | - public void kraskal() { |
164 | | - for (Edge edge : inputEdges) { |
165 | | - State state = nextStep(edge); |
166 | | - System.err.println(state); |
167 | | - if (state == State.END) { |
168 | | - break; |
169 | | - } |
170 | | - } |
171 | | - } |
| 27 | +// public void initGraph(List<Edge> inputEdges, List<Node> inputVertices) { |
| 28 | +// this.inputEdges = new LinkedList<>(inputEdges); |
| 29 | +// this.inputVertices = new LinkedList<>(inputVertices); |
| 30 | +// } |
172 | 31 |
|
173 | | - private boolean isCommon(Set<Character> first, Set<Character> second) { |
174 | | - Set<Character> all = new HashSet<>(); |
175 | | - all.addAll(first); |
176 | | - all.addAll(second); |
177 | | - return all.size() != (first.size() + second.size()); |
| 32 | + public void addEdge(Node from, Node to, int distance) { |
| 33 | + Edge newEdge = new Edge(from, to, distance); |
| 34 | + for (Edge edge : inputEdges) |
| 35 | + if (edge.equalsWay(newEdge)) |
| 36 | + return; |
| 37 | + inputEdges.add(newEdge); |
| 38 | + isModified = true; |
178 | 39 | } |
179 | 40 |
|
180 | | - private boolean isOld(Set<Character> first, Set<Character> second) { |
181 | | - Set<Character> all = new HashSet<>(); |
182 | | - all.addAll(first); |
183 | | - all.addAll(second); |
184 | | - return all.size() == first.size(); |
| 41 | + public void addVertex(Node node) { |
| 42 | + if (!inputVertices.contains(node)) |
| 43 | + inputVertices.add(node); |
| 44 | + isModified = true; |
185 | 45 | } |
186 | 46 |
|
187 | | - public void printResult() { |
188 | | - printEdges(outputEdges); |
189 | | - for (Set<Character> vertices : outputVertices) { |
190 | | - for (Character vertex : vertices) { |
191 | | - System.out.print(vertex + " "); |
192 | | - } |
193 | | - System.out.println(); |
194 | | - } |
| 47 | + public void addVertex() { |
| 48 | + int len = inputVertices.size(); |
| 49 | + inputVertices.add(new Node((char)('a' + len))); |
195 | 50 | } |
196 | 51 |
|
197 | | - public void printInputEdges() { |
198 | | - printEdges(inputEdges); |
| 52 | + public void clear() { |
| 53 | + inputVertices.clear(); |
| 54 | + inputEdges.clear(); |
| 55 | + isModified = false; |
199 | 56 | } |
200 | 57 | } |
0 commit comments