- ์ปดํ์ผ๋ฌ
LR ๊ตฌ๋ฌธ ๋ถ์
LL ๊ตฌ๋ฌธ ๋ถ์์ left-recursion ๋ฌธ์ ์ backtracking์ผ๋ก ์ธํ ์๋ ๋ฌธ์ ๋ก ์ธํด ์ค์ ์ปดํ์ผ๋ฌ ์ฌ์ฉํ๊ธฐ์ ๋ถ์ ์
๋ฐ๋ผ์ ์ 2๋ฌธ์ ๊ฐ ์๋ LR ํ์ฑ์ ์ฃผ๋ก ์ฌ์ฉ
LR์ L์ Left-to-Right๋ฅผ ์๋ฏธํ๋ฉฐ R์ Rightmost derivation์ ์ด์ฉํ๋ค๋ ๊ฒ์ ์๋ฏธํ๋ค.
LR ๊ตฌ๋ฌธ๋ถ์๋ฐฉ๋ฒ์ผ๋ก๋ SLR, CLR, LALR๋ฐฉ๋ฒ์ด ์๋ค.
SLR ๊ตฌ๋ฌธ๋ถ์ SLR ๊ตฌ๋ฌธ๋ถ์์ LR(0) ํญ๋ชฉ๊ณผ FOLLOW๋ฅผ ์ด์ฉํ์ฌ ํ์ฑํ ์์ฑ ํ์ฑํ ์์ฑ๋ฒ ๋์์
- Augmented grammar ์์ฑ(S' -> S)
- LR(0) ํญ๋ชฉ ๋์ด GOTO(I0, symbol)์ ๊ฒฝ์ฐ I0์งํฉ์์ symbol์ ์ฒ๋ฆฌํ ๊ฒฝ์ฐ์ ๋์ค๋ ์งํฉ์ผ๋ก I0์์ E ์ดํ์ dot symbol์ ์์นํ ํ CLOSURE๋ฅผ ๊ณ์ฐํ๋ค. ์๋ก ๋์ค๋ ์งํฉ์ ๋ํด์๋ In์ผ๋ก ์ด๋ฆ์ ๋ถ์ฌ์ฃผ๊ณ , ์๋ก์ด ์งํฉ์ด ๋์ค์ง ์์ ๋๊น์ง ๋ฐ๋ณตํ๋ค.
- ๋ชจ๋ non-Terminal symbol์ ๋ํด FOLLOW๋ฅผ ๊ณ์ฐํ์ฌ ๋์ดํ๋ค.
- SLR ํ์ฑํ์ GOTOํ ๋ถ๋ถ์ symbol์ด VN์ธ ๊ฒฝ์ฐ ๋ช๋ฒ ์งํฉ์ผ๋ก ์ด๋ํ๋์ง ํ์ํด์ค๋ค.
- ๊ตฌ๋ฌธ ๋ถ์๊ธฐ ํ๋์ ๊ฒฝ์ฐ
reduce๊ฐ ๊ฐ๋ฅํ(dot symbol์ด ๋ง์ง๋ง์ ์์นํ ๊ฒฝ์ฐ) reduce์ ํ๋ฉฐ E -> T์ธ ๊ฒฝ์ฐ FOLLOW(E)์ ํญ๋ชฉ๋ค์ reduce์์ฑํ๋ฉฐ ๋ช๋ฒ ์งํฉ์ ํตํด reduce ๋๋์ง ํ๊ธฐํ๋ค.
reduce๊ฐ ๋ถ๊ฐ๋ฅํ ๊ฒฝ์ฐ shiftํ๋ฉฐ GOTO(์ํ, VT)์ ์งํฉ ๋ฒํธ๋ฅผ ๊ฐ์ด ๊ธฐ์ฌํ๋ค.
1๋ฒ ์ํ์ $๋ ํญ์ acc์ด๋ค.
FOLLOW๋ ํด๋น symbol ๋ค์์ ๋์ค๋ ๋ชจ๋ Terminal Symbol์ set์ด๋ค.
๋ด์ผ๋ถํฐ๋ ์ฝ๋ ์์ฑ ์์ํด์ผํ๋ค!
- MarkDown Language
header(์ ๋ชฉ) h1 ~ h6 '#'์ผ๋ก ํ์ (์์: "###" h3)
bold ๋ string ์ผ๋ก ํ์
br๋์ space 2์นธ์ผ๋ก ํ์ฉ๊ฐ๋ฅ
"[๋งํฌ ํ์๋ ์ ๋ชฉ] (๋งํฌ)" ์ฝ๋์ ๊ฒฝ์ฐ ```๋ก ์ฝ๋ ์ข ๋ฅ ๋ช ์ ํ ๊ฐ์ธ์ฃผ๊ธฐ
์ธ์ฉ๋ฌธ์ ๊ฒฝ์ฐ > ์ด์ฉ
- Dijkstra Algorithm
์ค๋๋ง์ ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ ๋ฌธ์ ๋ฅผ ์ ํ๊ฒ ๋์๋ค. ์๋ ์๋ ํ์ฐธ ์ข ๋ง๋ถ์ ๋ณด๋ฉฐ ์ฐ์ตํ์๋๋ฐ, ์๋ฌด๋ฐ ๊ณต๋ถ์์ด ๋ฌธ์ ๋ฅผ ๋ง๋ฑ๋๋ฆฌ๊ฒ ๋๋ ๋จธ๋ฆฟ์์ด ํ์์ก๋ค.
๋ฆฌํธ๋จธ์ค ๋ฌธ์ ๋ฅผ ํ๊ธฐ ์ํด์ psuedo code๋ฅผ ๋ณด๊ธฐ๋ ํ์์ผ๋ ๊ธฐ์กด์ ์ด์ฉํ๋ ์ฝ๋์ ๋ฌ๋ผ ์์ฑํ๊ธฐ ํ๋ค์๋ค.
๋ฌผ๋ก ๋ฌธ์ ์ ์ค๋ฅ๊ฐ ์์ด์ ์๊ฐ์ ๋ชจ๋ ์ฌ์ฉํ๋ค.
๋ค์ต์คํธ๋ผ๊ฐ ๋์จ๊น์ ํ๋ฒ ๋ํ์ ์ธ ๋ฌธ์ ์ ์ฝ๋๋ฅผ ๋ณด์.
vector์ priority_queue๋ฅผ ์ด์ฉํด์ผ ๊ฐ์ฅ ํจ์จ์ ์ด๋ค.
#include<iostream>
#include<vector>
#include<queue>
#define INF 1e9
using namespace std;
int main()
{
int V,E;
scanf("%d %d", &V ,&E); //๋
ธ๋์ ๊ฐฏ์์ ์ฃ์ง์ ๊ฐฏ์๋ฅผ ์
๋ ฅ๋ฐ์ต๋๋ค.
int start;
scanf("%d",&start); //์์์ ์ ์
๋ ฅ๋ฐ์ต๋๋ค.
vector<pair<int,int> > arr[V+1];
for(int i=0;i<E;i++){
int from,to,val;
scanf("%d %d %d", &from , &to,&val); //๊ทธ๋ํ ์์ ์ฃ์ง๋ค์ ๋ํ ์ ๋ณด๋ฅผ ์
๋ ฅ๋ฐ์ต๋๋ค.
arr[from].push_back({to,val});
}
int dist[V+1]; //์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐฑ์ ํด์ฃผ๋ ๋ฐฐ์ด์
๋๋ค.
fill(dist,dist+V+1,INF); //๋จผ์ ๋ฌดํ๋๋ก ์ ๋ถ ์ด๊ธฐํ๋ฅผ ์์ผ๋ก๋๋ค.
priority_queue<pair<int,int> > qu;
qu.push({0,start}); //์ฐ์ ์์ ํ์ ์์์ ์ ๋ฃ์ด์ค๋๋ค.
dist[start]=0; //์์์ ์ ์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐฑ์ ํฉ๋๋ค.
while(!qu.empty()){
int cost=-qu.top().first; // cost๋ ๋ค์ ๋ฐฉ๋ฌธํ ์ ์ dist๊ฐ์ ๋ปํฉ๋๋ค.
int here=qu.top().second; // here์ ๋ฐฉ๋ฌธํ ์ ์ ๋ฒํธ๋ฅผ ๋ปํฉ๋๋ค
qu.pop();
for(int i=0; i<arr[here].size(); i++){
int next=arr[here][i].first;
int nextcost=arr[here][i].second;
if(dist[next] > dist[here] + nextcost){
//ํ์ฌ next์ ์ ์ฅ๋ dist์๊ฐ๋ณด๋ค ํ์ฌ์ ์ ์ ๊ฑฐ์ณ์ ๊ฐ ๊ฒฝ์ฐ๊ฐ
// ๊ฑฐ๋ฆฌ๊ฐ ๋์งง์ผ๋ฉด ๊ฐฑ์ ํด ์ฃผ๊ณ ํ์ ๋ฃ์ต๋๋ค.
dist[next]=dist[here]+nextcost;
qu.push({-dist[next],next});
}
}
}
for(int i=1;i<=V;i++){
printf("%d\n", dist[i]);
}
}
- ์ปดํ์ผ๋ฌ
C๋ก ์์ฑ๋ first, follow ์ฝ๋๋ ์ฐพ์์ผ๋,
์ด๋ฅผ C++๋ก ์์ ํด์ผํ๊ณ ํ์ผ ์ ์ถ๋ ฅ๋ ๋ฃ์ด์ผํ์ง๋ง ํ์ง ๋ชปํ๋ค....
๋ด์ผ์ ์ ๋๋ก ์ง์คํด์ ์ปดํ์ผ๋ฌ์๋ง ์๊ฐ์ ํ ์ ํด์ผ๊ฒ ๋ค.
์ค๋์ ํ๋ฃจ์ข ์ผ ์ปดํ์ผ๋ฌ์ ์ง์คํ๋ค.
- ์ปดํ์ผ๋ฌ
ํ์ผ์ ์ถ๋ ฅ์ ํตํด ๋ฌธ๋ฒ์ ์ ๋ ฅ๋ฐ๊ณ , ์ฃผ์ด์ง ๋ฌธ๋ฒ์ VN์ First์ Follow๋ฅผ ๊ณ์ฐํ์ฌ ์ถ๋ ฅํ๋ ์์ ์ ํ์๋ค. ๋ฌผ๋ก First์ Follow๋ ์ฒ์๋ถํฐ ๊ตฌํ์ ํ๋ค์ด github์ ๋ค๋ฅธ ์ฝ๋๋ฅผ ์ฐธ์กฐํ์๋ค.
ํ์ผ์ ์ถ๋ ฅ์ifstream
์ ํตํด ์์ ํ์์ผ๋ฉฐ,getline
์ ์ฌ์ฉํ์๋ค.
ifstream syntax;
syntax.open("syntax.txt");
if(syntax.is_open()) {
while(!syntax.eof()) {
syntax.getline(char[]name, maxlength); //์ฃผ์ํ ์ ์ name์ charํ๋ง ๊ฐ๋ฅ
}
}
๋ํ ๊ธฐ์กด ์ฝ๋์ ์ฒ๋ฆฌ๋์ด์์ง ์์ 2๊ฐ์ง์ ๋ฌธ๋ฒ์ or๋ก ๊ตฌ๋ถํ์ฌ ์์ฑ๋์ด ์๋ ๋ฌธ๋ฒ์ ์ฒ๋ฆฌํ์๋ค.
from (ex. A ==> B || C)
to (A ==> B) & (A == > C)
์ ๋ฌธ์ฅ์ 2๊ฐ์ ๋ฌธ์ฅ์ผ๋ก ๋ถํ ํ์ฌ production rule์ ์ถ๊ฐํ์๋ค.
SLR parser๋ฅผ ๊ตฌํํ๊ธฐ ์ํด์๋ ์์ผ๋ก CLOSURE
์ GOTO
ํจ์ ๊ตฌํ์ด ํ์ํ๋ค.
์ค๋์ ์ฆ๊ฐ๋ฌธ๋ฒ (augmented grammar)๋ ์์ฑํ์๋ค.
ํ์ฌ ํ์๋ ์ธ๋ฑ์ค ๊ตฌ์กฐ๋ก ๋์ด์๊ธฐ ๋๋ฌธ์ ๋ณต์กํ ๋ฌธ๋ฒ์ ์ฒ๋ฆฌํ์ง ๋ชปํ๋ค. ํนํ๋ ํฐ๋ฏธ๋ ์ฌ๋ณผ์ด ๋ ๋ฌธ์ ์ด์์ผ๋ก ๊ตฌ์ฑ๋์ด ์๋ ๊ฒฝ์ฐ ์ฒ๋ฆฌํ์ง ๋ชปํ๊ธฐ ๋๋ฌธ์ ๋ฌธ์ ๊ฐ ํฌ๋ค.
์์ผ๋ก ํด๊ฒฐํด์ผํ ๋ฌธ์ ์ด๋ค.
- OS / Main memory
Memory allocation method์ ๋ํด ์์๋ณด์๋ค. ๊ทธ ์ค ์๋ 3๊ฐ์ง์ ๋ํด ์์ธํ ์์๋ณด์๋ค.
- Contiguous Allocation
- Segmentation
- Paging
์ฃผ๋ก ๋ฉ๋ชจ๋ฆฌ ํ ๋น์ ๊ด๋ จํด์๋ 2๊ฐ์ง ํญ๋ชฉ์ ํ๊ฐํ๋ค.
Utilization(ํ์ฉ๋) & Performance(Address Translation ์์ ์๊ฐ)
Contiguous
๋ ๊ฐ์ฅ ํจ์จ์ ์ด์ง ๋ชปํ allocation policy์ด๋ค.
์ฐ์์ ์ผ๋ก ๋ฌด์กฐ๊ฑด ํ ๋นํด์ผํ๋ฏ๋ก Internal & External fragmentation์ด ๋งค์ฐ ํฌ๋ค.
๋ํ swap์ด ๋ฐ์ํ ๊ฒฝ์ฐ ์ ์ผํ๊ฒ ๋ฐ์ด๋ Performance๋ ์ ํ๋๋ค.
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ๋์จ ๊ฒ์ด Segmentation
์ด๋ค. ์ด๋ Logical Address Space๋ฅผ ๋ถํ ํด ๋ฉ๋ชจ๋ฆฌ์ ๋ก๋ ํ ์ ์๋๋ก ํ ๊ฒ์ธ๋ฐ, ์ด๋ฌํ ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์์ ๊ฐ๋ฐ์๋ค์ด ์ด์ฉํ์ง ์์ ์ฌ์๋ฌ๋ค. ๋ํ Address Translation์ ์ค๋ ์๊ฐ์ด ๊ฑธ๋ฆฐ๋ค.
๋ค์ ํ๋ฒ ๊ฐ์ ๋ ๊ฒ์ด ํ์ฌ์๋ ์ฐ์ด๋ Paging
๊ธฐ๋ฒ์ด๋ค.
๊ทธ๋ฌ๋ ํด๋น ๊ธฐ๋ฒ์๋ ๋ฌธ์ ์ ์ด ์๋ค.
Page Table์ ์ฐธ์กฐํ๊ธฐ ์ํด Memory์ 2๋ฒ Accessํด์ผ ํ๋ ๋ฌธ์ ๊ฐ ์๊ธด๋ค.
์ด๋ TLB Cache๋ฅผ ์ด์ฉํ์ฌ ํด๊ฒฐํ๋ค. TLB hit ratio๊ฐ ์ถฉ๋ถํ ๋ณด์ฅ๋๋ ๊ฒฝ์ฐ ์ฑ๋ฅ ์ ํ๊ฐ ๊ฑฐ์ ๋ฐ์ํ์ง ์๊ณ
Utilitzation์ page table์ hieraricial page table์ ๊ตฌ์ฑํ์ฌ ๋์ผ ์ ์๋ค.
- OS / Virtual memory
Virtual Memory๋ ๊ธฐ์กด์ ํ๋ก์ธ์ค์์ ์๊ตฌํ๋ 100%์ page์ frame์ ํ ๋นํด ๊ด๋ฆฌํ๋ ๊ฒ์ด ์๋๋ผ ์ผ๋ถ ํ์ํ ๋ถ๋ถ์ ํ ๋น์ ํ์ฌ utilitzation์ ๊ทน๋ํ ํ ์ ์๋ ๋ฐฉ๋ฒ์ด๋ค.
์ด๋ฅผ ์ํด์๋ LAS์ PAS ์ฌ์ด์ Virtual Address Space๋ฅผ ์ด์ฉํ๋ค.
๋ก๋๋ ์ด์ธ์ ์์ญ์ storage device์ธ backing store์ ์ ์ฅํ๋ค.
๋ก๋๋์ง ์์ ๋ฉ๋ชจ๋ฆฌ ์์ญ์ page๋ฅผ ์์ฒญํ๋ฉด page fault๊ฐ ๋ฐ์ํ๋ฉฐ swap์์ญ ๋ํ program image์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง๊ณ ์จ๋ค. ๋ฌผ๋ฆฌ ๋ฉ๋ชจ๋ฆฌ๊ฐ ๋ถ์กฑํ ๊ฒฝ์ฐ ํ์ฌ ์ ์ฌ์ฉ๋์ง ์๋ page๋ฅผ ๊ณจ๋ผ(๊ณ ๋ฅด๊ณ ๊ต์ฒดํ๋ ์์
์ ์ํ) page replacement๋ฅผ ์ค์ํ๋ค.
page replacement policy๋ demand paging(ํ์ด์ง๊ฐ ํ์ํ ๊ฒฝ์ฐ ์ฆ๊ฐ ๋ก๋ํ๋) ์ฌ์ฉ ์ Belady's Anomaly
๋ฅผ ๋ณผ ๊ฒฝ์ฐ ์ค์ํ๋ค๋ ๊ฒ์ ์ ์ ์๋ค.
(ํฌํค๊ฐ ์ปค์ ธ๋ page replace๊ฐ ์ ๋๋ก ์๋๋ฉด page fault ๋ฐ์ ํ์๊ฐ ์ฆ๊ฐ)
์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด LRU Approximation Algorithm์ ์ด์ฉํ๋ค. Enhanced Second-Chance Algorithm์ด ์ต์ ์ ๊ธฐ์ ์ด๋ค.
Take ordered pair (reference, modify)
- (0, 0) neither recently used not modified โ best page to replace
- (0, 1) not recently used but modified โ not quite as good, must write out before replacement
- (1, 0) recently used but clean โ probably will be used again soon
- (1, 1) recently used and modified โ probably will be used again soon and need to write out before replacement
์ ์์ ์ ํตํด circular queue๋ฅผ ์ด์ฉํด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ค.
- Data Communication ์ ๋ฆฌ
-
Network Models
5 ๊ณ์ธต ์ธํฐ๋ท ๋ชจ๋ธ์์ Transport : TCP / Network : IP๋ง ์ฌ์ฉํ๋ฉด ์ ๋ถ Interntet์ด๋ค.
๊ฐ ๊ณ์ธต๋ณ ์ญํ ์ ์ดํด๋ณด์
- ๋ฌผ๋ฆฌ ๊ณ์ธต(Physical layer) : ๊ฐ๋ณ ๋นํธ๋ฅผ ์ธ์ ๋ ธ๋์ ์ ๋ฌํ๋ ์ญํ , ์ ๋ก์ ๋ฌผ๋ฆฌ์ ํน์ฑ๊ณผ ์ ์ก๋ฐฉ์์ ๋ค๋ฃธ
- ๋ฐ์ดํฐ ๋งํฌ ๊ณ์ธต(Data Link layer) : ์์ ์ ์ธ Next-hop deilivery ์ญํ ์ ํจ. ๋ฐ์ดํฐ๋ฅผ Frame ๋จ์๋ก ์ ์กํ๋ Framing, Physical Addressing(MAC ์ฃผ์) ๋ฑ๋ฑ. ํ์ฌ๋ ์ฃผ๋ก Ethernet์ ๊ธฐ๋ฐ์ผ๋ก ํจ
- ๋คํธ์ํฌ ๊ณ์ธต(Network layer) : Source-to-Destination ์ ์ก์ ์ฑ ์. ์๋ก ๋ค๋ฅธ ๋คํธ์ํฌ ๊ฐ ์ ์ก๋ ์ฑ ์(Routing). ํ์ฌ๋ ์ฃผ๋ก IP ํ๋กํ ์ฝ ์ด์ฉ.
- ์ ์ก ๊ณ์ธต(Transport layer) : Process-to-Process delivery๋ฅผ ์ฑ ์. ์ฃผ๋ก port addressing ์์ ์ ์ด์ฉ
- ์์ฉ ๊ณ์ธต(Application layer) : ์ด๋ ํ ์๋น์ค์ ์ด๋ค ํ๋กํ ์ฝ์ ์ด์ฉํด ์ ์กํ ๊ฒ์ธ์ง ๊ฒฐ์ . ์ฌ์ฉ์์๊ฒ ์๋น์ค๋ฅผ ์ ๊ณต.
-
Signals & Transmission
data๊ฐ ์ ์ก๋๊ธฐ ์ํด์ ์ ์๊ธฐ์ ์ ํธ๋ก ๋ณํ์ด ๋์ด์ผํ๋ค.
๋ฐ์ดํฐ ํต์ ์์๋ ์ฃผ๋ก **์ฃผ๊ธฐ์ ์๋ ๋ก๊ทธ ์ ํธ(periodic analog signal)๊ณผ ๋น์ฃผ๊ธฐ์ ๋์งํธ ์ ํธ(aperiodic digital signal)**์ ์ฌ์ฉํ๋ค.-
Analog Signal
Fourier transform : any composite signal => a combination of simple sine waves with different freq, phase, and amplitude
ex) Square wave = ๋ฌดํํ ๋ง์ Sine wave๋ฅผ ๋ฐ๋ณตํ๋ฉด ๋ง๋ค์ด๋ผ ์ ์๋ค.
๊ทธ๋ฌ๋ medium์ ๋ฐ๋ผ ์ผ๋ถ ์ฃผํ์๋ง ํต๊ณผ์ํค๋ฉด ๋ณํ์ด ๋ ์ ์๋ค.BandWidth : medium์ด ์ ์์ ์ผ๋ก(์ถ๋ ฅ์ half ์ด์ ์์ค ์์ด) ํต๊ณผ์์ผ์ค ์ ์๋ ์ฃผํ์ ๋ฒ์ (highest - lowest)
๋ฐ๋ผ์ Analog Signal์ ํ์ธํ ๋๋ ์๋ 2๊ฐ์ง๋ฅผ ํ์ธํด์ผํจ
- Medium Bandwidth๊ฐ ์ ์กํ๋ ค๋ Signal์ Bandwidth๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ์์ง
- Medium์ด ํต๊ณผ์ํค๋ ์ฃผํ์๊ฐ Signal์ ์ฃผํ์ ๋ฒ์์ ๋ง๋์ง
-
Digital Signal
Analog์์์ period = Bit interval, freq = Bit rate
Digital Signal์ ๊ธฐ๋ณธ์ ์ผ๋ก infinite frequency๋ฅผ ๊ฐ์ง
๋ฐ๋ผ์ Digital Signal ์๋ณธ์ ์ก์์ ํ๊ธฐ ์ํด์ Wide-Band medium์ด ํ์ํ์ง๋ง ๊ฐ๊ฒฉ์ด ๋น์
Nyquist therom
๊ณผShannon capacity
์ ๋ฐ๋ฅด๋ฉด band limited medium์๋ ์ ์ก์ด ๊ฐ๋ฅํ๋ค.Nyquist Therom
Medium BandWidth >= (number of bps) / 2
๋จ 1๋นํธ๊ฐ ์๋ n๋นํธ๋ฅผ ์ ํธ ํ๋์ ์ค์ด ๋ณด๋ผ ๊ฒฝ์ฐ bps๋ ์ฆ๊ฐ๊ฐ๋ฅ -
Analog transmission vs. Digital transmission
- low-pass channel : ํน์ freq. 0 ์ด์ f ์ดํ์ bandwidth๋ฅผ ๊ฐ์ง๋ channel
- band-pass channel : ํน์ freq f1 ~ f2์ bandwidth๋ฅผ ๊ฐ์ง๋ channel
Digital transmission์ Digital signal์ ์ ์กํ๋ฏ๋ก ์ด๋ก ์ ์ผ๋ก 0 ~ infinity์ ๋์ญํญ(bandwidth)๊ฐ ํ์ํ๋ฏ๋ก low-pass channel์ด ํ์ํจ
๋ฐ๋ฉด์ Analog Transmission์ low ๋๋ band-pass์์ ์ฌ์ฉ๊ฐ๋ฅ ๋ํ B/W ๋์ญ์ ์ชผ๊ฐค ์ ์์
- Noiseless channel =>
Nyquist Therom
Bitrate in bps = 2 * Bandwidth * log2L
L : numbers of signal levels- Noisy channel =>
Shannon capcity
Capacity in bps = bandwidth * log2(1+SNR)
SNR : Powersignal / PowernoiseChannel Capacity๋ณด๋ค data rate๋ฅผ ๋๊ฒ ์ค์ ํ๋ ๊ฒ์ ๋ถ๊ฐ๋ฅํ๋ค.
-
-
Transmission Impairment(์์)
- Attentuation(๊ฐ์ ) : medium์ ์ ํญ์ผ๋ก ์ธํด ์์ค๋๋ ์๋์ง / amplifier๋ฅผ ํตํด ํด๊ฒฐ
dB = 10log10(P2/P1)
+์ด๋ฉด ์๋์ง ์ฆํญ, -์ด๋ฉด ์๋์ง ๊ฐ์ / ๋จ์ ๋ง์ , ๋บ์ ๊ฐ๋ฅ - Distortion(์๊ณก) : ๊ฐ๊ฐ์ signal์ ์ ํ ์ง์ฐ ์๋๊ฐ ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ ์ ํธ๊ฐ ์๊ณก๋ ์ ์์
- Noise(์ก์) : ์จ๋, ์ถฉ๊ฒฉ, ๋ํ(crosstalk), ์ ๊ธฐ์ ์ถฉ๊ฒฉ์ ์ํด ์ ํธ๊ฐ ์์๋ ์ ์์
- Attentuation(๊ฐ์ ) : medium์ ์ ํญ์ผ๋ก ์ธํด ์์ค๋๋ ์๋์ง / amplifier๋ฅผ ํตํด ํด๊ฒฐ
-
Digital Transmission
binary data => digital signal ๋ณํํ๋ ๊ณผ์ ์ Line Coding์ด๋ผ๊ณ ํจ-
Signal Level & Data Level
- Signal Level : signal์์ ๊ตฌ๋ถํ ์ ์๋ ๋จ๊ณ ์
- Data Level : data์์ ๊ตฌ๋ถํ ์ ์๋ ๋จ๊ณ ์
-
Pulse rate & Bit rate
- Pulse : symbol ํ๋๋ฅผ ์ ์กํ๋๋ฐ ๊ฑธ๋ฆฌ๋ ์ต์ ์๊ฐ
- pulse rate : ์ด๋น pulse์ ์ ์ก ๊ฐ์
- bit rate = pulse rate * log2L
L : number of data levels
-
DC components : Amplitude๊ฐ ์๊ฐ์ ์ผ๋ก 0์ด ๋๋ ์๊ฐ์ ์ ํธ๋ก์ ํํํ ์ signal
์ถ๊ฐ ์๋์ง๋ฅผ ํ์๋ก ํ๊ธฐ ๋๋ฌธ์ ๊ตฌํ์ด ์ฝ์ง ์์ -
Self-syncrhonization(๋น๋๊ธฐ)
- Unipolar encoding : 1๊ฐ์ voltage level์ ์ด์ฉํ์ฌ ๊ฐ๋จํ์ง๋ง, dc componet ๋ฌธ์ ์ ๋๊ธฐํํ ์ ์๋ค๋ ์ ์ด ๋จ์ ์ด๋ค.
- Polar encoding : 2๊ฐ์ voltage level(+/-, signal level)์ ์จ dc component๋ ์์ง๋ง, ๋๊ธฐํ ๋ฌธ์ ์กด์ฌ
- NRZ-L : data 0์ positive voltage, 1์ negative voltage๋ฅผ ์ก์์
๋ฐ๋ณต๋ 0 ๋๋ 1 data๋ ๋๊ธฐํ ๋ฌธ์ ๋ฅผ ์ ๋ฐ์ํจ๋ค. - NRZ-I : data 0์ ๊ธฐ์กด ์ ์ ์ ์ง, 1์ ์ ์ ๋ฐ๋๋ก ์ ํ
NRZ-L๋ณด๋ค ๋์ ๋๊ธฐํ ์ฑ๋ฅ - RZ : data์ ๋ง๋ ์ ํธ๋ฅผ ๋ณด๋ธ ํ 0 voltage๋ก ํญ์ ์ ํํ์ฌ syncronization์ ์ค์
๋จ, ์ฆ์ phase ๋ณํ๋ก frequncy ์ฆ๊ฐ -> ๋ง์ bandwidth ์๋ชจ - Manchester : RZ์์ 0 voltage ๋์ ์ค๊ฐ voltage transition์ ์ด์ฉํด b/w ์ ์ฝ
- Differential Manchester : Manchester ๋ฐฉ์์ NRZ-I ๋ฐฉ์์ ํฉํจ. data๊ฐ 0์ด๋ฉด transition, 1์ด๋ฉด no transition์ด๋ค.
- NRZ-L : data 0์ positive voltage, 1์ negative voltage๋ฅผ ์ก์์
- Bipolar encoding : 3๊ฐ์ Signal Level์ ๊ฐ์ง
- AMI : 1์์๋ง signal inverse
NRZ-L๊ณผ ๊ฐ์ ๋จ์ ์กด์ฌ - BnZS : ๋ค๋ฅด๊ฒ ์ธ์ฝ๋ฉํ๋ค...
์์ธํ ์ค๋ช ์ ์์์
- AMI : 1์์๋ง signal inverse
- 2B1Q : 2๊ฐ์ ๋นํธ, 4๊ฐ์ signal level
- MLT-3 : NRZ-I์ ๋์ผํ์ง๋ง, 3๊ฐ์ signal level ์ฌ์ฉ
-
Block Coding : ์ ์ก ๋นํธ๋ฅผ ๋๋ ์ ๋ธ๋ก ๋จ์๋ก syncronization ๋ฌธ์ ๋ฐ์ํ์ง ์๋ ๋นํธ๋ก ๋งคํ
- 4B/5B : 4bit๋ฅผ 5bit๋ก ์ธ์ฝ๋ฉ, ์์๋ 0์ด ์ต๋ 1๊ฐ, ๋ค์๋ 0์ด ์ต๋ 2๊ฐ๋ง ์ค๋ณตํจ์ผ๋ก์ 3๊ฐ ์ด์์ 0์ ์ฐ์๋์ง ์๋๋ก ํจ
- 8B/10B : 8bit๋ฅผ 10bit๋ก
- 8B/6T : 8bit๋ฅผ 3๊ฐ์ signal level์ ๊ฐ์ง 6๊ฐ์ signal 36๋ก
-
Analog data๋ฅผ Digital ์ ์กํ๊ธฐ ์ํด์๋ data sampling์ด ํ์
-
์ดํ ๋ด์ฉ์ ๋ด์ผ ์ด์ด์...
-
-
-
data communication ์ ๋ฆฌ (cont.)
- digital transmission
- sampling
- PAM(Pulse Amplitude Modulation) : analog data์ ํํ์ ๊ทธ๋๋ก digital๋ก ๋ฐ๋ผํ ๊ฒ
- PCM(Pulse Code Modulation)
PAM => Quantization => binary encoding => line coding
sampling rate๋ ์๋ ์ ํธ์ ์ต๋ ์ฃผํ์์ 2๋ฐฐ ์ด์์ผ๋ก ํด์ผํ๋ค!
band-pass signal์ low-pass signal๋ก ๋ณํํ์ฌ sampling ํ ๊ฒฝ์ฐ signal bandwidth์ 2๋ฐฐ๋ฅผ sampling rate๋ก ์ก์ผ๋ฉด ๋๋ค.
๊ฐ sample ๋น ํ์ํ bit ์๋ ceiling(log2N)๊ฐ ์ด๋ค.
๋ฐ๋ผ์ bit rate = sampling rate * number of bits per sample
- Transmission mode
- Parallel : ๊ณ ์์ด๋ ์ ๋ก ๋ถ๋ด์ด ์ปค ๋น์ฉ์ด ๋น์, ์ฃผ๋ก ์งง์ ๊ฑฐ๋ฆฌ์ ์ด์ฉ
- Serial
: ๋๊ธฐ์์คํ
๊ณผ ๋น๋๊ธฐ์์คํ
์ผ๋ก ๋๋๋ค.
- ๋น๋๊ธฐ ์์คํ ์ ์ ํธ์ ์๊ธฐ๋ ์์๊ฐ ์ค์ํ์ง ์์ ์์คํ . ์์๋นํธ, ์ข ๋ฃ๋นํธ๋ก ๋ฐ์ดํธ ๋จ์๋ฅผ ๊ตฌ๋ถํด ์ ์ก. ์ ๋ ดํ๋ฉฐ ํจ์จ์ ์ด์ง๋ง ๋๋ฆฌ๋ค.
- ๋๊ธฐ ์์คํ ์ ๋ฐ์ดํธ ๋จ์ ๊ตฌ๋ถ ์์ด ์ ์กํ๋ค. ๋ฐ๋ผ์ ๋ฐ์ดํธ ๋จ์์ ์์๊ฐ ์ค์ํ๋ค.
- Analog transmission
-
Terminology
- modulation : digital/analog data -> band-pass analog signal
- baud : signal unit / baud rate : # of signal units per second
- carrier signal : ๊ธฐ์ค ์ฃผํ์ (+ ์ ์ ์ ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ์ฌ ์ ์ก)
-
ASK(Amplitude Shift Keying) : ๋จ์ํ ์ ํธ์ ์ธ๊ธฐ๋ฅผ ๋ณ์กฐํด ๋ฐ์ดํฐ์ 0/1์ ํ์ํ๋ ๋ฐฉ๋ฒ
b/w = baud rate์ ๋์ผํ ๋งํผ ํ์(f-Nbaud/2 ~ f+Nbaud/2)
ASK์์ baud rate์ bit rate๊ฐ ๋์ผ
๋จ์ํ์ง๋ง noise์ ์ฝํจASK full-duplex ๋ฌธ์ ์ฐธ๊ณ .
-
FSK(Frequency Shift Keying) : 2๊ฐ์ ์ฃผํ์๋ฅผ ์ด์ฉํด ์ฃผํ์์ ๊ฐ 0, 1์ ํ ๋นํ์ฌ ๋ณ์กฐํ๋ ๋ฐฉ๋ฒ
FSK์ ํน๋ณํ ์ ํฉํ ์ฃผํ์๊ฐ ์กด์ฌ
b/w = fc1 - fc2 + Nbaud(baud-rate)
FSK์์๋ baud rate์ bit rate๊ฐ ๋์ผ -
PSK(Phase Shift Keying) : signal์ phase์ ๋ฐ๋ผ ๋ฐ์ดํฐ๋ฅผ ๋ณ์กฐํ๋ ๋ฐฉ๋ฒ
Minimum b/w = Nbaud
PSK์ ๊ฒฝ์ฐ baud rate์ bit rate๊ฐ ๋์ผํ์ง ์์์๋ ์์์ผ๋ก ์ ์ (21baud to bit-PSK) -
QAM(Quadrature Amplitude Modulation)
: ASK + PSK ๋ฐฉ์
PSK์ ๋์ผํ๊ฒ **(21baud to bit-QAM)**์ผ๋ก ํ๊ธฐ -
Modulation of analog signal
- AM : total required b/w = audio signal * 2
- FM : total required b/w = audio signal * 10
-
- chapter 1 Preliminaries
- Programming Domains
- Scientific application : Fotran - ๋ง์ ์์ ๋ถ๋์์์ ๊ณ์ฐ๊ณผ ๋ฐฐ์ด ํ์ฉ
- Business application : COBOL - ๋ ํฌํธ ์์ฑ, ์ญ์ง์์ ๋ฌธ์ ์ฌ์ฉ
- Artificial application : LISP - ์ซ์ ์กฐ์๋ณด๋ค๋ ๊ธฐํธ๋ฅผ ์ด์ฉ, ๋งํฌ๋ ๋ฆฌ์คํธ์ ํ์ฉ
- System programming : C - ์ง์์ ์ฌ์ฉ์ผ๋ก ์ธํ ํจ์จ์ฑ ๊ทน๋ํ ํ์
- Web software : HTML, PHP, Java - ํธํ์ฑ ํ์
- Language Evaluation criteria
Readability
: ๊ฐ๋ ์ฑ, ๊ฐ๋จํ๊ณ ์ง๊ต์ฑ(์ ์ ์์ ๊ตฌ์กฐ์ฒด๋ฅผ ๊ฐ์ง๊ณ ํ๋ก๊ทธ๋๋ฐ)์ ๊ฐ์ง๋ ์ง, ์ ์ ํ data types๋ฅผ ๊ฐ์ง๊ณ ์๋์งWritability
: ํํ์ฑ (๊ฐ๋ ์ฑ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ์ฝ๊ฐ ์ฃผ๊ด์ ), ์ถ์ํ์ ๋ํ ์ง์์ด ์๋์ง, ํํ์ด ์ผ๋ง๋ ํธ๋ฆฌํ์งReliability
: ์ ๋ขฐ์ฑ, language๊ฐ typesafe ํ์ง ์ฌ๋ถ๊ฐ ๋ํ์ , exception handling๊ณผ aliasing ์ง์ ์ฌ๋ถCost
: ์ปดํ์ผ๋ฌ ์คํ ์๊ฐ + ์คํ ์๋ + ์ ์ง๋ณด์ ๋น์ฉ + ... ๋ค์ํ ์๊ฐ, ๋น์ฉ ๊ณ์ฐ
- Language Design : ์ฌ๋์ด ๋ณํํ ๋, ์ปดํจํฐ ๊ตฌ์กฐ๊ฐ ๋ณํํ ๋, ํจ๊ป ๋ณํํ๋ค.
- ๋๋ถ๋ถ์ ์ปดํจํฐ ์ธ์ด์ ๊ตฌ์กฐ๋ ํฐ ๋ ธ์ด๋ง ๊ตฌ์กฐ์ ์ํฅ์ ํฌ๊ฒ ๋ฐ์
- ๋ช ๋ นํ ์ธ์ด์ ๊ธฐ๋ฐ : ๋ณ์ => ๋ฉ๋ชจ๋ฆฌ ์ ๋งคํ, assignment statement, iteration์ ๊ฐํจ
- Programming methodology
- 1950s ~ early 1960s : H/W์ธ ๊ธฐ๊ธฐ์ ํจ์จ์ฑ์ ์ค์ - ์ด์ ๋ธ๋ฆฌ์ด
- late 1960s : ์ฌ๋์ ํจ์จ์ฑ์ ์ ์ฐจ ์ค์ํ๊ฒ ๋๋ฉฐ ๊ฐ๋ ์ฑ, ๋ ๋์ control structure ๋ฅผ ์ํ๊ฒ ๋จ. ๊ตฌ์กฐํ๋ ํ๋ก๊ทธ๋๋ฐ, top-down ๋ฐฉ์์ ๋์์ธ์ ๋ฑ์ฅ
- late 1970s: data-oreiented ๋ก ๋ณํ๋ฅผ ์๋ํ์์ผ๋ ์คํจ
- Middle 1980s : object-oriented
data abstraction + inheritance + polymorphism
- language categories
- Imperative - ํฐ ๋ ธ์ด๋ง ๊ตฌ์กฐ ๊ธฐ๋ฐ : C, Java, Perl, JS, C++...
- Functional - lambda function์ ์ด์ฉํ ๊ตฌ์กฐ : LISP, Scheme
- Logic - expert system ๊ฐ๋ฐ์ ์ด์ฉ, ํ์ฌ ์ฌ์ฉ๋์ง ์์ : prolog
- Markup & hybrid - ๋ค๋ฅธ ํ๋ก๊ทธ๋จ์ ์ง์ํ๊ธฐ ์ํ language : JSTL, XSLT
- Implementaion methods
- Compilation : ์ปดํ์ผ ๋ฐฉ์ (์๋ โ, ๊ฐ๋ฐ๊ธฐ๊ฐ โ)
- Pure Interpretation : ์ธํฐํ๋ฆฌํฐ ๋ฐฉ์ (์๋ โ, ๊ฐ๋ฐ๊ธฐ๊ฐ โ)
- Hybrid ๋ฐฉ์ : compilation + interpretation
- Compilation : Slow translation, Fast execution
- Von Neumann Bottleneck : CPU์ Memory ์ฌ์ด์ bottleneck ํ์
- Pure Interpretation : Easier implementation, Slower execution
No translation, Often requires more space - Hybrid Implementation system : compilation + pure interpretation
- Just-in-Time Implementation System
Java -> bytecode ๋ interpretation ๋ฐฉ์์ผ๋ก ์ฆ์ ๋ณํํ๊ณ , bytecode -> machine language๋ ํธ์ถ ์ ์ปดํ์ผ
1ํ ์ปดํ์ผ, ์คํ ์ ํด๋น machine code๋ ์ ์ฅ(์ดํ์ ํธ์ถ ์ ์ด์ฉ)
- Programming Domains
- chapter 3 Syntax and Semantics
- BNF ๋ฌธ๋ฒ์ Context-Free grammar๋ฅผ ์ ๋ถ ํํ ๊ฐ๋ฅ
LHS : non-terminal => RHS : terminal & non-terminal
non-terminal์ <> ์์ ๋๋ฌ์์ฌ์ ํํ
ex) <ident_list> -> ident | ident, <ident_list>
sentential form : terminal symbol๋ก๋ง ์ด๋ฃจ์ด์ง sentence๊ฐ ๋๊ธฐ ์ด์ ๊น์ง์ ๊ณผ์ - ๊ฐ์ ๋ฌธ๋ฒ์ ๋ํด ๋ค๋ฅธ parse tree๊ฐ ๋ฐ์ํ๋ฉด ๋ชจํธํ(ambiguous)ํ ๋ฌธ๋ฒ
- ๋ฐ๋ผ์ ์ฐ์ ์์(precedence)์ ๊ฒฐํฉ๋ฒ์น(associativity)๋ฅผ ์ด์ฉ
- ์ฐ์ ์์๋ฅผ ํตํ ๋ถ๋ฆฌ๋ฐฉ๋ฒ
- ์ฐ์ ์์๊ฐ ๋ฎ์ ์ฐ์ฐ์๊ฐ ๋จผ์ ๋์ค๋๋ก ํ๋ค.
- | ์ ์ด์ฉํด ๋ณด๋ค ๋์ ์ฐ์ ์์์ ์ฐ์ฐ์๋ฅผ ์ฌ ๊ธฐ์
- -> ๋์ ์์ ์ฐ์ฐ์ ๊ธฐ์
- EBNF ํ๊ธฐ๋ฒ
"[ ]"๊ธฐํธ๋ ์๋ต๊ฐ๋ฅํ ๋ถ๋ถ
"(+/-)" ์ ๊ฐ์ ๊ธฐํธ๋ ๊ธฐํธ๋ค ์ค ํํด์ผ ํจ
"{ }" ๋ 0ํ ์ด์ ๋ฐ๋ณต๋๋ ๋ถ๋ถ(์๋ต๋ ๊ฐ๋ฅ)
-
Chapter 4 Lexical & Syntax analysis
- Regular grammar => Regualr expression => Finite-state machine(FSM)
- Context-free grammar => BNF => push-down automata
-
Chapter 5 Names, Bindings, Scopes
- ๋ฉ๋ชจ๋ฆฌ ์ฃผ์์ ๋ํ ์ธ์ด์์์ ์ถ์ํ ๋ฐฉ์์ ๋ณ์๋ฅผ ์ด์ฉํ๋ ๊ฒ์ด๋ค.
- 6๊ฐ์ง sextuple๋ก ๋ณ์์ ํน์ฑ์ ์ค๋ช ํ ์ ์๋ค.
-
Names : ๋ณ์์ ํน์ฑ ์ค ๊ทผ๋ณธ์ ์ธ ์์ฑ ํ๊ฐ์ง ์ด๋ค. ํจ์, ์ธ์์ ์ฐ๊ด๋์ด์์
- ์ฃผ์ ๋์์ธ ์ด์ : Case sensitive(๋์๋ฌธ์ ๊ตฌ๋ณ), reserved words, keywords
- ํํ : Camel notation์ ๋ง์ด ์ฌ์ฉ(ex. ScoreValue) ์๊ธ์๋ฅผ ๋๋ฌธ์๋ก
- Case-sensitivity / Special words : ํ์๋ ํ๋ ๋๋ฌด ๋ง์ผ๋ฉด ๋ฌธ์
-
Address : aliases๋ ๊ฐ์ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์๋ฅผ ๊ฐ๋ฅดํค์ง๋ง ๋ค๋ฅธ ์ด๋ฆ์ ๊ฐ์ง ๋ณ์นญ
-
Type : ๋ณ์ ๊ฐ์ ๋ฒ์์ ์ฐ์ฐ
-
Value : l-value ๋ address, r-value๋ value๋ฅผ ์๋ฏธ
-
Lifetime
Binding : entity์ attribute ์ฌ์ด์ ์ฐ๊ฒฐ
-
binding์ด ๋ฐ์ํ๋ ์๊ฐ์ ๋ฐ๋ผ Static, Dynamic binding์ด ์กด์ฌ
-
์ธ์ด ๋์์ธ, ๊ตฌํ, ์ปดํ์ผ ํ์์ ๋ฐ์ธ๋ฉ ๋์ด ์๋ ๊ฒ์ Static binding
-
ํ๋ก๊ทธ๋จ ๋ก๋ ์, ๋ฐํ์์ ๋ฐ์ธ๋ฉ ๋๋ ๊ฒ์ Dynamic binding
-
Static Binding
1์คํ(runtime) ์ด์ ์ ๋ฐ์ธ๋ฉ์ด ์๋ฃ๋๋ฉฐ, 2์คํ๋๋ ๋์ ๋ฐ์ธ๋ฉ์ด ๋ณ๊ฒฝ๋์ง ์์ -
Dynamic Binding
1Compile time ์ดํ์ ๋ฐ์ธ๋ฉ๋๊ฑฐ๋, 2ํ๋ก๊ทธ๋จ runtime์ binding์ด ๋ณ๊ฒฝ๋จ -
Type binding : ๋ณ์๋ฅผ ์ฌ์ฉํ๊ธฐ ์ด์ ์ ํ์์ ์ผ๋ก data type์ ๋ํ binding์ด ๋์ด์์ด์ผํ๋ค.
- type ์ง์ ๋ฐฉ๋ฒ
- static type binding : ์ ์ธ ์ type์ด ์ง์ ๋จ
- explicit(๋ช ์์ ) ๋ฐฉ๋ฒ : data type์ ์ง์ ๋ช ์ํจ(ex. int, char)
- implicit(๋ฌต์์ ๋ฐฉ๋ฒ)
- naming convention : type๋ณ ๋ณ์ ์ด๋ฆ์ ๊ท์น์ด ์ง์ ๋์ด์์
- context : ๋ฌธ๋งฅ์ ๋ณด๊ณ compiler๊ฐ data type์ ์ ์ถ
- dynamic type binding : ํ ๋น ์ binding
- ๋ณ์์๋ ํน๋ณํ ์ง์ ๋ data type์ด ์๋ ๊ฒ์ด ์๋
- ์ ์ธ ์ ์ํฉ์ ๋ง๊ฒ ๋งคํ๋จ
- ๋จ์ ์ผ๋ก๋ type checking์ด ๋ถ๊ฐ๋ฅํ๊ณ , H/W resource๋ฅผ ๋ง์ด ์ฐจ์ง
- static type binding : ์ ์ธ ์ type์ด ์ง์ ๋จ
- type ์ง์ ๋ฐฉ๋ฒ
-
Storage bindings & Lifetime
- ์ ์ฒด์ ์ธ ์๋ : static < heap = stack
- Static
- bind storage before execution
- ํ๋ก๊ทธ๋จ ์คํ ๋์์๋ ๊ณ์ ๊ฐ์ ๊ณต๊ฐ์ bound
- Stack-dynamic
- variable์ด declare ๋ ๋ stack์ ํ ๋น
- ๋ช๋ช ์ธ์ด๋ subprogram์ ์ฒ์์ด ์๋์ฌ๋ declare ๊ฐ๋ฅ
- ๊ฐํน ์ฑ๋ฅ ์ต์ ํ๋ฅผ ์ํด ๋ณ์ ์ ์ธ์ด subprogram ์ฒ์์ผ๋ก ์ด๋ํ๊ธฐ๋ ํ์ง๋ง, ๋ฌด์กฐ๊ฑด์ ์ผ๋ก ์ฌ๋ ค์๋ ์๋จ
- ์ฅ์
- recursion์ด ๊ฐ๋ฅํ๋ค. (๋งค์ฐ ํฐ ์ฅ์ )
- ๋จ์
- allocation๊ณผ deallocation์ overhead๊ฐ ๋ฐ์ํ๋ค.(์๋ โ)
- subprogram์ด histoy sensitiveํ์ง ๋ชปํ๋ค.
- indirect addressing์ผ๋ก ์ธํด reference๊ฐ ๋นํจ์จ์ ์ด๋ค.
- explicit heap-dynamic
- ๋ช ์์ ์ผ๋ก ๊ฐ๋ฐ์๊ฐ ์ฐ์ฐ์, ์์คํ ์ฝ ํธ์ถ์ ํตํด heap์์ญ์ ๋ฐ์ดํฐ๋ฅผ ์์ฑ
- ์ฅ์ : ๋์ ๊ณต๊ฐ ํ ๋น๊ณผ ๊ด๋ฆฌ๋ฅผ ์ ๊ณตํด์ค๋ค.
- ๋จ์
- pointer์ reference๋ฅผ ํตํด ๊ด๋ฆฌํ๋๋ฐ ์ด๋ ค์์ด ๋ฐ์
- ๋ณ์์ reference๋ก ์ธํ ๋น์ฉ์ด ๋ ๋ค.
- storage management๋ฅผ ๊ตฌํํ๊ธฐ ์ํ ๋ณต์ก์ฑ์ด ์ฆ๊ฐํ๋ค.
- implicit heap-dynamic
- assignment statement๋ฅผ ๋ง๋๋ ์๊ฐ allocation๊ณผ deallocation์ด ๋์์ ์ผ์ด๋๋ค.
- ์ด์ ์ ํด๋น ๋ณ์๊ฐ ์ด๋ ํ type์ด์๋์ง๋ ๊ด์ฌํ์ง ์๋๋ค.
- ์ฅ์ : ๊ฐ์ฅ ๋์ flexibility ์ ๊ณต
- ๋จ์
- dynamic attributes๋ฅผ ์ ์งํ๊ธฐ ์ํ runtime overhead ๋ฐ์ (์๋ โ)
- error detection์ด ๋ถ๊ฐ๋ฅํ๋ค. (realiability โ)
-
-
Scope
-
local variable : program unit๋ด์ ์ ์ธ๋ ๋ณ์
-
nonlocal variable : program unit๋ด์์ ์ฐธ์กฐํ ์๋ ์์ผ๋ ์ธ๋ถ์์ ์ ์ธ๋ ๋ณ์
-
Global variable : nonlocal variable์ค์ ํ๋
-
Static scope : compile time์ nonlocal ๋ณ์์ reference ์์น๋ฅผ ๊ฒฐ์ ํ ์ ์์
- static scoped language
- subprogram์ด nested๊ฐ ๊ฐ๋ฅํ ๊ฒฝ์ฐ : ์คํ์๋ โ, ์ ์ฝ ์์
- subprogram์ด nested๊ฐ ๋ถ๊ฐ๋ฅํ ๊ฒฝ์ฐ : ์คํ์๋ โ, ์ ์ฝ ์กด์ฌ
- ํธ์ถ ์คํ์ด ์๋ ์ฝ๋๋ง ๋ณด๊ณ ๊ฐ์ฅ ๊ฐ๊น์ด static ancestor๋ฅผ ์ฐพ์ ์ฐธ์กฐํ๋ค.
์ด ๊ทธ๋ฆผ ๊ผญ ํ์ธํ๊ธฐ!!
- ๊ฐ์ ์ด๋ฆ์ ๊ฐ์ง ๊ฒฝ์ฐ ๋ ๊ฐ๊น์ด ๋ณ์๊ฐ ์ ํ๋๋ค.
- ๋๋ถ๋ถ์ functional language๋
let
์ ์ด์ฉํด imperative language์ block๋ฅผ ์๋ฏธํ๋ค. - C99, C++, Java, C#์ ์ ์ธ ์์ ๋ถํฐ block์ ๋๊น์ง ๋ณ์๋ฅผ ์ด์ฉํ ์ ์๋ค.
- Global scope (global variables)
-
Static scope ๋ฐฉ์์ ๋๋ถ๋ถ์ ๊ฒฝ์ฐ์ ์ ์๋ํ๋, ๊ตฌํ์ ์ค๋ฒํค๋๊ฐ ํฌ๋ค.(enclosed scope)
-
Dynamic scope : nonlocal ๋ณ์์ reference ์์น๋ฅผ ์ฝ๋๋ก๋ ์ ์ ์๊ณ ์คํ์ call-chain์ ํตํด ์๊ฒ ๋จ(all active subprogram)
Static-scoped language์ Dynamic-scoped language์ ์ฐจ์ด์ ๋งค์ฐ ์ค์!
- Scope != Lifetime (ex.
static
variable) - Named Constant : 1ํ storage binding๋ง ๊ฐ๋ฅํ ํํ (๊ฐ๋
์ฑ์ด ๋ฐ์ด๋์ง)
- dynamic bound : expression ์ ์ฒด๊ฐ RHS์ ๊ฐ๋ฅ
- statically bound : ์ซ์ & named constant๋ก ๊ตฌ์ฑ๋ RHS๊ฐ ๊ฐ๋ฅ
-
-
Chapter 6. Data Types 1.data representation 2.operation
- Primitive Data Types (๊ธฐ๋ณธ ๋ฐ์ดํฐ ํ์
)
- ๋ช๋ช์ ํ๋์จ์ด์ ๋ฐ์๊ฒฐ๊ณผ์ด๋ค.(ex. integer types)
- ๋ค๋ฅธ structered type๋ฅผ ์ ์ํ๋๋ฐ ์ฌ์ฉ
- Integer
- representation : H/W mapping ์ํ(ํํ๊ณผ ์ ์ฅ๋ฐฉ์ ๋์ผ)
- operation : Two's complement
- Floating point
- ์ง์๋ถ(exponent)์ ๊ฐ์๋ถ(fraction) ๋ถ๋ถ์ผ๋ก ๋๋ ์ ์ฌ์ฉ
- String
- pattern matching ์ฐ์ฐ(Perl ์ง์)
- C/C++์์๋ non-primitive / Java, C#, Ruby, Perl.. ๋ฑ ๋๋ถ๋ถ์์ primitive ํ data
- ํนํ, Perl, JS, Ruby, PHP์์๋ regular expression์ ์ง์
- ๋ฌธ์์ด์ ๊ธธ์ด ์ง์
- static : ์ ์
- limited dynamic : static ๋ฐฉ์์ด๋ ๋ด๋ถ์ ์ผ๋ก ๋ณต์ฌ ์์ ์ ํตํด ์ ์ง
- dynamic : ์์ ํ ๋์ ์ธ ๋ฐฉ์์ผ๋ก ๊ธธ์ด์ ๋ํ ์ ํ์ด ์ ํ ์์
- Primitive Data Types (๊ธฐ๋ณธ ๋ฐ์ดํฐ ํ์
)
- Enumeration Types
- ์ฃผ์ ๋์์ธ ์ด์
- ์๋ก ๋ค๋ฅธ enum type์์ ๊ฐ์ ์ด๋ฆ์ ์ฌ์ฉํ ์ ์๋๋?
- enum value๊ฐ ์ ์์ ๋งคํ๋๋๋?
- ์ด๊ฑฐํ์ผ๋ก ์์ฑํ๋๋ก ์ ํ๋ ์ ํ์ด ์๋๋?
- ์ฝ๋์ ๊ฐ๋ ์ฑ๊ณผ ์ ๋ขฐ์ฑ์ด ๋์์ง, ๊ทธ๋ฌ๋ ์ฝ๋ ์์ฑ์ ์ด๋ ค์์ง(๊ธธ์ด์ง๊ณ , ์ง๋ฃจํจ)
- ์ฃผ์ ๋์์ธ ์ด์
- Array types
- homogeneous(๋์ผ)ํ ์ฌ๋ฌ๊ฐ์ ๋ฐ์ดํฐ๋ฅผ ์ง์ ์์ผ ๋์ ํํ
- ์ฃผ์ ๋์์ธ ์ด์
- array์ index(subscript)์ ์ ์ ํ ํ์ ์ ๋ฌด์์ธ๊ฐ
- subscript expression์ range check๋ฅผ ์ค์ํ๋๋
- Array indexing
- ๋ช๋ช ์ธ์ด์์๋ ๊ดํธ(parentheses)๋ฅผ ์ด์ฉ
- ๋๋ถ๋ถ์ bracket
[]
์ ์ด์ฉ - Ada : subscript type์ผ๋ก enumeration type์ ์ฌ์ฉ
- Java, ML, C#์ range check ์งํ
- Perl์ ๊ฒฝ์ฐ ๋ ํน
@signs = (0, 1, 2 ,3, 4); // ์ ์ธ $signs[2]; // 3๋ฒ์งธ ์์ ์ ๊ทผ $signs[-2]; // $signs[3]๊ณผ ๋์ผ
- lower bound(์ธ๋ฑ์ค ์์๊ฐ)์ ์ง์ ํ ์ ์๋ ๊ฒฝ์ฐ๋ ์์
- Array categories
- static array : data segment์ array ํ ๋น / subscript range์ storage ํ ๋น์ด staticํ๊ฒ ์ด๋ฃจ์ด์ง
- fixed stack-dynamic array : stack segment์ ๊ณ ์ ํฌ๊ธฐ๋ก ํ ๋น /
subscript range๋ staticํ๊ฒ, array storage allocation์ execution time์ stack์ ํ ๋น - fixed heap-dynamic array : heap segment์ ๊ณ ์ ํฌ๊ธฐ๋ก ํ ๋น /
fixed stack-dynamic array์ bound time์ ๋์ผํ๋, heap์ ํ ๋น - heap-dynamic : heap segment์ ๊ฐ๋ณ ํฌ๊ธฐ๋ก ํ ๋น /
1subscript range์ storage allocation ๋ชจ๋ execution time์ ๋ฐ์ํ๊ณ ,
2์ด๋ฅผ ํ๋ก๊ทธ๋จ ์ํ ์ค ๋ณ๊ฒฝ์ด ๊ฐ๋ฅํ๋ค.- ํ๋ก๊ทธ๋จ ์ ์ฐ์ฑ์ด ๋์์ง๋ ์๋๋ ํ ๋น๊ณผ ํด์ ๋ก ์ธํ ์ฑ๋ฅ ์ ํ
- JS์ ๊ฒฝ์ฐ array๊ฐ sparse ํ ์ ์์ผ๋ฉฐ, ์ด๋ฌํ ๊ฒฝ์ฐ ๋ฐฐ์ด ๋ด์์๋ nonexist element๊ฐ ์กด์ฌ
- Array initialization & operation
- Rectangular array : ์ง์ฌ๊ฐํ ํํ
[N][N]
array - jagged array : ๊ฐ๊ฐ ๊ธธ์ด๊ฐ ๋ค๋ฅธ ๋ฐฐ์ด๋ค์ ๋ฐฐ์ด
[N][M]
array (N != M) - Row major, Column major : ํ๊ณผ ์ด ์ค ์ด๋ค๊ฒ์ ๋ฉ๋ชจ๋ฆฌ ์ธ์ ํ์ฌ ์ ์ฅํ ๊ฒ์ธ์ง
- ๋ค์ฐจ์์ ๋ฐฐ์ด์ ์์น :
address of a(row์ lower bound, column์ lower bound) + ((i - 1) * n) + (j - 1) * element size
- Associative arrays
- (key : value) ์์ผ๋ก ์ด๋ฃจ์ด์ง ์์๊ฐ ์๋ data elements์ collection
- Perl :
%hi_temps = ("Mon" => 77, "Tue" => 79, "Wed" => 65, ...);
$hi_temps{"Wed"} = 83; delete $hi_temps{"Tue"};
- Record type : hecterogeneous(์๋ก ๋ค๋ฅธ) data elements๋ฅผ ๊ฐ์ง
- tuple types : Record type๊ณผ ๋น์ทํ๋, element ๋ณ name์ด ์กด์ฌํ์ง ์์
- immutable -> ํ๋ฒ ์ ์ธ ํ์๋ ๊ฐ์ ๋ฐ๊ฟ ์ ์์
- tuple์ ์ง์ํ์ง ์๋ C์์๋ ๋ฐํ๊ฐ์ 2๊ฐ ์ฃผ๋ ค๋ฉด ์ด๋ป๊ฒ ์ฝ๋ฉํ๋์ง ์์๋๊ฒ
- struct๋ฅผ ๋ฐํํ๋ ๋ฐฉ๋ฒ
- pointer๋ฅผ ์ด์ฉํด ๋ฐํํ๋ ๋ฐฉ๋ฒ
- list types : tuple๊ณผ ๋์ผํ๋ mutableํจ
- Python ์์๋
[]
๋ก List๋ฅผ ํํ - Python list comprehension : ์์ผ๋ก ํํํ์ฌ List ์ฐ์ฐ
[x * x for x in range(6) if x % 3 == 0] // result : [0, 9, 36];
- '(A B C) ์ด๋ฉด List type (A B C)๋ A๋ผ๋ ํจ์๋ฅผ B, C๋ผ๋ ์ธ์๋ก ํธ์ถ
- Python ์์๋
- Union types : ํ๋์ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์์ ์ฌ๋ฌ๊ฐ์ ๋ณ์๋ฅผ ๋ฃ์ด ์ฌ์ฉ
- Free union vs. Discriminant
- Free union : type checking ์์ -> unsafe
- Discriminant union : type checking ์์ -> safe, but slow
- Free union vs. Discriminant
- Pointer(์ด๊ธฐ๊ฐ ํ์์์) and Reference(์ด๊ธฐ๊ฐ ํ์) types
- range of values : memory address + nil
- Operation
- assignment : set a pointer
- dereferencing : explicit & implicit ๋ฐฉ์
- problems
- Dangling pointers -> danger
-
์๋ ๋ณ์(์ด๋ฏธ ํด์ ๋)์ ๋ฉ๋ชจ๋ฆฌ ์ฐธ์กฐ
-
Tombstone ๋ฐฉ๋ฒ
- ๋ชจ๋ pointer๋ค์ ๊ธฐ๋ณธ์ ์ผ๋ก tombstone์ด๋ผ๋ pointer๋ฅผ ์ฐธ์กฐ
- ๋ง์ฝ ํด๋น ๋ณ์๊ฐ ํด์ ๋์๋ค๋ฉด tombstone์ RIP์ ๋ช ์ํ๋ค.
- RIP์ฒ๋ฆฌ๋ ๋ณ์๋ ์ ๊ทผํ ์ ์๋๋ก ํ๋ค.Locks-and-keys ๋ฐฉ๋ฒ
- ๋ณ์ ํ ๋น ์ pointer์ heap variable์ ๊ฐ๊ฐ key์ lock ๊ฐ์ ์ถ๊ฐํ๋ค.
- ๊ธฐ๋ณธ์ ์ผ๋ก ๋ณ์๊ฐ ํ ๋น์ด ๋ ๊ฒฝ์ฐ pointer์ key๊ฐ๊ณผ lock ๊ฐ์ ๋์ผํ๋ค.
- lock == key ์ธ ๊ฒฝ์ฐ์๋ง ๋ณ์ ์ ๊ทผ ๊ฐ๋ฅ - ๋ณ์ ํด์ ์ lock ๊ฐ์ ์ด๊ธฐํํ์ฌ, ์ ๊ทผ์ ์ฐจ๋จ
-
- Lost heap-dynamic variable
- heap์์ญ์ ๋์ ํ ๋น๋ ๋ณ์์ ์ฃผ์๋ฅผ ์์ด๋ฒ๋ฆผ
- memory leakage ๋ฐ์
- Dangling pointers -> danger
- usage in C / C++
int a = 10; void* p = &a; *p; // error, void pointer can't de-reference *(int *)p; //ok
- Reference type
- C++์์๋ reference ์ง์ ํ ๋ณ๊ฒฝ ๋ถ๊ฐ๋ฅ
- Java ์์๋ ๊ฐ๋ฅ
- C#์์๋ 2๊ฐ์ง type ๊ฐ๋ฅ
- Heap management
- Reference counter
- ๊ฐ ๋ฉ๋ชจ๋ฆฌ์ ์ ์์ ์ ๊ฐ๋ฅดํค๊ณ ์๋ ํฌ์ธํฐ์ ์๋ฅผ ์ ์ฅ
- ์ด ์ซ์๊ฐ 0์ด ๋ ๊ฒฝ์ฐ ๋ฉ๋ชจ๋ฆฌ ํด์
- ๋จ์ : ์ถ๊ฐ์ ์ธ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ ํ์, ์ํ์๊ฐ ์ฆ๊ฐ
- ์ฅ์ : ์ ์ฐจ์ ์ผ๋ก ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ด ์ฆ๊ฐ, ๋๋ ์ด๊ฐ ์์
- Mark-sweep
- pointer๋ฅผ ๋ฐ๋ผ๊ฐ ๋ง๋์ง ์๋ ๋ฉ๋ชจ๋ฆฌ์ ๋ถ๋ถ์ ํด์
- ๋ฉ๋ชจ๋ฆฌ๊ฐ ๋ถ์กฑํ ๋๋ง ์คํํ๋ฏ๋ก delay๊ฐ ์กด์ฌ
- Reference counter
- Type checking : operator์ operand๋ก์ compatible type์ธ์ง ํ์ธํ๋ ์์
- compatible type : ์ธ์ด ํน์ฑ์ผ๋ก์ operator๊ฐ ์ฒ๋ฆฌํ ์ ์๋ ํฉ๋ฒ์ ์ธ ์ฐ์ฐ์์ธ์ง ํ์ธ, ์๋์ผ๋ก ํ๋ณํ์ผ๋ก ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํ์ง ์ฌ๋ถ
- ์๋์ผ๋ก compatible type์ผ๋ก ํ๋ณํ ๋๋ ๊ฒ์ coercion์ด๋ผ๊ณ ํ๋ค.
- coercion์ด ๋ถ๊ฐ๋ฅํ ๊ฒฝ์ฐ type casting์ด ํ์
- type binding์ด ์ ๋ถ static ํ ๊ฒฝ์ฐ type checking๋ ์ ๋ถ staticํ๋ค. dynamic์ด๋ฉด type checking๋ dynamicํ๋ค.
- strongly typed language์ ๊ฒฝ์ฐ ๋ชจ๋ type error๋ฅผ ์ฒดํฌํ ์ ์๋ค.
- Coercion ์ด ๋ง์์ง๋ฉด ๋ง์์ง์๋ก strong typing์ ์ฝํ๋๋ค
- Name type equivalence = Type์ Name์ด ๊ฐ์์ผ type equivalenceํ๋ค.
- ๊ตฌํํ๊ธฐ ์ฝ์ง๋ง, ์ฌ์ฉ์ด ์ ํ์ ์ด๋ค.
- Structure type equivalence = Type์ Name ๋์ผ ์ ๋ฌด์ ์๊ด์์ด ๊ตฌ์กฐ๊ฐ ๋์ผํ๋ฉด compatible type์ด๋ค.
- ์ฝ๋ ์์ฑ์ด ์์ ๋กญ์ง๋ง, ๊ตฌํ์ด ์ด๋ ต๊ณ ๋์ผํ๋ค๋ ๊ธฐ์ค์ด ๋ชจํธํ๋ค.
- Chapter 7. Expression and Assignment statements
- Expression : ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์์ ๊ธฐ๋ณธ์ ์ธ ๊ณ์ฐ ๋จ์๋ฅผ ํํ
- order of operator & operand evaluation์ ๋ํด ์ดํดํด์ผ ํ๋ค.
- ์ฐ์ ์์์ ๊ฒฐํฉ๋ฒ์น
- ๋๋ถ๋ถ ํผ์ฐ์ฐ์์ ๊ณ์ฐ ์ฐ์ ์์๋ ์ง์ ๋์ด ์์ง ์์ผ๋ฉฐ, ๊ตฌํ์ ๋ฐ๋ผ ๋ค๋ฆ
- ๋ช
๋ นํ ์ธ์ด(imperative)์ ํต์ฌ์ assignment statement๋ก ๋ถํฐ ์์๋๋ค.
- ๊ฐ์ฅ ํฐ ์ฅ์ ์ด์ ๋จ์ ์ด side effect(๋ถ์์ ์ธ ํจ๊ณผ) : ์์ ์ ์ค์ฝํ ์ธ๋ถ์ ๋ณ์์ ๊ฐ์ ๋ณ๊ฒฝ
- Arithmetic expression
- ์ฐ์ฐ์ ์์ ๋ฐ๋ผ Unary, Binary, Ternary operator๋ก ๋ถ๋ฅ
- ์ฐ์ฐ์ ์์น์ ๋ฐ๋ผ Prefix, Infix, Postfix operator๋ก ๋ถ๋ฅ
- Evaluation order
- Operator
- Precedence rule : ์ํ์๋ค์ ์ ์์ ์ํด
- (๊ดํธ -> ๋จ์ผ ์ฐ์ฐ์ -> ์น์(power) -> ๊ณฑ์ ๋๋์ -> ๋ง์ ๋บ์ )
- Associativity rule : ๊ธฐ๋ณธ์ ์ผ๋ก ์ผ์ชฝ๋ถํฐ ์งํํ๋, ์น์์ ๊ฒฝ์ฐ ์ค๋ฅธ์ชฝ๋ถํฐ ๊ณ์ฐ
- Operand
- ์์ด ์๋ ๊ฒฝ์ฐ, side-effect๊ฐ ์๋ค๋ฉด evaluation ์์์ ๋ฐ๋ผ ๋ค๋ฆ
- functional side-effect : ์์ ์ ์ธ์, ๋๋ ์ ์ญ ๋ณ์๋ฅผ ์์ ํ๋ ํจ์
int a = 5; int fun1() { a = 17; return 3; } void main() { a = a + fun1(); //์ผ์ชฝ๋ถํฐ ์คํํ๋ ๊ฒฝ์ฐ 8, ์ค๋ฅธ์ชฝ๋ถํฐ ์คํํ๋ ๊ฒฝ์ฐ 20. }
- functional side-effect๋ ํ์ค์ด ์์ผ๋ฏ๋ก, Implementer์ ๋ฐ๋ผ ๋ค๋ฅด๋ค.
- Operator
- Referential transparency
result = (fun(a) + b) / (fun(a) - c);
์์temp = fun(a);
์ดํfun(a)
๋ฅผtemp
๋ก ๋ฐ๊ฟ๋ ๋์ผ ํ ๊ฒฝ์ฐ Referential transparency ์กด์ฌ- ํจ์
fun
์ด side effect๊ฐ ์์ด์ผ ๊ฐ๋ฅ - program์ ์ดํดํ๊ธฐ ์ฝ๋ค๋ ์ฅ์
- Pure functional language๋ assignment ์์ -> side-effect ์์ -> referential transparency ๊ฐ์ง
- Overloaded operator
- ํ๋์ operator๊ฐ ๋ค์ํ ์ฉ๋๋ก ์ฌ์ฉ๋๋ฉด operator overloaded ์ํ
- ex) ์ ์์ ๋ง์ (+)์ฐ์ฐ๊ณผ ์ค์์ ๋ง์ (+)์ฐ์ฐ
- compiler์ error detection ๋ฐฉํด์ ๊ฐ๋ ์ฑ ์ ํด ๋ฌธ์ ๊ฐ ๋ฐ์ ๊ฐ๋ฅ
- user-defined overloaded operator
- ์ ์ฌ์ฉํ๋ฉด ๊ฐ๋ ์ฑ ์ฆ๊ฐ
- ์๋ชป ์ฌ์ฉํ๋ฉด ๊ฐ๋ ์ฑ ์ ํด
- Type conversion
- narrowing conversion : ๋ฐ์ดํฐ ์ฌ์ด์ฆ ๅคง -> ์ฌ์ด์ฆ ๅฐ, ๋ฐ์ดํฐ ์ ์ค
- widening conversion : ๋ฐ์ดํฐ ์ฌ์ด์ฆ ๅฐ -> ์ฌ์ด์ฆ ๅคง, ๋ฐ์ดํฐ ๋ณด์กด
- mixed mode : operand๊ฐ ์๋ก ๋ค๋ฅธ type์ ๊ฐ์ง expression์ mixed mode๋ผ๊ณ ํต์นญ
- type conversion
- widening conversion์ ๊ฒฝ์ฐ ***coercion(Implicit type conversion)***์ด ๋ฐ์
- program์ ์์ฑํ๊ธฐ ํธ๋ฆฌํด์ง
- compiler์ error detection ๋ฅ๋ ฅ ์ ํ(์ค์ ์ ๋ฐ ๊ฐ๋ฅ์ฑ)
- C๊ธฐ๋ฐ ์ธ์ด์์๋ int๋ณด๋ค ์์ type์ ๋ง์ ์ int๋ก coercion ํ ๊ณ์ฐ
- Explicit Type conversion (Casting in C) ๋ช ์์
- widening conversion์ ๊ฒฝ์ฐ ***coercion(Implicit type conversion)***์ด ๋ฐ์
- type conversion
- Errors in expression
- div by zero, overflow -> exception handler๋ก ์ฒ๋ฆฌ
- Expression : ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์์ ๊ธฐ๋ณธ์ ์ธ ๊ณ์ฐ ๋จ์๋ฅผ ํํ
-
Chapter 7. (cont.)
- Boolean Expression
- JS์ PHP์์๋
==
๋ type conversion์ดํ ๊ฐ์ ๋น๊ตํ์ง๋ง===
์ type conversion ์์ด ๋น๊ต
"7" == 7 // true, "7"์ intํ์ผ๋ก coerced ๋จ "7" === 7 // false, "==="์ coerce ์ ์ฉํ์ง ์์
if(a < b < c) // a < b evaluation ๊ฒฐ๊ณผ๊ฐ ๋ฐํ(true ๋๋ false)๋๊ณ , ๊ทธ ๊ฐ๊ณผ c๋ฅผ ๋น๊ต
- JS์ PHP์์๋
- Short circuit evaluation
- ์ ์ฒด๋ฅผ evaluationํ์ง ์๊ณ , ์ผ๋ถ๋ถ๋ง evaluation์ ํ๋ฉด ์ ์ฒด ๊ฐ์ ์ ์ ์๋ ๊ฒฝ์ฐ
int index = 0; while((index < length) && LIST[index] != value) // short-circuit evaluation ํ์ง ์๋ ๊ฒฝ์ฐ ์ค๋ฅ ๋ฐ์ index++; (a > b) || (b++ / 3); // b์ ๊ฐ์ a <= b์ธ ๊ฒฝ์ฐ๋ง ๋ณ๊ฒฝ // short-circuit evaluation ์ํฉ์์ side effect๊ฐ ๋ฐ์ํ๋ ๋งค์ฐ ๋ถ์ ์ ํ ์ํฉ
- Assignment statement
=
์ assign๊ณผ comparison 2๊ฐ์ ๊ธฐ๋ฅ์ overloadingํ๋ ๊ฒ์ ๋ถ์ ์ ํ๋ฏ๋ก==
์ฌ์ฉ- Conditional targets
($flag ? $total : &subtotal) = 0 // flag๊ฐ true์ด๋ฉด total = 0; ์๋๋ฉด subtotal = 0;
- Compound assignment operator :
a += b
=>a = a + b
- Unary assignment operator :
a++
or++a
- Assignment as an Expression :
while((ch = getchar()) != EOF) {...}
- side-effect๋ฅผ ์ผ์ผํฌ ์ ์์ด์ ์ฝ๋๋ฅผ ์ดํดํ๊ธฐ ํ๋ค๊ฒ ํจ
- ์กฐ์ฌํด์ ์ฌ์ฉํด์ผ ํจ
- ex)
a = b + (c = d / b) - 1;
- Multiple assignment :
($first, $second, $third) = (20, 30, 40);
- Assignment in functional language
- ์์ ํจ์ํ ์ธ์ด์๋ assignment๊ฐ ์กด์ฌํ์ง ์์
val fruit = apples + oranges;
๋ val ์ ๊ฐ์ ๋ฐ์ธ๋ฉํ๋ ์์- ์์ ๊ฐ์ด ๋ฐ์ธ๋ฉํ๋ฉด ๊ฐ์ ์ ๋ ๋ฐ๊ฟ ์ ์์(ํ์์ re-binding ํด์ผํจ)
- Boolean Expression
-
Chapter 8. Statement-Level Control structures
- Control structure : statement๊ฐ์ program flow๋ฅผ controlํ๋ ๊ตฌ์กฐ ex)
if-else
,for
,... - Selection statements
- two-way :
if - then
- nesting selector(์ค์ฒฉ if๋ฌธ)์ ๊ฒฝ์ฐ else๋ ๊ฐ์ฅ ๊ฐ๊น์ด if์ ๋งค์นญ์ด ๋๋ค.
- Selector expression : ํจ์ํ ์ธ์ด์์๋ if๊ฐ expression ์ญํ ๋ ํ ์ ์์
- multiple-way :
switch
switch(
control expr)
์์control expr
์ ์ด๋ค type์ ํ์ฉํ ๊ฒ์ธ์ง?- ํด๋นํ๋ case๋ง ์คํ์ํฌ ๊ฒ์ธ์ง, ๋ค์ case์ ๋ํ ์ฒ๋ฆฌ๊น์ง ์ํํ ๊ฒ์ธ์ง?
- C#์ C/C++์์์ ์ฐจ์ด์
- switch๋ฌธ ๋ด์์ ํ๊ฐ์ case(segment๋ผ๊ณ ์นญํจ)๋ง ์คํํด์ผํจ. ์ฆ, ๊ฐ case ๋ง๋ค unconditional branch(break)๋ก ๋ง๋ฌด๋ฆฌ ํด์ผํจ
control expr
์ด string์ด์ฌ๋ ์คํ์ด ๊ฐ๋ฅ
- Scheme์ ๊ฒฝ์ฐ
COND
๋ฅผ ์ฌ์ฉ
(COND ((> x y) "x is greater than y") ((< x y) "y is greater than x") (ELSE "x and y is equal") )
- two-way :
- Iterative statements
- Counter-Controlled Loop, ex)
for-loop
- loop variable + (initial; terminal; stepsize)
- C์ C++์ ์ฐจ์ด์
- terminal(control) expression์ด boolean๋ ํ์ฉ
- C++์์ initial expression์ ๋ณ์ ์ ์ธ๋ ๊ฐ๋ฅ
- Java์ C#
- terminal(control) expression์ ๋ฌด์กฐ๊ฑด boolean๋ง ๊ฐ๋ฅ
- functional language์์์ counter-controlled loop
- functional language์๋ assignment๊ฐ ์กด์ฌํ์ง ์์
- ๋ฐ๋ผ์ variable์ด ์กด์ฌํ์ง ์์ผ๋ฏ๋ก, recursive call๋ก ๊ตฌํ
- Logically-Controlled Loop, ex)
while, do-while
- pretest(loop ์ํ ์ ์กฐ๊ฑด ๊ฒ์ฌ) & posttest(loop ์ํ ํ ์กฐ๊ฑด ๊ฒ์ฌ)
- Java๋ ํญ์ boolean ํ์ ์ด์ฌ์ผํ๋ฉฐ, goto๋ฌธ์ด ์กด์ฌํ์ง ์๋๋ค.
- User-Located loop control - Loop ๋์ค ์ ์ด(
continue, break
) - Iteration based on Data structure
- C ex)
for (p = root; p != NULL; traverse(p)) {...}
์์traverse
๋ iterator function - PHP์์๋
current(), next(), reset()
์ ์ ๊ณตํ๋ค. - C#, Java 5.0์๋
foreach
์ฌ์ฉ
- C ex)
- Uncoditional branching :
goto
- ์ค๋ณต๋ ๋ฐ๋ณต๋ฌธ์ ํ๋ฒ์
break
ํ๊ธฐ ์ํด ์ฌ์ฉ
- ์ค๋ณต๋ ๋ฐ๋ณต๋ฌธ์ ํ๋ฒ์
- Counter-Controlled Loop, ex)
- Guarded Commands
- ์ฝ๋์ ์์ ์ฑ์ ์ํด ์๋ก์ด ๋ฐฉ๋ฒ์ ๊ฐ๋ฐ
- concurrent(๋์์ฑ)์ ๊ฐ์ง๊ธฐ ๋๋ฌธ์, evaluation ์งํ์์๋ ์ค์ํ์ง ์์(๋ฐ๋์ด๋ ๋ฌธ์ ๊ฐ ์์)
- Selection Guarded Command
if <Boolean expr> -> <statement> [] <Boolean expr> -> <statement> ... [] <Boolean expr> -> <statement> fi
- ๋ชจ๋ boolean expr์ parellelํ๊ฒ(์์์ ์๊ด์์ด) evaluation
- ํ๋๋ผ๋ true๊ฐ ๋๋ ๊ฒฝ์ฐ ์ฐธ์ธ
statement
์ค randomํ๊ฒ ํ๋๋ฅผ ์คํ - ๋ชจ๋ false์ธ ๊ฒฝ์ฐ error ๋ฐ์
- Loop Guarded Command
do <Boolean expr> -> <statement> [] <Boolean expr> -> <statement> ... [] <Boolean expr> -> <statement> od
- ์์ ๋์ผํ๊ฒ ๋ชจ๋ expr์ evaluateํจ
- ํ๋๋ผ๋ true์ธ expr์ด ์๋ค๋ฉด ๊ทธ๋ฌํ expr ์ค randomํ๊ฒ ์ ํํ์ฌ ์คํ
- ์คํ ํ์๋ ๋ค์ ์ฒ์๋ถํฐ ๋ฐ๋ณต
- ๋ชจ๋ expr์ evaluation result๊ฐ false์ฌ์ผ ์ข ๋ฃ
- Control structure : statement๊ฐ์ program flow๋ฅผ controlํ๋ ๊ตฌ์กฐ ex)
-
Chapter 9. Subprograms
- Function & Procedure
- Procedure : return value๊ฐ ์์ด, ๋ช ๋ น์ด์ ๋ฌถ์์ ๋ปํ๋ ๋จ์ด (void function)
- Function : return value๊ฐ ์กด์ฌํ์ฌ ๊ฒฐ๊ณผ ๊ฐ์ด ์๋ ๊ฒฝ์ฐ
- subprogram = procedure + function
- ๋ชจ๋ subprogram์ entry point(์ง์ ์ )์ด 1๊ฐ์ด๋ค.
- calling program์ called subprogram์ด ์ข ๋ฃ๋ ๋๊น์ง ์ ์งํด์๋๋ค.
- subprogram definition & call
- subprogram definition :
void f() { ... }
- subprogram call :
f()
- subprogram active : ํธ์ถ๋์ด ์์ง return ํ์ง ์๊ณ ํจ์๊ฐ ์คํ์ค์ธ ์ํ
- Python์์ function definition์ด excutable(์ํฉ์ ๋ง๊ฒ ์ ์๊ฐ ๊ฐ๋ฅํ๋ค.)
if a == 1 // a๊ฐ 1์ธ ๊ฒฝ์ฐ def fun(...) : ... else // a๊ฐ 1์ด ์๋ ๊ฒฝ์ฐ def fun(...) : ...
- ๋ฏธ๋ฆฌ ์ปดํ์ผ๋ฌ์๊ฒ ํจ์ ํธ์ถ์ ํ์ํ type๊ณผ ๋ฐํํ, ํจ์ ์ด๋ฆ์ ์๋ ค์ฃผ๋ ๊ฒ : function declarations
- formal parameter(ํจ์ ์ ์ ์) vs. actual parameter(ํธ์ถ ์)
- subprogram definition :
- Actual/Formal Parameter corresdpondence(๋งคํ)
- positional : ์ฒซ๋ฒ์งธ actual parameter๋ ์ฒซ๋ฒ์งธ formal parameter๋ก...
- ์์ ํ๊ณ ํจ์จ์
- keyword :
void f(int a, int b); f(a = 1, b = 2);
- ์์์ ์์๋ฅผ ๋ฐ๊ฟ๋ ์๊ด์๊ณ , ๋งคํ์ด ๋ถ๋ช ํจ
- ๊ฐ๋ฐ์๊ฐ formal parameter ์ด๋ฆ์ ์ ๋ถ ์์์ผ ํจ
- positional : ์ฒซ๋ฒ์งธ actual parameter๋ ์ฒซ๋ฒ์งธ formal parameter๋ก...
- Formal parameter default value
- python์ default ๊ฐ
def compute_pay(income, exemption = 1, tax_rate) pay = compute_pay(20000.0, tax_rate = 0.15) // exemption = 1์ด ์๋์ผ๋ก ์ฝ์
- ๊ฐ๋ณ์ธ์ ํ๋ผ๋ฏธํฐ
- ์ธ์์ ์๊ฐ ๊ณ ์ ์ด ์๋, ๋ณํ ์ ์๋ ํจ์ ex)printf
- ์์ default value๋ฅผ ์ง์ํ์ง ์๋ ์ธ์ด์์๋ ๊ฐ๋ณ์ธ์๋ฅผ ๋๋ถ๋ถ ์ง์ํ์ง ์์
- positional ๋งคํ ๋ฐฉ์์์๋ง ๊ฐ๋ณ์ธ์ ์ ๊ณต
- C#์์๋
params
ํค์๋๋ฅผ ์ด์ฉํ์ฌ ๊ฐํธํ๊ฒ ๊ฐ๋ณ์ธ์๋ฅผ ์ ์๊ฐ๋ฅ(๊ฐ์ data type์ ํํด)
- Local referencing environments
- subprogram์ local variable์ ์ ์ฅ๊ณต๊ฐ์ stack-dynamic ๋๋ staticํ๊ฒ ํ ๋น
- ๋๋ถ๋ถ์ ํ์ฌ ์ธ์ด๋ stack-dynamicํ ๋ณ์๋ฅผ ๋ง๋ฆ
- Parameter passing
- In mode
- Out mode
- Inout mode
- Implementation model of "Parameter Passing"
- Pass(Call)-by-Value (In Mode)
- ๋จ์ ๋ณต์ฌ(๊ฐ ๋๋ ํฌ์ธํฐ)๋ฅผ ํตํด ๊ตฌํํ๋ค
- disadvantage
- Physical move method : 2์ค์ผ๋ก ์ ์ฅ๊ณต๊ฐ์ด ์์๋๋ค.(์ ์ฅ๊ณต๊ฐ ๋ญ๋น, ๋ณต์ฌ์ ์ด์ฉ)
- Access Path method : subprogram์์ ๊ฐ์ ๋ณ๊ฒฝํ์ง ์๋๋ก write protect๊ฐ ๋ฐ๋ก ํ์ํ๋ฉฐ, indirect addressing์ผ๋ก ์ธํด ์๋๊ฐ ์ ํ๋๋ค.
- Pass-by-Result (Out Mode)
- Out Mode์ ๊ฐ๋ ์ ๊ทธ๋๋ก ๊ตฌํ
- formal parameter๋ก ๊ฐ์ ์ ๋ฌํ์ง ์๊ณ , call ์ข ๋ฃ ์ actual parameter๋ก ์ ๋ฌ
- potential problem
- actual parameter๋ก ๋์ผํ ๋ณ์๋ฅผ 2๋ฒ ์ ๋ฌํ๋ ๊ฒฝ์ฐ (
f(out a, out a);
)๋ฌธ์ ๊ฐ ๋ ์ ์์ - ๋๋
f(out list[sub], sub)
์ฒ๋ผ ๋ณ์๊ฐ์ dependency๋ฅผ ๊ฐ์ง ์ฑ๋ก ํธ์ถ๋๋ ๊ฒฝ์ฐ out mode๋ฅผ ์ง์ ํ ์์น๊ฐ ๋ช ํํ์ง ์์
- actual parameter๋ก ๋์ผํ ๋ณ์๋ฅผ 2๋ฒ ์ ๋ฌํ๋ ๊ฒฝ์ฐ (
- Pass-by-Value-Result (Inout Mode) : Pass-by-Value + Pass-by-Result
- Pass-by-Value์ ๋จ์ ๊ณผ Pass-by-Result์ ๋จ์ ์ ๊ฐ์ง๊ณ ์๋ค.
- Pass(Call)-by-Reference (Inout Mode)
- Access path๋ฅผ ์ ๋ฌํ๋ ๋ฐฉ์
- Passing process๊ฐ ํจ์จ์ ์ด๋ค
- disadvantage
- Slower Access ์๋
- ์ํ์ง ์๋ aliases๊ฐ ๋ฐ์ํด ๊ฐ๋ ์ฑ๊ณผ ์ ๋ขฐ์ฑ ์ ํ
- ex)
fun(total, total)
,i = j; fun(list[i], list[j]);
- Pass-by-Name (Inout Mode)
- textual substitution(๋จ์ ๋์ฒด)
- C์ธ์ด์์๋ ๋งคํฌ๋ก ํจ์๋ก ์ด์ฉ
- Pass(Call)-by-Value (In Mode)
- Type checking parameters : ์ผ์ ๋ถ๋ถ์ ํ๊ณ ์ผ์ ๋ถ๋ถ์ ํ์ง ์์(์๋ ๋ฌธ์ ๋ก ์ธํด)
- Multidimensional array as Parameter
- C์ธ์ด์์๋
void f(int x[][20]) {...}
,void f(int **x, int xsize, int ysize)
- Java, C#์์๋ ๋ฐฐ์ด ์์ฒด๊ฐ ๊ฐ์ฒด์์ผ๋ก ๋ฐฐ์ด๋ง ๋๊ธฐ๋ฉด ๋จ -> more flexible
- C์ธ์ด์์๋
- High-order function(subprogram์ ์ธ์๋ก ํ๋ ํจ์)
- subprogram๊ณผ Referencing Environment๋ฅผ ์ ์กํด์ผํ๋ค.
- Shallow binding : dynamic-scoped (call-chain์ ์์ nonlocal)
- Deep binding : static-scoped (code์ enclosing environemt์ nonlocal)
- Ad hoc binding : parameter passing์ ์ผ๊ธฐํ ํจ์์ environment๋ฅผ ์ด์ฉ
- subprogram๊ณผ Referencing Environment๋ฅผ ์ ์กํด์ผํ๋ค.
- Function pointer : calling subprograms indirectly
- C / C++
- ์ ์ธ :
void (*plot)(int x, int y);
- ์ฐธ์กฐ :
(*plot)(10, 20)
๋๋plot(10, 20)
- ์ ์ธ :
- C#
delegate
๋ ํจ์๋ฅผ ๋ด์ ์ ์๋ ๊ทธ๋ฆ
public delegate int Change(int x); // delegate ์ ์ธ static int fun1 (int x) {}; // fun1 ์ ์ธ Change chgfun1 = new Change(fun1); // delegate instantiate chgfun1(12); // fun1(12)์ ๋์ผ chgfun1 += fun2; // fun2 ํจ์ ์ถ๊ฐ chgfun1(20); // fun1(20)๊ณผ fun2(20) ๋ ๋ค ํธ์ถ
- C / C++
- Overloaded subprogram : ํจ์ ์ด๋ฆ์ ๋์ผํ๋, ์ธ์ ์ ๋๋ type์ด ๋ค๋ฅธ ๊ฒฝ์ฐ
- return type ๊ตฌ๋ถ์ฌ๋ถ๋ implementation์ ๋ฐ๋ผ ๋ค๋ฅด๋ค
- Generic subprogram
- cf) Overloaded subprogram => ad hoc polymorphism์ด๋ผ๊ณ ํจ
- Subtype polymorphism
- Parametric polymorphism :
template
programming
template <class Type> Type max(Type first, Type second) {...}
- User-Defined Overloaded operator :
operator+
,def __add__(self, second):
Closure : subprogram + referencing environment
- ์์์ ์ฅ์์์ ์คํํด๋ ์คํ์ด ๋ ์ ์๋๋ก ref env.๋ฅผ ํฌํจ
- nested subprogram์ ํ์ฉํ์ง ์๋ static-scoped language๋ closure๊ฐ ํ์ํ์ง ์๋ค.
// this is JAVASCRIPT version function makeAdder(x) { return function(y) {return x + y;} // returning closure(include nonlocal variable) } ... var add10 = makeAdder(10); var add5 = makeAdder(5); document.write("add 10 to 20: " + add10(20)); document.write("add 5 to 20: " + add5(20));
// this is C# version static Func<int, int> makeAdder(int x) { return delegate(int y) {return x + y;}; }
- Coroutines
- ์์์ ์ด ์ฌ๋ฌ๊ณณ์ด๋ฉฐ, ์ด๋ฅผ ์ ์ดํ ์ ์๋ subprogram
- Caller์ Callee๊ฐ ๋๋ฑํ ๊ด๊ณ :
symmetric control
- coroutine์ call์
resume
์ด๋ผ๊ณ ํจ - ์ฒซ๋ฒ์งธ call์๋ ์ฒ์๋ถํฐ ์์์ด ๋๋, ์ด์ด์ง๋ call์์๋ ์ด์ execution์์ ์ค์ง๋ฌ๋ ์ฝ๋๋ถํฐ ์์
- caller์ callee๊ฐ ๋ฐ๋ณต์ ์ผ๋ก ํธ์ถํ๋ฉด ๋ฌดํํ ํธ์ถ๋ ๊ฐ๋ฅ
quasi-concurrent execution
์ ์ ๊ณต : ์๋ก ๋์์ ํธ์ถ๋์ด ๋ณด์ด์ง๋ง, ๊ฐ๊ฐ์ ์คํ์ ๋ ๋ฆฝ๋์ด์์ผ๋ฉฐ, ๋์์ ์ผ์ด๋์ง ์๋๋ค.- Time sharing ๋ฐฉ์์ด์ง๋ง ์ฌ๋ฌ๊ฐ์ ์ฝ๋๊ฐ parellelํ๊ฒ ์คํ
- Function & Procedure
- Chapter 10. Implementing Subprograms
- subprogram linkage : 1subprogram call and 2return operations
- general semantics of calls to a subprogram
- parameter passing methods
- Stack-dynamic allocation of local variables
- Save the execution status of calling program
- Transef of control and arrange for the return
- If subprogram nesting is supported, access to nonlocal variables
- returns to a subprogram
- out mode parameters must have their values returned
- Deallocation of local variables
- Restore the execution status
- Return control to the caller
- Implementing "Simple" subprogram : local & nonlocal variable ์์, nested ํ์ฉ๋์ง ์์, parameter passing๋ง ์กด์ฌ
- Required storage : Status information, parameters, return address, return value for functions, temporaries => ์ฃผ๋ก stack segment ์ฌ์ฉ
- Required storage๋ 2๊ฐ์ง๋ก ๊ตฌ๋ถํ ์ ์์. actual code part, non-code part(data part)
- excuting subprogram์ non-code part ๊ตฌ์กฐ๋ฅผ activation record๋ผ๊ณ ํ๋ค.
- activation record instance๋ ์ด๋ฌํ activation record์ ์ค์ ๊ตฌ์ฒด์ ์ธ ์์ด๋ค.
- ARI ๊ตฌ์ฑ : Return address, Parameter, Local variables + (Nested๋ฅผ ์ง์ํ๋ ๊ฒฝ์ฐ Non-local variables)
- ์ฃผ๋ก Stack segment์ ํ ๋น ๋ฐ ํ์ฉ(recusion์ ์ง์ํ๊ธฐ ์ํด)
- "Typical" activation record instance : instance format์ ๊ณ ์ , size๋ ๊ฐ๋ณ
- Local variables : size๊ฐ ๊ฐ๋ณ์ ์ธ ํฌ๊ธฐ๋ฅผ ๊ฐ์ง, local variables ์์ ๋ฐ๋ผ ARI ์ฌ์ด์ฆ๊ฐ ๋ฌ๋ผ์ง์ ์์
- Parameters : formal parameters, parameter ์์ ๋ฐ๋ผ ์ด ๋ํ ๊ฐ๋ณ ์ฌ์ด์ฆ๋ฅผ ๊ฐ์ง
- Dynamic link : ํด๋น instance๊ฐ ์์ฑ๋๊ธฐ ์ด์ (Call ์์ )์ Stack์ top address(non-code part)๊ฐ ์ด๋์๋์ง๋ฅผ ์ ์ฅ(ARI deallocation ์ดํ ์ด๋๋ก ๊ฐ์ผํ ์ง ๋ชจ๋ฅด๊ธฐ ๋๋ฌธ) == Environment pointer
- Return address : code part๊ฐ ํ ๋น ํด์ ์ดํ code-part๊ฐ ๋์๊ฐ ์์น๋ฅผ ์ง์
- non-code๋ถ๋ถ(data part)์ ARI ๊ทธ๋ฆฌ๊ธฐ
- Return address, Dynamic link, parameters, local variables ์์ผ๋ก ๊ทธ๋ฆฌ๊ธฐ
- Dynamic Chain : Stack์ dynamic link๋ฅผ ๋ฐ๋ผ์ ์์ฑ๋๋ subprogram ํธ์ถ ์์ (call-chain์ด๋ผ๊ณ ๋ ํจ)
- Local offset : local variable์ EP์ ์์ ์ฃผ์๋ก ๋ถํฐ ์ผ๋ง๋ ๋จ์ด์ ธ ์๋์ง๋ก ์ ๊ทผ ๊ฐ๋ฅ. ๋จ์ด์ ธ ์๋ ๊ฑฐ๋ฆฌ๋ฅผ local offset์ด๋ผ๊ณ ํจ
- recursion์ด ์๋ค๋ฉด functional value ํญ๋ชฉ์ ARI format์ ์ถ๊ฐํ์ฌ ๊ฐ์ ๊ณ์ฐ
- Nested subprogram์ ํ์ฉํ๋ ์ธ์ด์์์ ARI(nonlocal variable ์ฐธ์กฐ)
- static scope, dynamic scope rule์ ๋ฐ๋ผ ํด๋นํ๋ ARI ์ฐพ๊ธฐ
- ARI๋ด ์์ ํด๋นํ๋ variable์ offset์ ์ฐพ๊ธฐ
- Static scoping
- offset์ code๋ฅผ ๋ณด๋ฉด ์์ ์ enclosing subprogram์ ๋ณด๋ฉด ์ฐพ๊ธฐ ์ฌ์
- ๋ฌธ์ ๋ activation record๋ฅผ ์ฐพ๋ ๊ฒ
static link
field๋ฅผ activation record format์ ์ถ๊ฐ- ์์ ์ ๊ฐ์ธ๊ณ ์๋ ์์ subprogram์ ๊ฐ๋ฅดํค๋๋ก ์ค์
- ์ด๋ฌํ static link์ ์ฐ์์
static chain
์ด๋ผ๊ณ ํจ. static chain ์ ํ์ํ๋ฉด nonlocal variable์ ์ฐพ์ ์ ์์ Static depth
๋๋ chain offset์ static scope ๊ด์ ์์ ๋ช ๋ฒ static link๋ฅผ ๋ฐ๋ผ๊ฐ์ผ ํด๋น nonlocal variable์ ์ฐพ์ ์ ์๋์ง์ ๋ํ ๊ฐ๋
- ๋ฐ๋ผ์ static scope์์๋
(Static depth, local offset)
์ผ๋ก nonlocal variable์ ์ฐธ์กฐ- ex)
(2, 3)
์ static link์ 2๋ฒ ๋ฐ๋ผ๊ฐ ARI์ 3๋ฒ์งธ ํญ๋ชฉ์ด ์ฐธ์กฐํ๊ณ ์๋ variable์ด๋ค.
- ex)
- example) p.29 ์ฐธ์กฐ
โป Dynamic link๋ ARI stack์ top์ฃผ์, Static link๋ ARI์ begining ์ฃผ์๋ฅผ ๊ฐ๋ฆฌํด - Static link๋ฅผ ์ฐพ๋ ๋ฐฉ๋ฒ
- Dynamic Chain ์ ์ฒด๋ฅผ ์ดํด๋ณด๋ ํ์
- nonlocal variable์ ์ฐธ์กฐํ๋ ๊ฒ๊ณผ ๊ฐ์ด static depth๋ฅผ ๋ฏธ๋ฆฌ ๊ณ์ฐํ์ฌ ์ฐธ์กฐํ๋ ๋ฐฉ์
๋๋ฌด ์์ธํ ์ ํ์์๋ค
- static chain์ ์ด์ฉํ๋ ๋ฐฉ์์ ๋๋ฆฌ๋ค -> C์์๋ nested subprogram ์ง์์ํจ
- Dynamic scoping
- Deep Access : dynamic link๋ฅผ ๋ฐ๋ผ๊ฐ๋ฉฐ ํด๋นํ๋ nonlocal variable์ด ์กด์ฌํ๋ ์ง ํ์ธ
- Chain์ ๊ธธ์ด๊ฐ ๊ธธ์ด, ์๊ฐ์ด ๋๋ฌด ์ค๋ ๊ฑธ๋ฆด ์ ์์
- ARI์ ๋ณ์์ ๊ฐ ๋ฟ๋ง ์๋๋ผ ์ด๋ฆ๋ ๊ธฐ์ตํ๊ณ ์์ด์ผ ํ๋ฏ๋ก ์ถ๊ฐ์ ์ธ ๊ณต๊ฐ์ด ํ์ํ๋ค.
- Shallow Access : ๋ณ์ ๋ณ Stack์ ์ ์งํด, stack์ top์ผ๋ก ํด๋น ๋ณ์๊ฐ ๋ง์ง๋ง์ผ๋ก ์ ์ธ๋ ๊ณณ์ด ์ด๋์ง ์ฐพ์๋
- ์๋๋ ๋น ๋ฅด์ง๋ง, ๋๋ฌด๋ ํฐ ์ฉ๋์ ์ฌ์ฉ
- ๊ตฌํ์ ์ด๋ ค์์ด ์์
- Deep Access : dynamic link๋ฅผ ๋ฐ๋ผ๊ฐ๋ฉฐ ํด๋นํ๋ nonlocal variable์ด ์กด์ฌํ๋ ์ง ํ์ธ
- blocks : ์ด๋ฆ์ด ์๋ subprogram
- implementation ๋ฐฉ์์ด subprogram์ ์์ฑํ๋ ๊ฒ๊ณผ ๋๊ฐ์ง๋ง ์ด๋ฆ์ด ์๋ค.
- ๊ทธ๋ฌ๋, ARI์ ๋ง๋ค๊ฑฐ๋ ํด์ ํ๋๋ฐ ๋๋ ์ค๋ฒํค๋๊ฐ ํฌ๊ธฐ ๋๋ฌธ์ ๊ตณ์ด ๊ถ์ฅํ์ง ์๋๋ค.
- ํ๋ก๊ทธ๋๋จธ์ ๊ฐ๋ ์ฑ์ ์ฆ๊ฐํ๋ค.
Algorithm 13. NP-Completeness
- ํ์ค์ ์ธ ์๊ฐ : ๋คํญ์ ์๊ฐ(n์ ๋คํญ์์ผ๋ก ํ์๋๋)
- ๋น๋คํญ์ ์๊ฐ์ ์ : 2n, n!
- YES/No ๋ฌธ์ ์ ๋ฐ๋ณต์ผ๋ก ์ต์ ํ ๋ฌธ์ ๋ฅผ ๋๋ถ๋ถ ํด๊ฒฐํ ์ ์์ผ๋ฏ๋ก NP-Complete ์ด๋ก ์์ YES/NO๋ฅผ ์ค์ ์ผ๋ก ๋ค๋ฃธ
- ๋คํญ์ ์๊ฐ ๋ณํ(Polynomial time reduction)
- ๋ต์ด ํญ์ ๋์ผํ(T->T, F->F) ๋ค๋ฅธ ๋ฌธ์ ๋ก ๋ฐ๊พธ๋ ๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ์ด ๋คํญ์ ์๊ฐ
- ์ฆ, ๋ ์ค ์ฌ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ฉด ๋ค๋ฅธ ๋ฌธ์ ๋ ์ฝ๊ฒ ํด๊ฒฐ์ด ๊ฐ๋ฅํ๋ค.
- ์ฌ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐ + ๋คํญ์ ์๊ฐ ๋ณํ => ์๋ ํ๊ณ ์ ํ๋ ๋ฌธ์ ํด๊ฒฐ
- ๋ฌธ์ ์ ๋ต์ ํญ์ ๋์ผํ๊ฒ ๋์์ผํ๋ค
- ๋ค๋ฅธ ๋ฌธ์ ๋ก ๋ณํํ๋๋ฐ ๋คํญ์ ์๊ฐ์ด ๊ฑธ๋ฆฐ๋ค.
- ์ 2๊ฐ์ง ์กฐ๊ฑด์ ๋ง์กฑํ๋ฉด ๋คํญ์ ์๊ฐ ๋ณํ์ด ๊ฐ๋ฅํ๋ค.
- P / NP
P
: Deterministic turing machine์์ ๋คํญ์ ์๊ฐ์ ํ๋ฆฌ๋ ๋ฌธ์ ์งํฉNP
: Non-deterministic turing machine์์ ๋คํญ์ ์๊ฐ์ ํ๋ฆฌ๋ ๋ฌธ์ ์งํฉ- Yes์ ํด๋นํ๋ ํด๋ฅผ ์ ๊ณตํ์๋, ์ด๊ฒ์ด Yes๊ฐ ๋ง๋ ์ง ๊ฒ์ฆํ๋๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ์ด ๋คํญ์ ์๊ฐ์ด๋ผ๋ฉด
NP ๋ฌธ์
- Yes์ ํด๋นํ๋ ํด๋ฅผ ์ ๊ณตํ์๋, ์ด๊ฒ์ด Yes๊ฐ ๋ง๋ ์ง ๊ฒ์ฆํ๋๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ์ด ๋คํญ์ ์๊ฐ์ด๋ผ๋ฉด
P
์ ๋ฌธ์ ๋ ๋ชจ๋ Non-deterministic turing machine์์๋ ๋คํญ์ ์๊ฐ์์ ํด๊ฒฐ (P => NP)- ๊ทธ๋ฌ๋ NP์ ๋ฌธ์ ๊ฐ P๊ฐ ๋๋์ง๋ ๋ฐํ์ง์ง ์์ (NP !=> P)
NP-complete
/NP-hard
NP-hard
: ๋ชจ๋ NP๋ฌธ์ ๊ฐ ๋ฌธ์ L๋ก ๋คํญ์ ์๊ฐ์ ๋ณํ๊ฐ๋ฅํ๋ฉด L์ NP-hardNP-complete
- L์ด NP์ด๊ณ ,
- L์ด NP-hard์ด๋ฉด L์ NP-complete์ด๋ค.
NP-complete
๋NP-hard
์ ํฌํจ๊ด๊ณ์ด๋ค.(NP-complete => NP-hard)- ๊ทธ๋ฌ๋ ๋๋ถ๋ถ์ ๋ฌธ์ ๊ฐ NP๋ผ๋ ๊ฒ์ ์ฆ๋ช
ํ๋ ๊ฒ์ ์ด๋ ต์ง ์๊ธฐ ๋๋ฌธ์
NP-hard
์ ์ง์ค - ๋ค๋ฅธ
NP-hard
๋ฌธ์ ๋ฅผ ์ด์ฉํ๋ ๋ฐฉ๋ฒ์ผ๋ก๋NP-hard
์ ๋ฆฌ ๊ฐ๋ฅ
- ์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ๋
P
๋ฌธ์ ์ด์ง๋ง, ์ต์ฅ๊ฒฝ๋ก๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ๋NP
๋ฌธ์ ์ด๋ค. - NP ์ด๋ก ์ ์ ์ฉ์ฑ
- ์ด๋ค ๋ฌธ์ ๊ฐ ํ๊ธฐ ํ๋ค๋ค๋ ๊ฒ์ ํ์ธํ์ฌ, ์ฐจ์ ์ฑ ์ธ greedy(ํด๋ฆฌ์คํฑ)์๊ณ ๋ฆฌ์ฆ์ ์ฑํํ๋ค.
- NP์ NP-complete, hard์ ๊ด๊ณ
Algorithm 10. Graph algorithm
- ๊ทธ๋ํ ํํ ๋ฐฉ๋ฒ
- ์ธ์ ํ๋ ฌ : N*N ํ๋ ฌ๋ก edge์ ๊ฐ์ค์น ์ ์ฅ -> ๊ฐ์ฅ ๋น ๋ฅด๋, ๊ณต๊ฐ ๋ญ๋น
- ์ธ์ ๋ฆฌ์คํธ : ๋งํฌ๋ ๋ฆฌ์คํธ ํํ๋ก ๊ฐ์ ์ ๋ํ ์ ๋ณด๋ฅผ ์ ์ฅ -> ๊ฐ์ฅ ๊ณต๊ฐ ํจ์จ์ ์ด๋
- ์ธ์ ๋ฐฐ์ด
-๊ฐ ์ ์ ๋ง๋ค ๋ฐฐ์ด์ ํ ๋นํ์ฌ ์ธ์ ๋ฆฌ์คํธ์ ๋งํฌ๋ถ๋ถ์ ๊ณต๊ฐ์ ์ ์ฝํ๋ฉฐ ์๋์ ์ผ๋ก ๋น ๋ฅด๊ฒ ๊ฒ์์ด ๊ฐ๋ฅ
- ๊ฐ ์ ์ ์ ๊ธด ํ๋์ ๋ฐฐ์ด๋ก๋ ์ ์ฅ ๊ฐ๋ฅ(๊ฐ ์ ์ ์ ์ธ์ ์ ์ ์ ์ ์ฅ ์)
- ๊ทธ๋ํ์ ๋ณํ๊ฐ ์์ด์ผ ํจ์จ์
- ๊ทธ๋ํ์ ๋ชจ๋ ์ ์ ๋ฐฉ๋ฌธ(traversal)
- DFS(Depth-First-Search) =>
ฮ(|V| + |E|)
DFS(G) { for each v โ V visied[v] = NO; for each v โ V if(visited[v] = NO) then aDFS(v); // ๋ชจ๋ ์ ์ ์ด ๋ฐฉ๋ฌธ์ด ์๋ ๊ฒฝ์ฐ๊ฐ ์๊ธฐ ๋๋ฌธ์, // BFS๋ ๋ชจ๋ ์ ์ ๋ฐฉ๋ฌธ์ด ์๋ ๊ฒฝ์ฐ for๋ฌธ์ผ๋ก ์ ์ฒด ๋ฐฉ๋ฌธ } aDFS(v) { visited[v] = YES; for each x โ L(v) if(visited[x] = NO) then aDFS(x); }
- BFS(Breadth-First-Search)
BFS(G, s) { for each v โ V - {s} visited[v] = NO; visited[s] = YES; enqueue(Q, s); while(Q โ empty) { u = dequeue(Q); for each v โ L(u) if(visited[v] = NO) { visited[u] = YES; enqueue(Q, v); } } }
- DFS(Depth-First-Search) =>
- ์คํจ๋ ํธ๋ฆฌ(Spanning tree)
- ๊ทธ๋ํ์์ ํฌํจ๋๋ ์ ์ ๊ณผ ๊ฐ์ ๋ค๋ง ์ด์ฉํด์ ๋ง๋ ํธ๋ฆฌ(์ธ์ดํด์ด ์๋ ์ฐ๊ฒฐ ๊ทธ๋ํ)
- ์ต์ ์คํจ๋ ํธ๋ฆฌ(MST) : ์คํจ๋ ํธ๋ฆฌ ์ค ๊ฐ์ ์ ๊ฐ์ค์น์ ํฉ์ด ์ต์์ธ ํธ๋ฆฌ
- ํ๋ฆผ ์๊ณ ๋ฆฌ์ฆ(Prim's algorithm)
- ํ์ฌ MST ์์ฑ์ด ์๋ฃ๋ ์ ์ ์งํฉ S์ V-S ์งํฉ ์ฌ์ด์ ์ต์ ๊ฐ์ค์น ๊ฐ์ ์ ์ฐพ์ ํด๋น ์ ์ ์ S์ ํฌํจ
- ํฌํจ๋ ์ ์ ์์ ์ธ์ ํ ์ ์ ๋ค์ ๋ํด(V-S์ ํฌํจ๋๋ ๊ฒฝ์ฐ) ์ด์์ ์ค์
- ์ํ์๊ฐ ๋ถ์
- Edge๋ฅผ ์ค์ฌ์ผ๋ก ๋ณด๋ฉด ๋ชจ๋ edge๋ 2๋ฒ์ฉ ํ์ธ, ๋ํ heap์ ๊ตฌ์ฑํ๊ณ ์ ๋ฐ์ดํธ ํ๋๋ฐ ํ์ํ ์๊ฐ O(log|V|)
- ๋ฐ๋ผ์ O(2|E|log|V|) => O(|E|log|V|)
- Spannig tree๋ฅผ ๊ตฌํ๊ธฐ ์ํด์
- ์ด์๋ ๋๋ง๋ค ์ด์์ํจ ์ ์ (parent)์ ๋ํ ๋ฐ์ดํฐ๋ฅผ ๋ณด๊ด
- ๋ง์ง๋ง์ ์ถ๋ ฅ
- ํฌ๋ฃจ์ค์นผ ์๊ณ ๋ฆฌ์ฆ(kruscal algorithm)
- Union-find๋ฅผ ์ด์ฉํด ๋ชจ๋ ์ ์ ์ ๊ฐ์์ ์งํฉ์ผ๋ก setํ๋ค
- ๊ฐ์ ์ ๊ฐ์ค์น๊ฐ ์์ ์๋ถํฐ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ค
- ์ต์ ๊ฐ์ค์น ๊ฐ์ ์ popํ์ฌ, ๋๊ฐ์ ๊ฐ์ ์ ์ ์ ์ด ๊ฐ์ ์งํฉ์ ์๋์ง ํ์ธํ๋ค.(์ฌ์ดํด์ด ํ์ฑ๋๋์ง)
- ๋ค๋ฅธ ์งํฉ์ ์๋ค๋ฉด ๋ ์งํฉ์ ํ๋๋ก ํฉ์น๋ค.
- ๊ฐ์ ์ด vertex-1๊ฐ ๋ณด๋ค ์์ ๋์(ํธ๋ฆฌ๊ฐ ๋๊ธฐ ์ ์) ๋ฐ๋ณต
- ์์ ์ ๋ ฌ (topological sort)
- cycle์ด ์๋ ์ ํฅ ๊ทธ๋ํ์์(DAG) partial ordered set์ ์์๋๋ก ์ ๋ ฌํ๊ธฐ ์ํด ์ฌ์ฉ
- ์๊ณ ๋ฆฌ์ฆ 1 : ์ง์ ๊ฐ์ ์ด ์๋ ์ ์ ์ ์ ํํ๋ ์๊ณ ๋ฆฌ์ฆ(|V|+|E|)
- ์๊ณ ๋ฆฌ์ฆ 2 : DFS๋ฐฉ๋ฌธ ์์์ ์ญ์์ผ๋ก ์ถ๋ ฅํ๋ ์๊ณ ๋ฆฌ์ฆ(|V|+|E|)
- ์ต๋จ ๊ฒฝ๋ก(Shortest path)
-
๊ฐ์ ๊ฐ์ค์น์ ํฉ์ด ์์์ธ ์ธ์ดํด์ด ์์ด์ผ ๋ฌธ์ ์ ์ ๊ฐ๋ฅ
-
V์ U์ฌ์ด์ ์ต๋จ ๊ฒฝ๋ก๋ฅผ ๊ตฌํ๊ธฐ ์ํด์ V๋ถํฐ U์ฌ์ด์ ์กด์ฌํ ์ ์๋ ๋ชจ๋ ์ ์ ๊ณผ์ ์ต๋จ ๊ฑฐ๋ฆฌ๋ฅผ ์์์ผ ํ๋ค.
-
๋ฐ๋ผ์, ํน์ ์ ์ U๋ถํฐ V์ ์ต๋จ๊ฒฝ๋ก๋ง์ ๊ตฌํ๋ ์๊ณ ๋ฆฌ์ฆ์ ์๋ฏธ๊ฐ ์๋ค.
-
๋จ์ผ ์์์ ์ต๋จ๊ฒฝ๋ก
- ๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ : ์์ ๊ฐ์ค์น๋ฅผ ํ์ฉํ์ง ์์
- ๋ฒจ๋ง-ํฌ๋ ์๊ณ ๋ฆฌ์ฆ : ์์ ๊ฐ์ค์น๋ฅผ ํ์ฉ
- ์ธ์ดํด์ด ์๋ ๊ทธ๋ํ(DAG)์ ์ต๋จ ๊ฒฝ๋ก : ์กฐ๊ธ ๋ ํจ์จ์ ์ธ ๋ฐฉ๋ฒ
-
๋ชจ๋ ์ ์ต๋จ ๊ฒฝ๋ก
- ํ๋ก์ด๋-์์ฌ ์๊ณ ๋ฆฌ์ฆ
-
๋ค์ต์คํธ๋ผ ์๊ณ ๋ฆฌ์ฆ
- ํ๋ฆผ ์๊ณ ๋ฆฌ์ฆ๊ณผ ๊ฑฐ์ ์ ์ฌํ ๋ฐฉ์์ผ๋ก ๋์
- ์ฐจ์ด์ ์ ์ด์ ์กฐ๊ฑด์ ๊ธฐ์กด u๊น์ง์ ์ต๋จ ๊ฒฝ๋ก๋ฅผ ํฉํ์ฌ ๋น๊ตํ๋ ๋ถ๋ถ
- ํ๋ฆผ์ MST ํ์ธํ๋ ๊ฒ๊ณผ ๋์ผํ๊ฒ prev(parent) ๊ฐ์ ๋๊ณ ๋ง์ง๋ง์ ์ถ๋ ฅํ๋ฉด ์ต๋จ ๊ฒฝ๋ก๋ฅผ ํ์ธํ ์ ์์
- ์ํ์๊ฐ๋ ํ๋ฆผ๊ณผ ๋์ผ
-
๋ฒจ๋ง-ํฌ๋ ์๊ณ ๋ฆฌ์ฆ
- |V|-1ํ ๋์์ ๋ฐ๋ณต ์ํํจ
- 1ํ ์ํํ ๋๋ง๋ค ์์ ์ ์ r๋ก๋ถํฐ 1ํ์ ๊ฐ์ ์ ๊ฑฐ์ณ ๊ฐ ์ ์๋ ์ ์ ์ ์ต๋จ๊ฑฐ๋ฆฌ๋ฅผ ๋ณด์ฅ
- ์์ ๊ฐ์ ๋ ๊ณ์ฐ์ด ๊ฐ๋ฅํจ
- |V|ํ ์ด์ ๋ฐ๋ณตํ์ฌ๋ ์ด์์ด ๊ณ์ฐ ๋ฐ์ํ๋ ๊ฒฝ์ฐ ๊ทธ๋ํ์ ์์ ์ฌ์ดํด์ด ์กด์ฌ.
if (d[u] + w[u, v] < d[v]) { d[v] = d[u] + w[u, v] prev[v] = u; }
-
ํ๋ก์ด๋-์์ฌ ์๊ณ ๋ฆฌ์ฆ
- dijk : {v1, v2, v3, ... , vk} ์ ์ํ๋ ์ ์ ๋ค๋ง ๊ฑฐ์ณ, vi์์ vj๋ก ๊ฐ๋ ์ต๋จ๊ฒฝ๋ก ๊ธธ์ด
- dijk = min{dijk-1, dikk-1 + dkjk-1}
- k ์ ์ ์ ํฌํจํ์ง ์๊ฑฐ๋, ํฌํจํ๋ ๊ฒฝ๋ก ์ค ์ต๋จ๊ฑฐ๋ฆฌ
FloydWarshall(G) { for i = 1 to n for j = 1 to n d0ij = wij; for k = 1 to n for i = 1 to n for j = 1 to n dkij = min{d<sub>ij</sub><sup>k-1</sup>, d<sub>ik</sub><sup>k-1</sup> + d<sub>kj</sub><sup>k-1</sup>} //์์ min ์ฐธ์กฐ }
- DAG์์ ์ต๋จ ๊ฒฝ๋ก ๊ตฌํ๊ธฐ
- ๊ทธ๋ํ์ ๋ชจ๋ ์ ์ ์ ์ต๋จ๊ฒฝ๋ก๋ฅผ ๋ฌดํ์ผ๋ก ์ ์ฅํด๋๊ณ ,
- ๊ทธ๋ํ๋ฅผ ์์์ ๋ ฌํ์ฌ ์์ ์ ๋ ฌ ์์๋๋ก ์ด์์ ์งํ
- ์์ ์ ์ ์ผ๋ก๋ถํฐ ์ต๋จ๊ฒฝ๋ก์ ๊ธธ์ด๋ฅผ ๊ตฌํ ์ ์๊ฒ๋จ
- ์ํ์๊ฐ์ ์์์ ๋ ฌ์ด ์ฃผ์ํจ(|V|+|E|)
- ๊ฐ์ฐ๊ฒฐ์์ ๊ตฌํ๊ธฐ
- ๊ฐ์ฐ๊ฒฐ์์ : ํน์ ์ ์ ์ ์ฌ์ด์ ์๋ฐฉํฅ์ผ๋ก ๊ฒฝ๋ก๊ฐ ์กด์ฌํ๋ฉด ๊ฐ์ฐ๊ฒฐ ์์
- ์๊ณ ๋ฆฌ์ฆ
- ๊ทธ๋ํ G์์ DFS๋ฅผ ์ํํ์ฌ ์ ์ v์ ์๋ฃ์๊ฐ f[v]๋ฅผ ๊ณ์ฐํ๋ค.
- G์ ๋ชจ๋ ๊ฐ์ ์ ๋ฐฉํฅ์ ๋ฐ๋๋ก ํ์ฌ GR์ ๋ง๋ ๋ค.
- DFS(GR)์ ๋ค์ ์ํํ๋ค
- DFS(GR)์์ ๋ง๋ค์ด ๋ถ๋ฆฌ๋ ํธ๋ฆฌ๋ค์ ๊ฐ ๊ฐ์ฐ๊ฒฐ์์๋ก ๋ฐํํ๋ค.
-
์ํํ ์ฝ๋ฉ์ฌ์ ๊ฐ์ ๋ด์ฉ ์ ๋ฆฌ(์ปดํจํฐ๊ณตํ)
- ๊ฐ๋น์ง ์ปฌ๋ ํฐ
- memory leak : ํ๋ก๊ทธ๋๋จธ๊ฐ ๋น์์ค์ผํ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ๋น์ฐ์ง ์์ ์๊ธฐ๋ ๋ฌธ์
- Mark & Sweep ๋ฐฉ์ : ํฌ์ธํฐ๋ฅผ ๋ฐ๋ผ๊ฐ ๋ฃจํธ์์ฌ ์ฐ๊ฒฐ๋์ง ์์ ๋ถ๋ถ ๋ฉ๋ชจ๋ฆฌ ํด์
- Reference counting : ํ ์์๊ฐ ๋ค๋ฅธ ์์์๊ฒ ์ผ๋ง๋ ์ฐธ์กฐ๋์๋์ง ์ธ๋ ๋ฐฉ๋ฒ(0๋๋ฉด ํด์ )
- garbarge collector๋ ์๋ฒฝํ์ง ์์ (ํ๋ก๊ทธ๋๋จธ์ ๊ด๋ฆฌ๊ฐ ํ์)
- managed language : ๊ฐ๋น์ง ์ปฌ๋ ํฐ๊ฐ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ์๋์ผ๋ก ๊ด๋ฆฌํด์ฃผ๋ ์ธ์ด(Java ๋ฑ)
- unmanage language : ํ๋ก๊ทธ๋๋จธ๊ฐ ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ๋ฅผ ์๋์ ์ผ๋ก ํด์ค์ผํ๋ ์ธ์ด
- ์ํ ์ฐธ์กฐํ์ง ๋ง ๊ฒ
- memory leak : ํ๋ก๊ทธ๋๋จธ๊ฐ ๋น์์ค์ผํ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ๋น์ฐ์ง ์์ ์๊ธฐ๋ ๋ฌธ์
- ์์ธ์ฒ๋ฆฌ
try - catch - finally
์์finally
๋ ํจ์๊ฐ ๋ฆฌํด๋ ๊ฒฝ์ฐ์๋ ์คํ ํ ๋ฐํ
- ๋ณ์์ ์ค์ฝํ (const, let, var)
var
๋ ES6 ์ด์ ์ JS์์ ์ฌ์ฉ๋๋ ๋ณ์ ์ ์ธ ๋ฌธ๋ฒ, ์ฌ์ ์ธ ๊ฐ๋ฅconst
๋ ์์๋ก ํ๋ฒ ์ ์ธ๋๋ฉด ๊ฐ ๋ณ๊ฒฝ ๋ถ๊ฐlet
๋var
๋ฅผ ๋์ฒดํ์ง๋ง ์ฌ์ ์ธ ๋ถ๊ฐconst
์let
์ ๋ธ๋ก ๋ ๋ฒจ ์ค์ฝํ๋ฅผ ๊ฐ์ง- ๋ฐ๋ผ์ ๋ธ๋ก ์ธ๋ถ๋ก ์ ๊ทผ ๋ถ๊ฐ
- ๋ธ๋ก ์ข ๋ฃ ์ ๋ณ์ ํ ๋น ํด์
- ์ ์ธ๋๊ธฐ ์ด์ ์ฌ์ฉ(hoisting) ๋ถ๊ฐ
- ๋ฐ๋ฉด์
var
๋ ํจ์ ์ค์ฝํ๋ฅผ ๊ฐ์ง๊ณ ์์- ๋ฐ๋ผ์ ํจ์ ๋ด์์๋ ์ด๋๋ ์ ๊ทผ์ด ๊ฐ๋ฅ
- ๋ณ์๊ฐ ํด์ ๋์ง ์์
- hoisting์ ํตํด ์ ์ธ๋๊ธฐ ์ด์ ์๋ ๊ฐ ์ฌ์ฉ ๊ฐ๋ฅ
- ์ฌ๊ทํจ์
- ์ฌ๊ทํจ์๋ ๊ณ์ ์ด์ด์ง ๊ฒฝ์ฐ ์คํ ์ค๋ฒํ๋ก์ ์ฑ๋ฅ ์ ํ์ ๋ฌธ์ ๊ฐ ๋ฐ์
return
์์ ๋ณ๋์ ๊ณ์ฐ์ด ํ์ํ์ง ์์ ์ฌ๊ทํจ์๋ **๊ผฌ๋ฆฌ ์ฌ๊ท ์ต์ ํ(Tail Recursion Optimization)**๋ก ์ ํ์๊ฐ ๊ณ์ฐ ๊ฐ๋ฅ
- ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ
- ๋๊ธฐ์ ์ผ๋ก ์๋ํ๋ค๋ ๊ฒ์ ์ฝ๋๊ฐ ๋ฐ๋์ ์์ฑ๋ ์์๋๋ก ์คํ๋จ์ ์๋ฏธ
- ๋ฐ๋ฉด ๋น๋๊ธฐ์ ์ผ๋ก ์๋ํ๋ค๋ ๊ฒ์ ์ฝ๋๊ฐ ์์๋๋ก ์คํ๋์ง ์์์ ์๋ฏธ
- ๋๊ธฐ์ ์ผ๋ก ์๋ํ๊ธฐ ์ํด์๋ ์ค๋ ๋, ํ๋ก์ธ์ค๊ฐ 2๊ฐ ์ด์์ด ๋์์ ์คํ๋์ด์ผ ํจ
function asyncBlackBeanTimer(seconds) { console.log("์ง์ฅ๋ฉด ๋ฐฐ๋ฌ๋จ"); setTimeout( function() { // Call-back function console.log("์์ฌ ์๋ฃ"); }, seconds*1000; //ํด๋น ์ฝ๋ ์คํ ํ ); console.log("๋ฐฐ๋ฌ๋ถ ๋ ๋จ"); } asyncBlackBeanTimer(1);
-
๋น๋๊ธฐ ์์ ์ด ๋๋ ์ดํ์๋ Call-back function์ ํธ์ถํด ์ดํ ์ ๋ฌด ์ฒ๋ฆฌ
-
์๋ฐ์คํฌ๋ฆฝํธ ์์ง์ ์ฑ๊ธ ์ค๋ ๋๋ก ๋์, ํ์ง๋ง JS ์์ง๊ณผ Web API๊ฐ ๋์์ ๋์
-
Web API๋ JS์์ง๊ณผ๋ ๋ฌ๋ฆฌ ๋์์ ์ฌ๋ฌ์์ ์ ์ฒ๋ฆฌ ๊ฐ๋ฅ(๋ฉํฐ์ค๋ ๋)
-
๋ฐ๋ผ์ ๋น๋๊ธฐ ์์ ์ ๊ฒฝ์ฐ Web API์์ ์ฒ๋ฆฌ ํ Task-Queue๋ก ์ฝ๋ฐฑํจ์(์ฑ๊ธ ์ค๋ ๋ ์ฒ๋ฆฌ ๊ฐ๋ฅํ๋๋ก) ๋ณด๋
-
๋น๋๊ธฐ ์์ ์ Web API์์ ์ฒ๋ฆฌํ๋ ๋์ JS์์ง์ ๋๊ธฐ ์์ ์ ์ํ
-
์ด๋ฒคํธ ๋ฃจํ๋ Task-queue์ ๋์ฐฉํ ์์๋๋ก ์ฝ๋ฐฑํจ์๋ค์ ๋ค์ JS ์ฒ๋ฆฌ ์์ง์ผ๋ก ๋ณด๋
-
์ฝ๋ฐฑ ์ง์ฅ
- ๋น๋๊ธฐ ์์ ์ด ์ฌ๋ฌ ๋ฒ ์ค๋ณต๋ ๊ฒฝ์ฐ(DB์ ์ค๋ณต ์ ๊ทผ ๋ฑ), ๋น๋๊ธฐ ์์ ํจ์๊ฐ ๊ต์ฅํ ๋ณต์กํด์ง
- ํจ์ ๋ด์ ํจ์๊ฐ ๋ฐ๋ณต๋๋ ํํ
- ๋น๋๊ธฐ ํ๋ก๊ทธ๋๋ฐ ํน์ฑ์ ๋๋ฒ๊น ๋ ํ๋ฆ
- ๋ฐ๋ผ์ ES6๋ถํฐ๋ Promise ๊ฐ์ฒด ์ฌ์ฉ (polyfill ๋ฑ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์์ด๋ ๋์์ํจ)
- ๋ฐ๋ผ์ IE์์๋ ์ฌ์ฉํ ์ ์์
// ๋น๋๊ธฐ ํจ์ ์์ฑ function ํ์์ ๋ณด_์กฐํ(ํ์_ํ๋ฒ) { return new Promise(function (resolve, reject) { ajax(baseUrl + "/student-info/" + ํ์_ํ๋ฒ, // ์ํํ ๋น๋๊ธฐ ์์ function(response) { // ์ฝ๋ฐฑ ํจ์ resolve(response); } ); }); } // ํธ์ถ ํ์์ ๋ณด_์กฐํ("12345") .then(function (ํ์_์ ๋ณด) { //do something return ๊ณ ๊ต_DB_์ฃผ์_์กฐํ(ํ์_์ฃผ๋ฏผ๋ฒํธ, ๊ณ ๊ต๋ช ) }) .then(function(ํ์_์ฃผ๋ฏผ๋ฒํธ_AND_๊ณ ๊ต_DB_์ฃผ์) { return ๊ณ ๊ต์ฌํ์_์๊ฐ์์ _์กฐํ( ํ์_์ฃผ๋ฏผ๋ฒํธ_AND_๊ณ ๊ต_DB_์ฃผ์[0], ํ์_์ฃผ๋ฏผ๋ฒํธ_AND_๊ณ ๊ต_DB_์ฃผ์[1] ) }) .then(function(...)) { //... }
- ์์ ๊ฐ์ด ์ฒด์ด๋ ๋ฐฉ์์ผ๋ก ์ฐ์๋ ๋น๋๊ธฐ ์์ ์ ์ฒ๋ฆฌ
- ES8์์๋
async/await
๊ฐ ์ถ๊ฐ๋์ด ๋์ฑ ์ฝ๊ฒ ํธ์ถ
async function ๊ณ 3_์ํ๊ต์ฌ_์ฐพ๊ธฐ(ํ์_ํ๋ฒ) { //async๋ ๋น๋๊ธฐ ํจ์์์ ์๋ฏธ let ํ์_์ ๋ณด = await ํ์์ ๋ณด_์กฐํ(ํ์_ํ๋ฒ); // await ์ฌ์ฉ ์ ์ฝ๋ฐฑ์ด ๋ถ๋ ค์ ๋ฐํ๋ ๋๊น์ง ๋๊ธฐ ํ ๋ฐํ ์ ๋ค์ ์คํ let ๊ณ ๊ณ _DB_์ฃผ์ = await ๊ณ ๊ต_DB_์ฃผ์_์กฐํ(ํ์_์ ๋ณด["๊ณ ๊ต๋ช "]); //... console.log(๋ด๋น๊ต์ฌ); }