-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymbolGraph.java
66 lines (53 loc) · 1.97 KB
/
SymbolGraph.java
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
package com.program.graph.algo;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class SymbolGraph {
Graph1 g;
public SymbolGraph(String fileName, String delim) throws FileNotFoundException {
g = new Graph1(Graph1.TYPE.UNDIRECTED);
/*FileInputStream fis = new FileInputStream(fileName);
InputStreamReader in = new InputStreamReader(fis);*/
Scanner in = new Scanner(new File(fileName));
//BufferedReader reader = new BufferedReader(new FileReader(fileName));
List<Graph1.Vertex> vertices = new ArrayList<Graph1.Vertex>();
List<Graph1.Edge> edges = new ArrayList<Graph1.Edge>();
int index;
while (in.hasNextLine()) {
String [] input = in.nextLine().split(delim);
Graph1.Vertex fromVertex, toVertex;
fromVertex = new Graph1.Vertex(input[0]);
vertices.add(fromVertex);
for (int i=1; i<input.length; i++) {
toVertex = new Graph1.Vertex(input[i]);
if (vertices.contains(toVertex)){
index = vertices.indexOf(toVertex);
toVertex = vertices.get(index);
} else {
vertices.add(toVertex);
}
Graph1.Edge e = new Graph1.Edge(0,fromVertex, toVertex);
fromVertex.getEdges().add(e);
edges.add(e);
if (g.getType() == Graph1.TYPE.UNDIRECTED) {
Graph1.Edge reciprocal = new Graph1.Edge(0, toVertex, fromVertex);
toVertex.getEdges().add(reciprocal);
edges.add(reciprocal);
}
}
}
g.setEdges(edges);
g.setVertices(vertices);
}
public Graph1 getGraph(){
return this.g;
}
public static void main(String[] args) throws FileNotFoundException {
SymbolGraph sg = new SymbolGraph("movies.txt", "/");
BFS bfs = new BFS(sg.getGraph().getVertices().size());
bfs.bfs(sg.getGraph(), sg.getGraph().getVertices().get(0));
bfs.printPath(sg.getGraph(), new Graph1.Vertex("Tin Men (1987)"), new Graph1.Vertex("Hershey, Barbara..."));
}
}