Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alexey-N-Chernyshov/bcb8b6e163ccf4cbec00 to your computer and use it in GitHub Desktop.
Save Alexey-N-Chernyshov/bcb8b6e163ccf4cbec00 to your computer and use it in GitHub Desktop.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package adjacencylistgraph;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Scanner;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
/**
*
* @author Yex
*/
public class AdjacencyListGraphApplication {
static class AdjacencyListGraph<Vertex, Edge> {
class EdgePair {
Vertex to;
Edge weight;
EdgePair(Vertex t, Edge w) {
to = t;
weight = w;
}
@Override
public int hashCode() {
return to.hashCode() + weight.hashCode();
}
@Override
public boolean equals(Object other) {
if (other == null) return false;
if (other == this) return true;
EdgePair otherEdge = (EdgePair)other;
return to == otherEdge.to && weight == otherEdge.weight;
}
}
private HashMap<Vertex, HashSet<EdgePair>> adjacencies = new HashMap<>();
void addVertex(Vertex v) {
if (adjacencies.get(v) != null)
throw new IllegalArgumentException("Vertex " + v.toString() + " already exists.");
else
adjacencies.put(v, new HashSet<EdgePair>());
}
void removeVertex(Vertex v) {
adjacencies.remove(v);
for (Map.Entry<Vertex, HashSet<EdgePair>> entry : adjacencies.entrySet()) {
entry.getValue().removeIf(p -> p.to.equals(v));
}
}
void addEdge(Vertex from, Vertex to, Edge e) {
// HashSet<EdgePair> from_adj = adjacencies.get(from);
HashSet<EdgePair> from_adj = from.equals("Белгород") ? adjacencies.get("Белгород") : adjacencies.get(from);
if (from_adj == null) {
throw new IllegalArgumentException("Vertex " + from.toString() + " does not exist.");
// } if (adjacencies.get(to) == null) {
} if ((to.equals("Белгород") ? adjacencies.get("Белгород") : adjacencies.get(to)) == null) {
throw new IllegalArgumentException("Vertex " + to.toString() + " does not exist.");
} else {
from_adj.add(new EdgePair(to, e));
}
}
void removeEdge(Vertex from, Vertex to, Edge e) {
HashSet<EdgePair> from_adj = adjacencies.get(from);
if (from_adj == null) {
throw new IllegalArgumentException("Vertex " + from.toString() + " does not exist.");
} if (adjacencies.get(to) == null) {
throw new IllegalArgumentException("Vertex " + to.toString() + " does not exist.");
} else {
from_adj.remove(new EdgePair(to, e));
}
}
void getAdjacentVertices(Vertex v) {
}
@Override
public boolean equals(Object other) {
if (other == null) return false;
if (other == this) return true;
AdjacencyListGraph<Vertex, HashSet<EdgePair>> otherAdjacencyListGraph =
(AdjacencyListGraph<Vertex, HashSet<EdgePair>>)other;
return adjacencies.equals(otherAdjacencyListGraph.adjacencies);
}
public String serialize() {
StringBuilder res = new StringBuilder();
for (Map.Entry<Vertex, HashSet<EdgePair>> entry : adjacencies.entrySet())
res.append(entry.getKey().toString()).append(" ");
res.append("\n");
for (Map.Entry<Vertex, HashSet<EdgePair>> entry : adjacencies.entrySet()) {
Vertex from = entry.getKey();
for (EdgePair edge : entry.getValue()) {
res.append(from).append(" ");
res.append(edge.to.toString()).append(" ");
res.append(edge.weight.toString()).append(" ");
}
}
return res.toString();
}
void deserialize(String str) {
String vertexStr = str.split("\n")[0];
String edgeStr = str.split("\n")[1];
adjacencies.clear();
for (String from : vertexStr.trim().split(" ")) {
addVertex((Vertex)from);
}
String[] edges = edgeStr.trim().split(" ");
for (int i = 0; i < edges.length; i += 3) {
addEdge((Vertex)edges[i], (Vertex)edges[i + 1], (Edge)edges[i + 2]);
}
}
}
public static String loadFile() {
Scanner in = null;
String str = null;
try {
in = new Scanner(new File("distances.txt"));
in.useLocale(Locale.US);
str = in.nextLine().trim();
str += "\n";
str += in.nextLine().trim();
} catch (FileNotFoundException e) {
System.out.println(e.toString());
} finally {
if (in != null) {
in.close();
}
}
return str;
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
AdjacencyListGraph<String, Integer> graph = new AdjacencyListGraph<>();
String str = loadFile();
System.out.println(str);
graph.deserialize(str);
// System.out.println(graph.serialize());
// graph.addVertex("A");
// graph.addVertex("B");
// graph.addVertex("C");
// graph.addVertex("D");
// graph.addVertex("E");
// graph.addEdge("A", "B", 1);
// graph.addEdge("A", "C", 2);
// graph.addEdge("A", "D", 3);
//
// AdjacencyListGraph<String, Integer> graph2 = new AdjacencyListGraph<>();
// graph2.addVertex("A");
// graph2.addVertex("D");
// graph2.addVertex("E");
// graph2.addVertex("B");
// graph2.addVertex("C");
// graph2.addEdge("A", "C", 2);
// graph2.addEdge("A", "B", 1);
// graph2.addEdge("A", "D", 3);
//
// System.out.println(graph.equals(graph2));
//
// System.out.println(graph.serialize());
// graph2.removeVertex("B");
// System.out.println(graph2.serialize());
//
// System.out.println("deserializable:");
// graph2.deserialize(graph.serialize());
// System.out.println(graph2.serialize());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment