From 710966ccba00054ff1152899a681e64a2a606901 Mon Sep 17 00:00:00 2001 From: EunJin Lee <50702052+witheunjin@users.noreply.github.com> Date: Wed, 4 Mar 2020 21:57:04 +0900 Subject: [PATCH] march_4th add BFS and HEAP code reorganize files and folders --- Algorithms/BFS/BFS.cpp | 83 ++++++++++++++++++ Algorithms/BFS/BFS_run.md | 86 +++++++++++++++++++ .../{DFS_DepthFirstSearch => DFS}/DFS.cpp | 0 .../{DFS_DepthFirstSearch => DFS}/DFS_code.md | 0 .../DFS_output.md => DFS/DFS_run.md} | 0 Algorithms/HEAP/heap.cpp | 86 +++++++++++++++++++ 6 files changed, 255 insertions(+) create mode 100644 Algorithms/BFS/BFS.cpp create mode 100644 Algorithms/BFS/BFS_run.md rename Algorithms/{DFS_DepthFirstSearch => DFS}/DFS.cpp (100%) rename Algorithms/{DFS_DepthFirstSearch => DFS}/DFS_code.md (100%) rename Algorithms/{DFS_DepthFirstSearch/DFS_output.md => DFS/DFS_run.md} (100%) create mode 100644 Algorithms/HEAP/heap.cpp diff --git a/Algorithms/BFS/BFS.cpp b/Algorithms/BFS/BFS.cpp new file mode 100644 index 0000000..2e98039 --- /dev/null +++ b/Algorithms/BFS/BFS.cpp @@ -0,0 +1,83 @@ +/*BFS_Breadth-First-Search*/ +#include +#include +#include +using namespace std; + +vector> adj; + + +void printQueue(queue pq) { + //vector temp; + int size = pq.size(); + cout << "q = {"; + for (size_t i = 0; i < size; ++i) { + cout << pq.front() << ", "; + pq.pop(); + } + cout << "}" << endl; +} +void printOrder(vector po) { + cout << "order = {"; + for (size_t i = 0; i < po.size(); ++i) { + cout << po.at(i) << ", "; + } + cout << "}" << endl; +} +void printDiscovered(vector dp) { + cout << "discovered = {"; + for (size_t i = 0; i < dp.size(); ++i) { + if (dp.at(i)) cout << "T, "; + else cout << "-, "; + } + cout << "}" << endl; +} + +vector bfs(int start) { + queue q; + vector discovered(adj.size(), false); + vector order; + discovered[start] = true; + printDiscovered(discovered); + q.push(start); + printQueue(q); + cout << endl; + while (!q.empty()) { + int here = q.front(); + cout << "here = q.front :" << here << endl; + q.pop(); + printQueue(q); + order.push_back(here); + printOrder(order); + cout << endl; + for (size_t i = 0; i < adj[here].size(); ++i) { + int there = adj[here][i]; + if (!discovered[there]) { + cout << "there = " << there << endl; + q.push(there); + printQueue(q); + discovered[there] = true; + printDiscovered(discovered); + cout << endl; + } + } + } + return order; +} + +int main() { + adj.resize(8); + int edge; + cout << " Էϼ : "; + cin >> edge; + cout << " Էϼ(ex.a b) " << endl; + for (int i = 0; i < edge; i++) { + int a, b; + cin >> a >> b; + adj[a].push_back(b); + adj[b].push_back(a); + } + vector vv = bfs(0); + printOrder(vv); + +} diff --git a/Algorithms/BFS/BFS_run.md b/Algorithms/BFS/BFS_run.md new file mode 100644 index 0000000..fb07420 --- /dev/null +++ b/Algorithms/BFS/BFS_run.md @@ -0,0 +1,86 @@ +## INPUT + +``` +간선의 개수를 입력하세요 : 9 +각 간선의 두 정점을 입력하세요(ex.a b) +0 1 +0 2 +0 3 +1 2 +1 4 +1 5 +2 6 +5 6 +6 7 +``` + +## OUTPUT + +``` +discovered = {T, -, -, -, -, -, -, -, } +q = {0, } + +here = q.front :0 +q = {} +order = {0, } + +there = 1 +q = {1, } +discovered = {T, T, -, -, -, -, -, -, } + +there = 2 +q = {1, 2, } +discovered = {T, T, T, -, -, -, -, -, } + +there = 3 +q = {1, 2, 3, } +discovered = {T, T, T, T, -, -, -, -, } + +here = q.front :1 +q = {2, 3, } +order = {0, 1, } + +there = 4 +q = {2, 3, 4, } +discovered = {T, T, T, T, T, -, -, -, } + +there = 5 +q = {2, 3, 4, 5, } +discovered = {T, T, T, T, T, T, -, -, } + +here = q.front :2 +q = {3, 4, 5, } +order = {0, 1, 2, } + +there = 6 +q = {3, 4, 5, 6, } +discovered = {T, T, T, T, T, T, T, -, } + +here = q.front :3 +q = {4, 5, 6, } +order = {0, 1, 2, 3, } + +here = q.front :4 +q = {5, 6, } +order = {0, 1, 2, 3, 4, } + +here = q.front :5 +q = {6, } +order = {0, 1, 2, 3, 4, 5, } + +here = q.front :6 +q = {} +order = {0, 1, 2, 3, 4, 5, 6, } + +there = 7 +q = {7, } +discovered = {T, T, T, T, T, T, T, T, } + +here = q.front :7 +q = {} +order = {0, 1, 2, 3, 4, 5, 6, 7, } + +order = {0, 1, 2, 3, 4, 5, 6, 7, } +``` + +마지막 줄(order = {0, 1, 2, 3, 4, 5, 6, 7, })을 제외한 모든 결과는 알고리즘의 진행과정을 확인하기 위한 것이므로 해당 코드를 생략해도 무방하다. \ No newline at end of file diff --git a/Algorithms/DFS_DepthFirstSearch/DFS.cpp b/Algorithms/DFS/DFS.cpp similarity index 100% rename from Algorithms/DFS_DepthFirstSearch/DFS.cpp rename to Algorithms/DFS/DFS.cpp diff --git a/Algorithms/DFS_DepthFirstSearch/DFS_code.md b/Algorithms/DFS/DFS_code.md similarity index 100% rename from Algorithms/DFS_DepthFirstSearch/DFS_code.md rename to Algorithms/DFS/DFS_code.md diff --git a/Algorithms/DFS_DepthFirstSearch/DFS_output.md b/Algorithms/DFS/DFS_run.md similarity index 100% rename from Algorithms/DFS_DepthFirstSearch/DFS_output.md rename to Algorithms/DFS/DFS_run.md diff --git a/Algorithms/HEAP/heap.cpp b/Algorithms/HEAP/heap.cpp new file mode 100644 index 0000000..cb37b71 --- /dev/null +++ b/Algorithms/HEAP/heap.cpp @@ -0,0 +1,86 @@ +#include +#include +#include +using namespace std; +#include +#include +#include +using namespace std; + +// Ҹ ϴ Լ +void push_heap(vector& heap, int newValue) { + heap.push_back(newValue); //ο Ҹ ڿ ϴ ־ Ģ Ų + int idx = heap.size() - 1; // ο ġ + //Ģ ҰĢ Ѻ~ + // ġ Ʈ ƴϰ θ ݺ + while (idx > 0 && heap[(idx - 1) / 2] < heap[idx]) { + // Դٴ Ұ Ʈ ƴϰ(Ʈ 쿣 newValue Ǵٴ ǹ. ) + //θ ڽĿ ũٴ ǵ 쿡 ҰĢ ̿ ٲش. + swap(heap[(idx - 1) / 2], heap[idx]);//θ ҿ Ҹ ٲ۴.( ) + idx = (idx - 1) / 2;//׸ ġ θġ ٲش.(ּ) + } +} + +//켱 켱 (=Ʈ) ϴ Լ +void pop_heap(vector& heap) { + //Ʈ ⼭ ϰ ⼭ ҷ ٽ ϴ + heap[0] = heap.back(); //ϴ ( ) ִ Ʈڸ ִ´. + heap.pop_back(); // (Ʈ ) ش. + int here = 0; //̰ ? // ġ here + while (1) { //ѹݺ. ܴ ݺ ȿ ǹ ó + int left = here * 2 + 1; // ڽ ε + int right = here * 2 + 2; // ڽ ε + // ڽ ε ũ ų ũ ݺ + //? 𸣰 ϴ н + if (left >= heap.size()) break; + int next = here; // ε ϴ next ϰ + // ڽ θ ڽİ Ŀϴ Ұ迡 ߳Ƿ + // ڽ ε next Ѵ. + if (heap[next] < heap[left]) next = left; + // ڽ ε ũ⺸ ۰ + // ڽ ҰĢ ߳Ƿ + // ڽ ε next Ѵ. + if (right < heap.size() && heap[next] < heap[right]) next = right; + if (next == here) break; // ׷ next ġ here , Ʈġ + swap(heap[here], heap[next]); //Ʈƴ϶ ӽú next here Ҹ ٲ۴. + here = next; //׸ ӽú here - + } +} +void push_heap(vector& heap, int newValue) { + heap.push_back(newValue); //ο Ҹ ڿ ϴ ־ Ģ Ų + int idx = heap.size() - 1; // ο ġ + //Ģ ҰĢ Ѻ~ + // ġ Ʈ ƴϰ θ ݺ + while (idx > 0 && heap[(idx - 1) / 2] < heap[idx]) { + // Դٴ Ұ Ʈ ƴϰ(Ʈ 쿣 newValue Ǵٴ ǹ. ) + //θ ڽĿ ũٴ ǵ 쿡 ҰĢ ̿ ٲش. + swap(heap[(idx - 1) / 2], heap[idx]);//θ ҿ Ҹ ٲ۴.( ) + idx = (idx - 1) / 2;//׸ ġ θġ ٲش.(ּ) + } +} + +//켱 켱 (=Ʈ) ϴ Լ +void pop_heap(vector& heap) { + //Ʈ ⼭ ϰ ⼭ ҷ ٽ ϴ + heap[0] = heap.back(); //ϴ ( ) ִ Ʈڸ ִ´. + heap.pop_back(); // (Ʈ ) ش. + int here = 0; //̰ ? // ġ here + while (1) { //ѹݺ. ܴ ݺ ȿ ǹ ó + int left = here * 2 + 1; // ڽ ε + int right = here * 2 + 2; // ڽ ε + // ڽ ε ũ ų ũ ݺ + //? 𸣰 ϴ н + if (left >= heap.size()) break; + int next = here; // ε ϴ next ϰ + // ڽ θ ڽİ Ŀϴ Ұ迡 ߳Ƿ + // ڽ ε next Ѵ. + if (heap[next] < heap[left]) next = left; + // ڽ ε ũ⺸ ۰ + // ڽ ҰĢ ߳Ƿ + // ڽ ε next Ѵ. + if (right < heap.size() && heap[next] < heap[right]) next = right; + if (next == here) break; // ׷ next ġ here , Ʈġ + swap(heap[here], heap[next]); //Ʈƴ϶ ӽú next here Ҹ ٲ۴. + here = next; //׸ ӽú here - + } +} \ No newline at end of file