Problem Solving/자료구조-알고리즘(4)
-
[알고리즘] DFS, BFS 구현 (Python)
DFS """ 1 / | \ 2 3 4 | | 5 | / \ / 6 7 """ graph = { 1: [2,3,4], 2: [5], 3: [5], 4: [], 5: [6,7], 6: [], 7: [3], } def recursive_dfs(v, discovered = []): discovered.append(v) # 시작 정점 방문 for w in graph[v]: if not w in discovered: # 방문 하지 않았으면 discovered = recursive_dfs(w, discovered) return discovered def iterative_dfs(start_v): discovered = [] stack = [start_v] while stack: v = stack.pop() if v..
2021.01.27 -
[자료구조] Circle Deque (원형 덱) 구현
Circle Deque (원형 덱) class MyCircularDeque: def __init__(self, k: int): self.q = [None] * (k+1) self.max = k+1 self.front = 0 self.rear = 0 def insert_front(self, value: int): if self.is_full(): print('Deque is Full') else: self.q[self.front] = value self.front = (self.front - 1 + self.max) % self.max def insert_last(self, value: int): if self.is_full(): print('Deque is Full') else: self.rear = (..
2021.01.19 -
[자료구조] Double Linked List (이중 연결 리스트) 구현
Double Linked List # Single Linked List class Node: def __init__(self, item, prev=None, next=None): self.item = item self.prev = prev self.next = next class DoubleLinkedList: def __init__(self): self.head = Node(None) self.tail = Node(None, self.head) self.head.next = self.tail self.size = 0 def list_size(self): return self.size def is_empty(self): if self.size != 0: return False else: return Tr..
2021.01.11 -
[자료구조] Single Linked List (단순 연결 리스트) 구현
Single Linked List (단순 연결 리스트) 파이썬으로 구현 해 보았다. # Single Linked List class Node: def __init__(self, item, next=None): self.item = item self.next = next class SingleLinkedList: def __init__(self): self.head = None self.size = 0 def list_size(self): return self.size def is_empty(self): if self.size != 0: return False else: return True def select_node(self, idx): if idx >= self.size: print("Index Er..
2021.01.11