Poscat seminar 3-2

73
POSCAT Seminar 3-2 : Data Structure yougatup @ POSCAT 1

Transcript of Poscat seminar 3-2

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-29 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-39 July 2014

yougatup

( ( ) ( ( ) ) ( ) )

5 1 1 3 2 2 3 4 4 5

Parenthesis Matching

Use Stack !

− “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함

− ‘(‘은 Push, ‘)’ 은 Pop

POSCAT Seminar 1-49 July 2014

yougatup

0 1 2 3 4 5 6 7 8 9

( ( ) ( ( ) ) ( ) )

( 0

index

Parenthesis Matching

Use Stack !

− “나”와 “나의 짝” 사이에는 완전한 괄호가 있어야 함

− ‘(‘은 Push, ‘)’ 은 Pop

POSCAT Seminar 1-59 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-69 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-79 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-89 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-99 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-109 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-119 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-129 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-139 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-149 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-159 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-169 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-179 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-189 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-199 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-209 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-219 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-229 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-239 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-249 July 2014

yougatup

Binary Tree

POSCAT Seminar 1-259 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-269 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-279 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-289 July 2014

yougatup

Divide & Conquer !

1

left right

4, 2 5, 3, 7, 6

Preorder : 2 4Inorder : 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-299 July 2014

yougatup

Divide & Conquer !

1

left right

5, 3, 7, 6

Preorder : 2 4Inorder : 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-309 July 2014

yougatup

Divide & Conquer !

1

left right

5, 3, 7, 6

Preorder : 2 4Inorder : 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-319 July 2014

yougatup

Divide & Conquer !

1

left right

5, 3, 7, 6

Preorder : 2 4Inorder : 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-329 July 2014

yougatup

Divide & Conquer !

1

left right

Preorder : 2 4Inorder : 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-339 July 2014

yougatup

Divide & Conquer !

1

left right

Preorder : 2 4Inorder : 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-349 July 2014

yougatup

Priority Queue

우선순위가 있는 Queue

− Pop을 하면, 우선순위가 가장 높은 element를 뺀다

− How to implement it? With array !

POSCAT Seminar 1-359 July 2014

yougatup

5 3 4 1 3 2 7

Priority Queue

우선순위가 있는 Queue

− Pop을 하면, 우선순위가 가장 높은 element를 뺀다

− How to implement it? With array !

POSCAT Seminar 1-369 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-379 July 2014

yougatup

5 3 4 3 2 7

Priority Queue

우선순위가 있는 Queue

− Pop을 하면, 우선순위가 가장 높은 element를 뺀다

− How to implement it? With array !

POSCAT Seminar 1-389 July 2014

yougatup

5 3 4 3 2 7

Pop !

Priority Queue

우선순위가 있는 Queue

− Pop을 하면, 우선순위가 가장 높은 element를 뺀다

− How to implement it? With array !

POSCAT Seminar 1-399 July 2014

yougatup

5 3 4 3 7

Push 8

Priority Queue

우선순위가 있는 Queue

− Pop을 하면, 우선순위가 가장 높은 element를 뺀다

− How to implement it? With array !

POSCAT Seminar 1-409 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-419 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-429 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-439 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-449 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-459 July 2014

yougatup

Min Heap

POSCAT Seminar 1-469 July 2014

yougatup

Min Heap

POSCAT Seminar 1-479 July 2014

yougatup

4

3 5 7 8

2 5

1

Min Heap

POSCAT Seminar 1-489 July 2014

yougatup

4

3 5 7 8

2 5

1

Push 2

Min Heap

POSCAT Seminar 1-499 July 2014

yougatup

Push 2

24

3 5 7 8

2 5

1

Min Heap

POSCAT Seminar 1-509 July 2014

yougatup

Push 2

34

2 5 7 8

2 5

1

Min Heap

POSCAT Seminar 1-519 July 2014

yougatup

Push 2 // Done

34

2 5 7 8

2 5

1

Min Heap

POSCAT Seminar 1-529 July 2014

yougatup

Push 3

34

2 5 7 8

2 5

1

Min Heap

POSCAT Seminar 1-539 July 2014

yougatup

Push 3

34

2 5 7 8

2 5

1

3

Min Heap

POSCAT Seminar 1-549 July 2014

yougatup

Push 3

34

2 3 7 8

2 5

1

5

Min Heap

POSCAT Seminar 1-559 July 2014

yougatup

Push 3 // Done

34

2 3 7 8

2 5

1

5

Min Heap

POSCAT Seminar 1-569 July 2014

yougatup

Pop

34

2 3 7 8

2 5

1

5

Min Heap

POSCAT Seminar 1-579 July 2014

yougatup

Pop

34

2 3 7 8

2 5

1

5

Min Heap

POSCAT Seminar 1-589 July 2014

yougatup

Pop

34

2 3 7 8

2 5

5

Min Heap

POSCAT Seminar 1-599 July 2014

yougatup

Pop

34

2 3 7 8

2 5

5

Min Heap

POSCAT Seminar 1-609 July 2014

yougatup

Pop

34

2 3 7 8

2 5

5

Min Heap

POSCAT Seminar 1-619 July 2014

yougatup

Pop

34

2 3 7 8

2 5

5

2 has high priority !

Min Heap

POSCAT Seminar 1-629 July 2014

yougatup

Pop

34

2 3 7 8

2 5

5

Min Heap

POSCAT Seminar 1-639 July 2014

yougatup

Pop

34

2 3 7 8

5 5

2

Min Heap

POSCAT Seminar 1-649 July 2014

yougatup

Pop

34

2 3 7 8

5 5

2

Min Heap

POSCAT Seminar 1-659 July 2014

yougatup

Pop

34

2 3 7 8

5 5

2

Min Heap

POSCAT Seminar 1-669 July 2014

yougatup

Pop

34

5 3 7 8

2 5

2

Min Heap

POSCAT Seminar 1-679 July 2014

yougatup

Pop

34

5 3 7 8

2 5

2

Min Heap

POSCAT Seminar 1-689 July 2014

yougatup

Pop

34

5 3 7 8

2 5

2

Min Heap

POSCAT Seminar 1-699 July 2014

yougatup

Pop

54

3 3 7 8

2 5

2

Min Heap

POSCAT Seminar 1-709 July 2014

yougatup

Pop // Done

54

3 3 7 8

2 5

2

Min Heap

POSCAT Seminar 1-719 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-729 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-739 July 2014

yougatup