我有一个包含源 - 目标节点和阈值的大文本文件(20 GB)文件。如果阈值> 0连接,否则没有连接。我想在数组或数组列表中添加连接的节点(哪一个适合大量的数据?)并找到巨大的连接组件。我认为BFS算法是最短路径的解决方案
Txt文件
100 101 -0.3434
100 102 1.0023
100 103 1.100
103 104 0.210
...
我的代码:
String line = null;
HashMap<Integer,ArrayList<Node>> arr = new HashMap<Integer,ArrayList<Node>>();
BufferedReader reader = new BufferedReader(new FileReader("C:/Users/UserPC/Desktop/output.txt"));
while((line = reader.readLine()) != null){
String[] spl = line.split("\\s+");
//System.out.println(spl[0]+","+spl[1]);
int source = Integer.parseInt(spl[0]);
int target = Integer.parseInt(spl[1]);
arr.computeIfAbsent( source, k -> new ArrayList<>()).add(new Node(target));
}
reader.close();
答案 0 :(得分:1)
如果你没有正确实现它们,20 GB会花费太多时间,因为你可以考虑使用图形数据结构,然后通过调整权重来应用最小spanningtree算法,这样它只能找到权重小于或大于某个权重的节点值。
步骤: 步骤1: 制作包含源节点,目的地和重量/阈值的一维行李袋。
int firstNode = readIn via scanner.
int secondNode = readIn.
Int thresHold = read;
Connected connected = new Conncted(firstNode,secondNode,threshold);
add all these connected component into array of bag so you have a graph then use minimumspanning tree or anyother algorithm, there are many.
创建一个类,表示您的组件已连接,其阈值是什么:呼叫已连接。
const spawnTime = 10000;
var coin = 0;
var intervalId = '';
var coinDiv = $('#coinDiv');
var coinImg = $('#coinImg');
var invDiv = $('#invDiv');
var invId = $('#inventoryId');
var invImg = $('#invLocation');
coinImg.on('click', collect);
intervalId = setInterval(setLocation, spawnTime);
function setLocation() {
var x = parseInt( Math.random()*(80-20+1) ) + 20;
var y = parseInt( Math.random()*(80-20+1) ) + 20;
coinImg.animate({
opacity: 1
}, 3000,
function() {
coinImg.css('top', x+'%');
coinImg.css('left', y+'%');
coinImg.css('display', 'initial');
setTimeout( () => coinImg.animate({ opacity: 0 }, 3000), 6000);
});
}
function collect() {
clearInterval(intervalId);
coinImg.stop();
coinImg.css('opacity', 1);
/* Increment coin counter */
coin++;
invId.text(coin);
/* In order to disable multiple clicks */
coinImg.css('pointer-events', 'none');
/* Double the size */
coinImg.css('width', '128px');
coinImg.css('height', '128px');
/* Animate and return to normal */
coinImg.animate({
width: '32px',
height: '32px',
left: invImg.offset().left + 'px',
top: invImg.offset().top + 'px'
}, 1500,
function() {
coinImg.css('pointer-events', 'auto');
coinImg.css('display', 'none');
coinImg.css('width', '64px');
coinImg.css('height', '64px');
intervalId = setInterval(setLocation, spawnTime);
}
);
}