Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Basov_AS/lab_dijkstra/doc/otchet_dheap.docx
Binary file not shown.
Binary file added Basov_AS/lab_dijkstra/doc/otchet_dheap.pages
Binary file not shown.
42 changes: 42 additions & 0 deletions Basov_AS/lab_dijkstra/include/d-heap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once
#include <iostream>
#include <algorithm>

#define maxSize 1000
typedef int dataType;

class Data {
public:
float priorities;
};

class DHeap {
protected:
Data** keys; // ����� (����������)
int d; // �-������� ����
int lastIdx; // ������� ������ ����
public:
/* ������������ */
DHeap(int d);
DHeap(const DHeap &heap);
~DHeap();

int isFull();
int isEmpty();

void add(Data *&key); // ������� �������� � ����
void addSet(Data **key, int num); // ������� ������ ��������� � ����

Data* erase(); // �������� ���������� ��������
Data* erase(int i); // ... i-�� ��������

void transposition(int i, int j); // ����� ���������� ��������
void surfacing(int i); // ��������
void immersion(int i); // ����������

void hilling(); // ����������

private:
int minChild(int i); // ����� ������������ �������
};

16 changes: 16 additions & 0 deletions Basov_AS/lab_dijkstra/include/dijkstra.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once
#include "graph.h"
#include "d-heap.h"
#include "priority-queue.h"
#include <cfloat>

class DataFloat : public Data {
public:
DataFloat(int v, float dist);
int v;
};

class Dijkstra {
public:
static void dijkstra(Graph *&graph, int s, float *&distance, int *&up);
};
56 changes: 56 additions & 0 deletions Basov_AS/lab_dijkstra/include/graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <iostream>
#include <cstdlib>
#include <ctime>

#define maxVerticesSize 1000


class WeightedEdge {
public:
int nac_ver; // ��������� �������
int kon_ver; // �������� �������
float weight; // ��� ����� �� N � K

WeightedEdge(int _nac_ver, int _kon_ver, float _weight);
};

class Graph {
private:
int vershini; // ���������� ������
int rebra; // ���������� �����
int tek_rebro; // ������� ���������� �����
int tek_vershina; // ������� ���������� ������
WeightedEdge** edges; // ������ ���������� �����
int* vertices; // ������ ������
public:
// ������������
Graph(int n);
Graph(int n, int m);
~Graph();

void generateGraph(float minRange, float maxRange); // ��������� ����� � ��������� ������
void addEdge(int N, int K, float weight); // ���������� �����
void removeEdge(int N, int K); // �������� �����

int getVerticesNum(); // ����� ������
int getEdgeSize(); // ������������ ����� �����
int getRealSize(); // �������� ����� �����
bool isConnectivity(); // �������� �� ��������� �����
WeightedEdge** getEdgeSet(); // ������ ���������� �����
WeightedEdge* getEdge(int j); // ���������� �������� �����
float getWeight(int N, int K); // ��� ����� � ��������� ���������
void printList(); // ������ �����
void printVertices(); // ������ ������� ������


/* ��������������� ������� */
private:
void generateVertices(int &N, int &K); // ��������� ������
float generateWeight(float minRange, float maxRange); // ��������� ��������� �����
void cleaner(); // �������� ���� ���������� ����� � �����
int searchEdge(int _nac_ver, int _kon_ver); // ����� ����� �� �������� ��������
bool searchVershinu(int _ver); // ����� �������� ������� (���������� �� ��� � ������� ���������� �����)
void addVershini(int _nac_ver, int _kon_ver); // ���������� ������ � ������ ������


};
32 changes: 32 additions & 0 deletions Basov_AS/lab_dijkstra/include/priority-queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "d-heap.h"

class PriorityQueue {
public:
PriorityQueue() {};

virtual void push(Data *&key) = 0;
virtual Data* pop() = 0;
virtual void refresh() = 0;

virtual int isFull() = 0;
virtual int isEmpty() = 0;
};


/* ������������ ������� �� D-���� */
class DHeapBasedPriorityQueue : public PriorityQueue {
protected:
DHeap * heap;
public:
DHeapBasedPriorityQueue(int d = 4);
DHeapBasedPriorityQueue(const DHeapBasedPriorityQueue &queue);
DHeapBasedPriorityQueue(Data **keys, int num, int d = 4);
~DHeapBasedPriorityQueue();

virtual void push(Data *&key); // ���������� ����� � �������
virtual Data* pop(); // �������� ������������
virtual void refresh(); // ���������� �������

virtual int isFull();
virtual int isEmpty();
};
134 changes: 134 additions & 0 deletions Basov_AS/lab_dijkstra/sample/sample_dijkstra.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#include "dijkstra.h"
#include <cfloat>
#include <iostream>

using namespace std;

int main() {
Graph* graph;
int n; // ���������� ������
int m; // ���������� �����
int s; // ��������� �������


int typeGenerate;
try
{
cout << "Write number vertices" << endl;
cin >> n;
cout << "Write number edges" << endl;
cin >> m;
graph = new Graph(n, m);
}
catch (...)
{
return -1;
}

cout << "Write type generate graph:" << endl;
cout << "1. Random" << endl;
cout << "2. Manual" << endl;


cin >> typeGenerate;
switch (typeGenerate)
{
/* ��������� ��������� �����*/
case 1: {
try
{
int minRange; // ����������� �������� ���� �����
int maxRange; // ������������ �������� ���� �����
cout << "Write minRange" << endl;
cin >> minRange;
cout << "Write maxRange" << endl;
cin >> maxRange;
graph->generateGraph(minRange, maxRange);
}
catch (...) {
return -1;
}
break;
}
/* ������ ��������� �����*/
case 2: {
int _nac_ver, _kon_ver;
float _weight;

try
{
for (int i = 0; i < m; i++) {
cout << "Write start vershinu" << endl;
cin >> _nac_ver;
cout << "Write finish vershinu" << endl;
cin >> _kon_ver;
cout << "Write weight" << endl;
cin >> _weight;
graph->addEdge(_nac_ver, _kon_ver, _weight);
}
}
catch (...)
{
return -1;
}
break;
}
}
if (!graph->isConnectivity()) // �������� �� ���������
return -3;
graph->printVertices();
cout << "Write start vertices for Dijkstra" << endl;
cin >> s;

graph->printList();
cout << endl;

float *dist; // ������ ���������� ����� S � i-��� ��������
int *up; // ������ ������, �������������� � ���������� ����
try {
Dijkstra::dijkstra(graph, s, dist, up);
}
catch (...) {
return -2;
}

cout << n << ' ' << m << endl; // ������ ���������� ������
cout << s << endl; // ������ ��������� �������

m = graph->getRealSize();
WeightedEdge* edge;
cout << "// Matrix edges //" << endl; // ������ ������� ���������� ������
for (int j = 0; j < m; j++) {
edge = graph->getEdge(j);
cout << edge->nac_ver << ' ' << edge->kon_ver << ' ' << edge->weight << endl;
}

cout << endl << "// Distances //" << endl; // ������ ���������� �� �������� ������� S �� i-��� �������
for (int i = 0; i < n; i++) {
if (dist[i] == FLT_MAX) return -4;
cout << s << " -> " << i << " distance = " << dist[i] << endl;
}


cout << endl << "// Ways //" << endl; // ������ ����� ���� ������
for (int i = 0; i < n; i++) {
int j = i;
cout << j << " <- ";
while (up[j] != s) {
cout << up[j] << " <- ";
j = up[j];
}
cout << s << endl;
}

// ������������ ������
delete graph;
delete[]dist;
delete[]up;


// �������� �������
cout << endl;
system("pause");
return 0;
}
Loading