-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
154 lines (137 loc) · 4.4 KB
/
main.js
File metadata and controls
154 lines (137 loc) · 4.4 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
//Arrays of cities
var cities=[]
var citiesGraph=math.zeros(0,0)
var list
var visited
var destinations
//Functions
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
function resizeGraph(){
let newGraph = math.zeros(cities.length, cities.length)
citiesGraph.forEach(function(value, index, matrix){
newGraph.set(index, citiesGraph.get(index))
});
return newGraph;
}
function logGraph(){
console.log("///////start///////")
for(let k=0;k<citiesGraph._data.length;k++){
console.log(citiesGraph._data[k])
}
console.log("////////end////////")
}
function findWays(start){
if(start==-1){
return
}
destinations=[]
//Converting matrix to adjacency list
list=Array(citiesGraph._size[0])
list.fill(['a'])
for(let i=0;i<citiesGraph._size[0];i++){
list[i]=[]
for(let k=0;k<citiesGraph._size[1];k++){
if(citiesGraph.get([i,k])==1){
list[i].push(k)
}
}
}
console.log(list)
visited=Array(citiesGraph._size[0])
visited.fill(false)
dfs(start-1)
if(destinations.length>0){
showResults(true)
}
else{
showResults(false)
}
}
function dfs(start){
visited[start]=true
for(let h=0;h<list[start].length;h++){
element=list[start][h]
if(visited[element]){
continue
}
destinations.push(element)
dfs(element)
}
}
function addCity(name){
if(name==""){
return
}
cities.push(name)
console.log(cities)
citiesGraph=resizeGraph()
$("#first-city").append("<option value="+cities.length+">"+name+" ("+cities.length+")</option>")
$("#second-city").append("<option value="+cities.length+">"+name+" ("+cities.length+")</option>")
$("#result-city").append("<option value="+cities.length+">"+name+" ("+cities.length+")</option>")
$("#display-cities").append("<div class='city'><h5>"+name+"</h5><div>City ID: "+cities.length+"</div></div>")
logGraph()
}
function addConnection(city1, city2){
if(citiesGraph.get([city1-1, city2-1]) == 0){
citiesGraph.set([city1-1, city2-1], 1)
citiesGraph.set([city2-1, city1-1], 1)
$("#display-connections").append("<div class='city flex-row-space-between'><h5>"+cities[city1-1]+" ("+city1+") <=> "+cities[city2-1]+" ("+city2+")</h5><button class='rmv-btn remove-connection' tag="+city1+":"+city2+">Remove</button></div>")
logGraph()
}
}
function removeConnection(city1, city2){
citiesGraph.set([city1-1, city2-1], 0)
citiesGraph.set([city2-1, city1-1], 0)
$(".remove-connection[tag='"+city1+":"+city2+"']").parent().remove()
logGraph()
}
function showResults(success){
if(success){
$("#results-output").html("")
destinations.map((val, ind)=>{
$("#results-output").append("<div class='city'><h5>"+cities[val]+"</h5><div>City ID:"+(val+1)+"</div></div>")
})
}
else{
$("#results-output").html("")
$("#results-output").append("<p>You can't go anywhere from here</p>")
}
}
//Triggers
$(document).ready(()=>{
$("#add-city").click(function(){
let newCity=$("#add-city-input").val()
if(newCity!=""){
addCity(newCity)
}
})
$("#add-connection").click(function(){
let city1=$("#first-city").val()
let city2=$("#second-city").val()
if(city1!=city2&&city1!=-1&&city2!=-1){
addConnection(city1, city2)
}
})
$("#display-connections").on("click", ".remove-connection", function(){
let tag=$(this).attr("tag").split(":")
removeConnection(tag[0], tag[1])
})
$("#destinations-show").click(function(){
findWays($("#result-city").val())
})
$("#clear-list").click(function(){
$("#display-connections").html("")
$("#display-cities").html("")
$("#results-output").html("")
$("#first-city").html("<option value='-1'>-</option>")
$("#second-city").html("<option value='-1'>-</option>")
$("#result-city").html("<option value='-1'>-</option>")
cities=[]
citiesGraph=math.zeros(0,0)
logGraph()
})
//Cosmetics
$("#add-city-input").bind("focus", function(){
$("#add-city-input").val("")
})
})