Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package graphen;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.LinkedList;
- public class Graph {
- public AdjacencyStructure graphStruct;
- public Graph createGraph(File f, String adjStruct) {
- FileReader fr;
- String s = null;
- // Read file into String
- try {
- fr = new FileReader(f);
- fr.read();
- s = fr.toString();
- fr.close();
- } catch (FileNotFoundException e) {
- return null;
- } catch (IOException e) {
- e.printStackTrace();
- }
- // return null for empty file
- if (s == null) {
- return null;
- }
- s.toString();
- // V={1,2,3}
- // E={[1,2,10],[2,1,10],[2,3,30],[3,1,50]}
- System.out.println(s);
- //TODO WHY THE BUGSSSS??!!!
- String[] edgesAndVertices = new String[3];
- edgesAndVertices = s.split("\n");
- edgesAndVertices[0].replace("V=", "");
- System.out.println(edgesAndVertices[0]);
- edgesAndVertices[1].replace("E=", "");
- System.out.println(edgesAndVertices[1]);
- LinkedList<Edge> edges = parseEdges(edgesAndVertices[0]);
- LinkedList<Vertice> vertices = parseVertices(edgesAndVertices[1]);
- // fill in => AdjacencyMatrix
- AdjacencyMatrix matrix = new AdjacencyMatrix(vertices, edges);
- AdjacencyList list = new AdjacencyList(edges, vertices);
- Graph resultGraph = new Graph();
- if (adjStruct == "matrix") {
- resultGraph.setGraphStruct(matrix);
- return resultGraph;
- }
- if (adjStruct == "list") {
- resultGraph.setGraphStruct(list);
- }
- return resultGraph;
- }
- // parsing
- public LinkedList<Edge> parseEdges(String eString) {
- LinkedList<Edge> res = new LinkedList<Edge>();
- boolean newEdge = false;
- eString.replace("{", "").replace("}", "");
- char[] eChar = eString.toCharArray();
- for (int i = 0; i < eChar.length; i++) {
- if (eChar[i] == '[') {
- newEdge = true;
- i++;
- }
- if (eChar[i] == ']') {
- newEdge = false;
- i++;
- }
- if (newEdge) {
- Edge e = new Edge(Integer.parseInt(Character
- .toString(eChar[i + 1])), Integer.parseInt(Character
- .toString(eChar[i + 3])), Integer.parseInt(Character
- .toString(eChar[i + 5])));
- res.add(e);
- newEdge = false;
- }
- }
- return res;
- }
- // parsing..
- public LinkedList<Vertice> parseVertices(String vString) {
- LinkedList<Vertice> res = new LinkedList<Vertice>();
- vString.replace("{", "").replace("}", "");
- String[] list = vString.split(",");
- for (String s : list) {
- Vertice v = new Vertice(Integer.parseInt(s));
- res.add(v);
- }
- return res;
- }
- public AdjacencyStructure getGraphStruct() {
- return graphStruct;
- }
- public void setGraphStruct(AdjacencyStructure graphStruct) {
- this.graphStruct = graphStruct;
- }
- public void print() {
- graphStruct.print();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement