-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.java
More file actions
163 lines (143 loc) · 5.26 KB
/
Utils.java
File metadata and controls
163 lines (143 loc) · 5.26 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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<Integer, HashMap<Integer, Integer>> readDijkstraGraph(String fpath) {
HashMap<Integer, HashMap<Integer, Integer>> graph =
new HashMap<Integer, HashMap<Integer, Integer>>();
try {
BufferedReader br = new BufferedReader(new FileReader(fpath));
String line = null;
while ((line = br.readLine()) != null) {
String[] arr = line.split("\t");
HashMap<Integer, Integer> adj = new HashMap<Integer, Integer>();
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<Integer, HashMap<Integer, Integer>> readDirectedGraph(String fpath) {
HashMap<Integer, HashMap<Integer, Integer>> adjList =
new HashMap<Integer, HashMap<Integer, Integer>>();
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<Integer, Integer> tmp = new HashMap<Integer, Integer>();
tmp.put(dest, 1);
adjList.put(source, tmp);
}
if (!adjList.containsKey(dest)) {
HashMap<Integer, Integer> tmp = new HashMap<Integer, Integer>();
adjList.put(dest, tmp);
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
return adjList;
}
public static <K extends Comparable, V extends Comparable> Map<K, V> sortByValue(Map<K, V> map) {
List<Map.Entry<K, V>> entries = new LinkedList<Map.Entry<K, V>>(map.entrySet());
Collections.sort(entries, new Comparator<Map.Entry<K, V>>() {
public int compare(Entry<K, V> o1, Entry<K, V> o2) {
return -o1.getValue().compareTo(o2.getValue());
}
});
Map<K, V> sortedMap = new LinkedHashMap<K, V>();
for (Map.Entry<K, V> 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<length; i++) {
int randNum = rand.nextInt();
iarr[i] = randNum;
}
return iarr;
}
/*
* Generate a random integer array with the size defined by length
* Return: an integer array that every element is less than limit
*/
public static int[] generateArray(int length, int limit) {
Random rand = new Random();
int[] iarr = new int[length];
int i;
for(i=0; i<length; i++) {
int randNum = rand.nextInt(limit);
iarr[i] = randNum;
}
return iarr;
}
/*
* Output an integer array
*/
public static void printArray(int[] arr) {
for(int i=0; i<arr.length; ++i) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
/*
* Read a list of integers from filePath, each integer is in one line
* Return an integer array
*/
public static int[] readIntArrayFromFile(String filePath) {
ArrayList<Integer> numList = new ArrayList<Integer>();
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<Integer> iterator = numList.iterator();
for(int i=0; i<numList.size(); i++) {
numArr[i] = iterator.next().intValue();
}
return numArr;
}
public static void main(String[] args) {
int[] arr = generateArray(100);
printArray(arr);
}
}