From 348740a4477abcd9b2a26dbdfdb03ffdee49220a Mon Sep 17 00:00:00 2001 From: wikankun Date: Thu, 7 Sep 2017 12:18:32 +0700 Subject: [PATCH] Jawaban Wikan --- soal1.py | 13 +++++++++ soal2.py | 30 +++++++++++++++++++++ soal3.py | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+) diff --git a/soal1.py b/soal1.py index 7b95558..ead3de7 100644 --- a/soal1.py +++ b/soal1.py @@ -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) + \ No newline at end of file diff --git a/soal2.py b/soal2.py index 890b106..b3692b5 100644 --- a/soal2.py +++ b/soal2.py @@ -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)) diff --git a/soal3.py b/soal3.py index d55212a..7c15cdb 100644 --- a/soal3.py +++ b/soal3.py @@ -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)