Skip to content
This repository has been archived by the owner on Aug 28, 2019. It is now read-only.

Commit

Permalink
include redirect processing in place
Browse files Browse the repository at this point in the history
  • Loading branch information
matpalm committed Aug 8, 2011
1 parent cfb416e commit 916558a
Showing 1 changed file with 20 additions and 25 deletions.
45 changes: 20 additions & 25 deletions distanceToPhilosophy/src/DistanceToPhilosophy.java
Expand Up @@ -12,19 +12,23 @@

public class DistanceToPhilosophy {

private final Map<String,Integer> tokenToIdx = new HashMap<String,Integer>();
private final Map<Integer,String> idxToToken = new HashMap<Integer,String>();
private int tokenIdx = 0;
private final String startNode;
private TokenDictionary tokenDictionary = new TokenDictionary();

private final String startNode, edgesFile, redirectFile;

private Map<Integer,List<Integer>> edges = new HashMap<Integer,List<Integer>>();

public static void main(String s[]) throws IOException {
new DistanceToPhilosophy(s[0]).go();
public static void main(String args[]) throws IOException {
if (args.length != 3) {
throw new RuntimeException("DistanceToPhilosophy <startNode> <edgesFile> <redirectFile>");
}
new DistanceToPhilosophy(args).go();
}

public DistanceToPhilosophy(String startNode) {
this.startNode = startNode;
public DistanceToPhilosophy(String[] args) {
this.startNode = args[0];
this.edgesFile = args[1];
this.redirectFile = args[2];
}

public void go() throws IOException {
Expand All @@ -41,13 +45,14 @@ private void parseInputFile() throws FileNotFoundException, IOException {

String[] cols = next.split("\t");
// note: we want the reverse graph
int toNode = tokenForIdx(cols[0]);
int fromNode = tokenForIdx(cols[1]);
int toNode = tokenDictionary.tokenForIdx(cols[0]);
int fromNode = tokenDictionary.tokenForIdx(cols[1]);

if (edges.containsKey(fromNode)) {
List<Integer> outboundEdges = edges.get(fromNode);
if (outboundEdges.contains(toNode)) {
System.err.println("already and edge from "+fromNode+" ("+idxToToken.get(fromNode)+") to "+toNode+" ("+idxToToken.get(toNode)+")");
System.err.println("already and edge from "+fromNode+
" ("+tokenDictionary.idxToToken.get(fromNode)+") to "+toNode+" ("+tokenDictionary.idxToToken.get(toNode)+")");
}
else {
outboundEdges.add(toNode);
Expand All @@ -65,19 +70,19 @@ private void parseInputFile() throws FileNotFoundException, IOException {

}

if (!tokenToIdx.containsKey(startNode)) {
if (!tokenDictionary.tokenToIdx.containsKey(startNode)) {
throw new RuntimeException("after parsing stdin never saw starting node ["+startNode+"]");
}
}

private void breadthFirstWalk() {
Queue<Integer> boundary = new LinkedList<Integer>();
boundary.add(tokenToIdx.get(startNode));
boundary.add(tokenDictionary.tokenToIdx.get(startNode));
int distance = 0;
while(boundary.size() != 0) {
Queue<Integer> frontier = new LinkedList<Integer>();
for(int edge : boundary) {
System.out.println(idxToToken.get(edge) + "\t" + distance);
System.out.println(tokenDictionary.idxToToken.get(edge) + "\t" + distance);
if (edges.containsKey(edge)) {
frontier.addAll(edges.get(edge));
}
Expand All @@ -88,15 +93,5 @@ private void breadthFirstWalk() {
}
}

public int tokenForIdx(String token) {
// build token -> idx
if (tokenToIdx.containsKey(token)) {
return tokenToIdx.get(token);
}
else {
tokenToIdx.put(token, tokenIdx);
idxToToken.put(tokenIdx, token);
return tokenIdx++;
}
}
}

0 comments on commit 916558a

Please sign in to comment.