Skip to content

Commit 0912868

Browse files
committed
feat : adding code for custom case and adjust heuristic reference
1 parent f8faf67 commit 0912868

File tree

3 files changed

+196
-2
lines changed

3 files changed

+196
-2
lines changed

Java Problem (Custom Made)/A-Star/HeuristicJalan.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ Sragen, 222
1616
Klaten, 18
1717
Tulungagung, 33
1818
Kediri, 76
19-
Pacitan, 31
19+
Pacitan, 31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import heapq\n",
10+
"javaGraph = {}\n"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 2,
16+
"metadata": {},
17+
"outputs": [],
18+
"source": [
19+
"class PriorityQueue:\n",
20+
" def __init__(self) -> None:\n",
21+
" self.cities = []\n",
22+
"\n",
23+
" def isEmpty(self):\n",
24+
" if self.cities == []:\n",
25+
" return True\n",
26+
" return False\n",
27+
"\n",
28+
" def check(self):\n",
29+
" print(self.cities)\n",
30+
"\n",
31+
" def push(self, city, cost):\n",
32+
" heapq.heappush(self.cities, (cost, city))\n",
33+
" \n",
34+
" def pop(self):\n",
35+
" return heapq.heappop(self.cities)[1]\n"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": 3,
41+
"metadata": {},
42+
"outputs": [],
43+
"source": [
44+
"class CityNode:\n",
45+
" def __init__(self, city, distance) -> None:\n",
46+
" self.city = str(city)\n",
47+
" self.distance = str(distance)"
48+
]
49+
},
50+
{
51+
"cell_type": "code",
52+
"execution_count": 4,
53+
"metadata": {},
54+
"outputs": [],
55+
"source": [
56+
"def makeDict():\n",
57+
" file = open(\"Jalan.txt\", 'r')\n",
58+
" for str in file:\n",
59+
" delimeter = str.split(',')\n",
60+
" city1 = delimeter[0]\n",
61+
" city2 = delimeter[1]\n",
62+
" dist = delimeter[2]\n",
63+
" javaGraph.setdefault(city1, []).append(CityNode(city2, dist))\n",
64+
" javaGraph.setdefault(city2, []).append(CityNode(city1, dist))\n",
65+
" "
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 5,
71+
"metadata": {},
72+
"outputs": [],
73+
"source": [
74+
"def makeHeuristicDict():\n",
75+
" h = {}\n",
76+
" file = open(\"HeuristicJalan.txt\", 'r')\n",
77+
" for str in file:\n",
78+
" delimeter = str.strip().split(',')\n",
79+
" node = delimeter[0].strip()\n",
80+
" sld = int(delimeter[1].strip()) # Jalan lurus\n",
81+
" h[node] = sld\n",
82+
" return h\n",
83+
"\n",
84+
"def heuristic(node, values):\n",
85+
" return values[node]\n"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": 6,
91+
"metadata": {},
92+
"outputs": [],
93+
"source": [
94+
"def greedyBFS(start, end):\n",
95+
" path = {}\n",
96+
" q = PriorityQueue()\n",
97+
" h = makeHeuristicDict()\n",
98+
"\n",
99+
" q.push(start, 0)\n",
100+
" path[start] = None\n",
101+
" expandList = []\n",
102+
"\n",
103+
" while q.isEmpty() == False:\n",
104+
" curr = q.pop()\n",
105+
" expandList.append(curr)\n",
106+
"\n",
107+
" if curr == end:\n",
108+
" break\n",
109+
" \n",
110+
" for new in javaGraph[curr]:\n",
111+
" if new.city not in path:\n",
112+
" f_cost = heuristic(new.city, h)\n",
113+
" q.push(new.city, f_cost)\n",
114+
" path[new.city] = curr\n",
115+
"\n",
116+
" printOutput(start, end, path, expandList)\n",
117+
"\n",
118+
"def printOutput(start, end, path, expandList):\n",
119+
" finalpath = []\n",
120+
" i = end\n",
121+
"\n",
122+
" while (path.get(i) != None):\n",
123+
" finalpath.append(i)\n",
124+
" i = path[i]\n",
125+
" finalpath.append(start)\n",
126+
" finalpath.reverse()\n",
127+
"\n",
128+
" print(\"Membuat Program Greedy Best-First-Search Sederhana\")\n",
129+
" print(\"\\tSurabaya => Tulungagung\")\n",
130+
" print(\"==============================================================================================================\")\n",
131+
" print(\"Kota yg mungkin dijelajah \\t\\t: \" + str(expandList))\n",
132+
" print(\"Jumlah kemungkinan kota \\t\\t: \" + str(len(expandList)))\n",
133+
" print(\"==============================================================================================================\")\n",
134+
" print(\"Kota yg dilewati dg jarak terpendek\\t: \" + str(finalpath))\n",
135+
" print(\"Jumlah kota yang dilewati \\t\\t: \" + str(len(finalpath)))\n",
136+
" print(\"==============================================================================================================\")\n"
137+
]
138+
},
139+
{
140+
"cell_type": "code",
141+
"execution_count": 7,
142+
"metadata": {},
143+
"outputs": [
144+
{
145+
"name": "stdout",
146+
"output_type": "stream",
147+
"text": [
148+
"Membuat Program Greedy Best-First-Search Sederhana\n",
149+
"\tSurabaya => Tulungagung\n",
150+
"==============================================================================================================\n",
151+
"Kota yg mungkin dijelajah \t\t: ['Surabaya', 'Gresik', 'Tuban', 'Rembang', 'Demak', 'Mojokerto', 'Jombang', 'Ponorogo', 'Malang', 'Kediri', 'Klaten', 'Tulungagung']\n",
152+
"Jumlah kemungkinan kota \t\t: 12\n",
153+
"==============================================================================================================\n",
154+
"Kota yg dilewati dg jarak terpendek\t: ['Surabaya', 'Gresik', 'Tuban', 'Mojokerto', 'Jombang', 'Malang', 'Kediri', 'Klaten', 'Tulungagung']\n",
155+
"Jumlah kota yang dilewati \t\t: 9\n",
156+
"==============================================================================================================\n"
157+
]
158+
}
159+
],
160+
"source": [
161+
"def main():\n",
162+
" src = \"Surabaya\"\n",
163+
" dst = \"Tulungagung\"\n",
164+
" makeDict()\n",
165+
" greedyBFS(src, dst)\n",
166+
"\n",
167+
"if __name__ == \"__main__\":\n",
168+
" main()"
169+
]
170+
}
171+
],
172+
"metadata": {
173+
"kernelspec": {
174+
"display_name": "Python 3",
175+
"language": "python",
176+
"name": "python3"
177+
},
178+
"language_info": {
179+
"codemirror_mode": {
180+
"name": "ipython",
181+
"version": 3
182+
},
183+
"file_extension": ".py",
184+
"mimetype": "text/x-python",
185+
"name": "python",
186+
"nbconvert_exporter": "python",
187+
"pygments_lexer": "ipython3",
188+
"version": "3.11.0"
189+
},
190+
"orig_nbformat": 4
191+
},
192+
"nbformat": 4,
193+
"nbformat_minor": 2
194+
}

Java Problem (Custom Made)/Greedy-BFS/HeuristicJalan.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ Sragen, 222
1616
Klaten, 18
1717
Tulungagung, 33
1818
Kediri, 76
19-
Pacitan, 31
19+
Pacitan, 31

0 commit comments

Comments
 (0)