import java.util.Random;
import java.util.HashMap;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import java.io.*;
import java.util.*;
class Utils {
/*
* Read file of graph for Dijkstra algorithm, see example in data/DijkstraData.txt
*/
public static HashMap> readDijkstraGraph(String fpath) {
HashMap> graph =
new HashMap>();
try {
BufferedReader br = new BufferedReader(new FileReader(fpath));
String line = null;
while ((line = br.readLine()) != null) {
String[] arr = line.split("\t");
HashMap adj = new HashMap();
for (int i = 1; i < arr.length; i++) {
String[] tmp = arr[i].split(",");
adj.put(Integer.parseInt(tmp[0]), Integer.parseInt(tmp[1]));
}
graph.put(Integer.parseInt(arr[0]), adj);
}
} catch (IOException ex) {
ex.printStackTrace();
}
return graph;
}
/*
* Read a directed graph from fpath, each row represents a arch using two
* columns. The first is the source vertext and the second end vertex.
* Return: Adjencency list of a hash map
*/
public static HashMap> readDirectedGraph(String fpath) {
HashMap> adjList =
new HashMap>();
try {
BufferedReader br = new BufferedReader(new FileReader(fpath));
String line = null;
while ((line = br.readLine()) != null) {
String[] arr = line.split(" ");
int source = Integer.parseInt(arr[0]);
int dest = Integer.parseInt(arr[1]);
if (adjList.containsKey(source)) {
adjList.get(source).put(dest, 1);
} else {
HashMap tmp = new HashMap();
tmp.put(dest, 1);
adjList.put(source, tmp);
}
if (!adjList.containsKey(dest)) {
HashMap tmp = new HashMap();
adjList.put(dest, tmp);
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
return adjList;
}
public static Map sortByValue(Map map) {
List> entries = new LinkedList>(map.entrySet());
Collections.sort(entries, new Comparator>() {
public int compare(Entry o1, Entry o2) {
return -o1.getValue().compareTo(o2.getValue());
}
});
Map sortedMap = new LinkedHashMap();
for (Map.Entry entry: entries) {
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}
/*
* Generate a random integer array with the size defined by length
* Return: an integer array
*/
public static int[] generateArray(int length) {
Random rand = new Random();
int[] iarr = new int[length];
int i;
for(i=0; i numList = new ArrayList();
try {
BufferedReader input = new BufferedReader(new FileReader(filePath));
String line = null;
while( (line=input.readLine()) != null ) {
numList.add(new Integer(line));
}
} catch (IOException ex) {
ex.printStackTrace();
}
int[] numArr = new int[numList.size()];
Iterator iterator = numList.iterator();
for(int i=0; i