POSCAT Seminar 3-2 :
Data Structure
yougatup @ POSCAT
1
Topic
 Topic today
− Data Structure
• Stack
• Queue
• Linked List
• Tree
• Graph ( definition )
• Priority Queue
• Heap
POSCAT Seminar 1-2
9 July 2014
yougatup
 More topic for you
− Advanced DS
• Binary Indexed Tree
• Fenwick Tree
− Well-known Problem
• Parenthesis Matching
• Range Minimum Query
• Lowest Common Ancestor
• Inversion Count
No slide
Stack
 LIFO ( Last-In-First-Out )
− Recursion 을 할 때 쓰이는 자료구조
− 실제로 유용하게 쓸 일은 많지 않습니다
 Parenthesis Matching
− 괄호의 짝을 서로 찾아주는 방법
− Stack을 잘 쓰면 됩니다
 Depth First Search on Graph
− Graph Traversal
POSCAT Seminar 1-3
9 July 2014
yougatup
( ( ) ( ( ) ) ( ) )
5 1 1 3 2 2 3 4 4 5
Parenthesis Matching
 Use Stack !
− “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함
− ‘(‘은 Push, ‘)’ 은 Pop
POSCAT Seminar 1-4
9 July 2014
yougatup
0 1 2 3 4 5 6 7 8 9
( ( ) ( ( ) ) ( ) )
( 0
index
Parenthesis Matching
 Use Stack !
− “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함
− ‘(‘은 Push, ‘)’ 은 Pop
POSCAT Seminar 1-5
9 July 2014
yougatup
0 1 2 3 4 5 6 7 8 9
( ( ) ( ( ) ) ( ) )
( 0
index
( 1
Parenthesis Matching
 Use Stack !
− “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함
− ‘(‘은 Push, ‘)’ 은 Pop
POSCAT Seminar 1-6
9 July 2014
yougatup
0 1 2 3 4 5 6 7 8 9
( ( ) ( ( ) ) ( ) )
1 1
( 0
index
( 1
It is my partner !
Parenthesis Matching
 Use Stack !
− “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함
− ‘(‘은 Push, ‘)’ 은 Pop
POSCAT Seminar 1-7
9 July 2014
yougatup
0 1 2 3 4 5 6 7 8 9
( ( ) ( ( ) ) ( ) )
1 1
( 0
index
Pop!
Parenthesis Matching
 Use Stack !
− “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함
− ‘(‘은 Push, ‘)’ 은 Pop
POSCAT Seminar 1-8
9 July 2014
yougatup
0 1 2 3 4 5 6 7 8 9
( ( ) ( ( ) ) ( ) )
1 1
( 0
index
( 3
Parenthesis Matching
 Use Stack !
− “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함
− ‘(‘은 Push, ‘)’ 은 Pop
POSCAT Seminar 1-9
9 July 2014
yougatup
0 1 2 3 4 5 6 7 8 9
( ( ) ( ( ) ) ( ) )
1 1
( 0
index
( 3
( 4
Parenthesis Matching
 Use Stack !
− “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함
− ‘(‘은 Push, ‘)’ 은 Pop
POSCAT Seminar 1-10
9 July 2014
yougatup
0 1 2 3 4 5 6 7 8 9
( ( ) ( ( ) ) ( ) )
1 1 2 2
( 0
index
( 3
( 4
It is my partner !
Parenthesis Matching
 Use Stack !
− “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함
− ‘(‘은 Push, ‘)’ 은 Pop
POSCAT Seminar 1-11
9 July 2014
yougatup
0 1 2 3 4 5 6 7 8 9
( ( ) ( ( ) ) ( ) )
1 1 2 2
( 0
index
( 3
Pop!
Parenthesis Matching
 Use Stack !
− “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함
− ‘(‘은 Push, ‘)’ 은 Pop
POSCAT Seminar 1-12
9 July 2014
yougatup
0 1 2 3 4 5 6 7 8 9
( ( ) ( ( ) ) ( ) )
1 1 3 2 2 3
( 0
index
( 3
It is my partner !
Parenthesis Matching
 Use Stack !
− “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함
− ‘(‘은 Push, ‘)’ 은 Pop
POSCAT Seminar 1-13
9 July 2014
yougatup
0 1 2 3 4 5 6 7 8 9
( ( ) ( ( ) ) ( ) )
1 1 3 2 2 3
( 0
index
Pop!
Parenthesis Matching
 Use Stack !
− “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함
− ‘(‘은 Push, ‘)’ 은 Pop
POSCAT Seminar 1-14
9 July 2014
yougatup
0 1 2 3 4 5 6 7 8 9
( ( ) ( ( ) ) ( ) )
1 1 3 2 2 3
( 0
index
( 7
Parenthesis Matching
 Use Stack !
− “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함
− ‘(‘은 Push, ‘)’ 은 Pop
POSCAT Seminar 1-15
9 July 2014
yougatup
0 1 2 3 4 5 6 7 8 9
( ( ) ( ( ) ) ( ) )
1 1 3 2 2 3 4 4
( 0
index
( 7
It is my partner !
Parenthesis Matching
 Use Stack !
− “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함
− ‘(‘은 Push, ‘)’ 은 Pop
POSCAT Seminar 1-16
9 July 2014
yougatup
0 1 2 3 4 5 6 7 8 9
( ( ) ( ( ) ) ( ) )
1 1 3 2 2 3 4 4
( 0
index
Pop!
Parenthesis Matching
 Use Stack !
− “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함
− ‘(‘은 Push, ‘)’ 은 Pop
POSCAT Seminar 1-17
9 July 2014
yougatup
0 1 2 3 4 5 6 7 8 9
( ( ) ( ( ) ) ( ) )
5 1 1 3 2 2 3 4 4 5
( 0
index
It is my partner !
Parenthesis Matching
 Use Stack !
− “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함
− ‘(‘은 Push, ‘)’ 은 Pop
POSCAT Seminar 1-18
9 July 2014
yougatup
0 1 2 3 4 5 6 7 8 9
( ( ) ( ( ) ) ( ) )
5 1 1 3 2 2 3 4 4 5
index
Pop!
Parenthesis Matching
 Use Stack !
− “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함
− ‘(‘은 Push, ‘)’ 은 Pop
 Invalid Parenthesis Check
− Pop을 해야 하는 상황에 Pop할 것이 없다면 ?  Invalid !
− 최종 Stack에 괄호가 남아있는 경우?  Also Invalid !
POSCAT Seminar 1-19
9 July 2014
yougatup
( ( ) ) ) ( ( ) ) ) ( ( ) ( ( ) ) ( )
We can’t pop because stack it empty After all operations, stack is not empty
Parenthesis Matching
 Use Stack !
− “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함
− ‘(‘은 Push, ‘)’ 은 Pop
 Invalid Parenthesis Check
− Pop을 해야 하는 상황에 Pop할 것이 없다면 ?  Invalid !
− 최종 Stack에 괄호가 남아있는 경우?  Also Invalid !
Verification : Is this algorithm true ? Think about it
Efficiency : What time does it take ? O(n)
POSCAT Seminar 1-20
9 July 2014
yougatup
Queue
 FIFO ( First – In – First – Out )
− 먼저 들어간 애가 먼저 나옴
− Front, Rear pointer
− 아직은 쓸 일 없습니다
− Circular Queue
 BFS ( Breadth First Search on Graph )
− Graph Traversal
POSCAT Seminar 1-21
9 July 2014
yougatup
Tree
 Cycle 이 없는 Graph
− Do you know what is Graph ?
− 두 노드 사이의 경로는 유일하다 // Why ?
 Binary Tree
− 자식 노드가 2개 이하인 Tree
− Binary Tree Traversal
 Complete Binary Tree
− 자식 노드가 왼쪽부터 채워지는 Binary Tree
− Heap
 Full Binary Tree
− Leaf node 를 제외한 모든 Internal node의 자식이 2개
− Binary Indexed Tree
POSCAT Seminar 1-22
9 July 2014
yougatup
Binary Tree
 Tree Traversal
− Preorder  Root – Left – Right
− Inorder  Left – Root – Right
− Postorder  Left – Right – Root
 Preorder와 Inorder만으로 Tree를 유일하게 결정
Preorder : 1 2 4 3 5 6 7
Inorder : 4 2 1 5 3 7 6 일 때 Tree는 어떻게 생겼나 ?
POSCAT Seminar 1-23
9 July 2014
yougatup
Binary Tree
 Preorder와 Inorder만으로 Tree를 유일하게 결정
Preorder : 1 2 4 3 5 6 7
Inorder : 4 2 1 5 3 7 6 일 때 Tree는 어떻게 생겼나 ?
1. Preorder의 가장 첫 번째 node는 Tree의 root 이다.
2. Root node r에 대하여 Inorder의 순서를 고려하였을 때,
r의 왼쪽에는 r의 left-subtree의 node들이,
오른쪽에는 right-subrree의 node들이 있다
POSCAT Seminar 1-24
9 July 2014
yougatup
Binary Tree
POSCAT Seminar 1-25
9 July 2014
yougatup
Root!
1
 Preorder와 Inorder만으로 Tree를 유일하게 결정
Preorder : 1 2 4 3 5 6 7
Inorder : 4 2 1 5 3 7 6 일 때 Tree는 어떻게 생겼나 ?
Binary Tree
POSCAT Seminar 1-26
9 July 2014
yougatup
Root!
1
left right
4, 2 5, 3, 7, 6
 Preorder와 Inorder만으로 Tree를 유일하게 결정
Preorder : 1 2 4 3 5 6 7
Inorder : 4 2 1 5 3 7 6 일 때 Tree는 어떻게 생겼나 ?
Binary Tree
POSCAT Seminar 1-27
9 July 2014
yougatup
Divide & Conquer !
1
left right
4, 2 5, 3, 7, 6
 Preorder와 Inorder만으로 Tree를 유일하게 결정
Preorder : 1 2 4 3 5 6 7
Inorder : 4 2 1 5 3 7 6 일 때 Tree는 어떻게 생겼나 ?
Binary Tree
POSCAT Seminar 1-28
9 July 2014
yougatup
Divide & Conquer !
1
left right
4, 2 5, 3, 7, 6
Preorder : 2 4
Inorder : 4 2
Preorder : 3 5 6 7
Inorder : 5 3 7 6
 Preorder와 Inorder만으로 Tree를 유일하게 결정
Preorder : 1 2 4 3 5 6 7
Inorder : 4 2 1 5 3 7 6 일 때 Tree는 어떻게 생겼나 ?
Binary Tree
POSCAT Seminar 1-29
9 July 2014
yougatup
Divide & Conquer !
1
left right
5, 3, 7, 6
Preorder : 2 4
Inorder : 4 2
Preorder : 3 5 6 7
Inorder : 5 3 7 6
Root !
4, 2
left
 Preorder와 Inorder만으로 Tree를 유일하게 결정
Preorder : 1 2 4 3 5 6 7
Inorder : 4 2 1 5 3 7 6 일 때 Tree는 어떻게 생겼나 ?
Binary Tree
 Preorder와 Inorder만으로 Tree를 유일하게 결정
Preorder : 1 2 4 3 5 6 7
Inorder : 4 2 1 5 3 7 6 일 때 Tree는 어떻게 생겼나 ?
POSCAT Seminar 1-30
9 July 2014
yougatup
Divide & Conquer !
1
left right
5, 3, 7, 6
Preorder : 2 4
Inorder : 4 2
Preorder : 3 5 6 7
Inorder : 5 3 7 6
Root !
left
2
4
Binary Tree
 Preorder와 Inorder만으로 Tree를 유일하게 결정
Preorder : 1 2 4 3 5 6 7
Inorder : 4 2 1 5 3 7 6 일 때 Tree는 어떻게 생겼나 ?
POSCAT Seminar 1-31
9 July 2014
yougatup
Divide & Conquer !
1
left right
5, 3, 7, 6
Preorder : 2 4
Inorder : 4 2
Preorder : 3 5 6 7
Inorder : 5 3 7 6
Root !
left
2
4
Binary Tree
 Preorder와 Inorder만으로 Tree를 유일하게 결정
Preorder : 1 2 4 3 5 6 7
Inorder : 4 2 1 5 3 7 6 일 때 Tree는 어떻게 생겼나 ?
POSCAT Seminar 1-32
9 July 2014
yougatup
Divide & Conquer !
1
left right
Preorder : 2 4
Inorder : 4 2
Preorder : 3 5 6 7
Inorder : 5 3 7 6
Root !
left
2
4
3
5 7, 6 Preorder : 6 7
Inorder : 7 6
Binary Tree
 Preorder와 Inorder만으로 Tree를 유일하게 결정
Preorder : 1 2 4 3 5 6 7
Inorder : 4 2 1 5 3 7 6 일 때 Tree는 어떻게 생겼나 ?
POSCAT Seminar 1-33
9 July 2014
yougatup
Divide & Conquer !
1
left right
Preorder : 2 4
Inorder : 4 2
Preorder : 3 5 6 7
Inorder : 5 3 7 6
Root !
left
2
4
3
5 Preorder : 6 7
Inorder : 7 6
6
7
Priority Queue
 우선순위가 있는 Queue
− Pop을 하면, 우선순위가 가장 높은 element를 뺀다
− How to implement it?
POSCAT Seminar 1-34
9 July 2014
yougatup
Priority Queue
 우선순위가 있는 Queue
− Pop을 하면, 우선순위가 가장 높은 element를 뺀다
− How to implement it? With array !
POSCAT Seminar 1-35
9 July 2014
yougatup
5 3 4 1 3 2 7
Priority Queue
 우선순위가 있는 Queue
− Pop을 하면, 우선순위가 가장 높은 element를 뺀다
− How to implement it? With array !
POSCAT Seminar 1-36
9 July 2014
yougatup
5 3 4 1 3 2 7
Pop !
Priority Queue
 우선순위가 있는 Queue
− Pop을 하면, 우선순위가 가장 높은 element를 뺀다
− How to implement it? With array !
POSCAT Seminar 1-37
9 July 2014
yougatup
5 3 4 3 2 7
Priority Queue
 우선순위가 있는 Queue
− Pop을 하면, 우선순위가 가장 높은 element를 뺀다
− How to implement it? With array !
POSCAT Seminar 1-38
9 July 2014
yougatup
5 3 4 3 2 7
Pop !
Priority Queue
 우선순위가 있는 Queue
− Pop을 하면, 우선순위가 가장 높은 element를 뺀다
− How to implement it? With array !
POSCAT Seminar 1-39
9 July 2014
yougatup
5 3 4 3 7
Push 8
Priority Queue
 우선순위가 있는 Queue
− Pop을 하면, 우선순위가 가장 높은 element를 뺀다
− How to implement it? With array !
POSCAT Seminar 1-40
9 July 2014
yougatup
5 3 4 8 3 7
Priority Queue
 우선순위가 있는 Queue
− Pop을 하면, 우선순위가 가장 높은 element를 뺀다
− How to implement it? With array !
Verification : Is this algorithm true ?
Efficiency : What time does it take ?
POSCAT Seminar 1-41
9 July 2014
yougatup
Priority Queue
 우선순위가 있는 Queue
− Pop을 하면, 우선순위가 가장 높은 element를 뺀다
− How to implement it? With array !
Verification : Is this algorithm true ? Trivial
Efficiency : What time does it take ? O(n) for push & pop
POSCAT Seminar 1-42
9 July 2014
yougatup
Priority Queue
 우선순위가 있는 Queue
− Pop을 하면, 우선순위가 가장 높은 element를 뺀다
− How to implement it? With array !
Verification : Is this algorithm true ? Trivial
Efficiency : What time does it take ? O(n) for push & pop
POSCAT Seminar 1-43
9 July 2014
yougatup
Is there another algorithm faster ?
Priority Queue
 우선순위가 있는 Queue
− Pop을 하면, 우선순위가 가장 높은 element를 뺀다
− How to implement it? With array !
Verification : Is this algorithm true ? Trivial
Efficiency : What time does it take ? O(n) for push & pop
POSCAT Seminar 1-44
9 July 2014
yougatup
Is there another algorithm faster ?
 Use heap !
Heap
 Complete Binary Tree with Invarient
− Complete Binary Tree
− Inv : 부모노드의 값이 항상 자식 노드의 값보다 우선순위가 높음
 Priority
− Greater First  Max heap
− Less First  Min heap
 Operation
− Push : Heap의 가장 끝에 추가시키고 Rearrange
− Pop : Heap의 root를 빼고, Heap의 가장 끝에 있는 elemen를
root로 올린 후 Rearrange
POSCAT Seminar 1-45
9 July 2014
yougatup
Min Heap
POSCAT Seminar 1-46
9 July 2014
yougatup
Min Heap
POSCAT Seminar 1-47
9 July 2014
yougatup
4
3 5 7 8
2 5
1
Min Heap
POSCAT Seminar 1-48
9 July 2014
yougatup
4
3 5 7 8
2 5
1
Push 2
Min Heap
POSCAT Seminar 1-49
9 July 2014
yougatup
Push 2
24
3 5 7 8
2 5
1
Min Heap
POSCAT Seminar 1-50
9 July 2014
yougatup
Push 2
34
2 5 7 8
2 5
1
Min Heap
POSCAT Seminar 1-51
9 July 2014
yougatup
Push 2 // Done
34
2 5 7 8
2 5
1
Min Heap
POSCAT Seminar 1-52
9 July 2014
yougatup
Push 3
34
2 5 7 8
2 5
1
Min Heap
POSCAT Seminar 1-53
9 July 2014
yougatup
Push 3
34
2 5 7 8
2 5
1
3
Min Heap
POSCAT Seminar 1-54
9 July 2014
yougatup
Push 3
34
2 3 7 8
2 5
1
5
Min Heap
POSCAT Seminar 1-55
9 July 2014
yougatup
Push 3 // Done
34
2 3 7 8
2 5
1
5
Min Heap
POSCAT Seminar 1-56
9 July 2014
yougatup
Pop
34
2 3 7 8
2 5
1
5
Min Heap
POSCAT Seminar 1-57
9 July 2014
yougatup
Pop
34
2 3 7 8
2 5
1
5
Min Heap
POSCAT Seminar 1-58
9 July 2014
yougatup
Pop
34
2 3 7 8
2 5
5
Min Heap
POSCAT Seminar 1-59
9 July 2014
yougatup
Pop
34
2 3 7 8
2 5
5
Min Heap
POSCAT Seminar 1-60
9 July 2014
yougatup
Pop
34
2 3 7 8
2 5
5
Min Heap
POSCAT Seminar 1-61
9 July 2014
yougatup
Pop
34
2 3 7 8
2 5
5
2 has high priority !
Min Heap
POSCAT Seminar 1-62
9 July 2014
yougatup
Pop
34
2 3 7 8
2 5
5
Min Heap
POSCAT Seminar 1-63
9 July 2014
yougatup
Pop
34
2 3 7 8
5 5
2
Min Heap
POSCAT Seminar 1-64
9 July 2014
yougatup
Pop
34
2 3 7 8
5 5
2
Min Heap
POSCAT Seminar 1-65
9 July 2014
yougatup
Pop
34
2 3 7 8
5 5
2
Min Heap
POSCAT Seminar 1-66
9 July 2014
yougatup
Pop
34
5 3 7 8
2 5
2
Min Heap
POSCAT Seminar 1-67
9 July 2014
yougatup
Pop
34
5 3 7 8
2 5
2
Min Heap
POSCAT Seminar 1-68
9 July 2014
yougatup
Pop
34
5 3 7 8
2 5
2
Min Heap
POSCAT Seminar 1-69
9 July 2014
yougatup
Pop
54
3 3 7 8
2 5
2
Min Heap
POSCAT Seminar 1-70
9 July 2014
yougatup
Pop // Done
54
3 3 7 8
2 5
2
Min Heap
POSCAT Seminar 1-71
9 July 2014
yougatup
Pop // Done
54
3 3 7 8
2 5
2
Min Heap
Verification : Is this algorithm true ?
Efficiency : What time does it take ?
POSCAT Seminar 1-72
9 July 2014
yougatup
Min Heap
Verification : Is this algorithm true ? By invarient
Efficiency : What time does it take ? O(log n) for push & pop
 Think about height of the tree
POSCAT Seminar 1-73
9 July 2014
yougatup

Poscat seminar 3-2

  • 1.
    POSCAT Seminar 3-2: Data Structure yougatup @ POSCAT 1
  • 2.
    Topic  Topic today −Data Structure • Stack • Queue • Linked List • Tree • Graph ( definition ) • Priority Queue • Heap POSCAT Seminar 1-2 9 July 2014 yougatup  More topic for you − Advanced DS • Binary Indexed Tree • Fenwick Tree − Well-known Problem • Parenthesis Matching • Range Minimum Query • Lowest Common Ancestor • Inversion Count No slide
  • 3.
    Stack  LIFO (Last-In-First-Out ) − Recursion 을 할 때 쓰이는 자료구조 − 실제로 유용하게 쓸 일은 많지 않습니다  Parenthesis Matching − 괄호의 짝을 서로 찾아주는 방법 − Stack을 잘 쓰면 됩니다  Depth First Search on Graph − Graph Traversal POSCAT Seminar 1-3 9 July 2014 yougatup ( ( ) ( ( ) ) ( ) ) 5 1 1 3 2 2 3 4 4 5
  • 4.
    Parenthesis Matching  UseStack ! − “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함 − ‘(‘은 Push, ‘)’ 은 Pop POSCAT Seminar 1-4 9 July 2014 yougatup 0 1 2 3 4 5 6 7 8 9 ( ( ) ( ( ) ) ( ) ) ( 0 index
  • 5.
    Parenthesis Matching  UseStack ! − “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함 − ‘(‘은 Push, ‘)’ 은 Pop POSCAT Seminar 1-5 9 July 2014 yougatup 0 1 2 3 4 5 6 7 8 9 ( ( ) ( ( ) ) ( ) ) ( 0 index ( 1
  • 6.
    Parenthesis Matching  UseStack ! − “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함 − ‘(‘은 Push, ‘)’ 은 Pop POSCAT Seminar 1-6 9 July 2014 yougatup 0 1 2 3 4 5 6 7 8 9 ( ( ) ( ( ) ) ( ) ) 1 1 ( 0 index ( 1 It is my partner !
  • 7.
    Parenthesis Matching  UseStack ! − “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함 − ‘(‘은 Push, ‘)’ 은 Pop POSCAT Seminar 1-7 9 July 2014 yougatup 0 1 2 3 4 5 6 7 8 9 ( ( ) ( ( ) ) ( ) ) 1 1 ( 0 index Pop!
  • 8.
    Parenthesis Matching  UseStack ! − “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함 − ‘(‘은 Push, ‘)’ 은 Pop POSCAT Seminar 1-8 9 July 2014 yougatup 0 1 2 3 4 5 6 7 8 9 ( ( ) ( ( ) ) ( ) ) 1 1 ( 0 index ( 3
  • 9.
    Parenthesis Matching  UseStack ! − “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함 − ‘(‘은 Push, ‘)’ 은 Pop POSCAT Seminar 1-9 9 July 2014 yougatup 0 1 2 3 4 5 6 7 8 9 ( ( ) ( ( ) ) ( ) ) 1 1 ( 0 index ( 3 ( 4
  • 10.
    Parenthesis Matching  UseStack ! − “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함 − ‘(‘은 Push, ‘)’ 은 Pop POSCAT Seminar 1-10 9 July 2014 yougatup 0 1 2 3 4 5 6 7 8 9 ( ( ) ( ( ) ) ( ) ) 1 1 2 2 ( 0 index ( 3 ( 4 It is my partner !
  • 11.
    Parenthesis Matching  UseStack ! − “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함 − ‘(‘은 Push, ‘)’ 은 Pop POSCAT Seminar 1-11 9 July 2014 yougatup 0 1 2 3 4 5 6 7 8 9 ( ( ) ( ( ) ) ( ) ) 1 1 2 2 ( 0 index ( 3 Pop!
  • 12.
    Parenthesis Matching  UseStack ! − “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함 − ‘(‘은 Push, ‘)’ 은 Pop POSCAT Seminar 1-12 9 July 2014 yougatup 0 1 2 3 4 5 6 7 8 9 ( ( ) ( ( ) ) ( ) ) 1 1 3 2 2 3 ( 0 index ( 3 It is my partner !
  • 13.
    Parenthesis Matching  UseStack ! − “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함 − ‘(‘은 Push, ‘)’ 은 Pop POSCAT Seminar 1-13 9 July 2014 yougatup 0 1 2 3 4 5 6 7 8 9 ( ( ) ( ( ) ) ( ) ) 1 1 3 2 2 3 ( 0 index Pop!
  • 14.
    Parenthesis Matching  UseStack ! − “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함 − ‘(‘은 Push, ‘)’ 은 Pop POSCAT Seminar 1-14 9 July 2014 yougatup 0 1 2 3 4 5 6 7 8 9 ( ( ) ( ( ) ) ( ) ) 1 1 3 2 2 3 ( 0 index ( 7
  • 15.
    Parenthesis Matching  UseStack ! − “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함 − ‘(‘은 Push, ‘)’ 은 Pop POSCAT Seminar 1-15 9 July 2014 yougatup 0 1 2 3 4 5 6 7 8 9 ( ( ) ( ( ) ) ( ) ) 1 1 3 2 2 3 4 4 ( 0 index ( 7 It is my partner !
  • 16.
    Parenthesis Matching  UseStack ! − “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함 − ‘(‘은 Push, ‘)’ 은 Pop POSCAT Seminar 1-16 9 July 2014 yougatup 0 1 2 3 4 5 6 7 8 9 ( ( ) ( ( ) ) ( ) ) 1 1 3 2 2 3 4 4 ( 0 index Pop!
  • 17.
    Parenthesis Matching  UseStack ! − “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함 − ‘(‘은 Push, ‘)’ 은 Pop POSCAT Seminar 1-17 9 July 2014 yougatup 0 1 2 3 4 5 6 7 8 9 ( ( ) ( ( ) ) ( ) ) 5 1 1 3 2 2 3 4 4 5 ( 0 index It is my partner !
  • 18.
    Parenthesis Matching  UseStack ! − “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함 − ‘(‘은 Push, ‘)’ 은 Pop POSCAT Seminar 1-18 9 July 2014 yougatup 0 1 2 3 4 5 6 7 8 9 ( ( ) ( ( ) ) ( ) ) 5 1 1 3 2 2 3 4 4 5 index Pop!
  • 19.
    Parenthesis Matching  UseStack ! − “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함 − ‘(‘은 Push, ‘)’ 은 Pop  Invalid Parenthesis Check − Pop을 해야 하는 상황에 Pop할 것이 없다면 ?  Invalid ! − 최종 Stack에 괄호가 남아있는 경우?  Also Invalid ! POSCAT Seminar 1-19 9 July 2014 yougatup ( ( ) ) ) ( ( ) ) ) ( ( ) ( ( ) ) ( ) We can’t pop because stack it empty After all operations, stack is not empty
  • 20.
    Parenthesis Matching  UseStack ! − “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함 − ‘(‘은 Push, ‘)’ 은 Pop  Invalid Parenthesis Check − Pop을 해야 하는 상황에 Pop할 것이 없다면 ?  Invalid ! − 최종 Stack에 괄호가 남아있는 경우?  Also Invalid ! Verification : Is this algorithm true ? Think about it Efficiency : What time does it take ? O(n) POSCAT Seminar 1-20 9 July 2014 yougatup
  • 21.
    Queue  FIFO (First – In – First – Out ) − 먼저 들어간 애가 먼저 나옴 − Front, Rear pointer − 아직은 쓸 일 없습니다 − Circular Queue  BFS ( Breadth First Search on Graph ) − Graph Traversal POSCAT Seminar 1-21 9 July 2014 yougatup
  • 22.
    Tree  Cycle 이없는 Graph − Do you know what is Graph ? − 두 노드 사이의 경로는 유일하다 // Why ?  Binary Tree − 자식 노드가 2개 이하인 Tree − Binary Tree Traversal  Complete Binary Tree − 자식 노드가 왼쪽부터 채워지는 Binary Tree − Heap  Full Binary Tree − Leaf node 를 제외한 모든 Internal node의 자식이 2개 − Binary Indexed Tree POSCAT Seminar 1-22 9 July 2014 yougatup
  • 23.
    Binary Tree  TreeTraversal − Preorder  Root – Left – Right − Inorder  Left – Root – Right − Postorder  Left – Right – Root  Preorder와 Inorder만으로 Tree를 유일하게 결정 Preorder : 1 2 4 3 5 6 7 Inorder : 4 2 1 5 3 7 6 일 때 Tree는 어떻게 생겼나 ? POSCAT Seminar 1-23 9 July 2014 yougatup
  • 24.
    Binary Tree  Preorder와Inorder만으로 Tree를 유일하게 결정 Preorder : 1 2 4 3 5 6 7 Inorder : 4 2 1 5 3 7 6 일 때 Tree는 어떻게 생겼나 ? 1. Preorder의 가장 첫 번째 node는 Tree의 root 이다. 2. Root node r에 대하여 Inorder의 순서를 고려하였을 때, r의 왼쪽에는 r의 left-subtree의 node들이, 오른쪽에는 right-subrree의 node들이 있다 POSCAT Seminar 1-24 9 July 2014 yougatup
  • 25.
    Binary Tree POSCAT Seminar1-25 9 July 2014 yougatup Root! 1  Preorder와 Inorder만으로 Tree를 유일하게 결정 Preorder : 1 2 4 3 5 6 7 Inorder : 4 2 1 5 3 7 6 일 때 Tree는 어떻게 생겼나 ?
  • 26.
    Binary Tree POSCAT Seminar1-26 9 July 2014 yougatup Root! 1 left right 4, 2 5, 3, 7, 6  Preorder와 Inorder만으로 Tree를 유일하게 결정 Preorder : 1 2 4 3 5 6 7 Inorder : 4 2 1 5 3 7 6 일 때 Tree는 어떻게 생겼나 ?
  • 27.
    Binary Tree POSCAT Seminar1-27 9 July 2014 yougatup Divide & Conquer ! 1 left right 4, 2 5, 3, 7, 6  Preorder와 Inorder만으로 Tree를 유일하게 결정 Preorder : 1 2 4 3 5 6 7 Inorder : 4 2 1 5 3 7 6 일 때 Tree는 어떻게 생겼나 ?
  • 28.
    Binary Tree POSCAT Seminar1-28 9 July 2014 yougatup Divide & Conquer ! 1 left right 4, 2 5, 3, 7, 6 Preorder : 2 4 Inorder : 4 2 Preorder : 3 5 6 7 Inorder : 5 3 7 6  Preorder와 Inorder만으로 Tree를 유일하게 결정 Preorder : 1 2 4 3 5 6 7 Inorder : 4 2 1 5 3 7 6 일 때 Tree는 어떻게 생겼나 ?
  • 29.
    Binary Tree POSCAT Seminar1-29 9 July 2014 yougatup Divide & Conquer ! 1 left right 5, 3, 7, 6 Preorder : 2 4 Inorder : 4 2 Preorder : 3 5 6 7 Inorder : 5 3 7 6 Root ! 4, 2 left  Preorder와 Inorder만으로 Tree를 유일하게 결정 Preorder : 1 2 4 3 5 6 7 Inorder : 4 2 1 5 3 7 6 일 때 Tree는 어떻게 생겼나 ?
  • 30.
    Binary Tree  Preorder와Inorder만으로 Tree를 유일하게 결정 Preorder : 1 2 4 3 5 6 7 Inorder : 4 2 1 5 3 7 6 일 때 Tree는 어떻게 생겼나 ? POSCAT Seminar 1-30 9 July 2014 yougatup Divide & Conquer ! 1 left right 5, 3, 7, 6 Preorder : 2 4 Inorder : 4 2 Preorder : 3 5 6 7 Inorder : 5 3 7 6 Root ! left 2 4
  • 31.
    Binary Tree  Preorder와Inorder만으로 Tree를 유일하게 결정 Preorder : 1 2 4 3 5 6 7 Inorder : 4 2 1 5 3 7 6 일 때 Tree는 어떻게 생겼나 ? POSCAT Seminar 1-31 9 July 2014 yougatup Divide & Conquer ! 1 left right 5, 3, 7, 6 Preorder : 2 4 Inorder : 4 2 Preorder : 3 5 6 7 Inorder : 5 3 7 6 Root ! left 2 4
  • 32.
    Binary Tree  Preorder와Inorder만으로 Tree를 유일하게 결정 Preorder : 1 2 4 3 5 6 7 Inorder : 4 2 1 5 3 7 6 일 때 Tree는 어떻게 생겼나 ? POSCAT Seminar 1-32 9 July 2014 yougatup Divide & Conquer ! 1 left right Preorder : 2 4 Inorder : 4 2 Preorder : 3 5 6 7 Inorder : 5 3 7 6 Root ! left 2 4 3 5 7, 6 Preorder : 6 7 Inorder : 7 6
  • 33.
    Binary Tree  Preorder와Inorder만으로 Tree를 유일하게 결정 Preorder : 1 2 4 3 5 6 7 Inorder : 4 2 1 5 3 7 6 일 때 Tree는 어떻게 생겼나 ? POSCAT Seminar 1-33 9 July 2014 yougatup Divide & Conquer ! 1 left right Preorder : 2 4 Inorder : 4 2 Preorder : 3 5 6 7 Inorder : 5 3 7 6 Root ! left 2 4 3 5 Preorder : 6 7 Inorder : 7 6 6 7
  • 34.
    Priority Queue  우선순위가있는 Queue − Pop을 하면, 우선순위가 가장 높은 element를 뺀다 − How to implement it? POSCAT Seminar 1-34 9 July 2014 yougatup
  • 35.
    Priority Queue  우선순위가있는 Queue − Pop을 하면, 우선순위가 가장 높은 element를 뺀다 − How to implement it? With array ! POSCAT Seminar 1-35 9 July 2014 yougatup 5 3 4 1 3 2 7
  • 36.
    Priority Queue  우선순위가있는 Queue − Pop을 하면, 우선순위가 가장 높은 element를 뺀다 − How to implement it? With array ! POSCAT Seminar 1-36 9 July 2014 yougatup 5 3 4 1 3 2 7 Pop !
  • 37.
    Priority Queue  우선순위가있는 Queue − Pop을 하면, 우선순위가 가장 높은 element를 뺀다 − How to implement it? With array ! POSCAT Seminar 1-37 9 July 2014 yougatup 5 3 4 3 2 7
  • 38.
    Priority Queue  우선순위가있는 Queue − Pop을 하면, 우선순위가 가장 높은 element를 뺀다 − How to implement it? With array ! POSCAT Seminar 1-38 9 July 2014 yougatup 5 3 4 3 2 7 Pop !
  • 39.
    Priority Queue  우선순위가있는 Queue − Pop을 하면, 우선순위가 가장 높은 element를 뺀다 − How to implement it? With array ! POSCAT Seminar 1-39 9 July 2014 yougatup 5 3 4 3 7 Push 8
  • 40.
    Priority Queue  우선순위가있는 Queue − Pop을 하면, 우선순위가 가장 높은 element를 뺀다 − How to implement it? With array ! POSCAT Seminar 1-40 9 July 2014 yougatup 5 3 4 8 3 7
  • 41.
    Priority Queue  우선순위가있는 Queue − Pop을 하면, 우선순위가 가장 높은 element를 뺀다 − How to implement it? With array ! Verification : Is this algorithm true ? Efficiency : What time does it take ? POSCAT Seminar 1-41 9 July 2014 yougatup
  • 42.
    Priority Queue  우선순위가있는 Queue − Pop을 하면, 우선순위가 가장 높은 element를 뺀다 − How to implement it? With array ! Verification : Is this algorithm true ? Trivial Efficiency : What time does it take ? O(n) for push & pop POSCAT Seminar 1-42 9 July 2014 yougatup
  • 43.
    Priority Queue  우선순위가있는 Queue − Pop을 하면, 우선순위가 가장 높은 element를 뺀다 − How to implement it? With array ! Verification : Is this algorithm true ? Trivial Efficiency : What time does it take ? O(n) for push & pop POSCAT Seminar 1-43 9 July 2014 yougatup Is there another algorithm faster ?
  • 44.
    Priority Queue  우선순위가있는 Queue − Pop을 하면, 우선순위가 가장 높은 element를 뺀다 − How to implement it? With array ! Verification : Is this algorithm true ? Trivial Efficiency : What time does it take ? O(n) for push & pop POSCAT Seminar 1-44 9 July 2014 yougatup Is there another algorithm faster ?  Use heap !
  • 45.
    Heap  Complete BinaryTree with Invarient − Complete Binary Tree − Inv : 부모노드의 값이 항상 자식 노드의 값보다 우선순위가 높음  Priority − Greater First  Max heap − Less First  Min heap  Operation − Push : Heap의 가장 끝에 추가시키고 Rearrange − Pop : Heap의 root를 빼고, Heap의 가장 끝에 있는 elemen를 root로 올린 후 Rearrange POSCAT Seminar 1-45 9 July 2014 yougatup
  • 46.
    Min Heap POSCAT Seminar1-46 9 July 2014 yougatup
  • 47.
    Min Heap POSCAT Seminar1-47 9 July 2014 yougatup 4 3 5 7 8 2 5 1
  • 48.
    Min Heap POSCAT Seminar1-48 9 July 2014 yougatup 4 3 5 7 8 2 5 1 Push 2
  • 49.
    Min Heap POSCAT Seminar1-49 9 July 2014 yougatup Push 2 24 3 5 7 8 2 5 1
  • 50.
    Min Heap POSCAT Seminar1-50 9 July 2014 yougatup Push 2 34 2 5 7 8 2 5 1
  • 51.
    Min Heap POSCAT Seminar1-51 9 July 2014 yougatup Push 2 // Done 34 2 5 7 8 2 5 1
  • 52.
    Min Heap POSCAT Seminar1-52 9 July 2014 yougatup Push 3 34 2 5 7 8 2 5 1
  • 53.
    Min Heap POSCAT Seminar1-53 9 July 2014 yougatup Push 3 34 2 5 7 8 2 5 1 3
  • 54.
    Min Heap POSCAT Seminar1-54 9 July 2014 yougatup Push 3 34 2 3 7 8 2 5 1 5
  • 55.
    Min Heap POSCAT Seminar1-55 9 July 2014 yougatup Push 3 // Done 34 2 3 7 8 2 5 1 5
  • 56.
    Min Heap POSCAT Seminar1-56 9 July 2014 yougatup Pop 34 2 3 7 8 2 5 1 5
  • 57.
    Min Heap POSCAT Seminar1-57 9 July 2014 yougatup Pop 34 2 3 7 8 2 5 1 5
  • 58.
    Min Heap POSCAT Seminar1-58 9 July 2014 yougatup Pop 34 2 3 7 8 2 5 5
  • 59.
    Min Heap POSCAT Seminar1-59 9 July 2014 yougatup Pop 34 2 3 7 8 2 5 5
  • 60.
    Min Heap POSCAT Seminar1-60 9 July 2014 yougatup Pop 34 2 3 7 8 2 5 5
  • 61.
    Min Heap POSCAT Seminar1-61 9 July 2014 yougatup Pop 34 2 3 7 8 2 5 5 2 has high priority !
  • 62.
    Min Heap POSCAT Seminar1-62 9 July 2014 yougatup Pop 34 2 3 7 8 2 5 5
  • 63.
    Min Heap POSCAT Seminar1-63 9 July 2014 yougatup Pop 34 2 3 7 8 5 5 2
  • 64.
    Min Heap POSCAT Seminar1-64 9 July 2014 yougatup Pop 34 2 3 7 8 5 5 2
  • 65.
    Min Heap POSCAT Seminar1-65 9 July 2014 yougatup Pop 34 2 3 7 8 5 5 2
  • 66.
    Min Heap POSCAT Seminar1-66 9 July 2014 yougatup Pop 34 5 3 7 8 2 5 2
  • 67.
    Min Heap POSCAT Seminar1-67 9 July 2014 yougatup Pop 34 5 3 7 8 2 5 2
  • 68.
    Min Heap POSCAT Seminar1-68 9 July 2014 yougatup Pop 34 5 3 7 8 2 5 2
  • 69.
    Min Heap POSCAT Seminar1-69 9 July 2014 yougatup Pop 54 3 3 7 8 2 5 2
  • 70.
    Min Heap POSCAT Seminar1-70 9 July 2014 yougatup Pop // Done 54 3 3 7 8 2 5 2
  • 71.
    Min Heap POSCAT Seminar1-71 9 July 2014 yougatup Pop // Done 54 3 3 7 8 2 5 2
  • 72.
    Min Heap Verification :Is this algorithm true ? Efficiency : What time does it take ? POSCAT Seminar 1-72 9 July 2014 yougatup
  • 73.
    Min Heap Verification :Is this algorithm true ? By invarient Efficiency : What time does it take ? O(log n) for push & pop  Think about height of the tree POSCAT Seminar 1-73 9 July 2014 yougatup