본문 바로가기

[알고리즘개론] - Algorithm/[Concept]

BinaryHeap time Check

계속 time이 0 뜨는데 이유를 찾아봐야겠음...

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <cstdio>
#include <cstdlib>
#include <random>
#include <chrono>
#include <algorithm>
#include <cstring>
 
using namespace std;
 
class MinHeap {
    int* harr;
    int capacity;
    int heap_size;
public:
    MinHeap(int capacity);
    void MinHeapify(int idx);
    int parent(int i) { return (i - 1/ 2; }
    int left(int i) { return (2 * i + 1); }
    int right(int i) { return (2 * i + 2); }
    int extractMin();
    int getMin() { return harr[0]; }
    void insertKey(int k);
    void decreaseKey(int idx, int newVal);
    void deleteKey(int idx);
};
 
MinHeap::MinHeap(int cap) {
    heap_size = 0;
    capacity = cap;
    harr = new int[cap];
}
 
void MinHeap::insertKey(int k) {
    if (heap_size == capacity) {
        printf("\nOVERFLOW!!!!!!\n");
        return;
    }
    heap_size++;
    int i = heap_size - 1;
    harr[i] = k;
 
    while (i != 0 && harr[parent(i)] > harr[i]) {
        swap(harr[i], harr[parent(i)]);
        i = parent(i);
    }
}
 
int MinHeap::extractMin() {
    if (heap_size <= 0)
        return -1;
    if (heap_size == 1) {
        heap_size--;
        return harr[0];
    }
    int root = harr[0];
    harr[0= harr[heap_size - 1];
    heap_size--;
    MinHeapify(0);
    return root;
}
 
void MinHeap::decreaseKey(int idx, int newVal) {
    harr[idx] = newVal;
    while (idx != 0 && harr[parent(idx)] > harr[idx]) {
        swap(harr[parent(idx)], harr[idx]);
        idx = parent(idx);
    }
}
 
void MinHeap::deleteKey(int idx) {
    harr[idx] = INT_MIN;
    extractMin();
}
 
void MinHeap::MinHeapify(int i) {
    int l = left(i);
    int r = right(i);
    int smallest = i;
    if (l < heap_size && harr[l] < harr[i]) {
        smallest = l;
    }
    if (r < heap_size && harr[r] < harr[smallest]) {
        smallest = r;
    }
    if (smallest != i) {
        swap(harr[i], harr[smallest]);
        MinHeapify(smallest);
    }
}
 
 
int main(int argc, char** argv)
{
    random_device rd;
    default_random_engine gen(rd());
    uniform_int_distribution<int> dist(01000*10*1000);
 
    int n = atoi(argv[1]);
    printf("n = %d\n", n);
 
    MinHeap h(n);
 
    int nT = 1;
 
    for (int i = 0; i < n-nT; i++) {
        h.insertKey(dist(gen));
    }
    //chrono::time_point<chrono::system_clock> startTime = chrono::system_clock::now();
 
 
    if (strcmp(argv[2], "pop"== 0){
        printf("Pop start\n");
 
        double ave = (double)0;
 
        for (int i = 0; i < nT; i++) {
            chrono::time_point<chrono::system_clock> startTime = chrono::system_clock::now();
 
            h.extractMin();
 
            chrono::time_point<chrono::system_clock> endTime = chrono::system_clock::now();
            chrono::milliseconds timeDiff = chrono::duration_cast<chrono::milliseconds> (endTime - startTime);
            double T = timeDiff.count();
            ave += T;
        }
        //chrono::time_point<chrono::system_clock> endTime = chrono::system_clock::now();
        //chrono::milliseconds timeDiff = chrono::duration_cast<chrono::milliseconds> (endTime - startTime);
        //double T = timeDiff.count();
        ave /= (double)nT;
        printf("Pop Time : %lf\n", ave);
 
    }
    else if (strcmp(argv[2], "push"== 0) {
        printf("Push start\n");
 
        double ave = (double)0;
 
        for (int i = 0; i < nT; i++) {
            chrono::time_point<chrono::system_clock> startTime = chrono::system_clock::now();
 
            int val = dist(gen);
            h.insertKey(val);
 
            chrono::time_point<chrono::system_clock> endTime = chrono::system_clock::now();
            chrono::milliseconds timeDiff = chrono::duration_cast<chrono::milliseconds> (endTime - startTime);
            double T = timeDiff.count();
            ave += T;
        }
        ave /= (double)nT;
        printf("Push Time : %lf\n", ave);
 
    }
    return 0;
}
cs

'[알고리즘개론] - Algorithm > [Concept]' 카테고리의 다른 글

Prim's 알고리즘  (0) 2020.11.13
Heap Sort  (0) 2020.10.30
알고리즘 Find_Max  (0) 2020.10.20
시간측정  (0) 2020.10.17
알고리즘 Part 4  (0) 2020.10.05