-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
129 lines (116 loc) · 4.92 KB
/
test.html
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
<html>
<head>
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
crossorigin="anonymous"></script>
<script src="kgraph.js"></script>
</head>
<body>
<p>Open developer console to see tests</p>
<script>
function Point(x, y) {
this.x = x;
this.y = y;
};
Point.prototype.toString = function () {
return '(' + this.x + ',' + this.y + ')'
}
function distance(point1, point2) {
return Math.hypot(point1.x - point2.x, point1.y - point2.y);
};
var vertices = [
new Point(0, 0),
new Point(2, 2),
new Point(2, 4),
new Point(5, 4),
new Point(6, 2),
new Point(1, 5),
new Point(4, 6),
new Point(6, 6),
new Point(8, 6),
new Point(9, 4),
new Point(7, 4),
new Point(10, 1),
new Point(11, 11)
];
console.log('Vertices:')
vertices.forEach(vertex => console.log(vertex))
var edges = [
new kgraph.Edge(0, 1, distance(vertices[0], vertices[1])),
new kgraph.Edge(1, 2, distance(vertices[1], vertices[2])),
new kgraph.Edge(2, 5, distance(vertices[2], vertices[5])),
new kgraph.Edge(2, 6, distance(vertices[2], vertices[6])),
new kgraph.Edge(2, 3, distance(vertices[2], vertices[3])),
new kgraph.Edge(1, 3, distance(vertices[1], vertices[3])),
new kgraph.Edge(6, 7, distance(vertices[6], vertices[7])),
new kgraph.Edge(3, 4, distance(vertices[3], vertices[4])),
new kgraph.Edge(3, 10, distance(vertices[3], vertices[10])),
new kgraph.Edge(4, 10, distance(vertices[4], vertices[10])),
new kgraph.Edge(10, 7, distance(vertices[10], vertices[7])),
new kgraph.Edge(7, 8, distance(vertices[7], vertices[8])),
new kgraph.Edge(10, 8, distance(vertices[10], vertices[8])),
new kgraph.Edge(3, 8, distance(vertices[3], vertices[8])),
new kgraph.Edge(10, 9, distance(vertices[10], vertices[9])),
new kgraph.Edge(8, 9, distance(vertices[8], vertices[9])),
new kgraph.Edge(9, 11, distance(vertices[9], vertices[11])),
new kgraph.Edge(4, 7, distance(vertices[4], vertices[7]))
];
console.log('Edges:')
edges.forEach(edge => console.log(edge))
// [TEST]: kgraph.GraphFactory & Graph
console.log('--------------')
console.log('GRAPHFACTORY TEST')
console.log('directed graph:')
var graph = kgraph.GraphFactory.createDirectedGraph(vertices, edges);
graph.show();
console.log('directed & weighted graph:')
graph = kgraph.GraphFactory.createDirectedWeightedGraph(vertices, edges);
graph.show();
console.log('undirected graph:')
graph = kgraph.GraphFactory.createUndirectedGraph(vertices, edges);
graph.show();
console.log('undirected & weighted graph:')
graph = kgraph.GraphFactory.createUndirectedWeightedGraph(vertices, edges);
graph.show()
console.log('--------------')
console.log('--------------')
console.log('GRAPH FUNCTIONS')
console.log('contains 1337 (false)')
console.log(graph.contains(1337))
console.log('contains 1 (true)')
console.log(graph.contains(1))
console.log('containsAll [0, 1, 9] (true)')
console.log(graph.containsAll([0, 1, 9]))
console.log('containsAll [1337, 0, 1, 9] (false)')
console.log(graph.containsAll([1337, 0, 1, 9]))
console.log('contains with list argument [0, 1, 2] (true)')
console.log(graph.contains([0, 1, 2]))
console.log('isIsolated 12 (true)')
console.log(graph.isIsolated(12))
console.log('degree 7 (4)')
console.log(graph.degree(7))
console.log('averageDegree')
console.log(graph.averageDegree())
console.log('--------------')
// [TEST]: kgraph.GraphSearcher
console.log('--------------')
console.log('GRAPH SEARCHER')
var start = 0;
var target = 10;
console.log('start: ' + start + '\n target: ' + target)
var searcher = new kgraph.GraphSearcher(graph)
console.log("DephtFirstSearch")
console.log(searcher.dfs(start, target))
console.log("A* Search")
console.log(searcher.aStarSearch(start, target, distance, distance))
console.log('BreadthFirstSearch')
console.log(searcher.bfs(start, target))
console.log('Distance')
console.log(searcher.distance(start,target))
console.log('Diameter')
console.log(searcher.diameter())
console.log('PathNeighbours')
console.log(searcher.getPathNeighbours())
console.log('--------------')
</script>
</body>
</html>