Showing
22 changed files
with
963 additions
and
0 deletions
.gitignore
0 → 100644
1 | + | ||
2 | +# Created by https://www.gitignore.io/api/c++,macos,python | ||
3 | + | ||
4 | +### C++ ### | ||
5 | +# Prerequisites | ||
6 | +*.d | ||
7 | + | ||
8 | +# Compiled Object files | ||
9 | +*.slo | ||
10 | +*.lo | ||
11 | +*.o | ||
12 | +*.obj | ||
13 | + | ||
14 | +# Precompiled Headers | ||
15 | +*.gch | ||
16 | +*.pch | ||
17 | + | ||
18 | +# Compiled Dynamic libraries | ||
19 | +*.so | ||
20 | +*.dylib | ||
21 | +*.dll | ||
22 | + | ||
23 | +# Fortran module files | ||
24 | +*.mod | ||
25 | +*.smod | ||
26 | + | ||
27 | +# Compiled Static libraries | ||
28 | +*.lai | ||
29 | +*.la | ||
30 | +*.a | ||
31 | +*.lib | ||
32 | + | ||
33 | +# Executables | ||
34 | +*.exe | ||
35 | +*.out | ||
36 | +*.app | ||
37 | + | ||
38 | +### macOS ### | ||
39 | +# General | ||
40 | +.DS_Store | ||
41 | +.AppleDouble | ||
42 | +.LSOverride | ||
43 | + | ||
44 | +# Icon must end with two \r | ||
45 | +Icon | ||
46 | + | ||
47 | +# Thumbnails | ||
48 | +._* | ||
49 | + | ||
50 | +# Files that might appear in the root of a volume | ||
51 | +.DocumentRevisions-V100 | ||
52 | +.fseventsd | ||
53 | +.Spotlight-V100 | ||
54 | +.TemporaryItems | ||
55 | +.Trashes | ||
56 | +.VolumeIcon.icns | ||
57 | +.com.apple.timemachine.donotpresent | ||
58 | + | ||
59 | +# Directories potentially created on remote AFP share | ||
60 | +.AppleDB | ||
61 | +.AppleDesktop | ||
62 | +Network Trash Folder | ||
63 | +Temporary Items | ||
64 | +.apdisk | ||
65 | + | ||
66 | +### Python ### | ||
67 | +# Byte-compiled / optimized / DLL files | ||
68 | +__pycache__/ | ||
69 | +*.py[cod] | ||
70 | +*$py.class | ||
71 | + | ||
72 | +# C extensions | ||
73 | + | ||
74 | +# Distribution / packaging | ||
75 | +.Python | ||
76 | +build/ | ||
77 | +develop-eggs/ | ||
78 | +dist/ | ||
79 | +downloads/ | ||
80 | +eggs/ | ||
81 | +.eggs/ | ||
82 | +lib/ | ||
83 | +lib64/ | ||
84 | +parts/ | ||
85 | +sdist/ | ||
86 | +var/ | ||
87 | +wheels/ | ||
88 | +*.egg-info/ | ||
89 | +.installed.cfg | ||
90 | +*.egg | ||
91 | +MANIFEST | ||
92 | + | ||
93 | +# PyInstaller | ||
94 | +# Usually these files are written by a python script from a template | ||
95 | +# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
96 | +*.manifest | ||
97 | +*.spec | ||
98 | + | ||
99 | +# Installer logs | ||
100 | +pip-log.txt | ||
101 | +pip-delete-this-directory.txt | ||
102 | + | ||
103 | +# Unit test / coverage reports | ||
104 | +htmlcov/ | ||
105 | +.tox/ | ||
106 | +.coverage | ||
107 | +.coverage.* | ||
108 | +.cache | ||
109 | +nosetests.xml | ||
110 | +coverage.xml | ||
111 | +*.cover | ||
112 | +.hypothesis/ | ||
113 | +.pytest_cache/ | ||
114 | + | ||
115 | +# Translations | ||
116 | +*.mo | ||
117 | +*.pot | ||
118 | + | ||
119 | +# Django stuff: | ||
120 | +*.log | ||
121 | +local_settings.py | ||
122 | +db.sqlite3 | ||
123 | + | ||
124 | +# Flask stuff: | ||
125 | +instance/ | ||
126 | +.webassets-cache | ||
127 | + | ||
128 | +# Scrapy stuff: | ||
129 | +.scrapy | ||
130 | + | ||
131 | +# Sphinx documentation | ||
132 | +docs/_build/ | ||
133 | + | ||
134 | +# PyBuilder | ||
135 | +target/ | ||
136 | + | ||
137 | +# Jupyter Notebook | ||
138 | +.ipynb_checkpoints | ||
139 | + | ||
140 | +# IPython | ||
141 | +profile_default/ | ||
142 | +ipython_config.py | ||
143 | + | ||
144 | +# pyenv | ||
145 | +.python-version | ||
146 | + | ||
147 | +# celery beat schedule file | ||
148 | +celerybeat-schedule | ||
149 | + | ||
150 | +# SageMath parsed files | ||
151 | +*.sage.py | ||
152 | + | ||
153 | +# Environments | ||
154 | +.env | ||
155 | +.venv | ||
156 | +env/ | ||
157 | +venv/ | ||
158 | +ENV/ | ||
159 | +env.bak/ | ||
160 | +venv.bak/ | ||
161 | + | ||
162 | +# Spyder project settings | ||
163 | +.spyderproject | ||
164 | +.spyproject | ||
165 | + | ||
166 | +# Rope project settings | ||
167 | +.ropeproject | ||
168 | + | ||
169 | +# mkdocs documentation | ||
170 | +/site | ||
171 | + | ||
172 | +# mypy | ||
173 | +.mypy_cache/ | ||
174 | +.dmypy.json | ||
175 | +dmypy.json | ||
176 | + | ||
177 | +### Python Patch ### | ||
178 | +.venv/ | ||
179 | + | ||
180 | +### Python.VirtualEnv Stack ### | ||
181 | +# Virtualenv | ||
182 | +# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/ | ||
183 | +[Bb]in | ||
184 | +[Ii]nclude | ||
185 | +[Ll]ib | ||
186 | +[Ll]ib64 | ||
187 | +[Ll]ocal | ||
188 | +[Ss]cripts | ||
189 | +pyvenv.cfg | ||
190 | +pip-selfcheck.json | ||
191 | + | ||
192 | + | ||
193 | +# End of https://www.gitignore.io/api/c++,macos,python |
01_algorithm_analysis/README.md
0 → 100644
01_algorithm_analysis/hw/README.md
0 → 100644
01_algorithm_analysis/hw/al_hw1
0 → 100755
No preview for this file type
01_algorithm_analysis/hw/al_hw1.cpp
0 → 100644
1 | +#include <iostream> | ||
2 | +#include <iomanip> | ||
3 | +#include <ctime> | ||
4 | +#include <cmath> | ||
5 | +using namespace std; | ||
6 | + | ||
7 | + | ||
8 | +// sorting algorithms | ||
9 | +void mergeSort(int n, int s[]); | ||
10 | +void merge(int h,int m, int u[], int v[], int s[]); | ||
11 | +void exchangeSort(int n, int s[]); | ||
12 | + | ||
13 | +// time checking algorithms | ||
14 | +double cpu_time ( void ); | ||
15 | +void functionTimeCheck(string sort_type, int s[], int sLength); | ||
16 | + | ||
17 | + | ||
18 | + | ||
19 | +int main() { | ||
20 | + | ||
21 | + cout << "Problem Scale : "; | ||
22 | + int input; // user input | ||
23 | + cin >> input; | ||
24 | + | ||
25 | + // create arrays | ||
26 | + int s_merge[input]; | ||
27 | + int s_ex[input]; | ||
28 | + | ||
29 | + // create random numbers to arrays | ||
30 | + srand(time(NULL)); | ||
31 | + for(int i=0; i<input; i++){ | ||
32 | + int rnum = rand()%1000; | ||
33 | + s_merge[i] = rnum; | ||
34 | + s_ex[i] = rnum; | ||
35 | + } | ||
36 | + | ||
37 | + // time check - merge sort | ||
38 | + functionTimeCheck("merge",s_merge, input); | ||
39 | + | ||
40 | + // print sorted values | ||
41 | + /* | ||
42 | + cout << "merge sort : ["; | ||
43 | + for(int i=0; i<input; i++){ | ||
44 | + if(i==(input-1)) | ||
45 | + cout << s_merge[i] << "]" << endl; | ||
46 | + else | ||
47 | + cout << s_merge[i] << ", "; | ||
48 | + } | ||
49 | + */ | ||
50 | + | ||
51 | + // time check - excahnge sort | ||
52 | + functionTimeCheck("exchange",s_ex, input); | ||
53 | + | ||
54 | + // print sorted values | ||
55 | + /* | ||
56 | + cout << "exchange sort : ["; | ||
57 | + for(int i=0; i<input; i++){ | ||
58 | + if(i==(input-1)) | ||
59 | + cout << s_ex[i] << "]" << endl; | ||
60 | + else | ||
61 | + cout << s_ex[i] << ", "; | ||
62 | + } | ||
63 | + */ | ||
64 | + | ||
65 | + return 0; | ||
66 | + | ||
67 | +} | ||
68 | + | ||
69 | +// Merge Sort | ||
70 | +void mergeSort(int n, int s[]){ | ||
71 | + int h = n/2; | ||
72 | + int m = n-h; | ||
73 | + | ||
74 | + if(n>1) { | ||
75 | + int leftHalf[h]; | ||
76 | + int rightHalf[m]; | ||
77 | + for(int i=0;i<h;i++) | ||
78 | + leftHalf[i] = s[i]; | ||
79 | + for(int i=0;i<m;i++) | ||
80 | + rightHalf[i] = s[h+i]; | ||
81 | + | ||
82 | + mergeSort(h,leftHalf); | ||
83 | + mergeSort(m,rightHalf); | ||
84 | + merge(h,m,leftHalf,rightHalf,s); | ||
85 | + } | ||
86 | +} | ||
87 | + | ||
88 | +// merge | ||
89 | +void merge(int h,int m, int u[], int v[], int s[]){ | ||
90 | + int i=0; int j=0; int k=0; | ||
91 | + | ||
92 | + while(i<=h-1 and j<=m-1){ | ||
93 | + if(u[i]<v[j]){ | ||
94 | + s[k]=u[i]; | ||
95 | + i+=1; | ||
96 | + } | ||
97 | + else{ | ||
98 | + s[k]=v[j]; | ||
99 | + j+=1; | ||
100 | + } | ||
101 | + | ||
102 | + k+=1; | ||
103 | + } | ||
104 | + if(i>h-1){ | ||
105 | + for(int ii=j; ii<m; ii++){ | ||
106 | + s[k]=v[ii]; | ||
107 | + k += 1; | ||
108 | + } | ||
109 | + } | ||
110 | + else{ | ||
111 | + for(int ii=i; ii<h; ii++){ | ||
112 | + s[k]=u[ii]; | ||
113 | + k += 1; | ||
114 | + } | ||
115 | + } | ||
116 | +} | ||
117 | + | ||
118 | + | ||
119 | +// Exchange Sort | ||
120 | +void exchangeSort(int n, int s[]){ | ||
121 | + for(int i=0; i<n-1; i++){ | ||
122 | + for(int j=i+1; j<n; j++){ | ||
123 | + if(s[i] > s[j]){ | ||
124 | + int temp = s[i]; | ||
125 | + s[i] = s[j]; | ||
126 | + s[j] = temp; | ||
127 | + } | ||
128 | + } | ||
129 | + } | ||
130 | +} | ||
131 | + | ||
132 | +double cpu_time ( ) { | ||
133 | + double value; | ||
134 | + value = ( double ) clock ( ) / ( double ) CLOCKS_PER_SEC; | ||
135 | + return value; | ||
136 | +} | ||
137 | + | ||
138 | +// time check and print | ||
139 | +void functionTimeCheck(string sort_type, int s[], int sLength){ | ||
140 | + | ||
141 | + double atime, btime; | ||
142 | + | ||
143 | + if(sort_type == "merge"){ | ||
144 | + atime = cpu_time(); | ||
145 | + mergeSort(sLength,s); | ||
146 | + btime = cpu_time(); | ||
147 | + cout << "merge sort : " << -atime + btime << endl; | ||
148 | + } | ||
149 | + else if(sort_type == "exchange"){ | ||
150 | + atime = cpu_time(); | ||
151 | + exchangeSort(sLength,s); | ||
152 | + btime = cpu_time(); | ||
153 | + cout << "exchange sort : " << -atime + btime << endl; | ||
154 | + } | ||
155 | +} | ||
156 | + |
01_algorithm_analysis/hw/al_hw1.py
0 → 100644
1 | +import time | ||
2 | +from random import * | ||
3 | + | ||
4 | +# Merge Sort | ||
5 | +def mergeSort(n, s): | ||
6 | + h=int(n/2) | ||
7 | + m=n-h | ||
8 | + | ||
9 | + | ||
10 | + if(n>1): | ||
11 | + leftHalf=s[:h] | ||
12 | + rightHalf=s[h:] | ||
13 | + mergeSort(h,leftHalf) | ||
14 | + mergeSort(m,rightHalf) | ||
15 | + merge(h,m,leftHalf,rightHalf,s) | ||
16 | + | ||
17 | +# merge | ||
18 | +def merge(h,m,u,v,s): | ||
19 | + i=j=k=0 | ||
20 | + while(i<=h-1 and j<=m-1): | ||
21 | + if(u[i]<v[j]): | ||
22 | + s[k]=u[i] | ||
23 | + i+=1 | ||
24 | + else: | ||
25 | + s[k]=v[j] | ||
26 | + j+=1 | ||
27 | + k+=1 | ||
28 | + if(i>h-1): | ||
29 | + for ii in range (j,m): | ||
30 | + s[k]=v[ii] | ||
31 | + k += 1 | ||
32 | + else: | ||
33 | + for ii in range (i,h): | ||
34 | + s[k]=u[ii] | ||
35 | + k += 1 | ||
36 | + | ||
37 | +# Exchange Sort | ||
38 | +def exchangeSort(n,s) : | ||
39 | + for i in range(n-1) : | ||
40 | + j = i+1 | ||
41 | + while j <= n-1 : | ||
42 | + if s[i] > s[j] : | ||
43 | + temp = s[i] | ||
44 | + s[i] = s[j] | ||
45 | + s[j] = temp | ||
46 | + j += 1 | ||
47 | + | ||
48 | + | ||
49 | +# time check function | ||
50 | +# sort_type : merge / exchange | ||
51 | +# s : random data set | ||
52 | +def functionTimeCheck(sort_type, s) : | ||
53 | + | ||
54 | + if sort_type == "merge" : | ||
55 | + stime = time.time() | ||
56 | + mergeSort(len(s),s) | ||
57 | + print('merge sort : %10.5f' % (time.time()-stime)) | ||
58 | + elif sort_type == "exchange" : | ||
59 | + stime = time.time() | ||
60 | + exchangeSort(len(s),s) | ||
61 | + print('exchange sort : %10.5f' % (time.time()-stime)) | ||
62 | + else : | ||
63 | + print("invalid input") | ||
64 | + | ||
65 | + | ||
66 | +# create random set | ||
67 | +n = input("Problem Scale : ") | ||
68 | +s = [] | ||
69 | +for i in range(n) : | ||
70 | + rand_value = randint(0,1000) | ||
71 | + s.append(rand_value) | ||
72 | + | ||
73 | +functionTimeCheck("merge",s) | ||
74 | +functionTimeCheck("exchange",s) |
1 | +def printMatrix(d): | ||
2 | + m = len(d) | ||
3 | + n=len(d[0]) | ||
4 | + | ||
5 | + for i in range(0,m): | ||
6 | + for j in range(0,n): | ||
7 | + print("%4d" % d[i][j]," ") | ||
8 | + print() | ||
9 | + | ||
10 | +#print float matrix | ||
11 | +def printMatrixF(d): | ||
12 | + n=len(d[0]) | ||
13 | + for i in range(0,n): | ||
14 | + for j in range(0,n): | ||
15 | + print("%5.2f" % d[i][j]," ") | ||
16 | + print() | ||
17 | + | ||
18 | +def print_inOrder(root): | ||
19 | + if not root: | ||
20 | + return | ||
21 | + print_inOrder(root.l_child) | ||
22 | + print(root.data) | ||
23 | + print_inOrder(root.r_child) | ||
24 | + | ||
25 | +def print_preOrder(root): | ||
26 | + if not root: | ||
27 | + return | ||
28 | + print(root.data) | ||
29 | + print_preOrder(root.l_child) | ||
30 | + print_preOrder(root.r_child) | ||
31 | + | ||
32 | +def print_postOrder(root): | ||
33 | + if not root: | ||
34 | + return | ||
35 | + | ||
36 | + print_postOrder(root.l_child) | ||
37 | + print_postOrder(root.r_child) | ||
38 | + print(root.data) |
01_algorithm_analysis/lecture lab/README.md
0 → 100644
1 | +# Algorithm Analysis | ||
2 | +- Lecture Lab | ||
3 | +- lecture2_P2_dvide_conquer_mergesort1.py | ||
4 | +- lecture2_P2_dvide_conquer_mergesort2.py | ||
5 | +- lecture3_P2_dvide_conquer_quick.py | ||
6 | +- lecture5_P3_dynamic_programming_bst.py | ||
7 | +- lecture7_P4_greedy.py | ||
8 | +- lecture8_P5_backtracking.py | ||
9 | +- lecture9_P5_backtracking.py | ||
10 | +- lecture10_P6_BB.py | ||
11 | +- lecture11_P7_Sorting.py | ||
12 | + | ||
13 | +- Import File | ||
14 | +- P3_utility.py | ||
15 | + | ||
16 | +- Warm Up Test Files | ||
17 | +- test_exsort.py | ||
18 | +- test.py | ||
19 | +- test2.py | ||
20 | + | ||
21 | +- Seperated Function - developing | ||
22 | +- pythonMathLog.py | ||
23 | + |
1 | +def kp(i, profit, weight): | ||
2 | + global bestset | ||
3 | + global maxprofit | ||
4 | + | ||
5 | + if( weight <= W and profit >maxp): | ||
6 | + maxp = profit | ||
7 | + bestset = include[:] | ||
8 | + # best = include는 best를 include의 reference로 만든다. | ||
9 | + # 한 번 동일한 값을 가진 후 그 이후는 계속 동일함. | ||
10 | + if(promising(i,weight,profit)): | ||
11 | + include[i+1]=1 | ||
12 | + kp(i+1,profit+p[i+1], weight+w[i+1]) | ||
13 | + include[i+1]=0 | ||
14 | + kp(i+1,profit, weight) | ||
15 | + | ||
16 | + | ||
17 | + | ||
18 | + | ||
19 | + | ||
20 | + | ||
21 | + | ||
22 | +def promising(i,weight,profit): | ||
23 | + global maxprofit | ||
24 | + | ||
25 | + if(weight >=W): | ||
26 | + return False | ||
27 | + else: | ||
28 | + j=i+1 | ||
29 | + bound = profit | ||
30 | + totweight = weight | ||
31 | + | ||
32 | + # item이 있고 그 무게 안에서 최대의 bound를 찾고 | ||
33 | + while((j <= n-1) and (totweight + w[j] <= W)): | ||
34 | + totweight += w[j] | ||
35 | + bound += p[j] | ||
36 | + j+=1 | ||
37 | + | ||
38 | + # k = j | ||
39 | + | ||
40 | + if(j <= n-1): | ||
41 | + bound += (W-totweight)*p[k]/w[k] # 최종 bound 검색 | ||
42 | + return bound > maxprofit | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
1 | +def insertionsort(n, s) : | ||
2 | + for i in range(1,n) : | ||
3 | + selected = s[i] | ||
4 | + j = i-1 | ||
5 | + | ||
6 | + # 선택된 값과 비교해서 s[j]가 더 크면, 계속 오른쪽으로 덮어쓴다. | ||
7 | + while(j>-1 and s[j]>selected) : | ||
8 | + s[j+1] = s[j] | ||
9 | + j-=1 | ||
10 | + | ||
11 | + # j가 -1로 만족못하고, 제자리 찾으면 while 나옴 | ||
12 | + s[j+1] = selected | ||
13 | + | ||
14 | + | ||
15 | +def selectionsort(n, s) : | ||
16 | + for i in range(0,n-1) : | ||
17 | + # i를 기준점으로 잡고 이후 가장 작은 값과 바꾼다. | ||
18 | + smallest = i | ||
19 | + for j in range(i+1,n) : | ||
20 | + | ||
21 | + # index만 기억하고 바꾸다가 | ||
22 | + if(s[j]<s[smallest]) : | ||
23 | + smallest = j | ||
24 | + | ||
25 | + # 마지막에 한번만 실제로 값을 바꾼다. | ||
26 | + temp = s[smallest] | ||
27 | + s[smallest] = s[i] | ||
28 | + s[i] = temp | ||
29 | + | ||
30 | + | ||
31 | + | ||
32 | +def exchangesort(n,s) : | ||
33 | + for i in range(0,n-1) : | ||
34 | + for j in range(i+1,n) : | ||
35 | + # 걍 매번 바꾸는 거. 그러니 선택정렬보다는 느릴 수 밖에 없다. | ||
36 | + if(s[i]>s[j]) : | ||
37 | + temp = s[i] | ||
38 | + s[i] = s[j] | ||
39 | + s[j] = temp | ||
40 | + | ||
41 | +def bubblesort(n,s) : | ||
42 | + for i in range(n-1,-1,-1) : | ||
43 | + for j in range(0,i) : | ||
44 | + | ||
45 | + # i는 최대 index로부터 감소하면서 j는 0부터 증가하고 버블버블 비교하고 가장 마지막에는 가장 큰 값이 할당되게 된다. | ||
46 | + if(s[j]>s[j+1]) : | ||
47 | + temp = s[j] | ||
48 | + s[j] = s[j+1] | ||
49 | + s[j+1] = temp | ||
50 | + | ||
51 | + | ||
52 | + | ||
53 | +s = [3,5,1,4,9879,8,2,55,42,6,99,99999,999,555] | ||
54 | + | ||
55 | + | ||
56 | + | ||
57 | +# insertionsort(len(s),s) | ||
58 | +# selectionsort(len(s),s) | ||
59 | +# exchangesort(len(s),s) | ||
60 | +bubblesort(len(s),s) | ||
61 | + | ||
62 | +print(s) |
1 | +def mergeSort(n, s): | ||
2 | + h=int(n/2) | ||
3 | + m=n-h | ||
4 | + u=h*[0] | ||
5 | + v=m*[0] | ||
6 | + | ||
7 | + if(n>1): | ||
8 | + leftHalf=s[:h] | ||
9 | + rightHalf=s[h:] | ||
10 | + mergeSort(h,leftHalf) | ||
11 | + mergeSort(m,rightHalf) | ||
12 | + merge(h,m,leftHalf,rightHalf,s) | ||
13 | + | ||
14 | + | ||
15 | +def merge(h,m,u,v,s): | ||
16 | + i=j=k=0 | ||
17 | + while(i<=h-1 and j<=m-1): | ||
18 | + if(u[i]<v[j]): | ||
19 | + s[k]=u[i] | ||
20 | + i+=1 | ||
21 | + else: | ||
22 | + s[k]=v[j] | ||
23 | + j+=1 | ||
24 | + k+=1 | ||
25 | + if(i>h-1): | ||
26 | + for ii in range (j,m): | ||
27 | + s[k]=v[ii] | ||
28 | + k += 1 | ||
29 | + else: | ||
30 | + for ii in range (i,h): | ||
31 | + s[k]=u[ii] | ||
32 | + k += 1 | ||
33 | + | ||
34 | + | ||
35 | +s=[3,5,2,9,10,14,4,8,7] | ||
36 | +mergeSort(9,s) | ||
37 | +print(s) |
1 | +def mergeSort2(n, s): | ||
2 | + mp=int(n/2) #4 | ||
3 | + mp2=n-mp #5 | ||
4 | + u=mp*[0] | ||
5 | + v=mp2*[0] | ||
6 | + | ||
7 | + if(n>1): | ||
8 | + leftHalf=s[:mp] | ||
9 | + rightHalf=s[mp:] | ||
10 | + mergeSort(mp,leftHalf) | ||
11 | + mergeSort(mp2,rightHalf) | ||
12 | + merge2(0,m,h-1) | ||
13 | + | ||
14 | + | ||
15 | + | ||
16 | +# based on index | ||
17 | +def merge2(low, mid, high): | ||
18 | + i=j=k=0 | ||
19 | + u = high * [0] | ||
20 | + i = low, j = mid+1, k = low | ||
21 | + while(i<=mid and j<=high) : | ||
22 | + if(s[i]<s[j]) : | ||
23 | + u[k] = s[i] | ||
24 | + i+=1 | ||
25 | + else : | ||
26 | + u[k] = s[j] | ||
27 | + j+=1 | ||
28 | + k+=1 | ||
29 | + if(i>mid) : | ||
30 | + while(j<=high) : | ||
31 | + u[k] = s[j] | ||
32 | + j+=1 | ||
33 | + else : | ||
34 | + while(i<=mid) : | ||
35 | + u[k] = s[i] | ||
36 | + i+=1 | ||
37 | + | ||
38 | + for ii in range(high+1) : | ||
39 | + s[ii] = u[ii] | ||
40 | + | ||
41 | + | ||
42 | +s=[3,5,2,9,10,14,4,8,7] | ||
43 | +mergeSort2(9,s) | ||
44 | +print(s) |
1 | +def quickSort(s,low, high): | ||
2 | + pivotPoint=-1 | ||
3 | + if(high>low): | ||
4 | + pivotPoint= partition(s,low,high) | ||
5 | + quickSort(s,low, pivotPoint-1) | ||
6 | + quickSort(s,pivotPoint+1,high) | ||
7 | + | ||
8 | + | ||
9 | +def partition(s,low,high): | ||
10 | + pivotItem=s[low] | ||
11 | + j=low | ||
12 | + for i in range(low+1,high+1): | ||
13 | + if(s[i]<pivotItem): | ||
14 | + j+=1 | ||
15 | + temp=s[i] | ||
16 | + s[i]=s[j] | ||
17 | + s[j]=temp | ||
18 | + pivotPoint=j | ||
19 | + temp=s[low] | ||
20 | + s[low]=s[pivotPoint] | ||
21 | + s[pivotPoint]=temp | ||
22 | + return pivotPoint | ||
23 | + | ||
24 | + | ||
25 | + | ||
26 | +s=[3,5,2,9,10,14,4,8] | ||
27 | +quickSort(s,0,7) | ||
28 | +print(s) | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
1 | +import P3_utility | ||
2 | + | ||
3 | +class Node: | ||
4 | + def __init__(self,data): | ||
5 | + self.l_child=None | ||
6 | + self.r_child=None | ||
7 | + self.data = data | ||
8 | + | ||
9 | +def tree(key,r,i,j): | ||
10 | + k=r[i][j] | ||
11 | + if(k==0): | ||
12 | + return | ||
13 | + else: | ||
14 | + p=Node(key[k]) | ||
15 | + p.l_child=tree(key,r,i,k-1) | ||
16 | + p.r_child=tree(key,r,k+1,j) | ||
17 | + return p | ||
18 | + | ||
19 | +key=[" ","A","B","C","D"] | ||
20 | +p=[0,0.375, 0.375, 0.125,0.125] | ||
21 | +n=len(p)-1 | ||
22 | + | ||
23 | +a=[[0 for j in range(0,n+2)] for i in range(0,n+2)] | ||
24 | +r=[[0 for j in range(0,n+2)] for i in range(0,n+2)] | ||
25 | + | ||
26 | +for i in range (1,n+1): | ||
27 | + a[i][i-1]=0 | ||
28 | + a[i][i]=p[i] | ||
29 | + r[i][i]=i | ||
30 | + r[i][i-1]=0 | ||
31 | +a[n+1][n]=0 | ||
32 | +r[n+1][n]=0 | ||
33 | + | ||
34 | +for dia in range(1,n): | ||
35 | + for i in range(1,n-dia+1): | ||
36 | + j=i+dia | ||
37 | + small=1000 | ||
38 | + for k in range(i,j+1): | ||
39 | + psum=0 | ||
40 | + for y in range(i,j+1): | ||
41 | + psum+=p[y] | ||
42 | + t=a[i][k-1]+a[k+1][j]+psum | ||
43 | + if( t< small): | ||
44 | + small = t | ||
45 | + a[i][j]=t | ||
46 | + r[i][j]=k | ||
47 | + | ||
48 | + | ||
49 | +P3_utility.printMatrixF(a) | ||
50 | +print() | ||
51 | +P3_utility.printMatrix(r) | ||
52 | + | ||
53 | +root=tree(key,r,1,n) | ||
54 | +P3_utility.print_inOrder(root) | ||
55 | +print() | ||
56 | +P3_utility.print_preOrder(root) |
1 | +inf=1000 #infinite | ||
2 | +w=[[0,7,4,6,1],[inf,0,inf,inf,inf], [inf,2,0,5,inf], [inf,3,inf,0,inf], [inf,inf,inf,1,0]] | ||
3 | +n=5 | ||
4 | +f=set() | ||
5 | +touch=n*[0] #처음에는 도착전 마지막 노드가 없으므로 0번 자체 | ||
6 | +length=n*[0] | ||
7 | + | ||
8 | +for i in range(1,n) : | ||
9 | + length[i] = w[0][i]; #처음은 0번 노드가 도착 전 마지막 노드이므로 0번 노드에서 i까지 가는 거리 설정. | ||
10 | + | ||
11 | +for k in range(1,n) : | ||
12 | + minVal = 1000 #min 큰수로 지정함. | ||
13 | + for i in range(1,n) : | ||
14 | + if(0<=length[i] and length[i]<min) : | ||
15 | + | ||
16 | + #처음 : 무조건 min이 교체될 것이고 | ||
17 | + #그 이후 : min보다 더 작으면 교체 | ||
18 | + min = length[i] | ||
19 | + vnear = i #vnear는 미포함 지역에 있는 node이다. | ||
20 | + | ||
21 | + f.add((touch[vnear],vnear)) #vnear에 연결되는 마지막 연결점과 vnear를 잇는 아크를 f에 추가. | ||
22 | + | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
1 | +def promising(i,col): | ||
2 | + k=0 | ||
3 | + switch=True | ||
4 | + while(k<i and switch==True): | ||
5 | + if(col[i]==col[k] or abs(col[i]-col[k])==i-k): | ||
6 | + switch=False | ||
7 | + k+=1 | ||
8 | + return switch | ||
9 | + | ||
10 | +def queens(n,i,col): | ||
11 | + | ||
12 | + if(promising(i,col)==True): | ||
13 | + if(i==n-1): | ||
14 | + print(col) | ||
15 | + return True #한가지 해만 찾아서 바로 리턴하는 것 | ||
16 | + else: | ||
17 | + for j in range(0,n): | ||
18 | + col[i+1]=j | ||
19 | + #원본 코드 | ||
20 | + #queens(n,i+1,col) | ||
21 | + if(queens(n,i+1,col)): #한가지 해만 찾아서 바로 리턴하는 것 | ||
22 | + return True | ||
23 | +n=5 | ||
24 | +col=n*[0] | ||
25 | +queens(n,-1,col) |
1 | +import time | ||
2 | + | ||
3 | +def promising(i,weight, total): | ||
4 | + return ((weight+total >= W) and (weight==W or weight+w[i+1] <=W)) | ||
5 | + | ||
6 | +def s_s(i, weight, total, include): | ||
7 | + | ||
8 | + #아주 조금 빨라지는 개선 | ||
9 | + if(weight+total == W) : | ||
10 | + for j in range(i+1,n) : | ||
11 | + include[j] = 1 | ||
12 | + print("sol",include) | ||
13 | + | ||
14 | + elif(promising(i, weight, total)==True): | ||
15 | + if(weight == W): | ||
16 | + print("sol",include ) | ||
17 | + else: | ||
18 | + include[i+1]=1 | ||
19 | + s_s(i+1, weight+w[i+1],total-w[i+1],include) | ||
20 | + include[i+1]=0 | ||
21 | + s_s(i+1,weight,total-w[i+1],include) | ||
22 | + | ||
23 | +w = 40*[0] | ||
24 | +n=len(w) | ||
25 | + | ||
26 | +for i in range(0,n): | ||
27 | + w[i] = i+1 | ||
28 | + | ||
29 | +W=815 | ||
30 | +print("items =",w, "W =", W) | ||
31 | +include = n*[0] | ||
32 | +total=0 | ||
33 | +for k in w: | ||
34 | + total+=k | ||
35 | + | ||
36 | +start_t = time.time() | ||
37 | +s_s(-1,0,total,include) | ||
38 | +end_t = time.time() | ||
39 | + | ||
40 | +print("time : ",str(end_t-start_t),'s') | ||
41 | + | ||
42 | + | ||
43 | + | ||
44 | + |
1 | +import math | ||
2 | + | ||
3 | +print("Finding most close integer n in n*log(n)") | ||
4 | +print("Please enter the number of n*log(n)") | ||
5 | +goal = input("Enter : ") | ||
6 | +start = 1 | ||
7 | +increaseValue = 1 | ||
8 | +presentComparer = start | ||
9 | + | ||
10 | +#increase or decrease : 0 - increase / 1 - decrease | ||
11 | +valueIndicator = 0 | ||
12 | + | ||
13 | +while(1): | ||
14 | + presentLogValue = presentComparer * math.log10(presentComparer) | ||
15 | + | ||
16 | + if presentLogValue<goal and valueIndicator==1 and increaseValue==1 : | ||
17 | + print("n : " + str(presentComparer)) | ||
18 | + print("most close number to goal : " + str(presentLogValue)) | ||
19 | + break | ||
20 | + | ||
21 | + # save previous value befor comparer increasment | ||
22 | + prevLogValue = presentLogValue | ||
23 | + prevComparer = presentComparer | ||
24 | + | ||
25 | + # comparer increasement | ||
26 | + if valueIndicator == 0 : | ||
27 | + presentComparer += increaseValue | ||
28 | + elif valueIndicator == 1 : | ||
29 | + presentComparer -= increaseValue | ||
30 | + | ||
31 | + | ||
32 | + # indicator and increasement changing logic | ||
33 | + if presentLogValue > goal : | ||
34 | + valueIndicator = 1 | ||
35 | + | ||
36 | + if presentComparer/10 == increaseValue*10 : | ||
37 | + increaseValue *= 10 | ||
38 | + | ||
39 | + elif presentLogValue < goal : | ||
40 | + valueIndicator = 0 | ||
41 | + | ||
42 | + if presentComparer/10 >= increaseValue*10 : | ||
43 | + increaseValue *= 10 | ||
44 | + | ||
45 | + | ||
46 | + | ||
47 | + | ||
48 | +# 10000/10= 1000 == 1000 | ||
49 | +# 86000/10= 1000 == 1000 | ||
50 | +# 100/10 = 9 <= 1 * 10 | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
01_algorithm_analysis/lecture lab/test.py
0 → 100644
01_algorithm_analysis/lecture lab/test2.py
0 → 100644
1 | +import time | ||
2 | +def fib(n) : | ||
3 | + if n==0 : | ||
4 | + return 0 | ||
5 | + elif n==1: | ||
6 | + return 1 | ||
7 | + else: | ||
8 | + return fib(n-1) + fib(n-2) | ||
9 | + | ||
10 | +def fib2(n): | ||
11 | + f = (n+1) * [0] | ||
12 | + f[0] = 0 | ||
13 | + if(n>0): | ||
14 | + f[1] = 1 | ||
15 | + for i in range(2,n+1): | ||
16 | + f[i] = f[i-1] + f[i-2] | ||
17 | + return f[n] | ||
18 | + | ||
19 | + | ||
20 | + | ||
21 | +for i in range(30,36): | ||
22 | + stime = time.time() | ||
23 | + fib(i) | ||
24 | + print('%2d %10.5f' % (i, time.time()-stime)) | ||
25 | + | ||
26 | + | ||
27 | +for i in range(30,36): | ||
28 | + stime = time.time() | ||
29 | + fib2(i) | ||
30 | + print('%2d %10.5f' % (i, time.time()-stime)) |
-
Please register or login to post a comment