Skip to content

Instantly share code, notes, and snippets.

@a4027971
Last active August 22, 2017 10:30
Show Gist options
  • Save a4027971/a210e9a7b5a13307b92fd11a26fc91d4 to your computer and use it in GitHub Desktop.
Save a4027971/a210e9a7b5a13307b92fd11a26fc91d4 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>
</head>
<body>
</body>
<script>
// TODO :
// 1. 道路反向怎麼辦
// 2. 交集道路
var graph = {
'vertices': [
'v0',
'v1',
'v2',
'v3',
'v4',
'v5',
],
};
var vertices = {
'v0': {'name': '一'},
'v1': {'name': '二'},
'v2': {'name': '三'},
'v3': {'name': '四'},
'v4': {'name': '五'},
'v5': {'name': '六'},
};
var edges = {
'e0': {'u': 'v0','v': 'v1', 'time': 0, 'distance': 0},
'e1': {'u': 'v1','v': 'v2', 'time': 1, 'distance': 10},
'e2': {'u': 'v2','v': 'v3', 'time': 2, 'distance': 20},
'e3': {'u': 'v3','v': 'v4', 'time': 3, 'distance': 30},
'e4': {'u': 'v4','v': 'v5', 'time': 4, 'distance': 40},
};
var result = {
'time': 0,
'distance': 0,
'money': 0,
'path': [],
};
var pricePerKm = 1.2;
function cost(start, end)
{
var node = start;
while (node !== end)
{
result['path'].push(node);
node = next(node);
}
}
function next(node)
{
var next;
$.each(edges, function(key, edge) {
if (edge['u'] === node) {
next = edge['v'];
result['time'] += edge['time'];
result['distance'] += edge['distance'];
return false;
}
});
return next;
}
var start = new Date().getTime();
for (var n = 0; n < 10000; n ++) {
cost('v0', 'v5');
}
var elapsed = new Date().getTime() - start;
result['money'] = result['distance'] * pricePerKm;
console.log(result);
console.log("Mesured Time: " + elapsed/1000 + " s");
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment