- toc {:toc}
Heap
-
루νΈλ‘ μ¬μλ‘ λ ν¬κ±°λ μμμ§λ μμ μ΄μ§ νΈλ¦¬
-
ν μ±μ§
- νμ κ° λ Έλμ λν΄ λ Έλ κ°μ μμμ κ°λ³΄λ€ μ»€μΌ νλ€.(Maxheap)
-
λ©λͺ¨λ¦¬ νκ³Ό μ°κ΄μ±μ΄ μλ κ²μ μλλ€.
-
루νΈκ° νμ κ°μ₯ ν° κ°μ΄κ±°λ κ°μ₯ μμ κ°μ΄λ€.
-
λ£¨νΈ μ κ±° β κ°μ₯ ν° κ° μΆμΆ
- 리ν λ Έλ μ€ κ°μ₯ μ€λ₯Έμͺ½ κ°μ 루νΈλ‘ 볡μ¬νκ³ λ¦¬ν λ Έλ κ°μ μμ νλ€.
- ν μ±μ§μ λ°λΌ ReheapDownμ ν΅ν΄ μμΉλ₯Ό μ°Ύμκ°λ€.
template<class ItemType>
void HeapType<ItemType>::MAX_ReheapDown(int root, int bottom)
{
int maxChild;
int rightChild;
int leftChild;
leftChild = root * 2 + 1;
rightChild = root * 2 + 2;
if (leftChild <= bottom){
if(leftChild == bottom){ // μμμ΄ 1κ° μλ κ²½μ°
maxChild = leftChild;
}
else{ // μμμ΄ 2κ° μλ κ²½μ°
if(elements[leftChild] <= elements[rightChild]){
maxChild = rightChild;
}
else{
maxChild = leftChild;
}
}
if(elements[maxChild] == null_val) return;
if(elements[root] < elements[maxChild]){
Swap(elements[root], elements[maxChild]);
MAX_ReheapDown(maxChild, bottom);
}
}
}- λ
Έλ μ½μ
- λ§μ§λ§ λΉμΉΈμ λ£κ³ μλ‘ μ¬λ¦°λ€.
template<class ItemType>
void HeapType<ItemType>::MAX_ReheapUp(int root, int bottom)
{
int parent;
if(bottom > root){ // treeκ° λΉμ΄μμ§ μλ€λ©΄
parent = (bottom - 1) / 2;
if(elements[bottom] > elements[parent]){
Swap(elements[bottom], elements[parent]);
MAX_ReheapUp(root, parent);
}
}
}Priority Queue
- λ€μ λ°©μμΌλ‘ μ°μ μμ νλ₯Ό ꡬνν μ μλ€.
- Unsorted Listv - Dequeue β μ 체 리μ€νΈλ₯Ό νμν΄μΌ νλ―λ‘ O(N)
- Array-Based Sorted List β Enqueue λΉμ©μ΄ λΉμΈλ€ O(N)
- Linked Sorted List β Enqueue λΉμ©μ΄ λΉμΈλ€ O(N)
- Binary Search Tree β νκ· μ μΌλ‘ Enqueue, Dequeueκ° O(logN)
- Heap β μ΅μ μ κ²½μ°μλ O(logN) μ΄ λ³΄μ₯λλ€.
class FullPQ(){};
class EmptyPQ(){};
template<class ItemType>
class PQType
{
public:
PQType(int);
~PQType();
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
void Enqueue(ItemType newItem);
void Dequeue(ItemType& item);
private:
int length;
HeapType<ItemType> items;
int maxItems;
};
template<class ItemType>
PQType<ItemType>::PQType(int max){
maxItems = max;
items = new ItemType[max];
length = 0;
}
template<class ItemType>
void PQType<ItemType>::MakeEmpty(){
length = 0;
}
template<class ItemType>
PQType<ItemType>::~PQType(){
delete [] items;
}
template<class ItemType>
void PQType<ItemType>::Dequeue(ItemType& item)
{
if (length == 0)
throw EmptyPQ();
else {
item = items[0];
items[0] = items[length-1];
length--;
items.ReheapDown(0, length-1);
}
}
template<class ItemType>
void PQType<ItemType>::Enqueue(ItemType newItem)
{
if (length == maxItems)
throw FullPQ();
else {
length++;
items[length-1] = newItem;
items.ReheapUp(0, length-1);
}
}- Enqueue, Dequeueλ νμ λ Έλλ₯Ό μΆκ°, μμ νλ κ²κ³Ό κ°λ€.
Graph
-
무방ν₯ κ·Έλν
-
λ°©ν₯ κ·Έλν
-
κ·Έλν νν
- μΈμ νλ ¬
- λ©λͺ¨λ¦¬ : O((Vμ κ°μ)^2)
- κ°μ νμΈμ΄ λΉ λ₯΄λ€. O(1)
- μΈμ 리μ€νΈ
- λ©λͺ¨λ¦¬ : O((Vμ κ°μ) + (Eμ κ°μ))
- λμ ν λΉμ΄ κ°λ₯νλ€.
- νΉμ λ Έλμ μΈμ ν λ Έλλ₯Ό λΉ λ₯΄κ² μ κ·Όν μ μλ€.
- μμ κ·ΈλνμΈ κ²½μ° κ° list λ³λ‘ n-1κ°μ μ£μ§λ₯Ό κ°μ§κΈ° λλ¬Έμ νλ ¬μ λΉν΄ μ΄λμ΄ λμ§ μλλ€.
- μΈμ νλ ¬
const int NULL_EDGE = 0;
template<class VertexType>
class GraphType {
public:
GraphType(int);
~GraphType();
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
void AddVertex(VertexType);
void AddEdge(VertexType, VertexType, int);
int WeightIs(VertexType, VertexType);
void GetAdjacents(VertexType,QueType<VertexType>&);
void ClearMarks();
void MarkVertex(VertexType);
bool IsMarked(VertexType) const;
private:
int numVertices;
int maxVertices;
VertexType* vertices;
int **edges; // μ΄μ€ ν¬μΈν° -> νλ ¬
bool* marks;
};
template<class VertexType>
GraphType<VertexType>::GraphType(int maxV){
numVertices = 0;
maxVertices = maxV;
vertices = new VertexType[maxV];
edges = new int[maxV]; // λ°°μ΄μ λμ ν λΉ λ°λλ€.
for(int i = 0; i < maxV; i++)
edges[i] = new int[maxV];
marks = new bool[maxV];
}
template<class VertexType>
GraphType<VertexType>::~GraphType(){
delete [] vertices;
for(int i = 0; i < maxVertices; i++)
delete [] edges[i];
delete [] edges;
delete [] marks;
}
void GraphType<VertexType>::AddVertex(VertexType vertex){
vertices[numVertices] = vertex; // μ΄λ¦ λͺ¨μμ μΆκ°
for(int index = 0; index < numVertices; index++) {
// μ£μ§ κ°μ€μΉμ NULLκ° μΆκ°
edges[numVertices][index] = NULL_EDGE;
edges[index][numVertices] = NULL_EDGE;
}
numVertices++;
}
template<class VertexType>
void GraphType<VertexType>::AddEdge(VertexType fromVertex,
VertexType toVertex, int weight){
int row;
int column;
row = IndexIs(vertices, fromVertex);
col = IndexIs(vertices, toVertex);
edges[row][col] = weight;
}
template<class VertexType>
int IndexIs(VertexType* vertices, VertexType vertex)
{
int index = 0;
while (!(vertex == vertices[index]))
index++;
return index;
}
template<class VertexType>
int GraphType<VertexType>::WeightIs(VertexType fromVertex,
VertexType toVertex){
int row;
int column;
row = IndexIs(vertices, fromVertex);
col = IndexIs(vertices, toVertex);
return edges[row][col];
}Searching
- DFS(Depth First Search) - κΉμ΄ μ°μ νμ. μ€νμΌλ‘ ꡬνλλ€.
template<class VertexType>
void GraphType<VertexType>::GetAdjacents(VertexType vertex,
QueTye<VertexType>& adjvertexQ){
int fromIndex;
int toIndex;
fromIndex = IndexIs(vertices, vertex);
for(toIndex = 0; toIndex < numVertices; toIndex++)
if(edges[fromIndex][toIndex] != NULL_EDGE)
adjvertexQ.Enqueue(vertices[toIndex]);
}
template <class ItemType>
void DepthFirstSearch(GraphType<VertexType> graph,
VertexType startVertex, VertexType endVertex)
{
StackType<VertexType> stack;
QueType<VertexType> vertexQ;
bool found = false;
VertexType vertex;
VertexType item;
graph.ClearMarks();
stack.Push(startVertex);
do {
stack.Pop(vertex);
if(vertex == endVertex)
found = true;
else {
if(!graph.IsMarked(vertex)) {
graph.MarkVertex(vertex);
graph.GetAdjacents(vertex, vertexQ);
while(!vertexQ.IsEmpty()) {
vertexQ.Dequeue(item);
if(!graph.IsMarked(item))
stack.Push(item);
}
}
} while(!stack.IsEmpty() && !found);
if(!found)
cout << "Path not found" << endl;
}- BFS(Breadth First Search) - λλΉ μ°μ νμ. νλ‘ κ΅¬νλλ€.
template<class VertexType>
void BreadthFirtsSearch(GraphType<VertexType> graph,
VertexType startVertex, VertexType endVertex);{
QueType<VertexType> queue;
QueType<VertexType> vertexQ;//
bool found = false;
VertexType vertex;
VertexType item;
graph.ClearMarks();
queue.Enqueue(startVertex);
do {
queue.Dequeue(vertex);
if(vertex == endVertex)
found = true;
else {
if(!graph.IsMarked(vertex)) {
graph.MarkVertex(vertex);
graph.GetAdjacents(vertex, vertexQ);
while(!vertxQ.IsEmpty()) {
vertexQ.Dequeue(item);
if(!graph.IsMarked(item))
queue.Enqueue(item);
}
}
}
} while (!queue.IsEmpty() && !found);
if(!found)
cout << "Path not found" << endl;
}