Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jawaban Wikan #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions soal1.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,16 @@

# FUNGSI INSERTION SORT
# Tulis algoritma disini...
def insertion_sort(array):
for i in range(1, len(array)):
nilai = array[i]
j = i - 1
while (j>=0) and (array[j]>nilai):
array[j+1] = array[j]
j = j - 1
array[j+1] = nilai

insertion_sort(arr)
for x in arr:
print(x)

30 changes: 30 additions & 0 deletions soal2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,36 @@
# pop() -> mengeluarkan item pada Stack
# size() -> menghitung jumlah item pada Stack
# printStack() -> output seluruh isi Stack
class Stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def size(self):
return len(self.items)
def printStack(self):
return self.items[len(self.items)-1]


# FUNGSI VALIDASI BRACKETS
# Tulis algoritma disini...
def validation(inputan):
stat = True
s = Stack()
for items in inputan:
if items == '(':
s.push(items)
elif items == ')':
if not s.isEmpty():
s.pop()
else:
return False
if not s.isEmpty():
return False
return stat

print(validation(inputan))
82 changes: 82 additions & 0 deletions soal3.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,87 @@
# KELAS GRAPH
# Buatlah kelas objek Graph
from collections import defaultdict
class Graph(object):
def __init__(self):
self.nodes = set()
self.edges = defaultdict(list)
self.distances = {}
def add_node(self, value):
self.nodes.add(value)
def add_edge(self, from_node, to_node, distances):
self.edges[from_node].append(to_node)
self.edges[to_node].append(from_node)
self.distances[(from_node, to_node)] = distances
self.distances[(to_node, from_node)] = distances

# FUNGSI PENCARIAN RUTE TERCEPAT
# Tulis algoritma disini...
def dijkstra(graph, initial):
visited = {initial: 0}
path = {}

nodes = set(graph.nodes)

while nodes:
min_node = None
for node in nodes:
if node in visited:
if min_node is None:
min_node = node
elif visited[node] < visited[min_node]:
min_node = node
if min_node is None:
break

nodes.remove(min_node)
current_weight = visited[min_node]

for edge in graph.edges[min_node]:
weight = current_weight + graph.distances[(min_node, edge)]
if edge not in visited or weight < visited[edge]:
visited[edge] = weight
path[edge] = min_node

return visited, path

def shortestPath(graph, start, end):
d,p = dijkstra(graph,start)
path = []
while 1:
path.append(end)
if end == start:
break
end = p[end]
path.reverse()
return path

# INISIALISASI GRAPH
g = Graph()

g.add_node('S')
g.add_node('A')
g.add_node('B')
g.add_node('C')
g.add_node('D')
g.add_node('E')
g.add_node('F')
g.add_node('G')

g.add_edge('S','A',6)
g.add_edge('S','B',14)
g.add_edge('S','C',10)
g.add_edge('A','B',6)
g.add_edge('A','D',24)
g.add_edge('B','C',4)
g.add_edge('B','E',15)
g.add_edge('C','F',18)
g.add_edge('D','E',4)
g.add_edge('D','G',9)
g.add_edge('E','F',4)
g.add_edge('E','G',9)
g.add_edge('F','G',9)

path = shortestPath(g, 'S', 'G')
print(path)
path_length,paths = dijkstra(g, 'S')
print(path_length)