Skip to content

Commit dbaf3e3

Browse files
committed
[Function add]
1.Add the conclusion about the symbol graph and its implementation.
1 parent 7a209a8 commit dbaf3e3

File tree

6 files changed

+4499
-1
lines changed

6 files changed

+4499
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ca.mcmaster.chapter.four.graph;
2+
3+
public interface SymbolGraph {
4+
/**
5+
* @Description: Is key a vertex.
6+
* @param key
7+
* @return
8+
*/
9+
public boolean contains(String key);
10+
/**
11+
* @Description: Index of key.
12+
* @param key
13+
* @return
14+
*/
15+
public int index(String key);
16+
/**
17+
* @Description: name of v in symbol table
18+
* @param v
19+
* @return
20+
*/
21+
public String name(int v);
22+
23+
/**
24+
* @Description: Get the anonymous graph object.
25+
* @return
26+
*/
27+
public Graph G();
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package ca.mcmaster.chapter.four.graph;
2+
3+
import java.io.File;
4+
import java.io.FileNotFoundException;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import java.util.Scanner;
8+
9+
public class SymbolGraphImpl implements SymbolGraph {
10+
private final Map<String, Integer> st;
11+
private String[] keys;
12+
private final Graph G;
13+
public SymbolGraphImpl(String file, String sp) throws FileNotFoundException {
14+
st = new HashMap<>();
15+
//fulfill the symbol table
16+
Scanner scanner = null;
17+
try {
18+
scanner = new Scanner(new File(file));
19+
int count = 0;
20+
while(scanner.hasNextLine()){
21+
String[] tokens = scanner.nextLine().split(sp);
22+
for(String t : tokens){
23+
if(!st.containsKey(t)){ //If not contains token then put it into the st.
24+
st.put(t, count++);
25+
}
26+
}
27+
}
28+
}finally{
29+
scanner.close();
30+
}
31+
keys = new String[st.size()];
32+
for(String name : st.keySet()){
33+
keys[st.get(name)] = name;
34+
}
35+
// Create graph
36+
G = new UndirectedGraph(st.size());
37+
//Add edges
38+
Scanner scanner2 = null;
39+
try{
40+
scanner2 = new Scanner(new File(file));
41+
while(scanner2.hasNextLine()){
42+
String[] tokens = scanner2.nextLine().split(sp);
43+
for(int i = 1; i < tokens.length; i++){
44+
int v = st.get(tokens[0]);
45+
G.addEdge(v, st.get(tokens[i]));
46+
}
47+
}
48+
}finally{
49+
scanner2.close();
50+
}
51+
52+
}
53+
@Override
54+
public boolean contains(String key) { return st.containsKey(key); }
55+
56+
@Override
57+
public int index(String key) { return st.get(key); }
58+
59+
@Override
60+
public String name(int v) { return keys[v]; }
61+
62+
@Override
63+
public Graph G() { return G; }
64+
65+
public static void main(String[] args) throws FileNotFoundException {
66+
SymbolGraphImpl symbolGraphImpl = new SymbolGraphImpl("src/ca/mcmaster/chapter/four/graph/movies.txt", "/");
67+
Graph graph = symbolGraphImpl.G();
68+
int vertexNum = graph.V();
69+
for(int v = 0; v < vertexNum; v++){
70+
StringBuilder sb = new StringBuilder(symbolGraphImpl.name(v) + " -> ");
71+
for(int w : graph.adj(v))
72+
sb.append(symbolGraphImpl.name(w) + " ");
73+
System.out.println(sb.toString());
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)